Skip to content

Commit af39c43

Browse files
authored
fix(verify): ensure verify with times respects rehearsal args (#57)
1 parent 12ecb88 commit af39c43

File tree

2 files changed

+55
-17
lines changed

2 files changed

+55
-17
lines changed

decoy/verifier.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,31 @@ def verify(
1414
calls: Sequence[SpyCall],
1515
times: Optional[int] = None,
1616
) -> None:
17-
"""Verify that a list of calls satisfies a given list of rehearsals."""
18-
if times is not None:
19-
if len(calls) == times:
20-
return None
21-
22-
else:
23-
for i in range(len(calls)):
24-
calls_subset = calls[i : i + len(rehearsals)]
25-
26-
if calls_subset == rehearsals:
27-
return None
28-
29-
raise VerifyError(
30-
rehearsals=rehearsals,
31-
calls=calls,
32-
times=times,
33-
)
17+
"""Verify that a list of calls satisfies a given list of rehearsals.
18+
19+
Arguments:
20+
rehearsals: Rehearsal calls to verify.
21+
calls: All calls made to the spies in the rehearsals list.
22+
times: Number of times the rehearsal sequence should appear in calls.
23+
If omitted, will look for "at least once."
24+
25+
Raises:
26+
VerifyError: the actual calls to the spy did not match the given
27+
rehearsals.
28+
"""
29+
match_count = 0
30+
31+
for i in range(len(calls)):
32+
calls_subset = calls[i : i + len(rehearsals)]
33+
34+
if calls_subset == rehearsals:
35+
match_count = match_count + 1
36+
37+
calls_verified = match_count != 0 if times is None else match_count == times
38+
39+
if not calls_verified:
40+
raise VerifyError(
41+
rehearsals=rehearsals,
42+
calls=calls,
43+
times=times,
44+
)

tests/test_verifier.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ class VerifySpec(NamedTuple):
6464
],
6565
times=1,
6666
),
67+
VerifySpec(
68+
rehearsals=[
69+
VerifyRehearsal(spy_id=101, spy_name="spy_101", args=(1, 2, 3), kwargs={}),
70+
],
71+
calls=[
72+
SpyCall(spy_id=101, spy_name="spy_101", args=(1, 2, 3), kwargs={}),
73+
SpyCall(spy_id=101, spy_name="spy_101", args=(1, 2, 3), kwargs={}),
74+
],
75+
times=0,
76+
),
6777
]
6878

6979
verify_pass_specs = [
@@ -110,6 +120,23 @@ class VerifySpec(NamedTuple):
110120
],
111121
times=2,
112122
),
123+
VerifySpec(
124+
rehearsals=[
125+
VerifyRehearsal(spy_id=101, spy_name="spy_101", args=(1, 2, 3), kwargs={}),
126+
],
127+
calls=[
128+
SpyCall(spy_id=101, spy_name="spy_101", args=(4, 5, 6), kwargs={}),
129+
SpyCall(spy_id=101, spy_name="spy_101", args=(1, 2, 3), kwargs={}),
130+
],
131+
times=1,
132+
),
133+
VerifySpec(
134+
rehearsals=[
135+
VerifyRehearsal(spy_id=101, spy_name="spy_101", args=(1, 2, 3), kwargs={}),
136+
],
137+
calls=[],
138+
times=0,
139+
),
113140
]
114141

115142

0 commit comments

Comments
 (0)