-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_stub_store.py
163 lines (134 loc) · 4.73 KB
/
test_stub_store.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"""Tests for stub behavior storage."""
import pytest
from decoy.spy_events import SpyCall, SpyEvent, SpyInfo, WhenRehearsal
from decoy.stub_store import StubStore, StubBehavior
def test_get_by_call() -> None:
"""It should be able to add a StubBehavior to the store and get it back."""
subject = StubStore()
rehearsal = WhenRehearsal(
spy=SpyInfo(id=42, name="my_spy", is_async=False),
payload=SpyCall(args=(), kwargs={}),
)
behavior = StubBehavior(return_value="hello world")
subject.add(rehearsal=rehearsal, behavior=behavior)
result = subject.get_by_call(
call=SpyEvent(
spy=SpyInfo(id=42, name="my_spy", is_async=False),
payload=SpyCall(args=(), kwargs={}),
)
)
assert result == behavior
def test_get_by_call_prefers_latest() -> None:
"""It should be prefer later stubs if multiple exist."""
subject = StubStore()
rehearsal_1 = WhenRehearsal(
spy=SpyInfo(id=42, name="my_spy", is_async=False),
payload=SpyCall(args=(), kwargs={}),
)
behavior_1 = StubBehavior(return_value="hello")
rehearsal_2 = WhenRehearsal(
spy=SpyInfo(id=42, name="my_spy", is_async=False),
payload=SpyCall(args=(), kwargs={}),
)
behavior_2 = StubBehavior(return_value="goodbye")
subject.add(rehearsal=rehearsal_1, behavior=behavior_1)
subject.add(rehearsal=rehearsal_2, behavior=behavior_2)
result = subject.get_by_call(
call=SpyEvent(
spy=SpyInfo(id=42, name="my_spy", is_async=False),
payload=SpyCall(args=(), kwargs={}),
)
)
assert result == behavior_2
def test_get_by_call_empty() -> None:
"""It should return None if store is empty."""
subject = StubStore()
result = subject.get_by_call(
call=SpyEvent(
spy=SpyInfo(id=42, name="my_spy", is_async=False),
payload=SpyCall(args=(), kwargs={}),
)
)
assert result is None
@pytest.mark.parametrize(
"call",
[
SpyEvent(
# spy_id does not match
spy=SpyInfo(id=1000000000, name="my_spy", is_async=False),
payload=SpyCall(
args=("hello", "world"),
kwargs={"goodbye": "so long"},
),
),
SpyEvent(
spy=SpyInfo(id=42, name="my_spy", is_async=False),
# args do not match
payload=SpyCall(
args=("hello", "wisconsin"),
kwargs={"goodbye": "so long"},
),
),
SpyEvent(
spy=SpyInfo(id=42, name="my_spy", is_async=False),
payload=SpyCall(
args=("hello", "wisconsin"),
# kwargs do not match
kwargs={"goodbye": "sofa"},
),
),
],
)
def test_get_by_call_no_match(call: SpyEvent) -> None:
"""It should return a no-op StubBehavior if there are no matching calls."""
subject = StubStore()
rehearsal = WhenRehearsal(
spy=SpyInfo(id=42, name="my_spy", is_async=False),
payload=SpyCall(
args=("hello", "world"),
kwargs={"goodbye": "so long"},
),
)
behavior = StubBehavior(return_value="fizzbuzz")
subject.add(rehearsal=rehearsal, behavior=behavior)
result = subject.get_by_call(call=call)
assert result is None
def test_get_by_call_once_behavior() -> None:
"""It should consume any behavior marked with the `once` flag."""
subject = StubStore()
rehearsal = WhenRehearsal(
spy=SpyInfo(id=42, name="my_spy", is_async=False),
payload=SpyCall(args=(1, 2, 3), kwargs={}),
)
behavior = StubBehavior(return_value="fizzbuzz", once=True)
subject.add(rehearsal=rehearsal, behavior=behavior)
result = subject.get_by_call(
call=SpyEvent(
spy=SpyInfo(id=42, name="my_spy", is_async=False),
payload=SpyCall(args=(1, 2, 3), kwargs={}),
)
)
assert result == behavior
result = subject.get_by_call(
call=SpyEvent(
spy=SpyInfo(id=42, name="my_spy", is_async=False),
payload=SpyCall(args=(1, 2, 3), kwargs={}),
)
)
assert result is None
def test_clear() -> None:
"""It should consume any behavior marked with the `once` flag."""
subject = StubStore()
call = SpyEvent(
spy=SpyInfo(id=42, name="my_spy", is_async=False),
payload=SpyCall(args=(), kwargs={}),
)
rehearsal = WhenRehearsal(
spy=SpyInfo(id=42, name="my_spy", is_async=False),
payload=SpyCall(args=(), kwargs={}),
)
behavior = StubBehavior(return_value="fizzbuzz")
subject.add(rehearsal=rehearsal, behavior=behavior)
subject.clear()
result = subject.get_by_call(call=call)
assert result is None