-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_integration.py
More file actions
355 lines (292 loc) · 12.5 KB
/
Copy pathtest_integration.py
File metadata and controls
355 lines (292 loc) · 12.5 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
"""Integration tests for gRPC recording and playback."""
from __future__ import annotations
from pathlib import Path
import pytest
from grpcvcr import (
Cassette,
MethodMatcher,
RecordingChannel,
RecordMode,
RequestMatcher,
recorded_channel,
)
from grpcvcr.errors import RecordingDisabledError
class TestUnaryRecordingPlayback:
"""Test unary RPC recording and playback."""
def test_record_unary_call(
self,
grpc_target: str,
tmp_cassette_path: Path,
grpc_servicer,
pb2,
pb2_grpc,
) -> None:
"""Record a unary RPC call."""
cassette = Cassette(tmp_cassette_path, record_mode=RecordMode.ALL)
with RecordingChannel(cassette, grpc_target) as recording:
stub = pb2_grpc.TestServiceStub(recording.channel)
response = stub.GetUser(pb2.GetUserRequest(id=42))
assert response.user.id == 42
assert response.user.name == "User 42"
assert grpc_servicer.call_count == 1
assert tmp_cassette_path.exists()
assert len(cassette.interactions) == 1
assert cassette.target == grpc_target
assert cassette.interactions[0].request.target == grpc_target
def test_preseeded_cassette_target_takes_precedence(
self,
grpc_target: str,
tmp_cassette_path: Path,
grpc_servicer,
pb2,
pb2_grpc,
) -> None:
"""An explicitly pre-seeded cassette target is not overwritten by the channel's target."""
cassette = Cassette(tmp_cassette_path, record_mode=RecordMode.ALL, target="preseeded-target")
with RecordingChannel(cassette, grpc_target) as recording:
stub = pb2_grpc.TestServiceStub(recording.channel)
stub.GetUser(pb2.GetUserRequest(id=42))
assert cassette.target == "preseeded-target"
def test_playback_unary_call(
self,
grpc_target: str,
tmp_cassette_path: Path,
grpc_servicer,
pb2,
pb2_grpc,
) -> None:
"""Record then playback a unary RPC call."""
cassette = Cassette(tmp_cassette_path, record_mode=RecordMode.ALL)
with RecordingChannel(cassette, grpc_target) as recording:
stub = pb2_grpc.TestServiceStub(recording.channel)
stub.GetUser(pb2.GetUserRequest(id=42))
assert grpc_servicer.call_count == 1
grpc_servicer.call_count = 0
cassette2 = Cassette(tmp_cassette_path, record_mode=RecordMode.NONE)
assert cassette2.interactions[0].request.target == grpc_target
with RecordingChannel(cassette2, grpc_target) as recording:
stub = pb2_grpc.TestServiceStub(recording.channel)
response = stub.GetUser(pb2.GetUserRequest(id=42))
assert response.user.id == 42
assert grpc_servicer.call_count == 0
def test_recorded_channel_context_manager(
self,
grpc_target: str,
tmp_cassette_path: Path,
grpc_servicer,
pb2,
pb2_grpc,
) -> None:
"""Test the recorded_channel convenience function."""
with recorded_channel(tmp_cassette_path, grpc_target, record_mode=RecordMode.ALL) as channel:
stub = pb2_grpc.TestServiceStub(channel)
response = stub.GetUser(pb2.GetUserRequest(id=1))
assert response.user.id == 1
assert grpc_servicer.call_count == 1
assert tmp_cassette_path.exists()
class TestRecordModes:
"""Test different recording modes."""
def test_none_mode_raises_without_cassette(
self,
grpc_target: str,
tmp_cassette_path: Path,
pb2,
pb2_grpc,
) -> None:
"""NONE mode raises when cassette doesn't exist."""
from grpcvcr.errors import CassetteNotFoundError
with pytest.raises(CassetteNotFoundError):
Cassette(tmp_cassette_path, record_mode=RecordMode.NONE)
def test_none_mode_raises_on_missing_interaction(
self,
grpc_target: str,
tmp_cassette_path: Path,
grpc_servicer,
pb2,
pb2_grpc,
) -> None:
"""NONE mode raises when interaction not found."""
matcher = MethodMatcher() & RequestMatcher()
cassette = Cassette(tmp_cassette_path, record_mode=RecordMode.ALL, match_on=matcher)
with RecordingChannel(cassette, grpc_target) as recording:
stub = pb2_grpc.TestServiceStub(recording.channel)
stub.GetUser(pb2.GetUserRequest(id=1))
cassette2 = Cassette(tmp_cassette_path, record_mode=RecordMode.NONE, match_on=matcher)
with pytest.raises(RecordingDisabledError), RecordingChannel(cassette2, grpc_target) as recording:
stub = pb2_grpc.TestServiceStub(recording.channel)
stub.GetUser(pb2.GetUserRequest(id=999))
def test_new_episodes_mode_records_new(
self,
grpc_target: str,
tmp_cassette_path: Path,
grpc_servicer,
pb2,
pb2_grpc,
) -> None:
"""NEW_EPISODES mode records new interactions."""
matcher = MethodMatcher() & RequestMatcher()
cassette = Cassette(tmp_cassette_path, record_mode=RecordMode.ALL, match_on=matcher)
with RecordingChannel(cassette, grpc_target) as recording:
stub = pb2_grpc.TestServiceStub(recording.channel)
stub.GetUser(pb2.GetUserRequest(id=1))
assert grpc_servicer.call_count == 1
cassette2 = Cassette(tmp_cassette_path, record_mode=RecordMode.NEW_EPISODES, match_on=matcher)
with RecordingChannel(cassette2, grpc_target) as recording:
stub = pb2_grpc.TestServiceStub(recording.channel)
stub.GetUser(pb2.GetUserRequest(id=1))
stub.GetUser(pb2.GetUserRequest(id=2))
assert grpc_servicer.call_count == 2
assert len(cassette2.interactions) == 2
def test_all_mode_overwrites(
self,
grpc_target: str,
tmp_cassette_path: Path,
grpc_servicer,
pb2,
pb2_grpc,
) -> None:
"""ALL mode always records, overwriting existing."""
matcher = MethodMatcher() & RequestMatcher()
cassette = Cassette(tmp_cassette_path, record_mode=RecordMode.ALL, match_on=matcher)
with RecordingChannel(cassette, grpc_target) as recording:
stub = pb2_grpc.TestServiceStub(recording.channel)
stub.GetUser(pb2.GetUserRequest(id=1))
cassette2 = Cassette(tmp_cassette_path, record_mode=RecordMode.ALL, match_on=matcher)
with RecordingChannel(cassette2, grpc_target) as recording:
stub = pb2_grpc.TestServiceStub(recording.channel)
stub.GetUser(pb2.GetUserRequest(id=1))
assert grpc_servicer.call_count == 2
assert len(cassette2.interactions) == 1
class TestServerStreaming:
"""Test server streaming RPC recording and playback."""
def test_record_server_streaming(
self,
grpc_target: str,
tmp_cassette_path: Path,
grpc_servicer,
pb2,
pb2_grpc,
) -> None:
"""Record a server streaming RPC call."""
cassette = Cassette(tmp_cassette_path, record_mode=RecordMode.ALL)
with RecordingChannel(cassette, grpc_target) as recording:
stub = pb2_grpc.TestServiceStub(recording.channel)
responses = list(stub.ListUsers(pb2.ListUsersRequest(limit=3)))
assert len(responses) == 3
assert responses[0].id == 1
assert responses[2].id == 3
assert grpc_servicer.call_count == 1
assert len(cassette.interactions) == 1
assert cassette.interactions[0].rpc_type == "server_streaming"
def test_playback_server_streaming(
self,
grpc_target: str,
tmp_cassette_path: Path,
grpc_servicer,
pb2,
pb2_grpc,
) -> None:
"""Playback a server streaming RPC call."""
cassette = Cassette(tmp_cassette_path, record_mode=RecordMode.ALL)
with RecordingChannel(cassette, grpc_target) as recording:
stub = pb2_grpc.TestServiceStub(recording.channel)
list(stub.ListUsers(pb2.ListUsersRequest(limit=3)))
grpc_servicer.call_count = 0
cassette2 = Cassette(tmp_cassette_path, record_mode=RecordMode.NONE)
with RecordingChannel(cassette2, grpc_target) as recording:
stub = pb2_grpc.TestServiceStub(recording.channel)
responses = list(stub.ListUsers(pb2.ListUsersRequest(limit=3)))
assert len(responses) == 3
assert grpc_servicer.call_count == 0
class TestClientStreaming:
"""Test client streaming RPC recording and playback."""
def test_record_client_streaming(
self,
grpc_target: str,
tmp_cassette_path: Path,
grpc_servicer,
pb2,
pb2_grpc,
) -> None:
"""Record a client streaming RPC call."""
cassette = Cassette(tmp_cassette_path, record_mode=RecordMode.ALL)
def request_iterator():
for name in ["Alice", "Bob", "Charlie"]:
yield pb2.CreateUserRequest(name=name, email=f"{name.lower()}@example.com")
with RecordingChannel(cassette, grpc_target) as recording:
stub = pb2_grpc.TestServiceStub(recording.channel)
response = stub.CreateUsers(request_iterator())
assert response.created_count == 3
assert grpc_servicer.call_count == 1
assert len(cassette.interactions) == 1
assert cassette.interactions[0].rpc_type == "client_streaming"
def test_playback_client_streaming(
self,
grpc_target: str,
tmp_cassette_path: Path,
grpc_servicer,
pb2,
pb2_grpc,
) -> None:
"""Playback a client streaming RPC call."""
cassette = Cassette(tmp_cassette_path, record_mode=RecordMode.ALL)
def request_iterator():
for name in ["Alice", "Bob", "Charlie"]:
yield pb2.CreateUserRequest(name=name, email=f"{name.lower()}@example.com")
with RecordingChannel(cassette, grpc_target) as recording:
stub = pb2_grpc.TestServiceStub(recording.channel)
stub.CreateUsers(request_iterator())
grpc_servicer.call_count = 0
cassette2 = Cassette(tmp_cassette_path, record_mode=RecordMode.NONE)
with RecordingChannel(cassette2, grpc_target) as recording:
stub = pb2_grpc.TestServiceStub(recording.channel)
response = stub.CreateUsers(request_iterator())
assert response.created_count == 3
assert grpc_servicer.call_count == 0
class TestBidiStreaming:
"""Test bidirectional streaming RPC recording and playback."""
def test_record_bidi_streaming(
self,
grpc_target: str,
tmp_cassette_path: Path,
grpc_servicer,
pb2,
pb2_grpc,
) -> None:
"""Record a bidirectional streaming RPC call."""
cassette = Cassette(tmp_cassette_path, record_mode=RecordMode.ALL)
def request_iterator():
yield pb2.ChatMessage(sender="client", content="Hello", timestamp=1000)
yield pb2.ChatMessage(sender="client", content="World", timestamp=2000)
with RecordingChannel(cassette, grpc_target) as recording:
stub = pb2_grpc.TestServiceStub(recording.channel)
responses = list(stub.Chat(request_iterator()))
assert len(responses) == 2
assert responses[0].content == "Echo: Hello"
assert responses[1].content == "Echo: World"
assert grpc_servicer.call_count == 1
assert len(cassette.interactions) == 1
assert cassette.interactions[0].rpc_type == "bidi_streaming"
def test_playback_bidi_streaming(
self,
grpc_target: str,
tmp_cassette_path: Path,
grpc_servicer,
pb2,
pb2_grpc,
) -> None:
"""Playback a bidirectional streaming RPC call."""
cassette = Cassette(tmp_cassette_path, record_mode=RecordMode.ALL)
def request_iterator():
yield pb2.ChatMessage(sender="client", content="Hello", timestamp=1000)
yield pb2.ChatMessage(sender="client", content="World", timestamp=2000)
with RecordingChannel(cassette, grpc_target) as recording:
stub = pb2_grpc.TestServiceStub(recording.channel)
list(stub.Chat(request_iterator()))
grpc_servicer.call_count = 0
cassette2 = Cassette(tmp_cassette_path, record_mode=RecordMode.NONE)
with RecordingChannel(cassette2, grpc_target) as recording:
stub = pb2_grpc.TestServiceStub(recording.channel)
responses = list(stub.Chat(request_iterator()))
assert len(responses) == 2
assert grpc_servicer.call_count == 0