def setUp(self): self.schema = JsonObject(TABLE_NAME, Property('id', 'VARCHAR(36)')) self.database = Database(psycopg2.connect(CONNECTION)) self.database.open() table = TargetTable(self.schema, self.database) table.create() self.database.commit()
def test_create_when_column_name_defined(self): schema = JsonObject(TABLE_NAME, Property('property1', 'VARCHAR(10)', 'someColumn'), Property('property2', 'TIMESTAMP', 'anotherColumn')) table = TargetTable(schema, self.database) table.create() self.assertColumns(schema.table, schema)
def test_create_when_column_name_not_defined_for_nested_property(self): with patch.object(Database, 'execute') as execute: schema = JsonObject( TABLE_NAME, Property('property1', 'VARCHAR(10)'), Property('property2', Property('timestamp', 'TIMESTAMP'))) table = TargetTable(schema, Database(Mock())) table.create() expected_sql = 'CREATE TABLE {0} (property1 VARCHAR(10), ' \ 'property2_timestamp TIMESTAMP)'.format(TABLE_NAME) execute.assert_called_once_with(expected_sql)
def test_create_when_column_name_defined(self): with patch.object(Database, 'execute') as execute: schema = JsonObject( TABLE_NAME, Property('property1', 'VARCHAR(10)', 'someColumn'), Property('property2', 'TIMESTAMP', 'anotherColumn')) table = TargetTable(schema, Database(Mock())) table.create() expected_sql = 'CREATE TABLE {0} (someColumn VARCHAR(10), ' \ 'anotherColumn TIMESTAMP)'.format(TABLE_NAME) execute.assert_called_once_with(expected_sql)
def test_create_when_column_name_not_defined(self): with patch.object(Database, 'execute') as execute: schema = JsonObject(TABLE_NAME, Property('property1', 'VARCHAR(10)'), Property('property2', 'TIMESTAMP')) table = TargetTable(schema, Database(Mock())) table.create() expected_sql = 'CREATE TABLE {0} (property1 VARCHAR(10), ' \ 'property2 TIMESTAMP)'.format(TABLE_NAME) execute.assert_called_once_with(expected_sql)
def assert_copy_schema_to_redshift_with_drop(self, sql, execute): with patch.object(TargetTable, 'exists') as exists: exists.return_value = True target_table = TargetTable(self.schema, Mock()) target_table.stage_update = Mock() target_table.drop = Mock() target_table.create = Mock() target_table.insert_update = Mock() step = ManifestCopyFromS3JsonStep(metadata='', source=SOURCE, schema=self.schema, aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY, bucket=self.bucket, table=target_table) step.manifest.save = Mock(return_value=self.updated_journal) step.manifest.journal_exists = Mock(return_value=False) step.sql = Mock() execute(step) self.assert_migration_with_drop(step) step.sql.execute.assert_called_once_with((sql, self.schema.table, step.manifest.manifest_url, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, step.schema_url, step.max_error_count)) return target_table.database
def test_insert_update(self): table = TargetTable(self.schema, self.database) table.create() table.stage_update() self.database.execute("INSERT INTO %s VALUES('%s')", (AsIs(TABLE_NAME), AsIs(1),)) self.database.execute("INSERT INTO %s VALUES('%s')", (AsIs(self.schema.update_table), AsIs(2),)) self.database.execute("INSERT INTO %s VALUES('%s')", (AsIs(self.schema.update_table), AsIs(3),)) table.insert_update() self.database.execute( "SELECT * FROM %s", (AsIs(TABLE_NAME),)) values = list(self.database.cursor.fetchall()) self.assertEqual([('1',), ('2',), ('3',)], values)
def test_exist(self): table = TargetTable(self.schema, self.database) table.create() self.assertEqual(True, table.exists())
def test_drop(self): table = TargetTable(self.schema, self.database) table.create() table.drop() self.assertEqual(False, table.exists())