Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace has_calls with assert_has_calls #773

Closed
tirkarthi opened this issue Jan 3, 2023 · 0 comments · Fixed by #777
Closed

Replace has_calls with assert_has_calls #773

tirkarthi opened this issue Jan 3, 2023 · 0 comments · Fixed by #777

Comments

@tirkarthi
Copy link
Contributor

has_calls is an incorrect method that just returns a Mock object and doesn't perform any assertion. Replacing the method with assert_has_calls has following test failure.

fs_mock.create_project_directory.has_calls(calls)

fs_mock.load_template.has_calls(calls)

fs_mock.generate_file.has_calls(calls)

pytest tests/test_project_generator.py
==================================================================== test session starts =====================================================================
platform linux -- Python 3.11.1, pytest-7.2.0, pluggy-1.0.0
rootdir: /root/checked_repos/taskcat, configfile: setup.cfg
plugins: cov-2.12.1
collected 1 item                                                                                                                                             

tests/test_project_generator.py F                                                                                                                      [100%]

========================================================================== FAILURES ==========================================================================
_________________________________________________________ TestProjectGenerator.test_generate_project _________________________________________________________

self = <tests.test_project_generator.TestProjectGenerator testMethod=test_generate_project>

    def test_generate_project(self):
        mock_filesystem_service = self._mock_filesystem_service()
        ProjectGenerator(
            self._quickstart_configuration(), TEST_DESTINATION, mock_filesystem_service
        ).generate()
>       self._verify_filesystem_calls(mock_filesystem_service)

tests/test_project_generator.py:29: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
tests/test_project_generator.py:60: in _verify_filesystem_calls
    self._verify_generate_file_calls(fs_mock)
tests/test_project_generator.py:79: in _verify_generate_file_calls
    fs_mock.generate_file.assert_has_calls(calls)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <Mock name='mock.generate_file' id='140093292046992'>
calls = [call('/tmp/new_project/README.md.jinja', '### awesome_new_project'), call('/tmp/new_project/ci/taskcat-autobucket.yml...emplate.json.jinja', 'owner owner@example.com'), call('/tmp/new_project/nested/dir/some-file.txt.jinja', 'quickstart')]
any_order = False

    def assert_has_calls(self, calls, any_order=False):
        """assert the mock has been called with the specified calls.
        The `mock_calls` list is checked for the calls.
    
        If `any_order` is False (the default) then the calls must be
        sequential. There can be extra calls before or after the
        specified calls.
    
        If `any_order` is True then the calls can be in any order, but
        they must all appear in `mock_calls`."""
        expected = [self._call_matcher(c) for c in calls]
        cause = next((e for e in expected if isinstance(e, Exception)), None)
        all_calls = _CallList(self._call_matcher(c) for c in self.mock_calls)
        if not any_order:
            if expected not in all_calls:
                if cause is None:
                    problem = 'Calls not found.'
                else:
                    problem = ('Error processing expected calls.\n'
                               'Errors: {}').format(
                                   [e if isinstance(e, Exception) else None
                                    for e in expected])
>               raise AssertionError(
                    f'{problem}\n'
                    f'Expected: {_CallList(calls)}'
                    f'{self._calls_repr(prefix="Actual").rstrip(".")}'
                ) from cause
E               AssertionError: Calls not found.
E               Expected: [call('/tmp/new_project/README.md.jinja', '### awesome_new_project'),
E                call('/tmp/new_project/ci/taskcat-autobucket.yml.jinja', 'project awesome_new_project'),
E                call('/tmp/new_project/template/some-template.json.jinja', 'owner owner@example.com'),
E                call('/tmp/new_project/nested/dir/some-file.txt.jinja', 'quickstart')]
E               Actual: [call('### ', '/tmp/new_project/README.md'),
E                call('project ', '/tmp/new_project/ci/taskcat-autobucket.yml'),
E                call('owner ', '/tmp/new_project/template/some-template.json'),
E                call('quickstart', '/tmp/new_project/nested/dir/some-file.txt')]

/usr/lib/python3.11/unittest/mock.py:970: AssertionError
====================================================================== warnings summary ======================================================================
.env/lib/python3.11/site-packages/pkg_resources/__init__.py:1154
  /root/checked_repos/taskcat/.env/lib/python3.11/site-packages/pkg_resources/__init__.py:1154: DeprecationWarning: Use of .. or absolute path in a resource path is not allowed and will raise exceptions in a future release.
    return get_provider(package_or_requirement).get_resource_filename(

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
================================================================== short test summary info ===================================================================
FAILED tests/test_project_generator.py::TestProjectGenerator::test_generate_project - AssertionError: Calls not found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant