def test_table_refs(self): p = PyDBML.parse_file(TEST_DATA_PATH / 'general.dbml') r = p['order_items'].refs self.assertEqual(r[0].col.name, 'order_id') self.assertEqual(r[0].ref_table.name, 'orders') self.assertEqual(r[0].ref_col.name, 'id') r = p['products'].refs self.assertEqual(r[0].col.name, 'merchant_id') self.assertEqual(r[0].ref_table.name, 'merchants') self.assertEqual(r[0].ref_col.name, 'id') r = p['users'].refs self.assertEqual(r[0].col.name, 'country_code') self.assertEqual(r[0].ref_table.name, 'countries') self.assertEqual(r[0].ref_col.name, 'code')
def test_refs(self): p = PyDBML.parse_file(TEST_DATA_PATH / 'general.dbml') r = p.refs self.assertEqual(r[0].table1.name, 'orders') self.assertEqual(r[0].col1.name, 'id') self.assertEqual(r[0].table2.name, 'order_items') self.assertEqual(r[0].col2.name, 'order_id') self.assertEqual(r[2].table1.name, 'users') self.assertEqual(r[2].col1.name, 'country_code') self.assertEqual(r[2].table2.name, 'countries') self.assertEqual(r[2].col2.name, 'code') self.assertEqual(r[4].table1.name, 'products') self.assertEqual(r[4].col1.name, 'merchant_id') self.assertEqual(r[4].table2.name, 'merchants') self.assertEqual(r[4].col2.name, 'id')
def test_reference_aliases(self): results = PyDBML.parse_file(TEST_DATA_PATH / 'relationships_aliases.dbml') posts, reviews, users = results['posts'], results['reviews'], results[ 'users'] posts2, reviews2, users2 = results['posts2'], results[ 'reviews2'], results['users2'] rs = results.refs self.assertEqual(rs[0].table1, users) self.assertEqual(rs[0].table2, posts) self.assertEqual(rs[1].table1, users) self.assertEqual(rs[1].table2, reviews) self.assertEqual(rs[2].table1, posts2) self.assertEqual(rs[2].table2, users2) self.assertEqual(rs[3].table1, reviews2) self.assertEqual(rs[3].table2, users2)
def test_composite_references(self): results = PyDBML.parse_file(TEST_DATA_PATH / 'relationships_composite.dbml') self.assertEqual(len(results.tables), 4) posts, reviews = results['posts'], results['reviews'] posts2, reviews2 = results['posts2'], results['reviews2'] rs = results.refs self.assertEqual(len(rs), 2) self.assertEqual(rs[0].table1, posts) self.assertEqual(rs[0].col1, [posts['id'], posts['tag']]) self.assertEqual(rs[0].table2, reviews) self.assertEqual(rs[0].col2, [reviews['post_id'], reviews['tag']]) self.assertEqual(rs[1].table1, posts2) self.assertEqual(rs[1].col1, [posts2['id'], posts2['tag']]) self.assertEqual(rs[1].table2, reviews2) self.assertEqual(rs[1].col2, [reviews2['post_id'], reviews2['tag']])
def processFile(target, emulationMode, tableExists=True, indexExists=True, idxNameFunc=uuid.uuid4, join=True): """ Given a target `.dbml` file, parse and generate a valid SQLite string. Parameters: target (Path): File with contents to convert to SQLite. emulationMode (str): Specifies "half" or "full" emulation for enum functionality in SQLite. tableExists (bool): Default is True. If True, all generated `CREATE TABLE` SQLite statements will have `IF NOT EXISTS` language included. indexExists (bool): Default is True. If True, all generated `CREATE INDEX` SQLite statements will have `IF NOT EXISTS` language included. join (bool): Default is True. If True, function will `join` the result list of string segments with an empty string and return the resulting string to you. Otherwise, the one-dimensional list of string segments will be returned to you directly. Returns: str: A valid SQLite string. """ parsed = PyDBML.parse_file(str(target)) statements = [] if emulationMode == 'full': for enum in parsed.enums: statements.append(processEnum(enum, tableExists, False)) for table in parsed.tables: statements.append( processTable(table, emulationMode, tableExists, False)) for table in parsed.tables: for index in table.indexes: statements.append( processIndex(table, index, idxNameFunc, indexExists=indexExists, join=False)) statements = list(chain.from_iterable(statements)) if join: statements = "".join(statements) return statements
def setUp(self): self.results = PyDBML.parse_file(TEST_DATA_PATH / 'general.dbml')