def test_should_create_persistence_with_sqlite_database(): 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.create() assert database.info() == { "name": "sqlite_test", "models": { "client": "tests.modules.persistence.ymls.sql.models.ClientModel", "product": "tests.modules.persistence.ymls.sql.models.ProductModel", "user": "******", }, } assert Persistence.is_available() persistence.delete() Persistence.clear()
def test_should_sql_executor_insert_statement(): 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.create() session_scope = Persistence.get_session_scope("sqlite_test") sql_executor = SqlExecutor(session_scope) sql_executor.execute_statement( 'INSERT INTO Client (client_id,name) VALUES ("65dd83ef-d315-417d-bfa8-1ab398e16f02","myclient")' ) sql_executor.execute_statement( 'DELETE FROM Client WHERE client_id="65dd83ef-d315-417d-bfa8-1ab398e16f02";' ) persistence.clear_data() persistence.delete() Persistence.clear()
def create_sql_database(): # sql_database = MySqlDatabase(name="taskmanager", connection=MySqlConnection.create_local(), model_filename=f"{ROOT_PATH}/petisco.sql.models.yml") sql_database = SqliteDatabase( name="taskmanager", connection=SqliteConnection.create("sqlite", "tasmanager.db"), model_filename=f"{ROOT_PATH}/petisco.sql.models.yml", ) return sql_database
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 test_should_sql_executor_with_sqlite_multithreading(): 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.create() def execute_sql_statement(name_thread): try: session_scope = Persistence.get_session_scope("sqlite_test") sql_executor = SqlExecutor(session_scope) sql_executor.execute_statement( 'INSERT INTO Client (client_id,name) VALUES ("65dd83ef-d315-417d-bfa8-1ab398e16f02","myclient")' ) sql_executor.execute_statement( 'DELETE FROM Client WHERE client_id=="65dd83ef-d315-417d-bfa8-1ab398e16f02";' ) return True except Exception as exp: print(exp) return False number_of_threads = 20 with concurrent.futures.ThreadPoolExecutor( max_workers=number_of_threads) as executor: for num_thread in range(number_of_threads): executor.submit(execute_sql_statement, num_thread) persistence.clear_data() persistence.delete() Persistence.clear()
def test_should_save_a_model_using_a_sql_repository_implementation(): 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.create() repository = SqlRepositoryMother.user("sqlite_test") user = User.random() repository.save(user) retrieved_user = repository.retrieve(user.user_id).unwrap() assert user == retrieved_user persistence.delete() Persistence.clear()
def test_should_sql_executor_from_filename_with_statement(): 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.create() session_scope = Persistence.get_session_scope("sqlite_test") sql_executor = SqlExecutor(session_scope) sql_executor.execute_from_filename( "tests/modules/persistence/sql/client_create.sql") sql_executor.execute_from_filename( "tests/modules/persistence/sql/client_delete.sql") persistence.clear_data() persistence.delete() Persistence.clear()
def given_petisco_flask_app(petisco_yml_path_flask_app): Petisco.clear() petisco = Petisco.from_filename( f"{petisco_yml_path_flask_app}/petisco.yml") petisco.configure_events(petisco_yml_path_flask_app + "/petisco.events.yml") load_app_services() sql_database = SqliteDatabase( name="petisco-sql", connection=SqliteConnection.create("sqlite", "petisco-test.db"), model_filename=f"{petisco_yml_path_flask_app}/petisco.sql.models.yml", ) persistence = Persistence() persistence.add(sql_database, skip_if_exist=True) persistence.create() load_repositories() yield petisco persistence.clear_data() Petisco.clear() petisco.stop()