def test_should_execute_lifecycle_of_persistence_with_fake_database(): filename = ModelFilenameMother.get("object/persistence.object.models.yml") database = FakeDatabase(name="fake", model_filename=filename) persistence = Persistence() persistence.add(database) assert ["fake"] == persistence.get_available_databases() assert ["user", "product"] == persistence.get_available_models_for_database("fake") assert ( "tests.modules.persistence.ymls.object.models.UserModel" == persistence.get_model("fake", "user") ) info = persistence.get_info() assert info == { "fake": { "name": "fake", "models": { "user": "******", "product": "tests.modules.persistence.ymls.object.models.ProductModel", }, } } assert ( str(persistence) == "Persistence: {'fake': {'name': 'fake', 'models': {'user': '******', 'product': 'tests.modules.persistence.ymls.object.models.ProductModel'}}}" ) persistence.remove("fake") assert [] == persistence.get_available_databases() with pytest.raises(IndexError): persistence.get_model("fake", "user") info = persistence.get_info() assert info == {} persistence.create() persistence.delete() Persistence.clear()
def test_should_add_a_product_with_sqlite_database_with_session_scope(): filename = ModelFilenameMother.get("sql/persistence.sql.models.yml") connection = SqliteConnection.create(server_name="sqlite", database_name="petisco.db") database = SqliteDatabase(name="sqlite_test", connection=connection, model_filename=filename) persistence = Persistence() persistence.add(database) persistence.delete() persistence.create() ProductModel = Persistence.get_model("sqlite_test", "product") session_scope = Persistence.get_session_scope("sqlite_test") with session_scope() as session: model = ProductModel(name="Petisco", price=2) session.add(model) persistence.clear_data() persistence.delete() Persistence.clear()
def test_should_add_an_user_with_sqlite_database_with_session(): filename = ModelFilenameMother.get("sql/persistence.sql.models.yml") connection = SqliteConnection.create(server_name="sqlite", database_name="petisco.db") database = SqliteDatabase(name="sqlite_test", connection=connection, model_filename=filename) persistence = Persistence() persistence.add(database) persistence.delete() persistence.create() UserModel = Persistence.get_model("sqlite_test", "user") session = Persistence.get_session("sqlite_test") model = UserModel(name="Petisco") session.add(model) session.commit() persistence.delete() Persistence.clear()
def build(): return SqlUserRepository( session_scope=Persistence.get_session_scope("petisco-sql"), user_model=Persistence.get_model("petisco-sql", "user"), )
def build(): return SqlEventRepository( session_scope=Persistence.get_session_scope("taskmanager"), event_model=Persistence.get_model("taskmanager", "event"), )
def __init__(self, database_name: str): self.session_scope = Persistence.get_session_scope(database_name) self.UserModel = Persistence.get_model(database_name, "user") self.ClientModel = Persistence.get_model(database_name, "client")
def build(): return SqlTasksCountCountRepository( session_scope=Persistence.get_session_scope("taskmanager"), tasks_count_model=Persistence.get_model("taskmanager", "tasks_count"), )