Skip to content

Commit

Permalink
fix(verify): ensure verify with times respects rehearsal args (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcous authored Aug 10, 2021
1 parent 12ecb88 commit af39c43
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 17 deletions.
45 changes: 28 additions & 17 deletions decoy/verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,31 @@ def verify(
calls: Sequence[SpyCall],
times: Optional[int] = None,
) -> None:
"""Verify that a list of calls satisfies a given list of rehearsals."""
if times is not None:
if len(calls) == times:
return None

else:
for i in range(len(calls)):
calls_subset = calls[i : i + len(rehearsals)]

if calls_subset == rehearsals:
return None

raise VerifyError(
rehearsals=rehearsals,
calls=calls,
times=times,
)
"""Verify that a list of calls satisfies a given list of rehearsals.
Arguments:
rehearsals: Rehearsal calls to verify.
calls: All calls made to the spies in the rehearsals list.
times: Number of times the rehearsal sequence should appear in calls.
If omitted, will look for "at least once."
Raises:
VerifyError: the actual calls to the spy did not match the given
rehearsals.
"""
match_count = 0

for i in range(len(calls)):
calls_subset = calls[i : i + len(rehearsals)]

if calls_subset == rehearsals:
match_count = match_count + 1

calls_verified = match_count != 0 if times is None else match_count == times

if not calls_verified:
raise VerifyError(
rehearsals=rehearsals,
calls=calls,
times=times,
)
27 changes: 27 additions & 0 deletions tests/test_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ class VerifySpec(NamedTuple):
],
times=1,
),
VerifySpec(
rehearsals=[
VerifyRehearsal(spy_id=101, spy_name="spy_101", args=(1, 2, 3), kwargs={}),
],
calls=[
SpyCall(spy_id=101, spy_name="spy_101", args=(1, 2, 3), kwargs={}),
SpyCall(spy_id=101, spy_name="spy_101", args=(1, 2, 3), kwargs={}),
],
times=0,
),
]

verify_pass_specs = [
Expand Down Expand Up @@ -110,6 +120,23 @@ class VerifySpec(NamedTuple):
],
times=2,
),
VerifySpec(
rehearsals=[
VerifyRehearsal(spy_id=101, spy_name="spy_101", args=(1, 2, 3), kwargs={}),
],
calls=[
SpyCall(spy_id=101, spy_name="spy_101", args=(4, 5, 6), kwargs={}),
SpyCall(spy_id=101, spy_name="spy_101", args=(1, 2, 3), kwargs={}),
],
times=1,
),
VerifySpec(
rehearsals=[
VerifyRehearsal(spy_id=101, spy_name="spy_101", args=(1, 2, 3), kwargs={}),
],
calls=[],
times=0,
),
]


Expand Down

0 comments on commit af39c43

Please sign in to comment.