Search code examples
pythonunit-testingmockingpython-mock

mocking python libraries or wrapper method


In my code release_bundler.py file

class MavenDependenciesPathsBuilderStrategy(PathsBuilderStrategy):

  def build_bundler_copy_paths(self, mapping_values):
    source = join(getcwd(),'target','dependency',mapping_values[0])
    destination = join(getcwd(),'target','generated-resources',mapping_values[1],mapping_values[0])
    return [source, destination]

class NestedModulePathsFilterStrategy(FilterStrategy):

  def filter_changeset_paths(self, changeset_paths, bundling_map, paths_builder_strategy):
    for mapping_key in bundling_map.keys():
      if(mapping_key in changeset_paths):
        mapping_values = bundling_map.get(mapping_key).values()
        copy_paths = paths_builder_strategy.build_bundler_copy_paths(mapping_values)
        return copy_paths

If I want to test the filter_changeset_paths method, I'll have to mock both getcwd method inside the build_bundler_copy_paths method or mocking only the latter will do?

I tried mocking the method in my tests release_bundler_test.py, importing classed like this:

from release_bundler import NestedModulePathsFilterStrategy, MavenDependenciesPathsBuilderStrategy

then patch the MavenDependenciesPathsBuilderStrategy class

def mock_build_bundler_copy_paths(self, mapping_values):
  return ['/cwd/foo','/cwd/bar']

@mock.patch('release_bundler.MavenDependenciesPathsBuilderStrategy', 'build_bundler_copy_paths', mock_build_bundler_copy_paths)
  def test_nested_module_filter_changeset_paths(self):
    pc_maps = {'ProcessingComponents/ProcessContainer':['ProcessContainerInstaller-bin.zip','SERVICES/Installer'],'ProcessingComponents/DataGrid':['ProcessContainerInstaller-bin.zip','SERVICES/Installer']}
    changed_paths =  ['ProcessingComponents/ProcessContainer/ProcessContainerRuntime/main/java/com/suntecgroup/tbms/container/ContainerException.java']
    filter_test = NestedModulePathsFilterStrategy()
    result = filter_test.filter_changeset_paths(changed_paths,pc_maps, MavenDependenciesPathsBuilderStrategy())
    self.assertIsNotNone(result)
    self.assertEquals(result[0], '/cwd/foo')
    self.assertEquals(result[0], '/cwd/bar')

but I don't think this mock works because self.assertIsNotNone(result) fails

So the questions are:

  1. Am I mocking the right way? can't get my head over it
  2. will mocking on the MavenDependenciesPathsBuilderStrategy method do or I have to mock the os.getcwd inside it's method also?

Solution

  • My bad, my newbie python brains took a while to get a hang of things:

    this is how it works well

      @mock.patch('release_bundler.MavenDependenciesPathsBuilderStrategy.build_bundler_copy_paths')
      def test_filters_pc_from_changeset_paths(self, mock_build_bundler_copy_paths):
        pc_maps = {'ProcessingComponents/ProcessContainer':['ProcessContainerInstaller-bin.zip','SERVICES/Installer']}
        changed_paths =  ['ProcessingComponents/ProcessContainer/ProcessContainerRuntime/main/java/com/suntecgroup/tbms/container/ContainerException.java']
        with mock.patch('release_bundler.NestedModulePathsFilterStrategy.do_copy') as mock_do_copy: 
          mock_do_copy.return_value = ''
          filter_test = NestedModulePathsFilterStrategy()
          filter_test.filter_changeset_paths(changed_paths,pc_maps, MavenDependenciesPathsBuilderStrategy())
          mock_build_bundler_copy_paths.assert_called_with(['ProcessContainerInstaller-bin.zip','SERVICES/Installer'])
          mock_do_copy.assert_called()
    

    I might be wrong but I think we cant apply multiple decorators to a method?