Skip to content

Commit b5ad486

Browse files
chore: fix mock runtime warnings (#2085)
Co-authored-by: Anthonios Partheniou <partheniou@google.com>
1 parent 54cdb42 commit b5ad486

File tree

8 files changed

+644
-496
lines changed

8 files changed

+644
-496
lines changed

gapic/templates/tests/unit/gapic/%name_%version/%sub/test_macros.j2

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,9 @@ def test_{{ method_name }}_use_cached_wrapped_rpc():
230230
assert mock_rpc.call_count == 1
231231

232232
{% if method.lro or method.extended_lro %}
233-
# Operation methods build a cached wrapper on first rpc call
234-
# subsequent calls should use the cached wrapper
233+
# Operation methods call wrapper_fn to build a cached
234+
# client._transport.operations_client instance on first rpc call.
235+
# Subsequent calls should use the cached wrapper
235236
wrapper_fn.reset_mock()
236237
{% endif %}
237238

@@ -321,8 +322,9 @@ async def test_{{ method_name }}_async_use_cached_wrapped_rpc(transport: str = "
321322
assert client._client._transport.{{method.transport_safe_name|snake_case}} in client._client._transport._wrapped_methods
322323

323324
# Replace cached wrapped function with mock
324-
mock_object = mock.AsyncMock()
325-
client._client._transport._wrapped_methods[client._client._transport.{{method.transport_safe_name|snake_case}}] = mock_object
325+
mock_rpc = mock.AsyncMock()
326+
mock_rpc.return_value = mock.Mock()
327+
client._client._transport._wrapped_methods[client._client._transport.{{method.transport_safe_name|snake_case}}] = mock_rpc
326328

327329
{% if method.client_streaming %}
328330
request = [{}]
@@ -333,11 +335,12 @@ async def test_{{ method_name }}_async_use_cached_wrapped_rpc(transport: str = "
333335
{% endif %}
334336

335337
# Establish that the underlying gRPC stub method was called.
336-
assert mock_object.call_count == 1
338+
assert mock_rpc.call_count == 1
337339

338340
{% if method.lro or method.extended_lro %}
339-
# Operation methods build a cached wrapper on first rpc call
340-
# subsequent calls should use the cached wrapper
341+
# Operation methods call wrapper_fn to build a cached
342+
# client._transport.operations_client instance on first rpc call.
343+
# Subsequent calls should use the cached wrapper
341344
wrapper_fn.reset_mock()
342345
{% endif %}
343346

@@ -349,7 +352,7 @@ async def test_{{ method_name }}_async_use_cached_wrapped_rpc(transport: str = "
349352

350353
# Establish that a new wrapper was not created for this call
351354
assert wrapper_fn.call_count == 0
352-
assert mock_object.call_count == 2
355+
assert mock_rpc.call_count == 2
353356

354357
@pytest.mark.asyncio
355358
async def test_{{ method_name }}_async(transport: str = 'grpc_asyncio', request_type={{ method.input.ident }}):

tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py

Lines changed: 127 additions & 100 deletions
Large diffs are not rendered by default.

tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -913,20 +913,21 @@ async def test_generate_access_token_async_use_cached_wrapped_rpc(transport: str
913913
assert client._client._transport.generate_access_token in client._client._transport._wrapped_methods
914914

915915
# Replace cached wrapped function with mock
916-
mock_object = mock.AsyncMock()
917-
client._client._transport._wrapped_methods[client._client._transport.generate_access_token] = mock_object
916+
mock_rpc = mock.AsyncMock()
917+
mock_rpc.return_value = mock.Mock()
918+
client._client._transport._wrapped_methods[client._client._transport.generate_access_token] = mock_rpc
918919

919920
request = {}
920921
await client.generate_access_token(request)
921922

922923
# Establish that the underlying gRPC stub method was called.
923-
assert mock_object.call_count == 1
924+
assert mock_rpc.call_count == 1
924925

925926
await client.generate_access_token(request)
926927

927928
# Establish that a new wrapper was not created for this call
928929
assert wrapper_fn.call_count == 0
929-
assert mock_object.call_count == 2
930+
assert mock_rpc.call_count == 2
930931

931932
@pytest.mark.asyncio
932933
async def test_generate_access_token_async(transport: str = 'grpc_asyncio', request_type=common.GenerateAccessTokenRequest):
@@ -1291,20 +1292,21 @@ async def test_generate_id_token_async_use_cached_wrapped_rpc(transport: str = "
12911292
assert client._client._transport.generate_id_token in client._client._transport._wrapped_methods
12921293

12931294
# Replace cached wrapped function with mock
1294-
mock_object = mock.AsyncMock()
1295-
client._client._transport._wrapped_methods[client._client._transport.generate_id_token] = mock_object
1295+
mock_rpc = mock.AsyncMock()
1296+
mock_rpc.return_value = mock.Mock()
1297+
client._client._transport._wrapped_methods[client._client._transport.generate_id_token] = mock_rpc
12961298

12971299
request = {}
12981300
await client.generate_id_token(request)
12991301

13001302
# Establish that the underlying gRPC stub method was called.
1301-
assert mock_object.call_count == 1
1303+
assert mock_rpc.call_count == 1
13021304

13031305
await client.generate_id_token(request)
13041306

13051307
# Establish that a new wrapper was not created for this call
13061308
assert wrapper_fn.call_count == 0
1307-
assert mock_object.call_count == 2
1309+
assert mock_rpc.call_count == 2
13081310

13091311
@pytest.mark.asyncio
13101312
async def test_generate_id_token_async(transport: str = 'grpc_asyncio', request_type=common.GenerateIdTokenRequest):
@@ -1674,20 +1676,21 @@ async def test_sign_blob_async_use_cached_wrapped_rpc(transport: str = "grpc_asy
16741676
assert client._client._transport.sign_blob in client._client._transport._wrapped_methods
16751677

16761678
# Replace cached wrapped function with mock
1677-
mock_object = mock.AsyncMock()
1678-
client._client._transport._wrapped_methods[client._client._transport.sign_blob] = mock_object
1679+
mock_rpc = mock.AsyncMock()
1680+
mock_rpc.return_value = mock.Mock()
1681+
client._client._transport._wrapped_methods[client._client._transport.sign_blob] = mock_rpc
16791682

16801683
request = {}
16811684
await client.sign_blob(request)
16821685

16831686
# Establish that the underlying gRPC stub method was called.
1684-
assert mock_object.call_count == 1
1687+
assert mock_rpc.call_count == 1
16851688

16861689
await client.sign_blob(request)
16871690

16881691
# Establish that a new wrapper was not created for this call
16891692
assert wrapper_fn.call_count == 0
1690-
assert mock_object.call_count == 2
1693+
assert mock_rpc.call_count == 2
16911694

16921695
@pytest.mark.asyncio
16931696
async def test_sign_blob_async(transport: str = 'grpc_asyncio', request_type=common.SignBlobRequest):
@@ -2051,20 +2054,21 @@ async def test_sign_jwt_async_use_cached_wrapped_rpc(transport: str = "grpc_asyn
20512054
assert client._client._transport.sign_jwt in client._client._transport._wrapped_methods
20522055

20532056
# Replace cached wrapped function with mock
2054-
mock_object = mock.AsyncMock()
2055-
client._client._transport._wrapped_methods[client._client._transport.sign_jwt] = mock_object
2057+
mock_rpc = mock.AsyncMock()
2058+
mock_rpc.return_value = mock.Mock()
2059+
client._client._transport._wrapped_methods[client._client._transport.sign_jwt] = mock_rpc
20562060

20572061
request = {}
20582062
await client.sign_jwt(request)
20592063

20602064
# Establish that the underlying gRPC stub method was called.
2061-
assert mock_object.call_count == 1
2065+
assert mock_rpc.call_count == 1
20622066

20632067
await client.sign_jwt(request)
20642068

20652069
# Establish that a new wrapper was not created for this call
20662070
assert wrapper_fn.call_count == 0
2067-
assert mock_object.call_count == 2
2071+
assert mock_rpc.call_count == 2
20682072

20692073
@pytest.mark.asyncio
20702074
async def test_sign_jwt_async(transport: str = 'grpc_asyncio', request_type=common.SignJwtRequest):

0 commit comments

Comments
 (0)