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()
Esempio n. 2
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()
Esempio n. 3
0
def taskmanager_sql_database():
    persistence = Persistence()
    persistence.create()

    yield

    persistence.delete()
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()
Esempio n. 5
0
def test_should_create_persistence_with_elastic_database():
    connection = ElasticConnection.create_local()
    database = ElasticDatabase(name="elastic_test", connection=connection)
    Persistence.clear()

    persistence = Persistence()
    persistence.add(database)

    assert database.info() == {"name": "elastic_test"}

    persistence.create()

    assert Persistence.is_available()

    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 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()
Esempio n. 9
0
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()
Esempio n. 10
0
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()
Esempio n. 11
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()
Esempio n. 12
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()
def persistence_setup():
    persistence = Persistence()
    database = create_sql_database()
    persistence.add(database)
    persistence.create()