예제 #1
0
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 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_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()
예제 #5
0
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()
예제 #6
0
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()