Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pass the model name along in get_relations #1388

Merged
merged 2 commits into from
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion plugins/bigquery/dbt/adapters/bigquery/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ def get_relation(self, database, schema, identifier, model_name=None):
)

try:
table = self.connections.get_bq_table(database, schema, identifier)
table = self.connections.get_bq_table(database, schema, identifier,
conn_name=model_name)
except google.api_core.exceptions.NotFound:
table = None
return self._bq_table_to_relation(table)
Expand Down
49 changes: 48 additions & 1 deletion test/unit/test_bigquery_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def _bq_conn():
return conn


class TestBigQueryAdapter(unittest.TestCase):
class BaseTestBigQueryAdapter(unittest.TestCase):

def setUp(self):
flags.STRICT_MODE = True
Expand Down Expand Up @@ -73,6 +73,7 @@ def get_adapter(self, target):
return adapter


class TestBigQueryAdapterAcquire(BaseTestBigQueryAdapter):
@patch('dbt.adapters.bigquery.BigQueryConnectionManager.open', return_value=_bq_conn())
def test_acquire_connection_oauth_validations(self, mock_open_connection):
adapter = self.get_adapter('oauth')
Expand Down Expand Up @@ -134,6 +135,52 @@ def test_location_value(self, mock_bq, mock_auth_default):
location='Luna Station')


class TestConnectionNamePassthrough(BaseTestBigQueryAdapter):

def setUp(self):
super(TestConnectionNamePassthrough, self).setUp()
self._conn_patch = patch.object(BigQueryAdapter, 'ConnectionManager')
self.conn_manager_cls = self._conn_patch.start()

self._relation_patch = patch.object(BigQueryAdapter, 'Relation')
self.relation_cls = self._relation_patch.start()

self.mock_connection_manager = self.conn_manager_cls.return_value
self.conn_manager_cls.TYPE = 'bigquery'
self.relation_cls.DEFAULTS = BigQueryRelation.DEFAULTS

self.adapter = self.get_adapter('oauth')

def tearDown(self):
super(TestConnectionNamePassthrough, self).tearDown()
self._conn_patch.stop()
self._relation_patch.stop()

def test_get_relation(self):
self.adapter.get_relation('db', 'schema', 'my_model', 'my_conn')
self.mock_connection_manager.get_bq_table.assert_called_once_with('db', 'schema', 'my_model', conn_name='my_conn')

def test_create_schema(self):
self.adapter.create_schema('db', 'schema', 'my_conn')
self.mock_connection_manager.create_dataset.assert_called_once_with('db', 'schema', 'my_conn')

@patch.object(BigQueryAdapter, 'check_schema_exists')
def test_drop_schema(self, mock_check_schema):
mock_check_schema.return_value = True
self.adapter.drop_schema('db', 'schema', 'my_conn')
self.mock_connection_manager.drop_dataset.assert_called_once_with('db', 'schema', 'my_conn')

def test_get_columns_in_relation(self):
self.mock_connection_manager.get_bq_table.side_effect = ValueError
self.adapter.get_columns_in_relation(
MagicMock(database='db', schema='schema', table_name='ident'),
'my_conn'
)
self.mock_connection_manager.get_bq_table.assert_called_once_with(
database='db', schema='schema', identifier='ident', conn_name='my_conn'
)


class TestBigQueryRelation(unittest.TestCase):
def setUp(self):
flags.STRICT_MODE = True
Expand Down