Skip to content

Commit

Permalink
Fix comparison on _message_published without data specified (#28)
Browse files Browse the repository at this point in the history
* Fix comparison on _message_published without data specified

In HedwigPublishMock._message_published(),
    (data is None or msg.data) == data
should be
    (data is None or msg.data == data)

Instead, simplify the comparison by using as the default for data a
dict subclass that compares equal to any dict.

Add a unit test for the plugin that exercises the case of omitting the
optional data argument.

* Replace .format() with f-string

...after noting that the code doesn't use .format() elsewhere and is
targeted only at 3.6+.
  • Loading branch information
charness committed Feb 15, 2019
1 parent f0106f1 commit 8ff3f47
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
16 changes: 13 additions & 3 deletions hedwig/testing/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,32 @@
__all__ = ['mock_hedwig_publish']


class AnyDict(dict):
"""An object equal to any dict."""

def __eq__(self, other):
return isinstance(other, dict)

def __repr__(self):
return f'{type(self).__name__}()'


class HedwigPublishMock(mock.MagicMock):
"""
Custom mock class used by :meth:`hedwig.testing.pytest_plugin.mock_hedwig_publish` to mock the publisher.
"""

def _message_published(self, msg_type, data: Optional[dict], version: Union[str, StrictVersion]) -> bool:
return any(
msg.type == msg_type and (data is None or msg.data) == data and msg.data_schema_version == version
msg.type == msg_type and msg.data == data and msg.data_schema_version == version
for (msg,), _ in self.call_args_list
)

def _error_message(self) -> str:
return pprint.pformat([(msg.type, msg.data, msg.data_schema_version) for (msg,), _ in self.call_args_list])

def assert_message_published(
self, msg_type, data: Optional[dict] = None, version: Union[str, StrictVersion] = StrictVersion('1.0')
self, msg_type, data: Optional[dict] = AnyDict(), version: Union[str, StrictVersion] = StrictVersion('1.0')
) -> None:
"""
Helper function to check if a Hedwig message with given type, data
Expand All @@ -36,7 +46,7 @@ def assert_message_published(
assert self._message_published(msg_type, data, version), self._error_message()

def assert_message_not_published(
self, msg_type, data: Optional[dict] = None, version: Union[str, StrictVersion] = StrictVersion('1.0')
self, msg_type, data: Optional[dict] = AnyDict(), version: Union[str, StrictVersion] = StrictVersion('1.0')
) -> None:
"""
Helper function to check that a Hedwig message of given type, data
Expand Down
7 changes: 6 additions & 1 deletion tests/test_pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,16 @@ def test_mock_hedwig_publish_publish_check_same_type(mock_hedwig_publish, messag
def test_mock_hedwig_publish_published(mock_hedwig_publish, message):
message.publish()
mock_hedwig_publish.assert_message_published(message.type, data=message.data, version=message.data_schema_version)
def test_mock_hedwig_publish_published_without_checking_data(mock_hedwig_publish, message):
message.publish()
mock_hedwig_publish.assert_message_published(message.type, version=message.data_schema_version)
"""
)

# run all tests with pytest
result = testdir.runpytest()

# check that all 3 tests passed
result.assert_outcomes(passed=4)
result.assert_outcomes(passed=5)

0 comments on commit 8ff3f47

Please sign in to comment.