Ejemplo n.º 1
0
def test_delete_table(engine_with_schema, if_exists):
    engine, schema = engine_with_schema
    table_name = "test_delete_table"
    tables.create_mathesar_table(table_name, schema, [], engine)
    tables.delete_table(table_name, schema, engine, if_exists=if_exists)
    with pytest.raises(NoSuchTableError):
        tables.reflect_table(table_name, schema, engine)
Ejemplo n.º 2
0
def test_update_table_column_types_infers_non_default_types(engine_with_schema):
    col1 = Column("col1", String)
    col2 = Column("col2", String)
    column_list = [col1, col2]
    engine, schema = engine_with_schema
    table_name = "table_with_columns"
    tables.create_mathesar_table(
        table_name, schema, column_list, engine
    )
    with patch.object(tables.inference, "infer_column_type") as mock_infer:
        tables.update_table_column_types(
            schema,
            table_name,
            engine
        )
    expect_calls = [
        call(
            schema,
            table_name,
            col1.name,
            engine,
        ),
        call(
            schema,
            table_name,
            col2.name,
            engine,
        ),
    ]
    mock_infer.assert_has_calls(expect_calls)
Ejemplo n.º 3
0
def test_table_creation_doesnt_reuse_defaults(engine_with_schema):
    column_list = []
    engine, schema = engine_with_schema
    t1 = tables.create_mathesar_table("t1", schema, column_list, engine)
    t2 = tables.create_mathesar_table("t2", schema, column_list, engine)
    assert all(
        [
            c1.name == c2.name and c1 != c2
            for c1, c2 in zip(t1.columns, t2.columns)
        ]
    )
Ejemplo n.º 4
0
def test_delete_schema_restricted(engine):
    test_schema = "test_delete_schema_restricted"
    test_table = "test_delete_schema_restricted_table"

    schemas.create_schema(test_schema, engine)
    tables.create_mathesar_table(test_table, test_schema, [], engine)

    with pytest.raises(DependentObjectsStillExist):
        schemas.delete_schema(test_schema, engine)

    current_schemas = schemas.get_mathesar_schemas(engine)
    assert test_schema in current_schemas
Ejemplo n.º 5
0
def test_update_table_column_types_skips_pkey_columns(engine_with_schema):
    column_list = [Column("checkcol", String, primary_key=True)]
    engine, schema = engine_with_schema
    table_name = "t1"
    tables.create_mathesar_table(
        table_name, schema, column_list, engine
    )
    with patch.object(tables.inference, "infer_column_type") as mock_infer:
        tables.update_table_column_types(
            schema,
            table_name,
            engine
        )
    mock_infer.assert_not_called()
Ejemplo n.º 6
0
def test_infer_table_column_types_doesnt_touch_defaults(engine_with_schema):
    column_list = []
    engine, schema = engine_with_schema
    table_name = "t1"
    tables.create_mathesar_table(
        table_name, schema, column_list, engine
    )
    with patch.object(tables.inference, "infer_column_type") as mock_infer:
        tables.update_table_column_types(
            schema,
            table_name,
            engine
        )
    mock_infer.assert_not_called()
Ejemplo n.º 7
0
def test_rename_column_foreign_keys(engine_with_schema):
    engine, schema = engine_with_schema
    table_name = "table_to_split"
    columns_list = [Column("Filler 1", Integer), Column("Filler 2", Integer)]
    tables.create_mathesar_table(table_name, schema, columns_list, engine)
    extracted, remainder, fk_name = tables.extract_columns_from_table(
        table_name, ["Filler 1"], "Extracted", "Remainder", schema, engine)
    new_fk_name = "new_" + fk_name
    remainder = _rename_column(schema, remainder.name, fk_name, new_fk_name,
                               engine)

    fk = list(remainder.foreign_keys)[0]
    assert fk.parent.name == new_fk_name
    assert fk.column.table.name == extracted.name
Ejemplo n.º 8
0
def test_delete_table_restricted_foreign_key(engine_with_schema):
    engine, schema = engine_with_schema
    table_name = "test_delete_table_restricted_foreign_key"
    related_table_name = "test_delete_table_restricted_foreign_key_related"

    table = tables.create_mathesar_table(table_name, schema, [], engine)
    _create_related_table(related_table_name, table, schema, engine)

    with pytest.raises(DependentObjectsStillExist):
        tables.delete_table(table_name, schema, engine, cascade=False)
Ejemplo n.º 9
0
def test_multi_db_tables(engine, multi_db_engine, client):
    schema_name = "test_multi_db_tables_schema"
    test_tables = ["test_table_1", "test_table_2"]
    for table_name in test_tables:
        tables.create_mathesar_table(table_name, schema_name, [], engine)
        tables.create_mathesar_table("multi_db_" + table_name, schema_name, [],
                                     multi_db_engine)

    cache.clear()
    response = client.get('/api/v0/tables/')
    response_tables = [s['name'] for s in response.json()['results']]

    assert response.status_code == 200
    expected_tables = test_tables + ["multi_db_" + s for s in test_tables]
    for table_name in expected_tables:
        assert table_name in response_tables

    # We have to delete the schema to not break later tests
    with engine.begin() as conn:
        conn.execute(DropSchema(schema_name, cascade=True))
Ejemplo n.º 10
0
def test_delete_table_cascade_foreign_key(engine_with_schema):
    engine, schema = engine_with_schema
    table_name = "test_delete_table_cascade_foreign_key"
    related_table_name = "test_delete_table_cascade_foreign_key_related"

    table = tables.create_mathesar_table(table_name, schema, [], engine)
    related_table = _create_related_table(related_table_name, table, schema, engine)

    tables.delete_table(table_name, schema, engine, cascade=True)

    related_table = tables.reflect_table(related_table.name, schema, engine)
    assert len(related_table.foreign_keys) == 0
Ejemplo n.º 11
0
def test_delete_schema_cascade(engine):
    test_schema = "test_delete_schema_cascade"
    test_table = "test_delete_schema_cascade_table"

    schemas.create_schema(test_schema, engine)
    table = tables.create_mathesar_table(test_table, test_schema, [], engine)

    schemas.delete_schema(test_schema, engine, cascade=True)

    current_schemas = schemas.get_mathesar_schemas(engine)
    assert test_schema not in current_schemas
    with pytest.raises(NoSuchTableError):
        tables.reflect_table(table.name, test_schema, engine)
Ejemplo n.º 12
0
def test_rename_table_foreign_key(engine_with_schema):
    engine, schema = engine_with_schema
    table_name = "test_rename_table_foreign_key"
    new_table_name = "test_rename_table_foreign_key_new"
    related_table_name = "test_rename_table_foreign_key_related"

    table = tables.create_mathesar_table(table_name, schema, [], engine)
    related_table = _create_related_table(related_table_name, table, schema, engine)

    tables.rename_table(table_name, schema, engine, new_table_name)

    related_table = tables.reflect_table(related_table_name, schema, engine)
    fk = list(related_table.foreign_keys)[0]
    assert fk.column.table.name == new_table_name
Ejemplo n.º 13
0
def test_rename_table(engine_with_schema):
    engine, schema = engine_with_schema
    table_name = "test_rename_table"
    new_table_name = "test_rename_table_new"
    old_table = tables.create_mathesar_table(table_name, schema, [], engine)
    old_oid = tables.get_oid_from_table(old_table.name, old_table.schema, engine)

    tables.rename_table(table_name, schema, engine, new_table_name)
    new_table = tables.reflect_table(new_table_name, schema, engine)
    new_oid = tables.get_oid_from_table(new_table.name, new_table.schema, engine)

    assert old_oid == new_oid
    assert new_table.name == new_table_name

    with pytest.raises(NoSuchTableError):
        tables.reflect_table(table_name, schema, engine)
Ejemplo n.º 14
0
def _create_related_table(schema, related_schema, table, related_table,
                          engine):
    schemas.create_schema(schema, engine)
    table = tables.create_mathesar_table(table, schema, [], engine)

    schemas.create_schema(related_schema, engine)
    metadata = MetaData(schema=related_schema, bind=engine)
    related_table = Table(
        related_table, metadata,
        Column('id', Integer, ForeignKey(table.c[constants.ID])))
    related_table.create()

    related_table = tables.reflect_table(related_table.name, related_schema,
                                         engine)
    fk = list(related_table.foreign_keys)[0]
    assert fk.column.table.schema == schema

    return related_table
Ejemplo n.º 15
0
def test_rename_column_sequence(engine_with_schema):
    old_col_name = constants.ID
    new_col_name = "new_" + constants.ID
    engine, schema = engine_with_schema
    table_name = "table_with_columns"
    table = tables.create_mathesar_table(table_name, schema, [], engine)
    with engine.begin() as conn:
        ins = table.insert()
        conn.execute(ins)

    table = _rename_column(schema, table_name, old_col_name, new_col_name,
                           engine)

    with engine.begin() as conn:
        ins = table.insert()
        conn.execute(ins)
        slct = select(table)
        result = conn.execute(slct)
    new_value = result.fetchall()[-1][new_col_name]
    assert new_value == 2