Skip to content

Commit c4244e1

Browse files
authored
Fix calling get_client in BigQueryHook.table_exists (#9916)
Adding `project_id` argument to `get_client` method otherwise this call always falls back to the default connection id.
1 parent c460529 commit c4244e1

2 files changed

Lines changed: 5 additions & 4 deletions

File tree

airflow/providers/google/cloud/hooks/bigquery.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def table_exists(self, dataset_id: str, table_id: str, project_id: str) -> bool:
215215
"""
216216
table_reference = TableReference(DatasetReference(project_id, dataset_id), table_id)
217217
try:
218-
self.get_client().get_table(table_reference)
218+
self.get_client(project_id=project_id).get_table(table_reference)
219219
return True
220220
except NotFound:
221221
return False

tests/providers/google/cloud/hooks/test_bigquery.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,19 @@ def test_bigquery_insert_rows_not_implemented(self):
104104
with self.assertRaises(NotImplementedError):
105105
self.hook.insert_rows(table="table", rows=[1, 2])
106106

107-
@mock.patch("airflow.providers.google.cloud.hooks.bigquery.Client")
107+
@mock.patch("airflow.providers.google.cloud.hooks.bigquery.BigQueryHook.get_client")
108108
def test_bigquery_table_exists_true(self, mock_client):
109109
result = self.hook.table_exists(project_id=PROJECT_ID, dataset_id=DATASET_ID, table_id=TABLE_ID)
110110
mock_client.return_value.get_table.assert_called_once_with(TABLE_REFERENCE)
111+
mock_client.assert_called_once_with(project_id=PROJECT_ID)
111112
assert result is True
112113

113-
@mock.patch("airflow.providers.google.cloud.hooks.bigquery.Client")
114+
@mock.patch("airflow.providers.google.cloud.hooks.bigquery.BigQueryHook.get_client")
114115
def test_bigquery_table_exists_false(self, mock_client):
115116
mock_client.return_value.get_table.side_effect = NotFound("Dataset not found")
116-
117117
result = self.hook.table_exists(project_id=PROJECT_ID, dataset_id=DATASET_ID, table_id=TABLE_ID)
118118
mock_client.return_value.get_table.assert_called_once_with(TABLE_REFERENCE)
119+
mock_client.assert_called_once_with(project_id=PROJECT_ID)
119120
assert result is False
120121

121122
@mock.patch('airflow.providers.google.cloud.hooks.bigquery.read_gbq')

0 commit comments

Comments
 (0)