Skip to content

Commit f8a5b9e

Browse files
committed
clean up test service
1 parent 0433a86 commit f8a5b9e

4 files changed

Lines changed: 8 additions & 95 deletions

File tree

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

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22
{% block content %}
33
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2121): Remove the following variable (and the condition later in this file) for async rest transport once support for it is GA. #}
44
{% set rest_async_io_enabled = api.all_library_settings[api.naming.proto_package].python_settings.experimental_features.rest_async_io_enabled %}
5-
{% set has_auto_populated_fields = api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|select|list %}
65
{% import "tests/unit/gapic/%name_%version/%sub/test_macros.j2" as test_macros %}
76
{% import "%namespace/%name_%version/%sub/services/%service/_shared_macros.j2" as shared_macros %}
87

98
import os
109
import asyncio
11-
{% if has_auto_populated_fields %}
12-
import re
13-
{% endif %}
1410
from unittest import mock
1511
from unittest.mock import AsyncMock
1612

@@ -107,9 +103,6 @@ CRED_INFO_JSON = {
107103
"principal": "service-account@example.com",
108104
}
109105
CRED_INFO_STRING = json.dumps(CRED_INFO_JSON)
110-
{% if has_auto_populated_fields %}
111-
_UUID4_RE = re.compile(r"{{ uuid4_re }}")
112-
{% endif %}
113106

114107

115108
async def mock_async_gen(data, chunk_size=1):
@@ -434,84 +427,6 @@ def test__add_cred_info_for_auth_errors_no_get_cred_info(error_code):
434427
client._add_cred_info_for_auth_errors(error)
435428
assert error.details == []
436429

437-
{% if has_auto_populated_fields %}
438-
def test__setup_request_id():
439-
class MockRequest:
440-
def __init__(self, **kwargs):
441-
for k, v in kwargs.items():
442-
setattr(self, k, v)
443-
def __contains__(self, key):
444-
return hasattr(self, key)
445-
446-
class MockProtoRequest:
447-
def __init__(self, **kwargs):
448-
for k, v in kwargs.items():
449-
setattr(self, k, v)
450-
def HasField(self, key):
451-
return hasattr(self, key)
452-
453-
# Test with proto3 optional field not in request
454-
request = MockRequest()
455-
{{ service.client_name }}._setup_request_id(request, "request_id", True)
456-
assert re.match(r"{{ test_macros.get_uuid4_re() }}", request.request_id)
457-
458-
# Test with proto3 optional field already in request
459-
request = MockRequest(request_id="already_set")
460-
{{ service.client_name }}._setup_request_id(request, "request_id", True)
461-
assert request.request_id == "already_set"
462-
463-
# Test with non-proto3 optional field empty
464-
request = MockRequest(request_id="")
465-
{{ service.client_name }}._setup_request_id(request, "request_id", False)
466-
assert re.match(r"{{ test_macros.get_uuid4_re() }}", request.request_id)
467-
468-
# Test with non-proto3 optional field already set
469-
request = MockRequest(request_id="already_set")
470-
{{ service.client_name }}._setup_request_id(request, "request_id", False)
471-
assert request.request_id == "already_set"
472-
473-
# Test with proto3 optional field not in request (MockProtoRequest)
474-
request = MockProtoRequest()
475-
{{ service.client_name }}._setup_request_id(request, "request_id", True)
476-
assert re.match(r"{{ test_macros.get_uuid4_re() }}", request.request_id)
477-
478-
# Test with proto3 optional field already in request (MockProtoRequest)
479-
request = MockProtoRequest(request_id="already_set")
480-
{{ service.client_name }}._setup_request_id(request, "request_id", True)
481-
assert request.request_id == "already_set"
482-
483-
# Test with ValueError
484-
class MockValueErrorRequest:
485-
def HasField(self, key):
486-
raise ValueError("Mismatched field")
487-
def __contains__(self, key):
488-
return hasattr(self, key)
489-
490-
request = MockValueErrorRequest()
491-
{{ service.client_name }}._setup_request_id(request, "request_id", True)
492-
assert re.match(r"{{ test_macros.get_uuid4_re() }}", request.request_id)
493-
494-
# Test with dict and proto3 optional field not in request
495-
request = {}
496-
{{ service.client_name }}._setup_request_id(request, "request_id", True)
497-
assert re.match(r"{{ test_macros.get_uuid4_re() }}", request["request_id"])
498-
499-
# Test with dict and proto3 optional field already in request
500-
request = {"request_id": "already_set"}
501-
{{ service.client_name }}._setup_request_id(request, "request_id", True)
502-
assert request["request_id"] == "already_set"
503-
504-
# Test with dict and non-proto3 optional field empty
505-
request = {"request_id": ""}
506-
{{ service.client_name }}._setup_request_id(request, "request_id", False)
507-
assert re.match(r"{{ test_macros.get_uuid4_re() }}", request["request_id"])
508-
509-
# Test with dict and non-proto3 optional field already set
510-
request = {"request_id": "already_set"}
511-
{{ service.client_name }}._setup_request_id(request, "request_id", False)
512-
assert request["request_id"] == "already_set"
513-
514-
{% endif %}
515430
@pytest.mark.parametrize("client_class,transport_name", [
516431
{% if 'grpc' in opts.transport %}
517432
({{ service.client_name }}, "grpc"),

packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/services/storage_batch_operations/async_client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from collections import OrderedDict
1818
import re
1919
from typing import Dict, Callable, Mapping, MutableMapping, MutableSequence, Optional, Sequence, Tuple, Type, Union
20+
import uuid
2021

2122
from google.cloud.storagebatchoperations_v1 import gapic_version as package_version
2223

packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/services/storage_batch_operations/client.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import os
2121
import re
2222
from typing import Dict, Callable, Mapping, MutableMapping, MutableSequence, Optional, Sequence, Tuple, Type, Union, cast
23+
import uuid
2324
import warnings
2425

2526
from google.cloud.storagebatchoperations_v1 import gapic_version as package_version
@@ -571,13 +572,12 @@ def __init__(self, *,
571572
google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport
572573
creation failed for any reason.
573574
"""
574-
if isinstance(client_options, dict):
575-
client_options = client_options_lib.from_dict(client_options)
576-
if client_options is None:
577-
client_options = client_options_lib.ClientOptions()
578-
self._client_options: client_options_lib.ClientOptions = cast(
579-
client_options_lib.ClientOptions, client_options
580-
)
575+
self._client_options = client_options
576+
if isinstance(self._client_options, dict):
577+
self._client_options = client_options_lib.from_dict(self._client_options)
578+
if self._client_options is None:
579+
self._client_options = client_options_lib.ClientOptions()
580+
self._client_options = cast(client_options_lib.ClientOptions, self._client_options)
581581

582582
universe_domain_opt = getattr(self._client_options, 'universe_domain', None)
583583

packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_storage_batch_operations.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16-
1716
import os
1817
import asyncio
19-
import re
2018
from unittest import mock
2119
from unittest.mock import AsyncMock
2220

@@ -76,7 +74,6 @@
7674
"principal": "service-account@example.com",
7775
}
7876
CRED_INFO_STRING = json.dumps(CRED_INFO_JSON)
79-
_UUID4_RE = re.compile(r"[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}")
8077

8178

8279
async def mock_async_gen(data, chunk_size=1):

0 commit comments

Comments
 (0)