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

Commit 8cdfec7

Browse files
committed
Test the creation of AsyncClient
1 parent 4ecf34d commit 8cdfec7

1 file changed

Lines changed: 27 additions & 21 deletions

File tree

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

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,38 @@ def test__get_default_mtls_endpoint():
5151
assert {{ service.client_name }}._get_default_mtls_endpoint(non_googleapi) == non_googleapi
5252

5353

54-
def test_{{ service.client_name|snake_case }}_from_service_account_file():
54+
@pytest.mark.parametrize("client_class", [{{ service.client_name }}, {{ service.async_client_name }}])
55+
def test_{{ service.client_name|snake_case }}_from_service_account_file(client_class):
5556
creds = credentials.AnonymousCredentials()
5657
with mock.patch.object(service_account.Credentials, 'from_service_account_file') as factory:
5758
factory.return_value = creds
58-
client = {{ service.client_name }}.from_service_account_file("dummy/file/path.json")
59+
client = client_class.from_service_account_file("dummy/file/path.json")
5960
assert client._transport._credentials == creds
6061

61-
client = {{ service.client_name }}.from_service_account_json("dummy/file/path.json")
62+
client = client_class.from_service_account_json("dummy/file/path.json")
6263
assert client._transport._credentials == creds
6364

6465
{% if service.host %}assert client._transport._host == '{{ service.host }}{% if ":" not in service.host %}:443{% endif %}'{% endif %}
6566

6667

67-
def test_{{ service.client_name|snake_case }}_client_options():
68+
@pytest.mark.parametrize("client_class,transport_class", [
69+
({{ service.client_name }}, transports.{{ service.grpc_transport_name }}),
70+
({{ service.async_client_name }}, transports.{{ service.grpc_asyncio_transport_name }})
71+
])
72+
def test_{{ service.client_name|snake_case }}_client_options(client_class, transport_class):
6873
# Check that if channel is provided we won't create a new one.
69-
with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.{{ service.client_name }}.get_transport_class') as gtc:
70-
transport = transports.{{ service.name }}GrpcTransport(
74+
with mock.patch.object(client_class, 'get_transport_class') as gtc:
75+
transport = transport_class(
7176
credentials=credentials.AnonymousCredentials()
7277
)
73-
client = {{ service.client_name }}(transport=transport)
78+
client = client_class(transport=transport)
7479
gtc.assert_not_called()
7580

7681
# Check mTLS is not triggered with empty client options.
7782
options = client_options.ClientOptions()
78-
with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.{{ service.client_name }}.get_transport_class') as gtc:
83+
with mock.patch.object(client_class, 'get_transport_class') as gtc:
7984
transport = gtc.return_value = mock.MagicMock()
80-
client = {{ service.client_name }}(client_options=options)
85+
client = client_class(client_options=options)
8186
transport.assert_called_once_with(
8287
credentials=None,
8388
host=client.DEFAULT_ENDPOINT,
@@ -86,10 +91,10 @@ def test_{{ service.client_name|snake_case }}_client_options():
8691
# Check mTLS is not triggered if api_endpoint is provided but
8792
# client_cert_source is None.
8893
options = client_options.ClientOptions(api_endpoint="squid.clam.whelk")
89-
with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}GrpcTransport.__init__') as grpc_transport:
90-
grpc_transport.return_value = None
91-
client = {{ service.client_name }}(client_options=options)
92-
grpc_transport.assert_called_once_with(
94+
with mock.patch.object(transport_class, '__init__') as patched:
95+
patched.return_value = None
96+
client = client_class(client_options=options)
97+
patched.assert_called_once_with(
9398
api_mtls_endpoint=None,
9499
client_cert_source=None,
95100
credentials=None,
@@ -100,10 +105,10 @@ def test_{{ service.client_name|snake_case }}_client_options():
100105
options = client_options.ClientOptions(
101106
client_cert_source=client_cert_source_callback
102107
)
103-
with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}GrpcTransport.__init__') as grpc_transport:
104-
grpc_transport.return_value = None
105-
client = {{ service.client_name }}(client_options=options)
106-
grpc_transport.assert_called_once_with(
108+
with mock.patch.object(transport_class, '__init__') as patched:
109+
patched.return_value = None
110+
client = client_class(client_options=options)
111+
patched.assert_called_once_with(
107112
api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT,
108113
client_cert_source=client_cert_source_callback,
109114
credentials=None,
@@ -115,16 +120,17 @@ def test_{{ service.client_name|snake_case }}_client_options():
115120
api_endpoint="squid.clam.whelk",
116121
client_cert_source=client_cert_source_callback
117122
)
118-
with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}GrpcTransport.__init__') as grpc_transport:
119-
grpc_transport.return_value = None
120-
client = {{ service.client_name }}(client_options=options)
121-
grpc_transport.assert_called_once_with(
123+
with mock.patch.object(transport_class, '__init__') as patched:
124+
patched.return_value = None
125+
client = client_class(client_options=options)
126+
patched.assert_called_once_with(
122127
api_mtls_endpoint="squid.clam.whelk",
123128
client_cert_source=client_cert_source_callback,
124129
credentials=None,
125130
host="squid.clam.whelk",
126131
)
127132

133+
128134
def test_{{ service.client_name|snake_case }}_client_options_from_dict():
129135
with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}GrpcTransport.__init__') as grpc_transport:
130136
grpc_transport.return_value = None

0 commit comments

Comments
 (0)