def filedb(tmp_path: Path) -> t.Generator[Database, None, None]: dbpath = tmp_path / "test.db" uri = f"sqlite:///{dbpath}" _db = Database(uri, model_class=Model) _db.create_all() yield _db _db.close()
def db() -> t.Generator[Database, None, None]: uri = "sqlite://" echo = False _db = Database(uri, model_class=Model, echo=echo) _db.create_all() yield _db _db.close()
def test_database_drop_all(): model_collection = create_model_collection() model_class = model_collection["model_class"] model_tables = model_collection["tables"] db = Database("sqlite://", model_class=model_class) conn = db.connect() db.create_all() count_tables = sa.text("SELECT COUNT(name) FROM sqlite_master") assert conn.execute(count_tables).scalar_one() == len(model_tables) db.drop_all() assert conn.execute(count_tables).scalar_one() == 0
def test_database_create_all(): model_collection = create_model_collection() model_class = model_collection["model_class"] model_tables = model_collection["tables"] db = Database("sqlite://", model_class=model_class) db.create_all() with db.connect() as conn: result = conn.execute(sa.text("SELECT name FROM sqlite_master")) table_names = result.scalars().all() assert len(table_names) == len(model_tables) for table_name in table_names: assert table_name in db.tables