예제 #1
0
def main():
    _, path = tempfile.mkstemp(suffix=".json")
    db = Database(path)

    table = db.table("testing")
    with db.transaction(table) as tr:
        tr.insert({"created_at": time.time(), "key": "test"})
        tr.update({"name": "random"}, db.where("key") == "test")

    print(table.all())

    # cleanup
    os.unlink(path)
예제 #2
0
def main():
    _, path = tempfile.mkstemp(suffix=".json")
    db = Database(path)

    model = TestingModel()
    model.name = "original"
    model.key = "test"
    model.created_at = time.time()

    table = db.table(model.__tablename__)
    with db.transaction(table) as tr:
        tr.insert(model.to_struct())
        model.name = "random"
        tr.update(model.to_struct(), db.where("key") == model.key)

    print(table.all())

    # create model from table's data
    row = table.get(db.where("key") == "test")
    new_model = TestingModel(**row)

    print(new_model.name)
    print(new_model.key)
    print(new_model.created_at)
    print(new_model.address)
    print(new_model.location)
    print(new_model.created_at_datetime())

    # cleanup
    os.unlink(path)
예제 #3
0
def test_database_init():
    from tinykit import Database
    from tinydb.storages import MemoryStorage

    db = Database(storage=MemoryStorage)
    assert db._conn.__class__.__name__ == "TinyDB"