def test_rev_eng_indices(self): catalog, schema, table = self._set_catalog_schema_table('AdventureWorks', 'HumanResources', 'EmployeeAddress') res = DbMssqlRE.reverseEngineerUserDatatypes(self.connection, catalog) self.assertEqual(res, 0) res = DbMssqlRE.reverseEngineerTableColumns(self.connection, table) self.assertEqual(res, 0) res = DbMssqlRE.reverseEngineerTableIndices(self.connection, table) self.assertEqual(res, 0) detected_index = 0 for index in table.indices: if index.name == 'PK_EmployeeAddress_EmployeeID_AddressID': detected_index += 1 elif index.name == 'AK_EmployeeAddress_rowguid': detected_index += 1 self.assertEqual(index.unique, 1) self.assertEqual(index.isPrimary, 0) self.assertEqual(index.clustered, 0) self.assertEqual(index.indexType, 'UNIQUE') self.assertEqual(set(icol.referencedColumn.name for icol in index.columns), set(['rowguid'])) self.assertEqual(detected_index, 1) # Check fail on table with empty columns: table1 = grt.classes.db_mssql_Table() table1.name = 'Vendor' table1.owner = schema res = DbMssqlRE.reverseEngineerTableIndices(self.connection, table1) self.assertEqual(res, 1) indices = [ index.name for index in table1.indices ] self.assertEqual(indices, [])
def test_migrate_table_columns(self): # Prepare source objects: source_catalog, source_schema, source_table = self._set_catalog_schema_table( 'AdventureWorks', 'Person', 'Contact') res = DbMssqlRE.reverseEngineerUserDatatypes(self.state, self.connection, source_catalog) self.assertEqual(res, 0) res = DbMssqlRE.reverseEngineerTableColumns(self.state, self.connection, source_table) self.assertEqual(res, 0) # Do migration: target_catalog = DbMssqlMigration.migrateCatalog( self.state, source_catalog) self.assertTrue(target_catalog != None) target_table = grt.classes.db_mysql_Schema() res = DbMssqlMigration.migrateTableToMySQL(self.state, source_table, target_schema) self.assertEqual(res, 0) self.assertEqual(target_table.name, 'Contact') res = DbMssqlMigration.migrateTableColumnsToMySQL( source_table, target_table) self.assertEqual(res, 0) self.assertEqual(len(target_table.columns), len(source_table.columns))
def test_migrate_indices(self): # Prepare source objects: source_catalog, source_schema, source_table = self._set_catalog_schema_table('AdventureWorks', 'HumanResources', 'EmployeeAddress') res = DbMssqlRE.reverseEngineerUserDatatypes(self.connection, source_catalog) self.assertEqual(res, 0) res = DbMssqlRE.reverseEngineerTableColumns(self.connection, source_table) self.assertEqual(res, 0) res = DbMssqlRE.reverseEngineerTableIndices(self.connection, source_table) self.assertEqual(res, 0) # Do migration: target_catalog = grt.classes.db_mysql_Catalog() res = DbMssqlMigration.migrateCatalog(source_catalog, target_catalog) self.assertEqual(res, 0) target_table = grt.classes.db_mysql_Table() res = DbMssqlMigration.migrateTableToMySQL(source_table, target_table) self.assertEqual(res, 0) res = DbMssqlMigration.migrateTableColumnsToMySQL(source_table, target_table) self.assertEqual(res, 0) res = DbMssqlMigration.migrateTableIndicesToMySQL(source_table, target_table) self.assertEqual(res, 0) # Additional tests: detected_index = 0 for index in target_table.indices: if index.name == 'PK_EmployeeAddress_EmployeeID_AddressID': detected_index += 1 elif index.name == 'AK_EmployeeAddress_rowguid': detected_index += 1 self.assertEqual(index.unique, 1) self.assertEqual(index.isPrimary, 0) self.assertEqual(index.indexType, 'UNIQUE') self.assertEqual(set(icol.referencedColumn.name for icol in index.columns), set(['rowguid'])) self.assertEqual(detected_index, 1)
def test_rev_eng_columns(self): catalog, schema, table = self._set_catalog_schema_table('AdventureWorks', 'Person', 'Contact') res = DbMssqlRE.reverseEngineerUserDatatypes(self.connection, catalog) self.assertEqual(res, 0) res = DbMssqlRE.reverseEngineerTableColumns(self.connection, table) self.assertEqual(res, 0) column = None for col in table.columns: if col.name == 'ModifiedDate': column = col break self.assertIsNotNone(column) self.assertEqual(column.defaultValue, 'getdate()') self.assertEqual(column.simpleType.name.upper(), 'DATETIME') self.assertEqual(column.length, 8) column = None for col in table.columns: if col.name == 'AdditionalContactInfo': column = col break self.assertIsNotNone(column) self.assertEqual(column.simpleType.name.upper(), 'XML') self.assertEqual(column.length, -1) column = None for col in table.columns: if col.name == 'Phone': column = col break self.assertIsNotNone(column) self.assertIsNone(column.simpleType) self.assertIsNotNone(column.userType) self.assertEqual(column.userType.name, 'PHONE')
def test_migrate_table_pks(self): # Prepare source objects: source_catalog, source_schema, source_table = self._set_catalog_schema_table( 'AdventureWorks', 'Purchasing', 'VendorContact') res = DbMssqlRE.reverseEngineerUserDatatypes(self.connection, source_catalog) self.assertEqual(res, 0) res = DbMssqlRE.reverseEngineerTableColumns(self.connection, source_table, grt.Dict()) self.assertEqual(res, 0) res = DbMssqlRE.reverseEngineerTablePK(self.connection, source_table) self.assertEqual(res, 0) # Do migration: target_catalog = grt.classes.db_mysql_Catalog() res = DbMssqlMigration.migrateCatalog(source_catalog, target_catalog) self.assertEqual(res, 0) target_table = grt.classes.db_mysql_Table() res = DbMssqlMigration.migrateTableToMySQL(source_table, target_table) self.assertEqual(res, 0) res = DbMssqlMigration.migrateTableColumnsToMySQL( source_table, target_table) self.assertEqual(res, 0) res = DbMssqlMigration.migrateTablePrimaryKeyToMySQL( source_table, target_table) self.assertEqual(res, 0) pk_cols = [ column.name for column in target_table.columns if target_table.isPrimaryKeyColumn(column) ] self.assertEqual(set(['VendorID', 'ContactID']), set(pk_cols))
def test_rev_eng_foreign_keys(self): catalog = grt.classes.db_mssql_Catalog() catalog.name = 'AdventureWorks' res = DbMssqlRE.reverseEngineerUserDatatypes(self.connection, catalog) self.assertEqual(res, 0) for schema_name in ['Person', 'Purchasing']: schema = grt.classes.db_mssql_Schema() schema.name = schema_name schema.owner = catalog res = DbMssqlRE.reverseEngineerTables(self.connection, schema) self.assertEqual(res, 0) for table in schema.tables: res = DbMssqlRE.reverseEngineerTableColumns(self.connection, table) self.assertEqual(res, 0) catalog.schemata.append(schema) test_table = None for table in catalog.schemata[1].tables: if table.name == 'VendorContact': test_table = table break self.assertIsNotNone(test_table) res = DbMssqlRE.reverseEngineerTableFKs(self.connection, test_table) self.assertEqual(res, 0) self.assertEqual(test_table.name, 'VendorContact') self.assertEqual(len(test_table.foreignKeys), 3) found_foreign_keys = 0 for foreign_key in test_table.foreignKeys: if foreign_key.name == 'FK_VendorContact_Contact_ContactID': found_foreign_keys += 1 self.assertEqual(set(['ContactID']), set([column.name for column in foreign_key.columns])) self.assertEqual(foreign_key.referencedTable.name, 'Contact') self.assertEqual(foreign_key.deleteRule, 'NO_ACTION') self.assertEqual(foreign_key.updateRule, 'NO_ACTION') self.assertEqual(set(['ContactID']), set([column.name for column in foreign_key.referencedColumns])) elif foreign_key.name == 'FK_VendorContact_ContactType_ContactTypeID': found_foreign_keys += 1 self.assertEqual(set(['ContactTypeID']), set([column.name for column in foreign_key.columns])) self.assertEqual(foreign_key.referencedTable.name, 'ContactType') self.assertEqual(foreign_key.deleteRule, 'NO_ACTION') self.assertEqual(foreign_key.updateRule, 'NO_ACTION') self.assertEqual(set(['ContactTypeID']), set([column.name for column in foreign_key.referencedColumns])) elif foreign_key.name == 'FK_VendorContact_Vendor_VendorID': found_foreign_keys += 1 self.assertEqual(set(['VendorID']), set([column.name for column in foreign_key.columns])) self.assertEqual(foreign_key.referencedTable.name, 'Vendor') self.assertEqual(foreign_key.deleteRule, 'NO_ACTION') self.assertEqual(foreign_key.updateRule, 'NO_ACTION') self.assertEqual(set(['VendorID']), set([column.name for column in foreign_key.referencedColumns])) self.assertEqual(found_foreign_keys, 3)
def test_get_schema_names(self): schemata = DbMssqlRE.getSchemaNames(self.connection, 'AdventureWorks') self.assertIsInstance(schemata, grt.List) self.assertTrue( len(schemata) > 0 ) self.assertIsInstance(schemata[0], str) self.assertTrue('.' in schemata[0]) self.assertTrue('AdventureWorks.Person' in schemata)
def test_migrate_table_columns(self): # Prepare source objects: source_catalog, source_schema, source_table = self._set_catalog_schema_table('AdventureWorks', 'Person', 'Contact') res = DbMssqlRE.reverseEngineerUserDatatypes(self.state, self.connection, source_catalog) self.assertEqual(res, 0) res = DbMssqlRE.reverseEngineerTableColumns(self.state, self.connection, source_table) self.assertEqual(res, 0) # Do migration: target_catalog = DbMssqlMigration.migrateCatalog(self.state, source_catalog) self.assertTrue(target_catalog != None) target_table = grt.classes.db_mysql_Schema() res = DbMssqlMigration.migrateTableToMySQL(self.state, source_table, target_schema) self.assertEqual(res, 0) self.assertEqual(target_table.name, 'Contact') res = DbMssqlMigration.migrateTableColumnsToMySQL(source_table, target_table) self.assertEqual(res, 0) self.assertEqual(len(target_table.columns), len(source_table.columns))
def test_migrate_indices(self): # Prepare source objects: source_catalog, source_schema, source_table = self._set_catalog_schema_table( 'AdventureWorks', 'HumanResources', 'EmployeeAddress') res = DbMssqlRE.reverseEngineerUserDatatypes(self.connection, source_catalog) self.assertEqual(res, 0) res = DbMssqlRE.reverseEngineerTableColumns(self.connection, source_table) self.assertEqual(res, 0) res = DbMssqlRE.reverseEngineerTableIndices(self.connection, source_table) self.assertEqual(res, 0) # Do migration: target_catalog = grt.classes.db_mysql_Catalog() res = DbMssqlMigration.migrateCatalog(source_catalog, target_catalog) self.assertEqual(res, 0) target_table = grt.classes.db_mysql_Table() res = DbMssqlMigration.migrateTableToMySQL(source_table, target_table) self.assertEqual(res, 0) res = DbMssqlMigration.migrateTableColumnsToMySQL( source_table, target_table) self.assertEqual(res, 0) res = DbMssqlMigration.migrateTableIndicesToMySQL( source_table, target_table) self.assertEqual(res, 0) # Additional tests: detected_index = 0 for index in target_table.indices: if index.name == 'PK_EmployeeAddress_EmployeeID_AddressID': detected_index += 1 elif index.name == 'AK_EmployeeAddress_rowguid': detected_index += 1 self.assertEqual(index.unique, 1) self.assertEqual(index.isPrimary, 0) self.assertEqual(index.indexType, 'UNIQUE') self.assertEqual( set(icol.referencedColumn.name for icol in index.columns), set(['rowguid'])) self.assertEqual(detected_index, 1)
def test_rev_eng_pks(self): catalog, schema, table = self._set_catalog_schema_table('AdventureWorks', 'Purchasing', 'VendorContact') res = DbMssqlRE.reverseEngineerUserDatatypes(self.connection, catalog) self.assertEqual(res, 0) res = DbMssqlRE.reverseEngineerTableColumns(self.connection, table) self.assertEqual(res, 0) res = DbMssqlRE.reverseEngineerTablePK(self.connection, table) self.assertEqual(res, 0) pk_cols = [ column.name for column in table.columns if table.isPrimaryKeyColumn(column) ] self.assertEqual(set(['VendorID', 'ContactID']), set(pk_cols)) # Check if the columns have been added to the table primary key columns list: pk_cols = [column.name for column in table.primaryKey.columns] self.assertEqual(set(['VendorID', 'ContactID']), set(pk_cols)) # Check fail on table with empty columns: table1 = grt.classes.db_mssql_Table() table1.name = 'Vendor' table1.owner = schema res = DbMssqlRE.reverseEngineerTablePK(self.connection, table1) self.assertEqual(res, 1) pk_cols = [ column.name for column in table1.columns if table1.isPrimaryKeyColumn(column) ] self.assertEqual(pk_cols, [])
def test_migrate_table_pks(self): # Prepare source objects: source_catalog, source_schema, source_table = self._set_catalog_schema_table('AdventureWorks', 'Purchasing', 'VendorContact') res = DbMssqlRE.reverseEngineerUserDatatypes(self.connection, source_catalog) self.assertEqual(res, 0) res = DbMssqlRE.reverseEngineerTableColumns(self.connection, source_table, grt.Dict()) self.assertEqual(res, 0) res = DbMssqlRE.reverseEngineerTablePK(self.connection, source_table) self.assertEqual(res, 0) # Do migration: target_catalog = grt.classes.db_mysql_Catalog() res = DbMssqlMigration.migrateCatalog(source_catalog, target_catalog) self.assertEqual(res, 0) target_table = grt.classes.db_mysql_Table() res = DbMssqlMigration.migrateTableToMySQL(source_table, target_table) self.assertEqual(res, 0) res = DbMssqlMigration.migrateTableColumnsToMySQL(source_table, target_table) self.assertEqual(res, 0) res = DbMssqlMigration.migrateTablePrimaryKeyToMySQL(source_table, target_table) self.assertEqual(res, 0) pk_cols = [ column.name for column in target_table.columns if target_table.isPrimaryKeyColumn(column) ] self.assertEqual(set(['VendorID', 'ContactID']), set(pk_cols))
def test_rev_eng_user_datatypes(self): catalog = grt.classes.db_mssql_Catalog() catalog.name = 'AdventureWorks' res = DbMssqlRE.reverseEngineerUserDatatypes(self.connection, catalog) self.assertEqual(res, 0) self.assertEqual(len(catalog.userDatatypes), 6) datatype = None for user_type in catalog.userDatatypes: if user_type.name == 'PHONE': datatype = user_type break self.assertIsNotNone(datatype) self.assertEqual(datatype.actualType.name, 'NVARCHAR') self.assertEqual(datatype.characterMaximumLength, 50) self.assertEqual(datatype.numericPrecision, 0) self.assertEqual(datatype.numericScale, 0) self.assertEqual(datatype.isNullable, 1)
def setUpClass(cls): DbMssqlRE.connect(cls.connection, db_mssql_test_main.test_params['password']) self.state = db_migration_Migration()
def test_migrate_foreign_keys(self): # Prepare the scenario migrating needed objects along the way: source_catalog = grt.classes.db_mssql_Catalog() source_catalog.name = 'AdventureWorks' res = DbMssqlRE.reverseEngineerUserDatatypes(self.connection, source_catalog) self.assertEqual(res, 0) for schema_name in ['Person', 'Purchasing']: source_schema = grt.classes.db_mssql_Schema() source_schema.name = schema_name source_schema.owner = source_catalog res = DbMssqlRE.reverseEngineerTables(self.connection, source_schema) self.assertEqual(res, 0) for table in source_schema.tables: # Only rev eng the relevant tables if source_schema.name + '.' + table.name not in ['Person.Contact', 'Person.ContactType', 'Purchasing.Vendor', 'Purchasing.VendorContact']: continue res = DbMssqlRE.reverseEngineerTableColumns(self.connection, table) self.assertEqual(res, 0) source_catalog.schemata.append(source_schema) target_catalog = grt.classes.db_mysql_Catalog() res = DbMssqlMigration.migrateCatalog(source_catalog, target_catalog) self.assertEqual(res, 0) # Second pass through the tables, to be sure that all of the columns are there and to RevEng those # objects that require rev eng of table columns: source_target_schemata = zip(source_catalog.schemata, target_catalog.schemata) self.assertEqual(len(source_target_schemata), 2) test_target_table = None for source_schema, target_schema in source_target_schemata: res = DbMssqlMigration.migrateSchema(source_schema, target_schema) self.assertEqual(res, 0) self.assertTrue(len(source_schema.tables) > 0) self.assertEqual(len(source_schema.tables), len(target_schema.tables)) for source_table, target_table in zip(source_schema.tables, target_schema.tables): self.assertEqual(source_table.name, target_table.name) # Only migrate the relevant tables if source_schema.name + '.' + source_table.name not in ['Person.Contact', 'Person.ContactType', 'Purchasing.Vendor', 'Purchasing.VendorContact']: continue res = DbMssqlMigration.migrateTableToMySQL(source_table, target_table) self.assertEqual(res, 0) res = DbMssqlMigration.migrateTableColumnsToMySQL(source_table, target_table) self.assertEqual(res, 0) if source_table.name == 'VendorContact': res = DbMssqlRE.reverseEngineerTableFKs(self.connection, source_table) self.assertEqual(res, 0) self.assertEqual(len(source_table.foreignKeys), 3) res = DbMssqlMigration.migrateTableForeignKeysToMySQL(source_table, target_table) self.assertEqual(res, 0) test_target_table = target_table # The actual tests for the foreign keys migration: self.assertIsNotNone(test_target_table) self.assertEqual(len(test_target_table.foreignKeys), 3) found_foreign_keys = 0 for foreign_key in test_target_table.foreignKeys: if foreign_key.name == 'FK_VendorContact_Contact_ContactID': found_foreign_keys += 1 self.assertEqual(set(['ContactID']), set([column.name for column in foreign_key.columns])) self.assertEqual(foreign_key.referencedTable.name, 'Contact') self.assertEqual(foreign_key.deleteRule, 'NO_ACTION') self.assertEqual(foreign_key.updateRule, 'NO_ACTION') self.assertEqual(set(['ContactID']), set([column.name for column in foreign_key.referencedColumns])) elif foreign_key.name == 'FK_VendorContact_ContactType_ContactTypeID': found_foreign_keys += 1 self.assertEqual(set(['ContactTypeID']), set([column.name for column in foreign_key.columns])) self.assertEqual(foreign_key.referencedTable.name, 'ContactType') self.assertEqual(foreign_key.deleteRule, 'NO_ACTION') self.assertEqual(foreign_key.updateRule, 'NO_ACTION') self.assertEqual(set(['ContactTypeID']), set([column.name for column in foreign_key.referencedColumns])) elif foreign_key.name == 'FK_VendorContact_Vendor_VendorID': found_foreign_keys += 1 self.assertEqual(set(['VendorID']), set([column.name for column in foreign_key.columns])) self.assertEqual(foreign_key.referencedTable.name, 'Vendor') self.assertEqual(foreign_key.deleteRule, 'NO_ACTION') self.assertEqual(foreign_key.updateRule, 'NO_ACTION') self.assertEqual(set(['VendorID']), set([column.name for column in foreign_key.referencedColumns])) self.assertEqual(found_foreign_keys, 3)
def setUpClass(cls): DbMssqlRE.connect(cls.connection, db_mssql_test_main.test_params['password']) version = DbMssqlRE.getServerVersion(cls.connection) cls.context = grt.Dict()
def test_rdbms_version(self): version = DbMssqlRE.getServerVersion(self.connection) self.assertIsInstance(version, grt.classes.GrtVersion)
def test_migrate_foreign_keys(self): # Prepare the scenario migrating needed objects along the way: source_catalog = grt.classes.db_mssql_Catalog() source_catalog.name = 'AdventureWorks' res = DbMssqlRE.reverseEngineerUserDatatypes(self.connection, source_catalog) self.assertEqual(res, 0) for schema_name in ['Person', 'Purchasing']: source_schema = grt.classes.db_mssql_Schema() source_schema.name = schema_name source_schema.owner = source_catalog res = DbMssqlRE.reverseEngineerTables(self.connection, source_schema) self.assertEqual(res, 0) for table in source_schema.tables: # Only rev eng the relevant tables if source_schema.name + '.' + table.name not in [ 'Person.Contact', 'Person.ContactType', 'Purchasing.Vendor', 'Purchasing.VendorContact' ]: continue res = DbMssqlRE.reverseEngineerTableColumns( self.connection, table) self.assertEqual(res, 0) source_catalog.schemata.append(source_schema) target_catalog = grt.classes.db_mysql_Catalog() res = DbMssqlMigration.migrateCatalog(source_catalog, target_catalog) self.assertEqual(res, 0) # Second pass through the tables, to be sure that all of the columns are there and to RevEng those # objects that require rev eng of table columns: source_target_schemata = list( zip(source_catalog.schemata, target_catalog.schemata)) self.assertEqual(len(source_target_schemata), 2) test_target_table = None for source_schema, target_schema in source_target_schemata: res = DbMssqlMigration.migrateSchema(source_schema, target_schema) self.assertEqual(res, 0) self.assertTrue(len(source_schema.tables) > 0) self.assertEqual(len(source_schema.tables), len(target_schema.tables)) for source_table, target_table in zip(source_schema.tables, target_schema.tables): self.assertEqual(source_table.name, target_table.name) # Only migrate the relevant tables if source_schema.name + '.' + source_table.name not in [ 'Person.Contact', 'Person.ContactType', 'Purchasing.Vendor', 'Purchasing.VendorContact' ]: continue res = DbMssqlMigration.migrateTableToMySQL( source_table, target_table) self.assertEqual(res, 0) res = DbMssqlMigration.migrateTableColumnsToMySQL( source_table, target_table) self.assertEqual(res, 0) if source_table.name == 'VendorContact': res = DbMssqlRE.reverseEngineerTableFKs( self.connection, source_table) self.assertEqual(res, 0) self.assertEqual(len(source_table.foreignKeys), 3) res = DbMssqlMigration.migrateTableForeignKeysToMySQL( source_table, target_table) self.assertEqual(res, 0) test_target_table = target_table # The actual tests for the foreign keys migration: self.assertIsNotNone(test_target_table) self.assertEqual(len(test_target_table.foreignKeys), 3) found_foreign_keys = 0 for foreign_key in test_target_table.foreignKeys: if foreign_key.name == 'FK_VendorContact_Contact_ContactID': found_foreign_keys += 1 self.assertEqual( set(['ContactID']), set([column.name for column in foreign_key.columns])) self.assertEqual(foreign_key.referencedTable.name, 'Contact') self.assertEqual(foreign_key.deleteRule, 'NO_ACTION') self.assertEqual(foreign_key.updateRule, 'NO_ACTION') self.assertEqual( set(['ContactID']), set([ column.name for column in foreign_key.referencedColumns ])) elif foreign_key.name == 'FK_VendorContact_ContactType_ContactTypeID': found_foreign_keys += 1 self.assertEqual( set(['ContactTypeID']), set([column.name for column in foreign_key.columns])) self.assertEqual(foreign_key.referencedTable.name, 'ContactType') self.assertEqual(foreign_key.deleteRule, 'NO_ACTION') self.assertEqual(foreign_key.updateRule, 'NO_ACTION') self.assertEqual( set(['ContactTypeID']), set([ column.name for column in foreign_key.referencedColumns ])) elif foreign_key.name == 'FK_VendorContact_Vendor_VendorID': found_foreign_keys += 1 self.assertEqual( set(['VendorID']), set([column.name for column in foreign_key.columns])) self.assertEqual(foreign_key.referencedTable.name, 'Vendor') self.assertEqual(foreign_key.deleteRule, 'NO_ACTION') self.assertEqual(foreign_key.updateRule, 'NO_ACTION') self.assertEqual( set(['VendorID']), set([ column.name for column in foreign_key.referencedColumns ])) self.assertEqual(found_foreign_keys, 3)
def test_get_catalog_names(self): catalogs = DbMssqlRE.getCatalogNames(self.connection) self.assertIsInstance(catalogs, grt.List) self.assertTrue( len(catalogs) > 0 ) self.assertIsInstance(catalogs[0], str) self.assertTrue('AdventureWorks' in catalogs)