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 test_should_sql_executor_with_mysql_multithreading(): connection = MySqlConnection.create_local() filename = ModelFilenameMother.get("sql/persistence.sql.models.yml") database = MySqlDatabase( name="mysql_test", connection=connection, model_filename=filename, print_sql_statements=True, ) persistence = Persistence() persistence.add(database) persistence.create() session_scope = Persistence.get_session_scope("mysql_test") def execute_sql_statement(name_thread): print(f"thread: {name_thread}") print(threading.current_thread()) try: sql_executor = SqlExecutor(session_scope) uuid = Uuid.generate().value sql_executor.execute_statement( f'INSERT INTO Client (client_id,name) VALUES ("{uuid}","myclient")' ) sql_executor.execute_statement("SELECT * FROM Client") sleep(1.0) sql_executor.execute_statement( f'DELETE FROM Client WHERE client_id="{uuid}";') return True except Exception as exp: print(exp) return False number_of_threads = 20 with concurrent.futures.ThreadPoolExecutor( max_workers=number_of_threads, thread_name_prefix="sql_threads") as executor: future_to_sql = { executor.submit(execute_sql_statement, num_thread): num_thread for num_thread in range(number_of_threads) } for future in concurrent.futures.as_completed(future_to_sql): num_thread = future_to_sql[future] assert future.result(), f"thread {num_thread} failed" persistence.clear_data() persistence.delete() Persistence.clear()
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
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_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 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"), )