Example #1
0
 def test_quoting_on_truncate(self):
     PostgresAdapter.truncate(profile=self.profile,
                              project_cfg=self.project,
                              schema='test_schema',
                              table='test_table')
     self.mock_execute.assert_has_calls(
         [mock.call('truncate table "test_schema".test_table', None)])
Example #2
0
    def test_quoting_on_drop_schema(self):
        PostgresAdapter.drop_schema(profile=self.profile,
                                    project_cfg=self.project,
                                    schema='test_schema')

        self.mock_execute.assert_has_calls(
            [mock.call('drop schema if exists "test_schema" cascade', None)])
Example #3
0
    def test__get_connection(self):
        connection = PostgresAdapter.get_connection(self.profile)
        duplicate = PostgresAdapter.get_connection(self.profile)

        self.assertEquals(connection.get('state'), 'open')
        self.assertNotEquals(connection.get('handle'), None)

        self.assertEquals(connection, duplicate)
Example #4
0
 def test_quoting_on_rename(self):
     PostgresAdapter.rename(profile=self.profile,
                            project_cfg=self.project,
                            schema='test_schema',
                            from_name='table_a',
                            to_name='table_b')
     self.mock_execute.assert_has_calls([
         mock.call('alter table "test_schema".table_a rename to table_b',
                   None)
     ])
Example #5
0
 def test_quoting_on_drop(self):
     PostgresAdapter.drop(profile=self.profile,
                          project_cfg=self.project,
                          schema='test_schema',
                          relation='test_table',
                          relation_type='table')
     self.mock_execute.assert_has_calls([
         mock.call('drop table if exists "test_schema".test_table cascade',
                   None)
     ])
Example #6
0
    def setUp(self):
        flags.STRICT_MODE = False

        self.target_dict = {
            'type': 'postgres',
            'dbname': 'postgres',
            'user': '******',
            'host': 'thishostshouldnotexist',
            'pass': '******',
            'port': 5432,
            'schema': 'public'
        }

        profile_cfg = {
            'outputs': {
                'test': self.target_dict,
            },
            'target': 'test'
        }
        project_cfg = {
            'name': 'X',
            'version': '0.1',
            'profile': 'test',
            'project-root': '/tmp/dbt/does-not-exist',
            'quoting': {
                'identifier': False,
                'schema': True,
            },
        }

        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.connections.psycopg2')
        self.psycopg2 = self.patcher.start()

        self.psycopg2.connect.return_value = self.handle
        self.adapter = PostgresAdapter(self.config)
        self.adapter.connections.query_header = MacroQueryStringSetter(
            self.config, mock.MagicMock(macros={}))

        self.qh_patch = mock.patch.object(
            self.adapter.connections.query_header, 'add')
        self.mock_query_header_add = self.qh_patch.start()
        self.mock_query_header_add.side_effect = lambda q: '/* dbt */\n{}'.format(
            q)
        self.adapter.acquire_connection()
        inject_adapter(self.adapter)

        self.load_patch = mock.patch('dbt.parser.manifest.make_parse_result')
        self.mock_parse_result = self.load_patch.start()
        self.mock_parse_result.return_value = ParseResult.rpc()
Example #7
0
    def setUp(self):
        flags.STRICT_MODE = False

        self.profile = {
            'dbname': 'postgres',
            'user': '******',
            'host': 'database',
            'pass': '******',
            'port': 5432,
            'schema': 'public'
        }

        self.project = {
            'name': 'X',
            'version': '0.1',
            'profile': 'test',
            'project-root': '/tmp/dbt/does-not-exist',
            'quoting': {
                'identifier': False,
                'schema': True,
            }
        }

        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)
Example #8
0
 def test_acquire_connection_validations(self):
     try:
         connection = PostgresAdapter.acquire_connection(self.profile)
         self.assertEquals(connection.get('type'), 'postgres')
     except ValidationException as e:
         self.fail('got ValidationException: {}'.format(str(e)))
     except BaseException as e:
         self.fail('validation failed with unknown exception: {}'.format(
             str(e)))
Example #9
0
    def test_default_keepalive(self, psycopg2):
        connection = PostgresAdapter.acquire_connection(self.profile, 'dummy')

        psycopg2.connect.assert_called_once_with(dbname='postgres',
                                                 user='******',
                                                 host='database',
                                                 password='******',
                                                 port=5432,
                                                 connect_timeout=10)
    def setUp(self):
        flags.STRICT_MODE = False

        profile_cfg = {
            'outputs': {
                'test': {
                    'type': 'postgres',
                    'dbname': 'postgres',
                    'user': '******',
                    'host': 'thishostshouldnotexist',
                    'pass': '******',
                    'port': 5432,
                    'schema': 'public'
                }
            },
            'target': 'test'
        }
        project_cfg = {
            'name': 'X',
            'version': '0.1',
            'profile': 'test',
            'project-root': '/tmp/dbt/does-not-exist',
            'quoting': {
                'identifier': False,
                'schema': True,
            }
        }

        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.connections.psycopg2')
        self.psycopg2 = self.patcher.start()
        # there must be a better way to do this...
        self.psycopg2.DatabaseError = DatabaseError
        self.psycopg2.Error = Error

        self.psycopg2.connect.return_value = self.handle
        self.adapter = PostgresAdapter(self.config)
        self.adapter.acquire_connection()
        inject_adapter(self.adapter)
Example #11
0
 def test_convert_boolean_type(self):
     rows = [
         ['', 'false', 'true'],
         ['', 'false', 'false'],
         ['', 'false', 'true'],
     ]
     agate_table = self._make_table_of(rows, agate.Boolean)
     expected = ['boolean', 'boolean', 'boolean']
     for col_idx, expect in enumerate(expected):
         assert PostgresAdapter.convert_boolean_type(agate_table,
                                                     col_idx) == expect
Example #12
0
 def test_convert_date_type(self):
     rows = [
         ['', '2019-01-01', '2019-01-04'],
         ['', '2019-01-02', '2019-01-04'],
         ['', '2019-01-03', '2019-01-04'],
     ]
     agate_table = self._make_table_of(rows, agate.Date)
     expected = ['date', 'date', 'date']
     for col_idx, expect in enumerate(expected):
         assert PostgresAdapter.convert_date_type(agate_table,
                                                  col_idx) == expect
Example #13
0
 def test_convert_text_type(self):
     rows = [
         ['', 'a1', 'stringval1'],
         ['', 'a2', 'stringvalasdfasdfasdfa'],
         ['', 'a3', 'stringval3'],
     ]
     agate_table = self._make_table_of(rows, agate.Text)
     expected = ['text', 'text', 'text']
     for col_idx, expect in enumerate(expected):
         assert PostgresAdapter.convert_text_type(agate_table,
                                                  col_idx) == expect
Example #14
0
 def test_convert_number_type(self):
     rows = [
         ['', '23.98', '-1'],
         ['', '12.78', '-2'],
         ['', '79.41', '-3'],
     ]
     agate_table = self._make_table_of(rows, agate.Number)
     expected = ['integer', 'float8', 'integer']
     for col_idx, expect in enumerate(expected):
         assert PostgresAdapter.convert_number_type(agate_table,
                                                    col_idx) == expect
Example #15
0
 def test_convert_time_type(self):
     # dbt's default type testers actually don't have a TimeDelta at all.
     agate.TimeDelta
     rows = [
         ['', '120s', '10s'],
         ['', '3m', '11s'],
         ['', '1h', '12s'],
     ]
     agate_table = self._make_table_of(rows, agate.TimeDelta)
     expected = ['time', 'time', 'time']
     for col_idx, expect in enumerate(expected):
         assert PostgresAdapter.convert_time_type(agate_table,
                                                  col_idx) == expect
Example #16
0
 def test_convert_datetime_type(self):
     rows = [
         ['', '20190101T01:01:01Z', '2019-01-01 01:01:01'],
         ['', '20190102T01:01:01Z', '2019-01-01 01:01:01'],
         ['', '20190103T01:01:01Z', '2019-01-01 01:01:01'],
     ]
     agate_table = self._make_table_of(
         rows, [agate.DateTime, agate_helper.ISODateTime, agate.DateTime])
     expected = [
         'timestamp without time zone', 'timestamp without time zone',
         'timestamp without time zone'
     ]
     for col_idx, expect in enumerate(expected):
         assert PostgresAdapter.convert_datetime_type(agate_table,
                                                      col_idx) == expect
Example #17
0
    def test_get_catalog_various_schemas(self, mock_run):
        column_names = ['table_schema', 'table_name']
        rows = [('foo', 'bar'), ('FOO', 'baz'), (None, 'bar'), ('quux', 'bar'),
                ('skip', 'bar')]
        mock_run.return_value = agate.Table(rows=rows,
                                            column_names=column_names)

        # we should accept the lowercase matching 'foo's only.
        mock_nodes = [
            mock.MagicMock(spec_set=['schema'], schema='foo') for k in range(2)
        ]
        mock_nodes.append(mock.MagicMock(spec_set=['schema'], schema='quux'))
        nodes = {str(idx): n for idx, n in enumerate(mock_nodes)}
        # give manifest the dict it wants
        mock_manifest = mock.MagicMock(spec_set=['nodes'], nodes=nodes)

        catalog = PostgresAdapter.get_catalog({}, {}, mock_manifest)
        self.assertEqual(set(map(tuple, catalog)), {('foo', 'bar'),
                                                    ('FOO', 'baz'),
                                                    ('quux', 'bar')})
    def setUp(self):
        flags.STRICT_MODE = False

        profile_cfg = {
            'outputs': {
                'test': {
                    'type': 'postgres',
                    'dbname': 'postgres',
                    'user': '******',
                    'host': 'database',
                    'pass': '******',
                    'port': 5432,
                    'schema': 'public'
                }
            },
            'target': 'test'
        }
        project_cfg = {
            'name': 'X',
            'version': '0.1',
            'profile': 'test',
            'project-root': '/tmp/dbt/does-not-exist',
            'quoting': {
                'identifier': False,
                'schema': True,
            }
        }

        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.connections.psycopg2')
        self.psycopg2 = self.patcher.start()

        self.psycopg2.connect.return_value = self.handle
        self.adapter = PostgresAdapter(self.config)
        inject_adapter('postgres', self.adapter)
Example #19
0
    def test__catalog_filter_table(self):
        manifest = mock.MagicMock()
        manifest.get_used_schemas.return_value = [['a', 'B'], ['a', '1234']]
        column_names = [
            'table_name', 'table_database', 'table_schema', 'something'
        ]
        rows = [
            ['foo', 'a', 'b', '1234'],  # include
            ['foo', 'a', '1234', '1234'],  # include, w/ table schema as str
            ['foo', 'c', 'B', '1234'],  # skip
            ['1234', 'A', 'B', '1234'],  # include, w/ table name as str
        ]
        table = agate.Table(rows, column_names,
                            agate_helper.DEFAULT_TYPE_TESTER)

        result = PostgresAdapter._catalog_filter_table(table, manifest)
        assert len(result) == 3
        for row in result.rows:
            assert isinstance(row['table_schema'], str)
            assert isinstance(row['table_database'], str)
            assert isinstance(row['table_name'], str)
            assert isinstance(row['something'], decimal.Decimal)
Example #20
0
 def test_dbname_verification_is_case_insensitive(self):
     # Override adapter settings from setUp()
     self.target_dict['dbname'] = 'Postgres'
     profile_cfg = {
         'outputs': {
             'test': self.target_dict,
         },
         'target': 'test'
     }
     project_cfg = {
         'name': 'X',
         'version': '0.1',
         'profile': 'test',
         'project-root': '/tmp/dbt/does-not-exist',
         'quoting': {
             'identifier': False,
             'schema': True,
         },
     }
     self.config = config_from_parts_or_dicts(project_cfg, profile_cfg)
     self.adapter.cleanup_connections()
     self._adapter = PostgresAdapter(self.config)
     self.adapter.verify_database('postgres')
class TestConnectingPostgresAdapter(unittest.TestCase):
    def setUp(self):
        flags.STRICT_MODE = False

        profile_cfg = {
            'outputs': {
                'test': {
                    'type': 'postgres',
                    'dbname': 'postgres',
                    'user': '******',
                    'host': 'database',
                    'pass': '******',
                    'port': 5432,
                    'schema': 'public'
                }
            },
            'target': 'test'
        }
        project_cfg = {
            'name': 'X',
            'version': '0.1',
            'profile': 'test',
            'project-root': '/tmp/dbt/does-not-exist',
            'quoting': {
                'identifier': False,
                'schema': True,
            }
        }

        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.connections.psycopg2')
        self.psycopg2 = self.patcher.start()

        self.psycopg2.connect.return_value = self.handle
        self.adapter = PostgresAdapter(self.config)
        inject_adapter('postgres', self.adapter)

    def tearDown(self):
        # we want a unique self.handle every time.
        self.adapter.cleanup_connections()
        self.patcher.stop()

    def test_quoting_on_drop_schema(self):
        self.adapter.drop_schema(database='postgres', schema='test_schema')

        self.mock_execute.assert_has_calls([
            mock.call('drop schema if exists "test_schema" cascade', None)
        ])

    def test_quoting_on_drop(self):
        relation = self.adapter.Relation.create(
            database='postgres',
            schema='test_schema',
            identifier='test_table',
            type='table',
            quote_policy=self.adapter.config.quoting,
        )
        self.adapter.drop_relation(relation)
        self.mock_execute.assert_has_calls([
            mock.call('drop table if exists "postgres"."test_schema".test_table cascade', None)
        ])

    def test_quoting_on_truncate(self):
        relation = self.adapter.Relation.create(
            database='postgres',
            schema='test_schema',
            identifier='test_table',
            type='table',
            quote_policy=self.adapter.config.quoting,
        )
        self.adapter.truncate_relation(relation)
        self.mock_execute.assert_has_calls([
            mock.call('truncate table "postgres"."test_schema".test_table', None)
        ])

    def test_quoting_on_rename(self):
        from_relation = self.adapter.Relation.create(
            database='postgres',
            schema='test_schema',
            identifier='table_a',
            type='table',
            quote_policy=self.adapter.config.quoting,
        )
        to_relation = self.adapter.Relation.create(
            database='postgres',
            schema='test_schema',
            identifier='table_b',
            type='table',
            quote_policy=self.adapter.config.quoting,
        )

        self.adapter.rename_relation(
            from_relation=from_relation,
            to_relation=to_relation
        )
        self.mock_execute.assert_has_calls([
            mock.call('alter table "postgres"."test_schema".table_a rename to table_b', None)
        ])
Example #22
0
class TestConnectingPostgresAdapter(unittest.TestCase):
    def setUp(self):
        flags.STRICT_MODE = False

        profile_cfg = {
            'outputs': {
                'test': {
                    'type': 'postgres',
                    'dbname': 'postgres',
                    'user': '******',
                    'host': 'database',
                    'pass': '******',
                    'port': 5432,
                    'schema': 'public'
                }
            },
            'target': 'test'
        }
        project_cfg = {
            'name': 'X',
            'version': '0.1',
            'profile': 'test',
            'project-root': '/tmp/dbt/does-not-exist',
            'quoting': {
                'identifier': False,
                'schema': True,
            }
        }

        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
        self.adapter = PostgresAdapter(self.config)
        self.adapter.get_connection()

    def tearDown(self):
        # we want a unique self.handle every time.
        self.adapter.cleanup_connections()
        self.patcher.stop()

    def test_quoting_on_drop_schema(self):
        self.adapter.drop_schema(schema='test_schema')

        self.mock_execute.assert_has_calls(
            [mock.call('drop schema if exists "test_schema" cascade', None)])

    def test_quoting_on_drop(self):
        self.adapter.drop(schema='test_schema',
                          relation='test_table',
                          relation_type='table')
        self.mock_execute.assert_has_calls([
            mock.call('drop table if exists "test_schema".test_table cascade',
                      None)
        ])

    def test_quoting_on_truncate(self):
        self.adapter.truncate(schema='test_schema', table='test_table')
        self.mock_execute.assert_has_calls(
            [mock.call('truncate table "test_schema".test_table', None)])

    def test_quoting_on_rename(self):
        self.adapter.rename(schema='test_schema',
                            from_name='table_a',
                            to_name='table_b')
        self.mock_execute.assert_has_calls([
            mock.call('alter table "test_schema".table_a rename to table_b',
                      None)
        ])
Example #23
0
    def test_acquire_connection(self):
        connection = PostgresAdapter.acquire_connection(self.profile)

        self.assertEquals(connection.get('state'), 'open')
        self.assertNotEquals(connection.get('handle'), None)
Example #24
0
 def adapter(self):
     return PostgresAdapter(self.config)
class TestConnectingPostgresAdapter(unittest.TestCase):
    def setUp(self):
        flags.STRICT_MODE = False

        profile_cfg = {
            'outputs': {
                'test': {
                    'type': 'postgres',
                    'dbname': 'postgres',
                    'user': '******',
                    'host': 'thishostshouldnotexist',
                    'pass': '******',
                    'port': 5432,
                    'schema': 'public'
                }
            },
            'target': 'test'
        }
        project_cfg = {
            'name': 'X',
            'version': '0.1',
            'profile': 'test',
            'project-root': '/tmp/dbt/does-not-exist',
            'quoting': {
                'identifier': False,
                'schema': True,
            }
        }

        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.connections.psycopg2')
        self.psycopg2 = self.patcher.start()
        # there must be a better way to do this...
        self.psycopg2.DatabaseError = DatabaseError
        self.psycopg2.Error = Error

        self.psycopg2.connect.return_value = self.handle
        self.adapter = PostgresAdapter(self.config)
        self.adapter.acquire_connection()
        inject_adapter(self.adapter)

    def tearDown(self):
        # we want a unique self.handle every time.
        self.adapter.cleanup_connections()
        self.patcher.stop()

    def test_quoting_on_drop_schema(self):
        self.adapter.drop_schema(database='postgres', schema='test_schema')

        self.mock_execute.assert_has_calls(
            [mock.call('drop schema if exists "test_schema" cascade', None)])

    def test_quoting_on_drop(self):
        relation = self.adapter.Relation.create(
            database='postgres',
            schema='test_schema',
            identifier='test_table',
            type='table',
            quote_policy=self.adapter.config.quoting,
        )
        self.adapter.drop_relation(relation)
        self.mock_execute.assert_has_calls([
            mock.call(
                'drop table if exists "postgres"."test_schema".test_table cascade',
                None)
        ])

    def test_quoting_on_truncate(self):
        relation = self.adapter.Relation.create(
            database='postgres',
            schema='test_schema',
            identifier='test_table',
            type='table',
            quote_policy=self.adapter.config.quoting,
        )
        self.adapter.truncate_relation(relation)
        self.mock_execute.assert_has_calls([
            mock.call('truncate table "postgres"."test_schema".test_table',
                      None)
        ])

    def test_quoting_on_rename(self):
        from_relation = self.adapter.Relation.create(
            database='postgres',
            schema='test_schema',
            identifier='table_a',
            type='table',
            quote_policy=self.adapter.config.quoting,
        )
        to_relation = self.adapter.Relation.create(
            database='postgres',
            schema='test_schema',
            identifier='table_b',
            type='table',
            quote_policy=self.adapter.config.quoting,
        )

        self.adapter.rename_relation(from_relation=from_relation,
                                     to_relation=to_relation)
        self.mock_execute.assert_has_calls([
            mock.call(
                'alter table "postgres"."test_schema".table_a rename to table_b',
                None)
        ])
Example #26
0
class TestConnectingPostgresAdapter(unittest.TestCase):
    def setUp(self):
        flags.STRICT_MODE = False

        self.target_dict = {
            'type': 'postgres',
            'dbname': 'postgres',
            'user': '******',
            'host': 'thishostshouldnotexist',
            'pass': '******',
            'port': 5432,
            'schema': 'public'
        }

        profile_cfg = {
            'outputs': {
                'test': self.target_dict,
            },
            'target': 'test'
        }
        project_cfg = {
            'name': 'X',
            'version': '0.1',
            'profile': 'test',
            'project-root': '/tmp/dbt/does-not-exist',
            'quoting': {
                'identifier': False,
                'schema': True,
            },
        }

        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.connections.psycopg2')
        self.psycopg2 = self.patcher.start()

        self.psycopg2.connect.return_value = self.handle
        self.adapter = PostgresAdapter(self.config)
        self.adapter.connections.query_header = MacroQueryStringSetter(
            self.config, mock.MagicMock(macros={}))

        self.qh_patch = mock.patch.object(
            self.adapter.connections.query_header, 'add')
        self.mock_query_header_add = self.qh_patch.start()
        self.mock_query_header_add.side_effect = lambda q: '/* dbt */\n{}'.format(
            q)
        self.adapter.acquire_connection()
        inject_adapter(self.adapter)

        self.load_patch = mock.patch('dbt.parser.manifest.make_parse_result')
        self.mock_parse_result = self.load_patch.start()
        self.mock_parse_result.return_value = ParseResult.rpc()

    def tearDown(self):
        # we want a unique self.handle every time.
        self.adapter.cleanup_connections()
        self.qh_patch.stop()
        self.patcher.stop()
        self.load_patch.stop()

    def test_quoting_on_drop_schema(self):
        relation = self.adapter.Relation.create(
            database='postgres',
            schema='test_schema',
            quote_policy=self.adapter.config.quoting,
        )
        self.adapter.drop_schema(relation)

        self.mock_execute.assert_has_calls([
            mock.call('/* dbt */\ndrop schema if exists "test_schema" cascade',
                      None)
        ])

    def test_quoting_on_drop(self):
        relation = self.adapter.Relation.create(
            database='postgres',
            schema='test_schema',
            identifier='test_table',
            type='table',
            quote_policy=self.adapter.config.quoting,
        )
        self.adapter.drop_relation(relation)
        self.mock_execute.assert_has_calls([
            mock.call(
                '/* dbt */\ndrop table if exists "postgres"."test_schema".test_table cascade',
                None)
        ])

    def test_quoting_on_truncate(self):
        relation = self.adapter.Relation.create(
            database='postgres',
            schema='test_schema',
            identifier='test_table',
            type='table',
            quote_policy=self.adapter.config.quoting,
        )
        self.adapter.truncate_relation(relation)
        self.mock_execute.assert_has_calls([
            mock.call(
                '/* dbt */\ntruncate table "postgres"."test_schema".test_table',
                None)
        ])

    def test_quoting_on_rename(self):
        from_relation = self.adapter.Relation.create(
            database='postgres',
            schema='test_schema',
            identifier='table_a',
            type='table',
            quote_policy=self.adapter.config.quoting,
        )
        to_relation = self.adapter.Relation.create(
            database='postgres',
            schema='test_schema',
            identifier='table_b',
            type='table',
            quote_policy=self.adapter.config.quoting,
        )

        self.adapter.rename_relation(from_relation=from_relation,
                                     to_relation=to_relation)
        self.mock_execute.assert_has_calls([
            mock.call(
                '/* dbt */\nalter table "postgres"."test_schema".table_a rename to table_b',
                None)
        ])

    def test_debug_connection_ok(self):
        DebugTask.validate_connection(self.target_dict)
        self.mock_execute.assert_has_calls(
            [mock.call('/* dbt */\nselect 1 as id', None)])

    def test_debug_connection_fail_nopass(self):
        del self.target_dict['pass']
        with self.assertRaises(DbtConfigError):
            DebugTask.validate_connection(self.target_dict)

    def test_connection_fail_select(self):
        self.mock_execute.side_effect = DatabaseError()
        with self.assertRaises(DbtConfigError):
            DebugTask.validate_connection(self.target_dict)
        self.mock_execute.assert_has_calls(
            [mock.call('/* dbt */\nselect 1 as id', None)])

    def test_dbname_verification_is_case_insensitive(self):
        # Override adapter settings from setUp()
        self.target_dict['dbname'] = 'Postgres'
        profile_cfg = {
            'outputs': {
                'test': self.target_dict,
            },
            'target': 'test'
        }
        project_cfg = {
            'name': 'X',
            'version': '0.1',
            'profile': 'test',
            'project-root': '/tmp/dbt/does-not-exist',
            'quoting': {
                'identifier': False,
                'schema': True,
            },
        }
        self.config = config_from_parts_or_dicts(project_cfg, profile_cfg)
        self.adapter.cleanup_connections()
        self._adapter = PostgresAdapter(self.config)
        self.adapter.verify_database('postgres')
Example #27
0
 def adapter(self):
     if self._adapter is None:
         self._adapter = PostgresAdapter(self.config)
         inject_adapter(self._adapter)
     return self._adapter
Example #28
0
 def tearDown(self):
     # we want a unique self.handle every time.
     PostgresAdapter.cleanup_connections()
     self.patcher.stop()
    def setUp(self):
        flags.STRICT_MODE = False

        self.target_dict = {
            'type': 'postgres',
            'dbname': 'postgres',
            'user': '******',
            'host': 'thishostshouldnotexist',
            'pass': '******',
            'port': 5432,
            'schema': 'public'
        }

        profile_cfg = {
            'outputs': {
                'test': self.target_dict,
            },
            'target': 'test'
        }
        project_cfg = {
            'name': 'X',
            'version': '0.1',
            'profile': 'test',
            'project-root': '/tmp/dbt/does-not-exist',
            'quoting': {
                'identifier': False,
                'schema': True,
            },
            'config-version': 2,
        }

        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.connections.psycopg2')
        self.psycopg2 = self.patcher.start()

        # Create the Manifest.state_check patcher
        @mock.patch(
            'dbt.parser.manifest.ManifestLoader.build_manifest_state_check')
        def _mock_state_check(self):
            config = self.root_project
            all_projects = self.all_projects
            return ManifestStateCheck(
                vars_hash=FileHash.from_contents('vars'),
                project_hashes={
                    name: FileHash.from_contents(name)
                    for name in all_projects
                },
                profile_hash=FileHash.from_contents('profile'),
            )

        self.load_state_check = mock.patch(
            'dbt.parser.manifest.ManifestLoader.build_manifest_state_check')
        self.mock_state_check = self.load_state_check.start()
        self.mock_state_check.side_effect = _mock_state_check

        self.psycopg2.connect.return_value = self.handle
        self.adapter = PostgresAdapter(self.config)
        self.adapter._macro_manifest_lazy = load_internal_manifest_macros(
            self.config)
        self.adapter.connections.query_header = MacroQueryStringSetter(
            self.config, self.adapter._macro_manifest_lazy)

        self.qh_patch = mock.patch.object(
            self.adapter.connections.query_header, 'add')
        self.mock_query_header_add = self.qh_patch.start()
        self.mock_query_header_add.side_effect = lambda q: '/* dbt */\n{}'.format(
            q)
        self.adapter.acquire_connection()
        inject_adapter(self.adapter, PostgresPlugin)