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 test_database_settings__accepts_engine_options_dict(): engine_options = {"echo": True} other_options = {"encoding": "utf8", "echo": False} expected_options = {**other_options, **engine_options} db = Database("sqlite://", engine_options=engine_options, **other_options) assert db.settings.get_engine_options() == expected_options
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_settings__accepts_session_options_dict(): session_options = {"autoflush": False} other_options = {"expire_on_commit": True, "autoflush": True} expected_options = {**other_options, **session_options} db = Database("sqlite://", session_options=session_options, **other_options) assert db.settings.get_session_options() == expected_options
def test_database_reflect(tmp_path: Path): db_file = tmp_path / "reflect.db" uri = f"sqlite:///{db_file}" model_collection = create_model_collection() model_class = model_collection["model_class"] model_tables = model_collection["tables"] Database(uri, model_class=model_class).create_all() db = Database(uri) assert len(db.tables) == 0 db.reflect() assert len(db.tables) == len(model_tables) model_tables_by_name = {table.name: table for table in model_tables} for table_name, _table in db.tables.items(): assert table_name in model_tables_by_name
def test_database_models(): model_collection = create_model_collection() model_class = model_collection["model_class"] models = model_collection["models"] db = Database("sqlite://", model_class=model_class) assert len(db.models) == len(models) for _model_name, model in db.models.items(): orig_model = models[models.index(model)] assert model is orig_model
def test_database_tables(): model_collection = create_model_collection() model_class = model_collection["model_class"] model_tables = model_collection["tables"] db = Database("sqlite://", model_class=model_class) assert len(db.tables) == len(model_tables) for table_name, table in db.tables.items(): assert table in model_tables model_table = model_tables[model_tables.index(table)] assert model_table.name == table_name
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
def test_database_settings(key: str, value: t.Any, kind: str): settings = {key: value} # NOTE: Using poolclass so that pool_* options can be used with sqlite. settings.setdefault("poolclass", QueuePool) db = Database("sqlite://", **settings) assert getattr(db.settings, key) == value assert db.settings[key] == value assert dict(db.settings)[key] == value if kind == "session": assert db.settings.get_session_options()[key] == value elif kind == "engine": assert db.settings.get_engine_options()[key] == value else: raise RuntimeError( f"kind must be one of 'session' or 'engine', not {kind!r}")
def test_database_settings__len(): db = Database("sqlite://") assert len(db.settings) == len(db.settings.__dict__)
def test_database_repr(uri: str, rep: str): db = Database(uri) assert repr(db) == rep
def test_database_name(uri: str, expected_name: t.Optional[str]): db = Database(uri) assert db.name == expected_name
def test_database_uri(uri: str, expected_uri: str): db = Database(uri) assert db.uri == expected_uri