Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Beck committed Sep 14, 2018
1 parent 6652ece commit 18a5e44
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 49 deletions.
75 changes: 43 additions & 32 deletions test/unit/test_postgres_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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:
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand All @@ -144,14 +157,16 @@ 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
self.patcher = mock.patch('dbt.adapters.postgres.impl.psycopg2')
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.
Expand All @@ -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'
)

Expand All @@ -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'
Expand All @@ -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'
)
Expand All @@ -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'
Expand Down
39 changes: 22 additions & 17 deletions test/unit/test_snowflake_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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'
)

Expand All @@ -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'
Expand All @@ -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'
)
Expand All @@ -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'
Expand Down

0 comments on commit 18a5e44

Please sign in to comment.