Skip to content

Commit

Permalink
Fix calling get_client in BigQueryHook.table_exists (#9916)
Browse files Browse the repository at this point in the history
Adding `project_id` argument to `get_client` method 
otherwise this call always falls back to the default connection id.
  • Loading branch information
nathadfield committed Jul 22, 2020
1 parent c460529 commit c4244e1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion airflow/providers/google/cloud/hooks/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def table_exists(self, dataset_id: str, table_id: str, project_id: str) -> bool:
"""
table_reference = TableReference(DatasetReference(project_id, dataset_id), table_id)
try:
self.get_client().get_table(table_reference)
self.get_client(project_id=project_id).get_table(table_reference)
return True
except NotFound:
return False
Expand Down
7 changes: 4 additions & 3 deletions tests/providers/google/cloud/hooks/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,19 @@ def test_bigquery_insert_rows_not_implemented(self):
with self.assertRaises(NotImplementedError):
self.hook.insert_rows(table="table", rows=[1, 2])

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

@mock.patch("airflow.providers.google.cloud.hooks.bigquery.Client")
@mock.patch("airflow.providers.google.cloud.hooks.bigquery.BigQueryHook.get_client")
def test_bigquery_table_exists_false(self, mock_client):
mock_client.return_value.get_table.side_effect = NotFound("Dataset not found")

result = self.hook.table_exists(project_id=PROJECT_ID, dataset_id=DATASET_ID, table_id=TABLE_ID)
mock_client.return_value.get_table.assert_called_once_with(TABLE_REFERENCE)
mock_client.assert_called_once_with(project_id=PROJECT_ID)
assert result is False

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

0 comments on commit c4244e1

Please sign in to comment.