Esempio n. 1
0
def all_to_intermediary(filename_or_input, schema=None):
    """ Dispatch the filename_or_input to the different function to produce the intermediary syntax.
    All the supported classes names are in `swich_input_class_to_method`.
    The input can also be a list of strings in markdown format or a filename finishing by '.er' containing markdown
    format.
    """
    # Try to convert from the name of the class
    input_class_name = filename_or_input.__class__.__name__
    try:
        this_to_intermediary = switch_input_class_to_method[input_class_name]
        tables, relationships = this_to_intermediary(filename_or_input)
        return tables, relationships
    except KeyError:
        pass

    # try to read markdown file.
    if isinstance(filename_or_input, basestring):
        if filename_or_input.split('.')[-1] == 'er':
            return markdown_file_to_intermediary(filename_or_input)

    # try to read a markdown in a string
    if not isinstance(filename_or_input, basestring):
        if all(isinstance(e, basestring) for e in filename_or_input):
            return line_iterator_to_intermediary(filename_or_input)

    # try to read DB URI.
    try:
        make_url(filename_or_input)
        return database_to_intermediary(filename_or_input, schema=schema)
    except ArgumentError:
        pass

    msg = 'Cannot process filename_or_input {}'.format(input_class_name)
    raise ValueError(msg)
Esempio n. 2
0
def all_to_intermediary(filename_or_input, schema=None):
    """ Dispatch the filename_or_input to the different function to produce the intermediary syntax.
    All the supported classes names are in `swich_input_class_to_method`.
    The input can also be a list of strings in markdown format or a filename finishing by '.er' containing markdown
    format.
    """
    # Try to convert from the name of the class
    input_class_name = filename_or_input.__class__.__name__
    try:
        this_to_intermediary = switch_input_class_to_method[input_class_name]
        tables, relationships = this_to_intermediary(filename_or_input)
        return tables, relationships
    except KeyError:
        pass

    # try to read markdown file.
    if isinstance(filename_or_input, basestring):
        if filename_or_input.split('.')[-1] == 'er':
            return markdown_file_to_intermediary(filename_or_input)

    # try to read a markdown in a string
    if not isinstance(filename_or_input, basestring):
        if all(isinstance(e, basestring) for e in filename_or_input):
            return line_iterator_to_intermediary(filename_or_input)

    # try to read DB URI.
    try:
        make_url(filename_or_input)
        return database_to_intermediary(filename_or_input, schema=schema)
    except ArgumentError:
        pass

    msg = 'Cannot process filename_or_input {}'.format(input_class_name)
    raise ValueError(msg)
def test_database_to_intermediary_with_schema():
    db_uri = create_db()
    tables, relationships = database_to_intermediary(db_uri, schema='test')

    assert len(tables) == 3
    assert len(relationships) == 2
    assert all(isinstance(t, Table) for t in tables)
    assert all(isinstance(r, Relation) for r in relationships)
    # Not in because different schema.
    assert relation not in relationships
    assert exclude_relation not in relationships
def test_database_to_intermediary_with_schema():
    db_uri = create_db()
    tables, relationships = database_to_intermediary(db_uri, schema='test')

    assert len(tables) == 3
    assert len(relationships) == 2
    assert all(isinstance(t, Table) for t in tables)
    assert all(isinstance(r, Relation) for r in relationships)
    # Not in because different schema.
    assert relation not in relationships
    assert exclude_relation not in relationships
def test_table_names_in_relationships():
    db_uri = create_db()
    tables, relationships = database_to_intermediary(db_uri)
    table_names = [t.name for t in tables]

    # Assert column names are table names
    assert all(r.right_col in table_names for r in relationships)
    assert all(r.left_col in table_names for r in relationships)

    # Assert column names match table names
    for r in relationships:
        r_name = table_names[table_names.index(r.right_col)]
        l_name = table_names[table_names.index(r.left_col)]

        # Table name in relationship should *NOT* have a schema
        assert (r_name.find('.') == -1)
        assert (l_name.find('.') == -1)
def test_table_names_in_relationships_with_schema():
    db_uri = create_db()
    schema_name = 'test'
    matcher = re.compile(r"{}\.[\S+]".format(schema_name), re.I)
    tables, relationships = database_to_intermediary(db_uri,
                                                     schema=schema_name)
    table_names = [t.name for t in tables]

    # Assert column names match table names, including schema
    assert all(r.right_col in table_names for r in relationships)
    assert all(r.left_col in table_names for r in relationships)

    # Assert column names match table names, including schema
    for r in relationships:
        r_name = table_names[table_names.index(r.right_col)]
        l_name = table_names[table_names.index(r.left_col)]

        # Table name in relationship *SHOULD* have a schema
        assert (re.match(matcher, r_name) is not None)
        assert (re.match(matcher, l_name) is not None)
Esempio n. 7
0
def render_er_inline(db_conn_str):
    tables, relationships = database_to_intermediary(db_conn_str)
    t = '\n'.join(t.to_dot() for t in tables)
    r = '\n'.join(r.to_dot() for r in relationships)
    the_dot = '{}\n{}\n{}\n}}'.format(GRAPH_BEGINNING, t, r)
    return Image(AGraph(the_dot).draw(format='png', prog='dot'))
def test_database_to_intermediary():
    db_uri = create_db()
    tables, relationships = database_to_intermediary(db_uri)
    check_intermediary_representation_simple_table(tables, relationships)
def test_database_to_intermediary():
    db_uri = create_db()
    tables, relationships = database_to_intermediary(db_uri)
    check_intermediary_representation_simple_table(tables, relationships)