2525import test_utils .prefixer
2626
2727import bigframes
28+ import bigframes .clients
2829import bigframes .dtypes
2930import bigframes .exceptions
3031from bigframes .functions import _utils as bff_utils
@@ -93,6 +94,11 @@ def session_with_bq_connection(bq_cf_connection) -> bigframes.Session:
9394 return session
9495
9596
97+ def get_bq_connection_id_path_format (connection_id_dot_format ):
98+ fields = connection_id_dot_format .split ("." )
99+ return f"projects/{ fields [0 ]} /locations/{ fields [1 ]} /connections/{ fields [2 ]} "
100+
101+
96102@pytest .mark .flaky (retries = 2 , delay = 120 )
97103def test_remote_function_direct_no_session_param (
98104 bigquery_client ,
@@ -155,11 +161,8 @@ def square(x):
155161
156162
157163@pytest .mark .flaky (retries = 2 , delay = 120 )
158- def test_remote_function_direct_no_session_param_location_specified (
159- bigquery_client ,
160- bigqueryconnection_client ,
161- cloudfunctions_client ,
162- resourcemanager_client ,
164+ def test_remote_function_connection_w_location (
165+ session ,
163166 scalars_dfs ,
164167 dataset_id_permanent ,
165168 bq_cf_connection_location ,
@@ -170,10 +173,7 @@ def square(x):
170173 square = bff .remote_function (
171174 input_types = int ,
172175 output_type = int ,
173- bigquery_client = bigquery_client ,
174- bigquery_connection_client = bigqueryconnection_client ,
175- cloud_functions_client = cloudfunctions_client ,
176- resource_manager_client = resourcemanager_client ,
176+ session = session ,
177177 dataset = dataset_id_permanent ,
178178 bigquery_connection = bq_cf_connection_location ,
179179 # See e2e tests for tests that actually deploy the Cloud Function.
@@ -210,11 +210,8 @@ def square(x):
210210
211211
212212@pytest .mark .flaky (retries = 2 , delay = 120 )
213- def test_remote_function_direct_no_session_param_location_mismatched (
214- bigquery_client ,
215- bigqueryconnection_client ,
216- cloudfunctions_client ,
217- resourcemanager_client ,
213+ def test_remote_function_connection_w_location_mismatched (
214+ session ,
218215 dataset_id_permanent ,
219216 bq_cf_connection_location_mismatched ,
220217):
@@ -223,32 +220,41 @@ def square(x):
223220 # connection doesn't match the location of the dataset.
224221 return x * x # pragma: NO COVER
225222
226- with pytest .raises (
227- ValueError ,
228- match = re .escape ("The location does not match BigQuery connection location:" ),
229- ):
230- bff .remote_function (
231- input_types = int ,
232- output_type = int ,
233- bigquery_client = bigquery_client ,
234- bigquery_connection_client = bigqueryconnection_client ,
235- cloud_functions_client = cloudfunctions_client ,
236- resource_manager_client = resourcemanager_client ,
237- dataset = dataset_id_permanent ,
238- bigquery_connection = bq_cf_connection_location_mismatched ,
239- # See e2e tests for tests that actually deploy the Cloud Function.
240- reuse = True ,
241- name = get_function_name (square ),
242- cloud_function_service_account = "default" ,
243- )(square )
223+ bq_cf_connection_location_mismatched_path_fmt = get_bq_connection_id_path_format (
224+ bigframes .clients .get_canonical_bq_connection_id (
225+ bq_cf_connection_location_mismatched ,
226+ session .bqclient .project ,
227+ session ._location ,
228+ )
229+ )
230+ connection_ids = [
231+ bq_cf_connection_location_mismatched ,
232+ bq_cf_connection_location_mismatched_path_fmt ,
233+ ]
234+
235+ for connection_id in connection_ids :
236+ with pytest .raises (
237+ ValueError ,
238+ match = re .escape (
239+ "The location does not match BigQuery connection location:"
240+ ),
241+ ):
242+ bff .remote_function (
243+ input_types = int ,
244+ output_type = int ,
245+ session = session ,
246+ dataset = dataset_id_permanent ,
247+ bigquery_connection = connection_id ,
248+ # See e2e tests for tests that actually deploy the Cloud Function.
249+ reuse = True ,
250+ name = get_function_name (square ),
251+ cloud_function_service_account = "default" ,
252+ )(square )
244253
245254
246255@pytest .mark .flaky (retries = 2 , delay = 120 )
247- def test_remote_function_direct_no_session_param_location_project_specified (
248- bigquery_client ,
249- bigqueryconnection_client ,
250- cloudfunctions_client ,
251- resourcemanager_client ,
256+ def test_remote_function_connection_w_location_project (
257+ session ,
252258 scalars_dfs ,
253259 dataset_id_permanent ,
254260 bq_cf_connection_location_project ,
@@ -259,10 +265,7 @@ def square(x):
259265 square = bff .remote_function (
260266 input_types = int ,
261267 output_type = int ,
262- bigquery_client = bigquery_client ,
263- bigquery_connection_client = bigqueryconnection_client ,
264- cloud_functions_client = cloudfunctions_client ,
265- resource_manager_client = resourcemanager_client ,
268+ session = session ,
266269 dataset = dataset_id_permanent ,
267270 bigquery_connection = bq_cf_connection_location_project ,
268271 # See e2e tests for tests that actually deploy the Cloud Function.
@@ -299,11 +302,8 @@ def square(x):
299302
300303
301304@pytest .mark .flaky (retries = 2 , delay = 120 )
302- def test_remote_function_direct_no_session_param_project_mismatched (
303- bigquery_client ,
304- bigqueryconnection_client ,
305- cloudfunctions_client ,
306- resourcemanager_client ,
305+ def test_remote_function_connection_w_project_mismatched (
306+ session ,
307307 dataset_id_permanent ,
308308 bq_cf_connection_location_project_mismatched ,
309309):
@@ -312,26 +312,38 @@ def square(x):
312312 # connection doesn't match the project of the dataset.
313313 return x * x # pragma: NO COVER
314314
315- with pytest .raises (
316- ValueError ,
317- match = re .escape (
318- "The project_id does not match BigQuery connection gcp_project_id:"
319- ),
320- ):
321- bff .remote_function (
322- input_types = int ,
323- output_type = int ,
324- bigquery_client = bigquery_client ,
325- bigquery_connection_client = bigqueryconnection_client ,
326- cloud_functions_client = cloudfunctions_client ,
327- resource_manager_client = resourcemanager_client ,
328- dataset = dataset_id_permanent ,
329- bigquery_connection = bq_cf_connection_location_project_mismatched ,
330- # See e2e tests for tests that actually deploy the Cloud Function.
331- reuse = True ,
332- name = get_function_name (square ),
333- cloud_function_service_account = "default" ,
334- )(square )
315+ bq_cf_connection_location_project_mismatched_path_fmt = (
316+ get_bq_connection_id_path_format (
317+ bigframes .clients .get_canonical_bq_connection_id (
318+ bq_cf_connection_location_project_mismatched ,
319+ session .bqclient .project ,
320+ session ._location ,
321+ )
322+ )
323+ )
324+ connection_ids = [
325+ bq_cf_connection_location_project_mismatched ,
326+ bq_cf_connection_location_project_mismatched_path_fmt ,
327+ ]
328+
329+ for connection_id in connection_ids :
330+ with pytest .raises (
331+ ValueError ,
332+ match = re .escape (
333+ "The project_id does not match BigQuery connection gcp_project_id:"
334+ ),
335+ ):
336+ bff .remote_function (
337+ input_types = int ,
338+ output_type = int ,
339+ session = session ,
340+ dataset = dataset_id_permanent ,
341+ bigquery_connection = connection_id ,
342+ # See e2e tests for tests that actually deploy the Cloud Function.
343+ reuse = True ,
344+ name = get_function_name (square ),
345+ cloud_function_service_account = "default" ,
346+ )(square )
335347
336348
337349@pytest .mark .flaky (retries = 2 , delay = 120 )
0 commit comments