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 called_with with assert_called_with #182

Closed
tirkarthi opened this issue Jan 3, 2023 · 1 comment
Closed

Replace called_with with assert_called_with #182

tirkarthi opened this issue Jan 3, 2023 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@tirkarthi
Copy link

called_with is an incorrect method that just returns a Mock object that evaluates to True and doesn't perform appropriate assertion. This method should be replaced with assert_called_with . Related CPython issue : python/cpython#100690 . Changes cause below test failures.

pytest tests/test_core.py           
==================================================================== test session starts =====================================================================
platform linux -- Python 3.11.1, pytest-7.2.0, pluggy-1.0.0
rootdir: /root/checked_repos_clone_3900_4000/datacompy, configfile: pytest.ini
plugins: cov-2.12.1
collected 81 items                                                                                                                                           

tests/test_core.py .............................FF..................................................                                                   [100%]

========================================================================== FAILURES ==========================================================================
________________________________________________________________________ test_subset _________________________________________________________________________

mock_debug = <MagicMock name='debug' id='140448511508688'>

    @mock.patch("datacompy.logging.debug")
    def test_subset(mock_debug):
        df1 = pd.DataFrame([{"a": 1, "b": 2, "c": "hi"}, {"a": 2, "b": 2, "c": "yo"}])
        df2 = pd.DataFrame([{"a": 1, "c": "hi"}])
        comp = datacompy.Compare(df1, df2, ["a"])
        assert comp.subset()
>       mock_debug.assert_called_with("Checking equality")

tests/test_core.py:529: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <MagicMock name='debug' id='140448511508688'>, args = ('Checking equality',), kwargs = {}, expected = "debug('Checking equality')"
actual = 'not called.', error_message = "expected call not found.\nExpected: debug('Checking equality')\nActual: not called."

    def assert_called_with(self, /, *args, **kwargs):
        """assert that the last call was made with the specified arguments.
    
        Raises an AssertionError if the args and keyword args passed in are
        different to the last call to the mock."""
        if self.call_args is None:
            expected = self._format_mock_call_signature(args, kwargs)
            actual = 'not called.'
            error_message = ('expected call not found.\nExpected: %s\nActual: %s'
                    % (expected, actual))
>           raise AssertionError(error_message)
E           AssertionError: expected call not found.
E           Expected: debug('Checking equality')
E           Actual: not called.

/usr/lib/python3.11/unittest/mock.py:924: AssertionError
______________________________________________________________________ test_not_subset _______________________________________________________________________

mock_info = <MagicMock name='info' id='140448491660176'>

    @mock.patch("datacompy.logging.info")
    def test_not_subset(mock_info):
        df1 = pd.DataFrame([{"a": 1, "b": 2, "c": "hi"}, {"a": 2, "b": 2, "c": "yo"}])
        df2 = pd.DataFrame([{"a": 1, "b": 2, "c": "hi"}, {"a": 2, "b": 2, "c": "great"}])
        comp = datacompy.Compare(df1, df2, ["a"])
        assert not comp.subset()
>       mock_info.assert_called_with("Sample c mismatch: a: 2, df1: yo, df2: great")

tests/test_core.py:538: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <MagicMock name='info' id='140448491660176'>, args = ('Sample c mismatch: a: 2, df1: yo, df2: great',), kwargs = {}
expected = "info('Sample c mismatch: a: 2, df1: yo, df2: great')", actual = 'not called.'
error_message = "expected call not found.\nExpected: info('Sample c mismatch: a: 2, df1: yo, df2: great')\nActual: not called."

    def assert_called_with(self, /, *args, **kwargs):
        """assert that the last call was made with the specified arguments.
    
        Raises an AssertionError if the args and keyword args passed in are
        different to the last call to the mock."""
        if self.call_args is None:
            expected = self._format_mock_call_signature(args, kwargs)
            actual = 'not called.'
            error_message = ('expected call not found.\nExpected: %s\nActual: %s'
                    % (expected, actual))
>           raise AssertionError(error_message)
E           AssertionError: expected call not found.
E           Expected: info('Sample c mismatch: a: 2, df1: yo, df2: great')
E           Actual: not called.

/usr/lib/python3.11/unittest/mock.py:924: AssertionError
====================================================================== warnings summary ======================================================================
../../checked_repos/taskcat/.env/lib/python3.11/site-packages/_pytest/config/__init__.py:1294
  /root/checked_repos/taskcat/.env/lib/python3.11/site-packages/_pytest/config/__init__.py:1294: PytestConfigWarning: Unknown config option: spark_options
  
    self._warn_or_fail_if_strict(f"Unknown config option: {key}\n")

tests/test_core.py:29
  /root/checked_repos_clone_3900_4000/datacompy/tests/test_core.py:29: FutureWarning: pandas.util.testing is deprecated. Use the functions in the public API at pandas.testing instead.
    from pandas.util.testing import assert_series_equal

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
================================================================== short test summary info ===================================================================
FAILED tests/test_core.py::test_subset - AssertionError: expected call not found.
FAILED tests/test_core.py::test_not_subset - AssertionError: expected call not found.
========================================================== 2 failed, 79 passed, 2 warnings in 1.69s ==========================================================
@fdosani
Copy link
Member

fdosani commented Jan 4, 2023

@tirkarthi Thanks for flagging this. I'll take a look shortly and put in a fix. Appreciate the heads up.

@fdosani fdosani self-assigned this Jan 4, 2023
@fdosani fdosani added the bug Something isn't working label Jan 4, 2023
fdosani pushed a commit that referenced this issue Jan 5, 2023
@fdosani fdosani closed this as completed in 6da52ff Feb 2, 2023
rhaffar pushed a commit to rhaffar/datacompy that referenced this issue Sep 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants