示例#1
0
 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')
示例#2
0
 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')
示例#3
0
    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)
示例#4
0
    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 _process_jinja(self, spec: PosixPath, options: CombinedOptions) -> str:
        """Process dbml spec with jinja and return the resulting string"""
        data = PyDBML(spec)
        result = ''

        if options['doc']:
            if options.is_default('template') and not Path(
                    options['template']).exists():
                # copy default template to project dir if it's doesn't exist there
                copyfile(
                    resource_filename(__name__,
                                      'template/' + self.defaults['template']),
                    options['template'])
            try:
                template = self._env.get_template(str(options['template']))
                result += template.render(data=data)
            except Exception as e:
                self._warning(
                    f'\nFailed to render doc template {options["template"]}',
                    error=e)
                return result

        if options['scheme']:
            if options.is_default('scheme_template') and\
                    not Path(options['scheme_template']).exists():
                # copy default template to project dir if it's doesn't exist there
                copyfile(
                    resource_filename(
                        __name__,
                        'template/' + self.defaults['scheme_template']),
                    options['scheme_template'])
            try:
                template = self._env.get_template(
                    str(options['scheme_template']))
                result += template.render(data=data)
            except Exception as e:
                self._warning(
                    f'\nFailed to render scheme template {options["scheme_template"]}',
                    error=e)
                return result

        return result
示例#6
0
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
示例#7
0
 def test_bad_index(self):
     with self.assertRaises(ColumnNotFoundError):
         PyDBML(TEST_DATA_PATH / 'wrong_index.dbml')
示例#8
0
 def test_bad_reference(self):
     with self.assertRaises(TableNotFoundError):
         PyDBML(TEST_DATA_PATH / 'wrong_inline_ref_table.dbml')
     with self.assertRaises(ColumnNotFoundError):
         PyDBML(TEST_DATA_PATH / 'wrong_inline_ref_column.dbml')
示例#9
0
 def setUp(self):
     self.results = PyDBML.parse_file(TEST_DATA_PATH / 'general.dbml')