Example #1
0
def test_table_creation_additional_fields(empty_db, simple_config):
    db = empty_db

    for tablename, fieldnames in get_fieldnames_from_config(
            simple_config).items():
        db[tablename].create({fieldname: str for fieldname in fieldnames})

    additional_fields = {"album": ["description", "notes"]}
    for tablename, fieldnames in additional_fields.items():
        if tablename in db.table_names():
            for fieldname in fieldnames:
                if fieldname not in db[tablename].columns:
                    db[tablename].add_column(fieldname, str)

    expected_schema = """
    CREATE TABLE [album] (
        [name] TEXT,
        [artist] TEXT,
        [released] TEXT,
        [label] TEXT,
        [genre] TEXT,
        [description] TEXT,
        [notes] TEXT
    )
    """

    assert "album" in db.table_names()
    assert normalize_schema(
        db["album"].schema) == normalize_schema(expected_schema)
Example #2
0
def test_simple_data_insertion(empty_db, simple_config):
    db = empty_db

    for tablename, fieldnames in get_fieldnames_from_config(
            simple_config).items():
        db[tablename].create({fieldname: str for fieldname in fieldnames})

    for tablename, rows in PARSED_DATA.items():
        db[tablename].insert_all(rows)

    selected = db.execute("SELECT * FROM album;").fetchall()

    assert selected == [
        ("Five Leaves Left", "Nick Drake", "1969", "Island", "Folk"),
        ("Bryter Layter", "Nick Drake", "1971", "Island", "Folk"),
        ("Pink Moon", "Nick Drake", "1972", "Island", "Folk"),
    ]
Example #3
0
def test_simple_table_creation(empty_db, simple_config):
    db = empty_db

    for tablename, fieldnames in get_fieldnames_from_config(
            simple_config).items():
        db[tablename].create({fieldname: str for fieldname in fieldnames})

    expected_schema = """
    CREATE TABLE [album] (
        [name] TEXT,
        [artist] TEXT,
        [released] TEXT,
        [label] TEXT,
        [genre] TEXT
    )
    """

    assert "album" in db.table_names()
    assert normalize_schema(
        db["album"].schema) == normalize_schema(expected_schema)
Example #4
0
def test_writing_to_filesystem(simple_config):
    dirpath = tempfile.mkdtemp()
    db_path = Path(dirpath) / "test_db.sqlite"

    db = SqliteDB(db_path)

    for tablename, fieldnames in get_fieldnames_from_config(
            simple_config).items():
        db[tablename].create({fieldname: str for fieldname in fieldnames})

    for tablename, rows in PARSED_DATA.items():
        db[tablename].insert_all(rows)

    selected = db.execute("SELECT * FROM album;").fetchall()

    assert selected == [
        ("Five Leaves Left", "Nick Drake", "1969", "Island", "Folk"),
        ("Bryter Layter", "Nick Drake", "1971", "Island", "Folk"),
        ("Pink Moon", "Nick Drake", "1972", "Island", "Folk"),
    ]
Example #5
0
def test_table_creation_with_fields_reversed(empty_db, simple_config):
    db = empty_db

    for tablename, fieldnames in get_fieldnames_from_config(
            simple_config).items():
        params = {"column_order": list(reversed(fieldnames))}
        db[tablename].create({fieldname: str
                              for fieldname in fieldnames}, **params)

    expected_schema = """
    CREATE TABLE [album] (
        [genre] TEXT,
        [label] TEXT,
        [released] TEXT,
        [artist] TEXT,
        [name] TEXT
    )
    """

    assert "album" in db.table_names()
    assert normalize_schema(
        db["album"].schema) == normalize_schema(expected_schema)