1717import random
1818import time
1919from typing import List , AsyncIterator
20- from unittest import mock
20+ import mock
2121
2222import proto
2323import pytest
24- from google .auth .aio .transport import Response
2524
2625from google .api_core import rest_streaming_async
2726from google .api import http_pb2
2827from google .api import httpbody_pb2
28+ from google .auth .aio .transport import Response
2929
30- # TODO (ohmayr): confirm if relative path is not an issue.
31- from .._helpers import Composer , Song , EchoResponse , parse_responses
30+ from ..conftest import Composer , Song , EchoResponse , parse_responses
3231
3332# TODO (ohmayr): check if we need to log.
3433__protobuf__ = proto .module (package = __name__ )
3736random .seed (SEED )
3837
3938
40- class AIOHTTPContentMock :
41- def __init__ (self , iter_chunked ):
42- self .iter_chunked = iter_chunked
39+ async def mock_async_gen (data , chunk_size = 1 ):
40+ for i in range (0 , len (data )):
41+ chunk = data [i : i + chunk_size ]
42+ yield chunk .encode ("utf-8" )
4343
4444
4545class ResponseMock (Response ):
@@ -49,6 +49,9 @@ def __init__(self, _response_bytes: bytes, random_split=False):
4949 self ._i = 0
5050 self ._random_split = random_split
5151
52+ def __aiter__ (self ):
53+ return self
54+
5255 async def __anext__ (self ):
5356 if self ._i == len (self ._responses_bytes ):
5457 raise StopAsyncIteration
@@ -74,13 +77,15 @@ def __init__(
7477 async def close (self ):
7578 raise NotImplementedError ()
7679
77- @property
78- def content (self ):
79- iter_chunked = lambda chunk_size : self ._ResponseItr (
80- self ._parse_responses (),
81- random_split = self ._random_split ,
80+ async def content (self , chunk_size = None ):
81+ itr = self ._ResponseItr (
82+ self ._parse_responses (), random_split = self ._random_split
8283 )
83- return AIOHTTPContentMock (iter_chunked )
84+ async for chunk in itr :
85+ yield chunk
86+
87+ async def read (self ):
88+ raise NotImplementedError ()
8489
8590 @property
8691 async def headers (self ):
@@ -273,14 +278,10 @@ async def test_next_escaped_characters_in_string(
273278@pytest .mark .parametrize ("response_type" , [EchoResponse , httpbody_pb2 .HttpBody ])
274279async def test_next_not_array (response_type ):
275280
276- mock_content = mock .Mock ()
277281 data = '{"hello": 0}'
278- mock_content .iter_chunked = lambda chunk_size : async_iter (data , chunk_size )
279-
280282 with mock .patch .object (
281- ResponseMock , "content" , new_callable = mock . PropertyMock
283+ ResponseMock , "content" , return_value = mock_async_gen ( data )
282284 ) as mock_method :
283- mock_method .return_value = mock_content
284285 resp = ResponseMock (responses = [], response_cls = response_type )
285286 itr = rest_streaming_async .AsyncResponseIterator (resp , response_type )
286287 with pytest .raises (ValueError ):
@@ -321,24 +322,14 @@ async def test_check_buffer(response_type, return_value):
321322 await itr .__anext__ ()
322323
323324
324- async def async_iter (data , chunk_size ):
325- for i in range (0 , len (data ) + chunk_size ):
326- chunk = data [i : i + chunk_size ]
327- yield chunk .encode ("utf-8" )
328-
329-
330325@pytest .mark .asyncio
331326@pytest .mark .parametrize ("response_type" , [EchoResponse , httpbody_pb2 .HttpBody ])
332327async def test_next_html (response_type ):
333328
334- mock_content = mock .Mock ()
335329 data = "<!DOCTYPE html><html></html>"
336- mock_content .iter_chunked = lambda chunk_size : async_iter (data , chunk_size )
337-
338330 with mock .patch .object (
339- ResponseMock , "content" , new_callable = mock . PropertyMock
331+ ResponseMock , "content" , return_value = mock_async_gen ( data )
340332 ) as mock_method :
341- mock_method .return_value = mock_content
342333 resp = ResponseMock (responses = [], response_cls = response_type )
343334
344335 itr = rest_streaming_async .AsyncResponseIterator (resp , response_type )
0 commit comments