示例#1
0
def test_update_row(tmpdir):
    dbm = DbManager(tmpdir)
    dbm.create_db('test_db')
    dbm.use_db('test_db')

    test_schema = {'name': 'str', 'age': 'int', 'employed': 'bool'}
    dbm.create_table("test_table", test_schema)

    # insert 3 dummy rows
    test_row1 = {'name': 'EchipaRacheta', 'age': '24', 'employed': "False"}
    dbm.insert_row("test_table", test_row1)
    test_row2 = {'name': 'EchipaRacheta2', 'age': '25', 'employed': "False"}
    dbm.insert_row("test_table", test_row2)
    test_row3 = {'name': 'EchipaRacheta2', 'age': '25', 'employed': "False"}
    dbm.insert_row("test_table", test_row3)

    # update row id 1
    new_row = {'name': 'EchipaRachetaTest', 'age': '29', 'employed': 'True'}
    dbm.update_row("test_table", 1, new_row)

    rows = dbm.scan_rows('test_table')
    for row in rows:
        if row['_rowid'] == 1:
            new_row['_rowid'] = 1
            assert new_row == row
            break

    dbm.delete_db('test_db')
示例#2
0
def test_delete_db(tmpdir):
    dbm = DbManager(tmpdir)
    path = os.path.join(tmpdir, 'test_db')

    dbm.create_db('test_db')
    dbm.delete_db('test_db')
    assert not os.path.exists(path)
示例#3
0
def test_get_current_db(tmpdir):
    dbm = DbManager(tmpdir)
    dbm.create_db('test_db')
    dbm.use_db('test_db')

    assert dbm.get_current_db() == 'test_db'

    dbm.delete_db('test_db')
示例#4
0
def test_db_path_set(tmpdir):
    dbm = DbManager(tmpdir)
    dbm.create_db('test_db')
    dbm.use_db('test_db')
    path = os.path.join(tmpdir, "test_db")

    assert dbm.db_path == path

    dbm.delete_db('test_db')
示例#5
0
def test_use_db_existing(tmpdir):
    dbm = DbManager(tmpdir)
    dbm.create_db('test_db')
    path = os.path.join(tmpdir, "test_db")

    dbm.use_db('test_db')
    assert dbm._db_name == 'test_db'
    assert dbm._db_path == path

    dbm.delete_db('test_db')
示例#6
0
def test_get_schema(tmpdir):
    dbm = DbManager(tmpdir)
    dbm.create_db('test_db')
    dbm.use_db('test_db')

    test_schema = {'name': 'str', 'age': 'int', 'employed': 'bool'}
    dbm.create_table("test_db", test_schema)

    created_schema = dbm.get_table_schema('test_db')
    assert created_schema == test_schema
    dbm.delete_db('test_db')
示例#7
0
def test_insert_row(tmpdir):
    dbm = DbManager(tmpdir)
    dbm.create_db('test_db')
    dbm.use_db('test_db')

    test_schema = {'name': 'str', 'age': 'int', 'employed': 'bool'}
    dbm.create_table("test_table", test_schema)

    test_row1 = {'name': 'EchipaRacheta', 'age': '24', 'employed': "False"}
    dbm.insert_row("test_table", test_row1)
    test_row1["_rowid"] = 0

    test_row2 = {'name': 'EchipaRacheta2', 'age': '25', 'employed': "False"}
    dbm.insert_row("test_table", test_row2)
    test_row2["_rowid"] = 1

    test_row3 = {'name': 'EchipaRacheta3', 'age': '26', 'employed': "False"}
    dbm.insert_row("test_table", test_row3)
    test_row3["_rowid"] = 2

    test_row4 = {'name': 'EchipaRacheta4', 'age': '27', 'employed': "False"}
    dbm.insert_row("test_table", test_row4)
    test_row4["_rowid"] = 3

    test_row5 = {'name': 'EchipaRacheta5', 'age': '28', 'employed': "False"}
    dbm.insert_row("test_table", test_row5)
    test_row5["_rowid"] = 4

    inserted_testRows = [test_row1, test_row2, test_row3, test_row4, test_row5]

    rows = dbm.scan_rows("test_table")
    isFound = True

    #check if all rows inserted in the table are exactly those created above
    for row in rows:
        if row not in inserted_testRows:
            isFound = False
            break

    assert isFound == True
    dbm.delete_db('test_db')
示例#8
0
def test_get_tables(tmpdir):
    dbm = DbManager(tmpdir)
    dbm.create_db('test_db')
    dbm.use_db('test_db')

    test_schema = {'name': 'str', 'age': 'int', 'employed': 'bool'}
    dbm.create_table("test_table", test_schema)

    test_schema2 = {'name': 'str', 'description': 'str', 'isMale': 'bool'}
    dbm.create_table("test_table2", test_schema2)

    test_schema3 = {'name': 'str', 'age': 'int'}
    dbm.create_table("test_table3", test_schema3)

    existing_schemas = [test_schema, test_schema2, test_schema3]

    tables_list = dbm.get_tables("test_db")
    for table in tables_list:
        current_schema = dbm.get_table_schema(table)
        assert current_schema in existing_schemas
    dbm.delete_db('test_db')
示例#9
0
def test_to_csv(tmpdir):
    dbm = DbManager(tmpdir)
    dbm.create_db('test_db')
    dbm.use_db('test_db')

    csv_path = os.path.join(tmpdir, "db.csv")
    test_schema = {'name': 'str', 'age': 'int', 'employed': 'bool'}
    dbm.create_table("test_table1", test_schema)

    test_row1 = {'name': 'EchipaRacheta', 'age': '24', 'employed': "False"}
    dbm.insert_row("test_table1", test_row1)
    test_row2 = {'name': 'EchipaRacheta2', 'age': '25', 'employed': "False"}
    dbm.insert_row("test_table1", test_row2)

    test_schema = {'description': 'str', 'isMember': 'bool'}
    dbm.create_table("test_table2", test_schema)

    test_row3 = {'description': 'descrierea dummy ', 'isMember': 'True'}
    dbm.insert_row("test_table2", test_row3)
    test_row4 = {'description': 'descrierea dummy 2', 'isMember': "False"}
    dbm.insert_row("test_table2", test_row4)

    dbm.to_csv(csv_path)

    tables_created = {}
    table_schemas = {}
    inserted_testRows = [test_row1, test_row2, test_row3, test_row4]

    assert os.path.exists(csv_path)

    last_table = ""
    fw = open(csv_path, "r")
    isSchema = True
    records = {}
    #check schema
    for line in fw:
        if isSchema:
            line = line.replace('\n', '')
            current_schema = {}
            schema_str = line.split(',')
            table_name = schema_str[0]
            isSchema = False
            for i in range(1, len(schema_str)):
                column_name, column_type = schema_str[i].split(':')
                current_schema[column_name] = column_type
            test_schema = dbm.get_table_schema(table_name)
            assert current_schema == test_schema
        elif line == "\n":
            isSchema = True
            currentRow = {}
        else:
            #print(line)
            line.replace('\n', '')
            print(line)
            line = line.split(',')
            if line != ['']:
                db_rowid = line[0] + '-' + line[1]
                if db_rowid in records.keys():
                    records[db_rowid] += ';' + (line[2] + '-' +
                                                line[3].replace('\n', ''))
                else:
                    records[db_rowid] = line[2] + '-' + line[3].replace(
                        '\n', '')

    # check if the records are exactly the same
    for key in records.keys():
        row = {}
        content = records[key].split(';')
        for column in content:
            row[column.split('-')[0]] = column.split('-')[1]
        print(row)
        assert row in inserted_testRows

    dbm.delete_db('test_db')