示例#1
0
def test_build_demo_metadata_without_tables():
    metadata = Metadata()

    metadata.add_table('users')
    metadata.add_field('users', 'user_id', 'id', 'integer')
    metadata.add_field('users', 'country', 'categorical')
    metadata.add_field('users', 'gender', 'categorical')
    metadata.add_field('users', 'age', 'numerical', 'integer')
    metadata.set_primary_key('users', 'user_id')

    metadata.add_table('sessions')
    metadata.add_field('sessions', 'session_id', 'id', 'integer')
    metadata.add_field('sessions', 'user_id', 'id', 'integer')
    metadata.add_field('sessions', 'device', 'categorical')
    metadata.add_field('sessions', 'os', 'categorical')
    metadata.add_field('sessions', 'minutes', 'numerical', 'integer')
    metadata.set_primary_key('sessions', 'session_id')
    metadata.add_relationship('users', 'sessions')

    metadata.add_table('transactions')
    metadata.add_field('transactions', 'transaction_id', 'id', 'integer')
    metadata.add_field('transactions', 'session_id', 'id', 'integer')
    metadata.add_field('transactions',
                       'timestamp',
                       'datetime',
                       properties={'format': '%Y-%m-%dT%H:%M'})
    metadata.add_field('transactions', 'amount', 'numerical', 'float')
    metadata.add_field('transactions', 'cancelled', 'boolean')
    metadata.set_primary_key('transactions', 'transaction_id')
    metadata.add_relationship('sessions', 'transactions')

    assert DEMO_METADATA == metadata.to_dict()
示例#2
0
def load_multi_foreign_key():
    parent = pd.DataFrame({
        'parent_id': range(10),
        'value': range(10)
    })
    child = pd.DataFrame({
        'parent_1_id': range(10),
        'parent_2_id': range(10),
        'value': range(10)
    })

    metadata = Metadata()
    metadata.add_table('parent', parent, primary_key='parent_id')
    metadata.add_table('child', child, parent='parent', foreign_key='parent_1_id')
    metadata.add_relationship('parent', 'child', 'parent_2_id')

    return metadata, {'parent': parent, 'child': child}
metadata = Metadata()
metadata.add_table(name='Products',
                   data=tables['Products'],
                   primary_key='Product id')
metadata.add_table(name='Sales orders',
                   data=tables['Sales orders'],
                   primary_key='Sales order id',
                   foreign_key='Product id',
                   parent='Products')
metadata.add_table(name='Purchase orders',
                   data=tables['Purchase orders'],
                   primary_key='Purchase order id',
                   foreign_key='Product id',
                   parent='Products')
metadata.add_table(name='Customers',
                   data=tables['Customers'],
                   primary_key='Customer id')
metadata.add_relationship(parent='Customers',
                          child='Sales orders',
                          foreign_key='Customer id')
metadata.add_table(name='Suppliers',
                   data=tables['Suppliers'],
                   primary_key='Supplier id')
metadata.add_relationship(parent='Suppliers',
                          child='Purchase orders',
                          foreign_key='Supplier id')

metadata.visualize('Schema2.png')

# %%