Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit 6d585ec

Browse files
committed
refactoring
1 parent c628d21 commit 6d585ec

2 files changed

Lines changed: 39 additions & 41 deletions

File tree

tests/unit/data/_async/test_metrics_interceptor.py

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,19 @@
3333

3434
__CROSS_SYNC_OUTPUT__ = "tests.unit.data._sync_autogen.test_metrics_interceptor"
3535

36-
37-
36+
@CrossSync.convert(replace_symbols={"__aiter__": "__iter__"})
37+
def _make_mock_stream_call(values, exc=None):
38+
"""
39+
Create a mock call object that can be used for streaming calls
40+
"""
41+
call = CrossSync.Mock()
42+
async def gen():
43+
for val in values:
44+
yield val
45+
if exc:
46+
raise exc
47+
call.__aiter__ = mock.Mock(return_value=gen())
48+
return call
3849

3950

4051
@CrossSync.convert_class(sync_name="TestMetricsInterceptor")
@@ -251,15 +262,8 @@ async def test_unary_stream_interceptor_success(self):
251262
op.first_response_latency = None
252263
instance.operation_map[op.uuid] = op
253264

254-
continuation = CrossSync.Mock()
265+
continuation = CrossSync.Mock(return_value=_make_mock_stream_call([1, 2]))
255266
call = continuation.return_value
256-
if CrossSync.is_async:
257-
async def gen():
258-
yield 1
259-
yield 2
260-
call.__aiter__ = mock.Mock(return_value=gen())
261-
else:
262-
call.__iter__ = mock.Mock(return_value=iter([1, 2]))
263267
call.trailing_metadata = CrossSync.Mock(return_value=[("a", "b")])
264268
call.initial_metadata = CrossSync.Mock(return_value=[("c", "d")])
265269
details = mock.Mock()
@@ -288,19 +292,8 @@ async def test_unary_stream_interceptor_failure_mid_stream(self):
288292
op.first_response_latency = None
289293
instance.operation_map[op.uuid] = op
290294
exc = ValueError("test")
291-
292-
continuation = CrossSync.Mock()
295+
continuation = CrossSync.Mock(return_value=_make_mock_stream_call([1], exc=exc))
293296
call = continuation.return_value
294-
if CrossSync.is_async:
295-
async def mock_generator():
296-
yield 1
297-
raise exc
298-
call.__aiter__ = mock.Mock(return_value=mock_generator())
299-
else:
300-
def mock_generator():
301-
yield 1
302-
raise exc
303-
call.__iter__ = mock.Mock(return_value=mock_generator())
304297
call.trailing_metadata = CrossSync.Mock(return_value=[("a", "b")])
305298
call.initial_metadata = CrossSync.Mock(return_value=[("c", "d")])
306299
details = mock.Mock()
@@ -445,15 +438,8 @@ async def test_unary_stream_interceptor_start_operation(self, initial_state):
445438
op.state = initial_state
446439
instance.operation_map[op.uuid] = op
447440

448-
continuation = CrossSync.Mock()
441+
continuation = CrossSync.Mock(return_value=_make_mock_stream_call([1, 2]))
449442
call = continuation.return_value
450-
if CrossSync.is_async:
451-
async def gen():
452-
yield 1
453-
yield 2
454-
call.__aiter__ = mock.Mock(return_value=gen())
455-
else:
456-
call.__iter__ = mock.Mock(return_value=iter([1, 2]))
457443
call.trailing_metadata = CrossSync.Mock(return_value=[])
458444
call.initial_metadata = CrossSync.Mock(return_value=[])
459445
details = mock.Mock()

tests/unit/data/_sync_autogen/test_metrics_interceptor.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@
2929
)
3030

3131

32+
def _make_mock_stream_call(values, exc=None):
33+
"""Create a mock call object that can be used for streaming calls"""
34+
call = CrossSync._Sync_Impl.Mock()
35+
36+
def gen():
37+
for val in values:
38+
yield val
39+
if exc:
40+
raise exc
41+
42+
call.__iter__ = mock.Mock(return_value=gen())
43+
return call
44+
45+
3246
class TestMetricsInterceptor:
3347
@staticmethod
3448
def _get_target_class():
@@ -224,9 +238,10 @@ def test_unary_stream_interceptor_success(self):
224238
op.start_time_ns = 0
225239
op.first_response_latency = None
226240
instance.operation_map[op.uuid] = op
227-
continuation = CrossSync._Sync_Impl.Mock()
241+
continuation = CrossSync._Sync_Impl.Mock(
242+
return_value=_make_mock_stream_call([1, 2])
243+
)
228244
call = continuation.return_value
229-
call.__iter__ = mock.Mock(return_value=iter([1, 2]))
230245
call.trailing_metadata = CrossSync._Sync_Impl.Mock(return_value=[("a", "b")])
231246
call.initial_metadata = CrossSync._Sync_Impl.Mock(return_value=[("c", "d")])
232247
details = mock.Mock()
@@ -254,14 +269,10 @@ def test_unary_stream_interceptor_failure_mid_stream(self):
254269
op.first_response_latency = None
255270
instance.operation_map[op.uuid] = op
256271
exc = ValueError("test")
257-
continuation = CrossSync._Sync_Impl.Mock()
272+
continuation = CrossSync._Sync_Impl.Mock(
273+
return_value=_make_mock_stream_call([1], exc=exc)
274+
)
258275
call = continuation.return_value
259-
260-
def mock_generator():
261-
yield 1
262-
raise exc
263-
264-
call.__iter__ = mock.Mock(return_value=mock_generator())
265276
call.trailing_metadata = CrossSync._Sync_Impl.Mock(return_value=[("a", "b")])
266277
call.initial_metadata = CrossSync._Sync_Impl.Mock(return_value=[("c", "d")])
267278
details = mock.Mock()
@@ -397,9 +408,10 @@ def test_unary_stream_interceptor_start_operation(self, initial_state):
397408
op.uuid = "test-uuid"
398409
op.state = initial_state
399410
instance.operation_map[op.uuid] = op
400-
continuation = CrossSync._Sync_Impl.Mock()
411+
continuation = CrossSync._Sync_Impl.Mock(
412+
return_value=_make_mock_stream_call([1, 2])
413+
)
401414
call = continuation.return_value
402-
call.__iter__ = mock.Mock(return_value=iter([1, 2]))
403415
call.trailing_metadata = CrossSync._Sync_Impl.Mock(return_value=[])
404416
call.initial_metadata = CrossSync._Sync_Impl.Mock(return_value=[])
405417
details = mock.Mock()

0 commit comments

Comments
 (0)