@@ -65,21 +65,30 @@ def _load_snippet_config(
6565
6666
6767def _make_configured_snippet (
68- request_path : Path , config_path : Path , api_version : str , is_sync : bool
68+ request_path : Path = SPEECH_V1_REQUEST_PATH ,
69+ config_path : Path = CONFIG_JSON_PATH ,
70+ api_version : str = "v1" ,
71+ is_sync : bool = True ,
6972) -> configured_snippet .ConfiguredSnippet :
7073 api_schema = _load_api_schema (request_path )
7174 snippet_config = _load_snippet_config (config_path )
72-
7375 return configured_snippet .ConfiguredSnippet (
7476 api_schema , snippet_config , api_version , is_sync
7577 )
7678
7779
7880@pytest .fixture
7981def snippet ():
80- return _make_configured_snippet (
81- SPEECH_V1_REQUEST_PATH , CONFIG_JSON_PATH , api_version = "v1" , is_sync = True
82+ return _make_configured_snippet ()
83+
84+
85+ @pytest .fixture
86+ def snippet_without_endpoint ():
87+ snippet = _make_configured_snippet ()
88+ snippet .config .snippet .service_client_initialization .ClearField (
89+ "custom_service_endpoint"
8290 )
91+ return snippet
8392
8493
8594def test_gapic_module_name (snippet ):
@@ -94,9 +103,7 @@ def test_gapic_module_name(snippet):
94103 ],
95104)
96105def test_region_tag (is_sync , expected ):
97- snippet = _make_configured_snippet (
98- SPEECH_V1_REQUEST_PATH , CONFIG_JSON_PATH , api_version = "v1" , is_sync = is_sync
99- )
106+ snippet = _make_configured_snippet (is_sync = is_sync )
100107 assert snippet .region_tag == expected
101108
102109
@@ -112,9 +119,7 @@ def test_sample_function_name(snippet):
112119 ],
113120)
114121def test_client_class_name (is_sync , expected ):
115- snippet = _make_configured_snippet (
116- SPEECH_V1_REQUEST_PATH , CONFIG_JSON_PATH , api_version = "v1" , is_sync = is_sync
117- )
122+ snippet = _make_configured_snippet (is_sync = is_sync )
118123 assert snippet .client_class_name == expected
119124
120125
@@ -126,12 +131,72 @@ def test_client_class_name(is_sync, expected):
126131 ],
127132)
128133def test_filename (is_sync , expected ):
129- snippet = _make_configured_snippet (
130- SPEECH_V1_REQUEST_PATH , CONFIG_JSON_PATH , api_version = "v1" , is_sync = is_sync
131- )
134+ snippet = _make_configured_snippet (is_sync = is_sync )
132135 assert snippet .filename == expected
133136
134137
138+ @pytest .mark .parametrize (
139+ "snippet_config_dict,expected" ,
140+ [
141+ ({}, None ),
142+ (
143+ {
144+ "snippet" : {
145+ "serviceClientInitialization" : {
146+ "customServiceEndpoint" : {
147+ "host" : "speech.googleapis.com" ,
148+ }
149+ },
150+ }
151+ },
152+ "speech.googleapis.com" ,
153+ ),
154+ (
155+ {
156+ "snippet" : {
157+ "serviceClientInitialization" : {
158+ "customServiceEndpoint" : {
159+ "host" : "speech.googleapis.com" ,
160+ "region" : "us" ,
161+ }
162+ },
163+ }
164+ },
165+ "us-speech.googleapis.com" ,
166+ ),
167+ (
168+ {
169+ "snippet" : {
170+ "serviceClientInitialization" : {
171+ "customServiceEndpoint" : {
172+ "host" : "speech.googleapis.com" ,
173+ "region" : "us" ,
174+ "port" : 123 ,
175+ }
176+ },
177+ }
178+ },
179+ "us-speech.googleapis.com:123" ,
180+ ),
181+ ],
182+ )
183+ def test_api_endpoint (snippet_config_dict , expected ):
184+ # api_schema, api_version, and is_sync do not matter here.
185+ api_schema = _load_api_schema (SPEECH_V1_REQUEST_PATH )
186+ api_version = "v1"
187+ is_sync = True
188+
189+ snippet_config = json_format .ParseDict (
190+ snippet_config_dict , snippet_config_language_pb2 .SnippetConfig ()
191+ )
192+
193+ snippet = configured_snippet .ConfiguredSnippet (
194+ api_schema , snippet_config , api_version , is_sync
195+ )
196+
197+ assert snippet .api_endpoint == expected
198+
199+
135200def test_AppendToSampleFunctionBody ():
136201 # Start with a function def with nonempty body to we can be sure the
137202 # transformer appends the statement.
@@ -152,6 +217,17 @@ def test_code(snippet):
152217 # until the generated code is the same as that of the golden file.
153218 expected_code = """def sample_create_custom_class_Basic(parent = "projects/[PROJECT]/locations/us", custom_class_id = "passengerships"):
154219 \" \"
155- client = speech_v1.AdaptationClient()
220+ client = speech_v1.AdaptationClient(client_options = {"api_endpoint": "us-speech.googleapis.com"} )
156221"""
157222 assert snippet .code == expected_code
223+
224+
225+ def test_code_without_endpoint (snippet_without_endpoint ):
226+ snippet_without_endpoint .generate ()
227+
228+ # https://github.com/googleapis/gapic-generator-python/issues/1522
229+ expected_code = """def sample_create_custom_class_Basic(parent = "projects/[PROJECT]/locations/us", custom_class_id = "passengerships"):
230+ \" \"
231+ client = speech_v1.AdaptationClient()
232+ """
233+ assert snippet_without_endpoint .code == expected_code
0 commit comments