Skip to content

Commit

Permalink
fix(spy): ensure __eq__ of rehearsal arg is preferred (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcous authored Jun 8, 2023
1 parent 7189c56 commit 63413d8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
6 changes: 3 additions & 3 deletions decoy/spy_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ def match_event(event: AnySpyEvent, rehearsal: SpyRehearsal) -> bool:

try:
args_match = all(
call.args[i] == value for i, value in enumerate(rehearsed_call.args)
value == call.args[i] for i, value in enumerate(rehearsed_call.args)
)
kwargs_match = all(
call.kwargs[key] == value
value == call.kwargs[key]
for key, value in rehearsed_call.kwargs.items()
)

Expand All @@ -95,4 +95,4 @@ def match_event(event: AnySpyEvent, rehearsal: SpyRehearsal) -> bool:
except (IndexError, KeyError):
return False

return event.payload == rehearsal.payload
return rehearsal.payload == event.payload
47 changes: 47 additions & 0 deletions tests/test_spy_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,50 @@ def test_match_event(
"""It should match a call to a rehearsal."""
result = match_event(event, rehearsal)
assert result is expected_result


def test_match_eq_override() -> None:
"""It should prefer __eq__ from the rehearsal."""

class _Matcher:
def __eq__(self, other: object) -> bool:
return True

class _Value:
def __eq__(self, other: object) -> bool:
return False

event_args = SpyEvent(
spy=SpyInfo(id=42, name="my_spy", is_async=False),
payload=SpyCall(args=(_Value(),), kwargs={}),
)

event_kwargs = SpyEvent(
spy=SpyInfo(id=42, name="my_spy", is_async=False),
payload=SpyCall(args=(), kwargs={"value": _Value()}),
)

rehearsal_ars = WhenRehearsal(
spy=SpyInfo(id=42, name="my_spy", is_async=False),
payload=SpyCall(args=(_Matcher(),), kwargs={}),
)

rehearsal_kwargs = WhenRehearsal(
spy=SpyInfo(id=42, name="my_spy", is_async=False),
payload=SpyCall(args=(), kwargs={"value": _Matcher()}),
)

rehearsal_args_ignore_extra = WhenRehearsal(
spy=SpyInfo(id=42, name="my_spy", is_async=False),
payload=SpyCall(args=(_Matcher(),), kwargs={}, ignore_extra_args=True),
)

rehearsal_kwargs_ignore_extra = WhenRehearsal(
spy=SpyInfo(id=42, name="my_spy", is_async=False),
payload=SpyCall(args=(), kwargs={"value": _Matcher()}, ignore_extra_args=True),
)

assert match_event(event_args, rehearsal_ars) is True
assert match_event(event_kwargs, rehearsal_kwargs) is True
assert match_event(event_args, rehearsal_args_ignore_extra) is True
assert match_event(event_kwargs, rehearsal_kwargs_ignore_extra) is True

0 comments on commit 63413d8

Please sign in to comment.