Skip to content

Commit

Permalink
fix(warnings): do not trigger MiscalledStubWarnings from prop events (#…
Browse files Browse the repository at this point in the history
…126)

Closes #125
  • Loading branch information
mcous authored Mar 25, 2022
1 parent 5db349b commit 396eae2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
12 changes: 7 additions & 5 deletions decoy/warning_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from .spy_events import (
AnySpyEvent,
SpyCall,
SpyEvent,
VerifyRehearsal,
WhenRehearsal,
Expand All @@ -22,14 +23,15 @@ def check(all_calls: Sequence[AnySpyEvent]) -> None:
_check_no_redundant_verify(all_calls)


def _check_no_miscalled_stubs(all_calls: Sequence[AnySpyEvent]) -> None:
def _check_no_miscalled_stubs(all_events: Sequence[AnySpyEvent]) -> None:
"""Ensure every call matches a rehearsal, if the spy has rehearsals."""
all_calls_by_id: Dict[int, List[AnySpyEvent]] = {}

for call in all_calls:
spy_id = call.spy_id
spy_calls = all_calls_by_id.get(spy_id, [])
all_calls_by_id[spy_id] = spy_calls + [call]
for event in all_events:
if isinstance(event.payload, SpyCall):
spy_id = event.spy_id
spy_calls = all_calls_by_id.get(spy_id, [])
all_calls_by_id[spy_id] = spy_calls + [event]

for spy_calls in all_calls_by_id.values():
unmatched: List[SpyEvent] = []
Expand Down
27 changes: 27 additions & 0 deletions tests/test_warning_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
AnySpyEvent,
SpyCall,
SpyEvent,
SpyPropAccess,
PropAccessType,
WhenRehearsal,
VerifyRehearsal,
)
Expand Down Expand Up @@ -77,6 +79,31 @@ class WarningCheckerSpec(NamedTuple):
],
expected_warnings=[],
),
# it should not warn due to spy prop access
WarningCheckerSpec(
all_calls=[
WhenRehearsal(
spy_id=1,
spy_name="spy",
payload=SpyPropAccess(
prop_name="prop_name", access_type=PropAccessType.GET
),
),
SpyEvent(
spy_id=1,
spy_name="spy",
payload=SpyPropAccess(
prop_name="other_prop", access_type=PropAccessType.GET
),
),
WhenRehearsal(
spy_id=2,
spy_name="spy.other_prop",
payload=SpyCall(args=(), kwargs={}, ignore_extra_args=False),
),
],
expected_warnings=[],
),
# it should warn if a spy has multiple rehearsals without a matching call
WarningCheckerSpec(
all_calls=[
Expand Down

0 comments on commit 396eae2

Please sign in to comment.