0

I am trying to write a function or decorator that can apply multiple decorators at once. Specifically, I have a set of patches that I want to apply multiple times, without having to decorate each single time. For example:

    @patch(COLUMNS_API_FUNC_PATH, side_effect=[])
    @patch(BUSINESS_AREA_API_FUNC_PATH, side_effect=[business_areas_mocks])
    @patch(TABLES_API_FUNC_PATH, side_effect=[mock_main_base_table, mock_sub_base_table] + valid_default_tables)
    @patch(TABLES_BY_FILTER_FUNC_PATH,
           side_effect=[mock_filtered_tables_main_group(), mock_filtered_tables_sub_group()])
    @patch(TABLE_COLUMNS_API_FUNC_PATH, side_effect=[default_tables_columns1])
    def test_valid_model(self, *_):

        self.assert_valid_model(model=valid_model)

These patches are to be applied to many tests.

  1. I tried writing a wrapper to include all of the patches but with this wrapper it seemed like the tests was not executed.

  2. I also tried to put the patches in a variable, which was successful, but only with 1 patch at a time:

    patch_in_variable = patch(COLUMNS_API_FUNC_PATH, side_effect=[])
        def test_valid_model(self, *_):
    
            patch_in_variable(self.assert_valid_model(model=valid_model))
    
  3. Last thing I tried was from How to create a function that can apply multiple decorators to another function?, but patches were not applied:

    def multi_patch(func):
        return patch(COLUMNS_API_FUNC_PATH, side_effect=[])(
            patch(BUSINESS_AREA_API_FUNC_PATH, side_effect=[business_areas_mocks])(
                patch(TABLES_API_FUNC_PATH, side_effect=[mock_main_base_table, mock_sub_base_table] + valid_default_tables)(
                    patch(TABLES_BY_FILTER_FUNC_PATH,
                          side_effect=[mock_filtered_tables_main_group(), mock_filtered_tables_sub_group()])(
                        patch(TABLE_COLUMNS_API_FUNC_PATH, side_effect=[default_tables_columns1])(
                            func
                        )
                    )
                )
            )
        )
    @multi_patch
    def test_valid_model(self, *_):
    
    self.assert_valid_model(model=valid_model)
    
    
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Hadas
  • 1
  • 1

0 Answers0