From 18a5e44dbcc9805c0988e3c78c1a3796ea970904 Mon Sep 17 00:00:00 2001 From: Jacob Beck Date: Thu, 13 Sep 2018 13:12:08 -0600 Subject: [PATCH] fix unit tests --- test/unit/test_postgres_adapter.py | 75 +++++++++++++++++------------ test/unit/test_snowflake_adapter.py | 39 ++++++++------- 2 files changed, 65 insertions(+), 49 deletions(-) diff --git a/test/unit/test_postgres_adapter.py b/test/unit/test_postgres_adapter.py index b65410b0a7c..a3ac793411d 100644 --- a/test/unit/test_postgres_adapter.py +++ b/test/unit/test_postgres_adapter.py @@ -10,15 +10,20 @@ from psycopg2 import extensions as psycopg2_extensions import agate -from dbt.config import Profile +from .utils import config_from_parts_or_dicts class TestPostgresAdapter(unittest.TestCase): def setUp(self): flags.STRICT_MODE = True - - self.profile = Profile.from_raw_profile_info({ + project_cfg = { + 'name': 'X', + 'version': '0.1', + 'profile': 'test', + 'project-root': '/tmp/dbt/does-not-exist', + } + profile_cfg = { 'outputs': { 'test': { 'type': 'postgres', @@ -31,11 +36,13 @@ def setUp(self): } }, 'target': 'test' - }, 'test') + } + + self.config = config_from_parts_or_dicts(project_cfg, profile_cfg) def test_acquire_connection_validations(self): try: - connection = PostgresAdapter.acquire_connection(self.profile, + connection = PostgresAdapter.acquire_connection(self.config, 'dummy') self.assertEquals(connection.type, 'postgres') except ValidationException as e: @@ -45,14 +52,14 @@ def test_acquire_connection_validations(self): .format(str(e))) def test_acquire_connection(self): - connection = PostgresAdapter.acquire_connection(self.profile, 'dummy') + connection = PostgresAdapter.acquire_connection(self.config, 'dummy') self.assertEquals(connection.state, 'open') self.assertNotEquals(connection.handle, None) @mock.patch('dbt.adapters.postgres.impl.psycopg2') def test_default_keepalive(self, psycopg2): - connection = PostgresAdapter.acquire_connection(self.profile, 'dummy') + connection = PostgresAdapter.acquire_connection(self.config, 'dummy') psycopg2.connect.assert_called_once_with( dbname='postgres', @@ -64,9 +71,9 @@ def test_default_keepalive(self, psycopg2): @mock.patch('dbt.adapters.postgres.impl.psycopg2') def test_changed_keepalive(self, psycopg2): - credentials = self.profile.credentials.incorporate(keepalives_idle=256) - self.profile.credentials = credentials - connection = PostgresAdapter.acquire_connection(self.profile, 'dummy') + credentials = self.config.credentials.incorporate(keepalives_idle=256) + self.config.credentials = credentials + connection = PostgresAdapter.acquire_connection(self.config, 'dummy') psycopg2.connect.assert_called_once_with( dbname='postgres', @@ -79,9 +86,9 @@ def test_changed_keepalive(self, psycopg2): @mock.patch('dbt.adapters.postgres.impl.psycopg2') def test_set_zero_keepalive(self, psycopg2): - credentials = self.profile.credentials.incorporate(keepalives_idle=0) - self.profile.credentials = credentials - connection = PostgresAdapter.acquire_connection(self.profile, 'dummy') + credentials = self.config.credentials.incorporate(keepalives_idle=0) + self.config.credentials = credentials + connection = PostgresAdapter.acquire_connection(self.config, 'dummy') psycopg2.connect.assert_called_once_with( dbname='postgres', @@ -114,26 +121,32 @@ def test_get_catalog_various_schemas(self, mock_run): # give manifest the dict it wants mock_manifest = mock.MagicMock(spec_set=['nodes'], nodes=nodes) - catalog = PostgresAdapter.get_catalog({}, {}, mock_manifest) + catalog = PostgresAdapter.get_catalog(mock.MagicMock(), mock_manifest) self.assertEqual( set(map(tuple, catalog)), {('foo', 'bar'), ('FOO', 'baz'), ('quux', 'bar')} ) + class TestConnectingPostgresAdapter(unittest.TestCase): def setUp(self): flags.STRICT_MODE = False - self.profile = { - 'dbname': 'postgres', - 'user': 'root', - 'host': 'database', - 'pass': 'password', - 'port': 5432, - 'schema': 'public' + profile_cfg = { + 'outputs': { + 'test': { + 'type': 'postgres', + 'dbname': 'postgres', + 'user': 'root', + 'host': 'database', + 'pass': 'password', + 'port': 5432, + 'schema': 'public' + } + }, + 'target': 'test' } - - self.project = { + project_cfg = { 'name': 'X', 'version': '0.1', 'profile': 'test', @@ -144,6 +157,8 @@ def setUp(self): } } + self.config = config_from_parts_or_dicts(project_cfg, profile_cfg) + self.handle = mock.MagicMock(spec=psycopg2_extensions.connection) self.cursor = self.handle.cursor.return_value self.mock_execute = self.cursor.execute @@ -151,7 +166,7 @@ def setUp(self): self.psycopg2 = self.patcher.start() self.psycopg2.connect.return_value = self.handle - conn = PostgresAdapter.get_connection(self.profile) + conn = PostgresAdapter.get_connection(self.config) def tearDown(self): # we want a unique self.handle every time. @@ -160,8 +175,7 @@ def tearDown(self): def test_quoting_on_drop_schema(self): PostgresAdapter.drop_schema( - profile=self.profile, - project_cfg=self.project, + config=self.config, schema='test_schema' ) @@ -171,8 +185,7 @@ def test_quoting_on_drop_schema(self): def test_quoting_on_drop(self): PostgresAdapter.drop( - profile=self.profile, - project_cfg=self.project, + config=self.config, schema='test_schema', relation='test_table', relation_type='table' @@ -183,8 +196,7 @@ def test_quoting_on_drop(self): def test_quoting_on_truncate(self): PostgresAdapter.truncate( - profile=self.profile, - project_cfg=self.project, + config=self.config, schema='test_schema', table='test_table' ) @@ -194,8 +206,7 @@ def test_quoting_on_truncate(self): def test_quoting_on_rename(self): PostgresAdapter.rename( - profile=self.profile, - project_cfg=self.project, + config=self.config, schema='test_schema', from_name='table_a', to_name='table_b' diff --git a/test/unit/test_snowflake_adapter.py b/test/unit/test_snowflake_adapter.py index 71746e2e30b..9aa290c7f57 100644 --- a/test/unit/test_snowflake_adapter.py +++ b/test/unit/test_snowflake_adapter.py @@ -9,20 +9,28 @@ from dbt.logger import GLOBAL_LOGGER as logger # noqa from snowflake import connector as snowflake_connector +from .utils import config_from_parts_or_dicts + class TestSnowflakeAdapter(unittest.TestCase): def setUp(self): flags.STRICT_MODE = False - self.profile = { - 'dbname': 'postgres', - 'user': 'root', - 'host': 'database', - 'pass': 'password', - 'port': 5432, - 'schema': 'public' + profile_cfg = { + 'outputs': { + 'test': { + 'type': 'snowflake', + 'account': 'test_account', + 'user': 'test_user', + 'password': 'test_password', + 'database': 'test_databse', + 'warehouse': 'test_warehouse', + 'schema': 'public', + }, + }, + 'target': 'test', } - self.project = { + project_cfg = { 'name': 'X', 'version': '0.1', 'profile': 'test', @@ -32,6 +40,7 @@ def setUp(self): 'schema': True, } } + self.config = config_from_parts_or_dicts(project_cfg, profile_cfg) self.handle = mock.MagicMock(spec=snowflake_connector.SnowflakeConnection) self.cursor = self.handle.cursor.return_value @@ -40,7 +49,7 @@ def setUp(self): self.snowflake = self.patcher.start() self.snowflake.return_value = self.handle - conn = SnowflakeAdapter.get_connection(self.profile) + conn = SnowflakeAdapter.get_connection(self.config) def tearDown(self): # we want a unique self.handle every time. @@ -49,8 +58,7 @@ def tearDown(self): def test_quoting_on_drop_schema(self): SnowflakeAdapter.drop_schema( - profile=self.profile, - project_cfg=self.project, + config=self.config, schema='test_schema' ) @@ -60,8 +68,7 @@ def test_quoting_on_drop_schema(self): def test_quoting_on_drop(self): SnowflakeAdapter.drop( - profile=self.profile, - project_cfg=self.project, + config=self.config, schema='test_schema', relation='test_table', relation_type='table' @@ -72,8 +79,7 @@ def test_quoting_on_drop(self): def test_quoting_on_truncate(self): SnowflakeAdapter.truncate( - profile=self.profile, - project_cfg=self.project, + config=self.config, schema='test_schema', table='test_table' ) @@ -83,8 +89,7 @@ def test_quoting_on_truncate(self): def test_quoting_on_rename(self): SnowflakeAdapter.rename( - profile=self.profile, - project_cfg=self.project, + config=self.config, schema='test_schema', from_name='table_a', to_name='table_b'