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

Commit 909f3be

Browse files
committed
Add tests for LRO client
1 parent 85e4d81 commit 909f3be

1 file changed

Lines changed: 111 additions & 1 deletion

File tree

gapic/templates/tests/unit/%name_%version/%sub/test_%service.py.j2

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ async def test_{{ method.name|snake_case }}_flattened_async():
504504

505505
# Establish that the underlying call was made with the expected
506506
# request object values.
507-
assert len(call.mock_calls) == 1
507+
assert len(call.mock_calls)
508508
_, args, _ = call.mock_calls[0]
509509
{% for key, field in method.flattened_fields.items() -%}
510510
assert args[0].{{ key }} == {{ field.mock_value }}
@@ -891,6 +891,23 @@ def test_{{ service.name|snake_case }}_grpc_transport_channel():
891891
assert not callback.called
892892

893893

894+
def test_{{ service.name|snake_case }}_grpc_asyncio_transport_channel():
895+
channel = aio.insecure_channel('http://localhost/')
896+
897+
# Check that if channel is provided, mtls endpoint and client_cert_source
898+
# won't be used.
899+
callback = mock.MagicMock()
900+
transport = transports.{{ service.name }}GrpcAsyncIOTransport(
901+
host="squid.clam.whelk",
902+
channel=channel,
903+
api_mtls_endpoint="mtls.squid.clam.whelk",
904+
client_cert_source=callback,
905+
)
906+
assert transport.grpc_channel == channel
907+
assert transport._host == "squid.clam.whelk:443"
908+
assert not callback.called
909+
910+
894911
@mock.patch("grpc.ssl_channel_credentials", autospec=True)
895912
@mock.patch("google.api_core.grpc_helpers.create_channel", autospec=True)
896913
def test_{{ service.name|snake_case }}_grpc_transport_channel_mtls_with_client_cert_source(
@@ -928,6 +945,43 @@ def test_{{ service.name|snake_case }}_grpc_transport_channel_mtls_with_client_c
928945
assert transport.grpc_channel == mock_grpc_channel
929946

930947

948+
@mock.patch("grpc.ssl_channel_credentials", autospec=True)
949+
@mock.patch("google.api_core.grpc_helpers_async.create_channel", autospec=True)
950+
def test_{{ service.name|snake_case }}_grpc_asyncio_transport_channel_mtls_with_client_cert_source(
951+
grpc_create_channel, grpc_ssl_channel_cred
952+
):
953+
# Check that if channel is None, but api_mtls_endpoint and client_cert_source
954+
# are provided, then a mTLS channel will be created.
955+
mock_cred = mock.Mock()
956+
957+
mock_ssl_cred = mock.Mock()
958+
grpc_ssl_channel_cred.return_value = mock_ssl_cred
959+
960+
mock_grpc_channel = mock.Mock()
961+
grpc_create_channel.return_value = mock_grpc_channel
962+
963+
transport = transports.{{ service.name }}GrpcAsyncIOTransport(
964+
host="squid.clam.whelk",
965+
credentials=mock_cred,
966+
api_mtls_endpoint="mtls.squid.clam.whelk",
967+
client_cert_source=client_cert_source_callback,
968+
)
969+
grpc_ssl_channel_cred.assert_called_once_with(
970+
certificate_chain=b"cert bytes", private_key=b"key bytes"
971+
)
972+
grpc_create_channel.assert_called_once_with(
973+
"mtls.squid.clam.whelk:443",
974+
credentials=mock_cred,
975+
ssl_credentials=mock_ssl_cred,
976+
scopes=(
977+
{%- for scope in service.oauth_scopes %}
978+
'{{ scope }}',
979+
{%- endfor %}
980+
),
981+
)
982+
assert transport.grpc_channel == mock_grpc_channel
983+
984+
931985
@pytest.mark.parametrize(
932986
"api_mtls_endpoint", ["mtls.squid.clam.whelk", "mtls.squid.clam.whelk:443"]
933987
)
@@ -967,6 +1021,45 @@ def test_{{ service.name|snake_case }}_grpc_transport_channel_mtls_with_adc(
9671021
assert transport.grpc_channel == mock_grpc_channel
9681022

9691023

1024+
@pytest.mark.parametrize(
1025+
"api_mtls_endpoint", ["mtls.squid.clam.whelk", "mtls.squid.clam.whelk:443"]
1026+
)
1027+
@mock.patch("google.api_core.grpc_helpers_async.create_channel", autospec=True)
1028+
def test_{{ service.name|snake_case }}_grpc_asyncio_transport_channel_mtls_with_adc(
1029+
grpc_create_channel, api_mtls_endpoint
1030+
):
1031+
# Check that if channel and client_cert_source are None, but api_mtls_endpoint
1032+
# is provided, then a mTLS channel will be created with SSL ADC.
1033+
mock_grpc_channel = mock.Mock()
1034+
grpc_create_channel.return_value = mock_grpc_channel
1035+
1036+
# Mock google.auth.transport.grpc.SslCredentials class.
1037+
mock_ssl_cred = mock.Mock()
1038+
with mock.patch.multiple(
1039+
"google.auth.transport.grpc.SslCredentials",
1040+
__init__=mock.Mock(return_value=None),
1041+
ssl_credentials=mock.PropertyMock(return_value=mock_ssl_cred),
1042+
):
1043+
mock_cred = mock.Mock()
1044+
transport = transports.{{ service.name }}GrpcAsyncIOTransport(
1045+
host="squid.clam.whelk",
1046+
credentials=mock_cred,
1047+
api_mtls_endpoint=api_mtls_endpoint,
1048+
client_cert_source=None,
1049+
)
1050+
grpc_create_channel.assert_called_once_with(
1051+
"mtls.squid.clam.whelk:443",
1052+
credentials=mock_cred,
1053+
ssl_credentials=mock_ssl_cred,
1054+
scopes=(
1055+
{%- for scope in service.oauth_scopes %}
1056+
'{{ scope }}',
1057+
{%- endfor %}
1058+
),
1059+
)
1060+
assert transport.grpc_channel == mock_grpc_channel
1061+
1062+
9701063
{% if service.has_lro -%}
9711064
def test_{{ service.name|snake_case }}_grpc_lro_client():
9721065
client = {{ service.client_name }}(
@@ -984,6 +1077,23 @@ def test_{{ service.name|snake_case }}_grpc_lro_client():
9841077
# Ensure that subsequent calls to the property send the exact same object.
9851078
assert transport.operations_client is transport.operations_client
9861079

1080+
1081+
def test_{{ service.name|snake_case }}_grpc_lro_async_client():
1082+
client = {{ service.async_client_name }}(
1083+
credentials=credentials.AnonymousCredentials(),
1084+
transport='grpc_asyncio',
1085+
)
1086+
transport = client._client._transport
1087+
1088+
# Ensure that we have a api-core operations client.
1089+
assert isinstance(
1090+
transport.operations_client,
1091+
operations_v1.OperationsAsyncClient,
1092+
)
1093+
1094+
# Ensure that subsequent calls to the property send the exact same object.
1095+
assert transport.operations_client is transport.operations_client
1096+
9871097
{% endif -%}
9881098

9891099
{% for message in service.resource_messages -%}

0 commit comments

Comments
 (0)