@@ -149,11 +149,12 @@ async def test_get_metadata(
149149 parameters.
150150 """
151151 project = "my-project"
152+ region = "my-region"
152153 instance = "my-instance"
153154 # mock Cloud SQL Admin API call
154155 with aioresponses () as mocked :
155156 mocked .get (
156- "https://sqladmin.googleapis.com/sql/v1beta4/projects/my- project/instances/my- instance/connectSettings" ,
157+ f "https://sqladmin.googleapis.com/sql/v1beta4/projects/{ project } /instances/{ instance } /connectSettings" ,
157158 status = 200 ,
158159 body = mock_instance .connect_settings (),
159160 repeat = True ,
@@ -165,6 +166,7 @@ async def test_get_metadata(
165166 "https://sqladmin.googleapis.com" ,
166167 credentials ,
167168 project ,
169+ region ,
168170 instance ,
169171 )
170172
@@ -184,6 +186,7 @@ async def test_get_metadata_TypeError(credentials: Credentials) -> None:
184186 """
185187 client_session = Mock (aiohttp .ClientSession )
186188 project = "my-project"
189+ region = "my-region"
187190 instance = "my-instance"
188191
189192 # incorrect credentials type
@@ -193,6 +196,7 @@ async def test_get_metadata_TypeError(credentials: Credentials) -> None:
193196 sqladmin_api_endpoint = "https://sqladmin.googleapis.com" ,
194197 credentials = "bad-credentials" ,
195198 project = project ,
199+ region = region ,
196200 instance = instance ,
197201 )
198202 # incorrect project type
@@ -202,6 +206,17 @@ async def test_get_metadata_TypeError(credentials: Credentials) -> None:
202206 sqladmin_api_endpoint = "https://sqladmin.googleapis.com" ,
203207 credentials = credentials ,
204208 project = 12345 ,
209+ region = region ,
210+ instance = instance ,
211+ )
212+ # incorrect region type
213+ with pytest .raises (TypeError ):
214+ await _get_metadata (
215+ client_session = client_session ,
216+ sqladmin_api_endpoint = "https://sqladmin.googleapis.com" ,
217+ credentials = credentials ,
218+ project = project ,
219+ region = 1 ,
205220 instance = instance ,
206221 )
207222 # incorrect instance type
@@ -211,10 +226,46 @@ async def test_get_metadata_TypeError(credentials: Credentials) -> None:
211226 sqladmin_api_endpoint = "https://sqladmin.googleapis.com" ,
212227 credentials = credentials ,
213228 project = project ,
229+ region = region ,
214230 instance = 12345 ,
215231 )
216232
217233
234+ @pytest .mark .asyncio
235+ @no_type_check
236+ async def test_get_metadata_region_mismatch (
237+ mock_instance : FakeCSQLInstance , credentials : Credentials
238+ ) -> None :
239+ """
240+ Test to check whether _get_metadata throws proper ValueError
241+ when given mismatched region.
242+ """
243+ client_session = Mock (aiohttp .ClientSession )
244+ project = "my-project"
245+ region = "bad-region"
246+ instance = "my-instance"
247+
248+ # mock Cloud SQL Admin API call
249+ with aioresponses () as mocked :
250+ mocked .get (
251+ f"https://sqladmin.googleapis.com/sql/v1beta4/projects/{ project } /instances/{ instance } /connectSettings" ,
252+ status = 200 ,
253+ body = mock_instance .connect_settings (),
254+ repeat = True ,
255+ )
256+
257+ async with aiohttp .ClientSession () as client_session :
258+ with pytest .raises (ValueError ):
259+ await _get_metadata (
260+ client_session = client_session ,
261+ sqladmin_api_endpoint = "https://sqladmin.googleapis.com" ,
262+ credentials = credentials ,
263+ project = project ,
264+ region = region ,
265+ instance = instance ,
266+ )
267+
268+
218269@pytest .mark .asyncio
219270@no_type_check
220271async def test_is_valid_with_valid_metadata () -> None :
0 commit comments