@@ -7249,50 +7249,26 @@ async def test_no_a2a_interaction_for_no_metadata(
72497249
72507250
72517251# ================================================================
7252- # TEST CLASS: Dataset location auto-detection (Issue #5476)
7252+ # TEST CLASS: Dataset location handling (Issue #5476)
72537253# ================================================================
7254- class TestDatasetLocationDetection :
7255- """Tests for auto-detecting the dataset location."""
7254+ class TestDatasetLocationHandling :
7255+ """Tests that BQ client is created without a default location.
72567256
7257- @pytest .mark .asyncio
7258- async def test_location_detected_from_dataset (
7259- self ,
7260- mock_auth_default ,
7261- mock_to_arrow_schema ,
7262- mock_asyncio_to_thread ,
7263- ):
7264- """Resolved location comes from get_dataset, not constructor."""
7265- mock_dataset = mock .MagicMock ()
7266- mock_dataset .location = "europe-west1"
7267-
7268- with mock .patch .object (bigquery , "Client" , autospec = True ) as mock_bq_cls :
7269- mock_bq_cls .return_value .get_dataset .return_value = mock_dataset
7270- mock_bq_cls .return_value .get_table .side_effect = (
7271- cloud_exceptions .NotFound ("table" )
7272- )
7273- mock_bq_cls .return_value .create_table .return_value = None
7274-
7275- async with managed_plugin (
7276- project_id = PROJECT_ID ,
7277- dataset_id = DATASET_ID ,
7278- table_id = TABLE_ID ,
7279- config = bigquery_agent_analytics_plugin .BigQueryLoggerConfig (
7280- create_views = False ,
7281- ),
7282- ) as plugin :
7283- await plugin ._ensure_started ()
7284- assert plugin ._resolved_location == "europe-west1"
7257+ When location is omitted from bigquery.Client(), client.query()
7258+ sends no location field in the API request, letting BigQuery
7259+ infer location from the referenced dataset. This prevents
7260+ silent view-creation failures for non-US datasets.
7261+ """
72857262
72867263 @pytest .mark .asyncio
7287- async def test_location_falls_back_on_get_dataset_failure (
7264+ async def test_client_created_without_location (
72887265 self ,
72897266 mock_auth_default ,
72907267 mock_to_arrow_schema ,
72917268 mock_asyncio_to_thread ,
72927269 ):
7293- """Falls back to configured location when get_dataset fails ."""
7270+ """bigquery.Client is created without a location parameter ."""
72947271 with mock .patch .object (bigquery , "Client" , autospec = True ) as mock_bq_cls :
7295- mock_bq_cls .return_value .get_dataset .side_effect = Exception ("not found" )
72967272 mock_bq_cls .return_value .get_table .side_effect = (
72977273 cloud_exceptions .NotFound ("table" )
72987274 )
@@ -7302,28 +7278,27 @@ async def test_location_falls_back_on_get_dataset_failure(
73027278 project_id = PROJECT_ID ,
73037279 dataset_id = DATASET_ID ,
73047280 table_id = TABLE_ID ,
7305- location = "asia-northeast1 " ,
7281+ location = "europe-west1 " ,
73067282 config = bigquery_agent_analytics_plugin .BigQueryLoggerConfig (
73077283 create_views = False ,
73087284 ),
73097285 ) as plugin :
73107286 await plugin ._ensure_started ()
7311- assert plugin ._resolved_location == "asia-northeast1"
7287+
7288+ mock_bq_cls .assert_called_once ()
7289+ _ , kwargs = mock_bq_cls .call_args
7290+ assert "location" not in kwargs
73127291
73137292 @pytest .mark .asyncio
7314- async def test_views_created_with_resolved_location (
7293+ async def test_view_query_omits_location (
73157294 self ,
73167295 mock_auth_default ,
73177296 mock_to_arrow_schema ,
73187297 mock_asyncio_to_thread ,
73197298 ):
7320- """View creation DDL passes resolved location to client.query()."""
7321- mock_dataset = mock .MagicMock ()
7322- mock_dataset .location = "europe-west1"
7323-
7299+ """View creation DDL queries do not pass an explicit location."""
73247300 with mock .patch .object (bigquery , "Client" , autospec = True ) as mock_bq_cls :
73257301 mock_client = mock_bq_cls .return_value
7326- mock_client .get_dataset .return_value = mock_dataset
73277302 mock_client .get_table .return_value = mock .MagicMock ()
73287303 mock_client .query .return_value .result .return_value = None
73297304
@@ -7337,11 +7312,11 @@ async def test_views_created_with_resolved_location(
73377312 ) as plugin :
73387313 await plugin ._ensure_started ()
73397314
7340- # Verify query() was called with location kwarg
73417315 assert mock_client .query .call_count > 0
73427316 for call in mock_client .query .call_args_list :
73437317 _ , kwargs = call
7344- assert kwargs .get ("location" ) == "europe-west1"
7318+ # No explicit location — BQ infers from dataset
7319+ assert "location" not in kwargs
73457320
73467321 @pytest .mark .asyncio
73477322 async def test_view_error_still_logged (
@@ -7351,12 +7326,8 @@ async def test_view_error_still_logged(
73517326 mock_asyncio_to_thread ,
73527327 ):
73537328 """View creation errors are logged but not raised."""
7354- mock_dataset = mock .MagicMock ()
7355- mock_dataset .location = "US"
7356-
73577329 with mock .patch .object (bigquery , "Client" , autospec = True ) as mock_bq_cls :
73587330 mock_client = mock_bq_cls .return_value
7359- mock_client .get_dataset .return_value = mock_dataset
73607331 mock_client .get_table .return_value = mock .MagicMock ()
73617332 mock_client .query .return_value .result .side_effect = Exception (
73627333 "view error"
0 commit comments