def _(_redis_container):
     db = redis.Redis(host=get_docker_host(), port=redis_config["port"])
     db.flushall()
     assign_fixture_credentials(
         db,
         drivername="redis",
         host=get_docker_host(),
         port=redis_config["port"],
         database=None,
         username=None,
         password=None,
     )
     return db
def get_sqlalchemy_engine(database_name, isolation_level=None):
    URI_TEMPLATE = (
        "postgresql+psycopg2://{username}:{password}@{host}:{port}/{database}?sslmode=disable"
    )
    DB_URI = URI_TEMPLATE.format(
        database=database_name,
        username=config["username"],
        password=config["password"],
        host=get_docker_host(),
        port=config["port"],
    )

    options = {}
    if isolation_level:
        options["isolation_level"] = isolation_level

    # Trigger any psycopg2-based import failures
    from pytest_mock_resources.compat import psycopg2

    psycopg2.connect

    engine = sqlalchemy.create_engine(DB_URI, **options)

    # Verify engine is connected
    engine.connect()

    return engine
Exemple #3
0
def get_sqlalchemy_engine(database_name, isolation_level=None):
    URI_TEMPLATE = (
        "postgresql+psycopg2://{username}:{password}@{host}:{port}/{database}?sslmode=disable"
    )
    DB_URI = URI_TEMPLATE.format(
        database=database_name,
        username=config["username"],
        password=config["password"],
        host=get_docker_host(),
        port=config["port"],
    )

    options = {}
    if isolation_level:
        options["isolation_level"] = isolation_level

    try:
        engine = sqlalchemy.create_engine(DB_URI, **options)
    except ModuleNotFoundError:
        from pytest_mock_resources.compat import ImportAdaptor

        ImportAdaptor("postgres").fail()

    # Verify engine is connected
    engine.connect()

    return engine
Exemple #4
0
def check_redis_fn():
    try:
        client = redis.Redis(host=get_docker_host(), port=redis_config["port"])
        client.ping()
    except redis.ConnectionError:
        raise ContainerCheckFailed(
            "Unable to connect to a presumed Redis test container via given config: {}"
            .format(redis_config))
Exemple #5
0
def _create_clean_database():
    from pymongo import MongoClient

    client = get_pymongo_client()
    db = client[config["root_database"]]

    # Create a collection called `pytestMockResourceDbs' in the admin tab if it hasnt already been
    # created.
    db_collection = db["pytestMockResourcesDbs"]

    #  create a Document in the `pytestMockResourcesDbs` collection:
    result = db_collection.insert_one({})
    db_id = str(result.inserted_id)

    #  Create a database where the name is equal to that ID:
    create_db = client[db_id]

    #  Create a user as that databases owner
    create_db.command("createUser", db_id, pwd="password",
                      roles=["dbOwner"])  # nosec

    #  pass back an authenticated db connection
    limited_client = MongoClient(get_docker_host(), config["port"])
    db = limited_client[db_id]
    db.authenticate(db_id, "password")

    db.config = {"username": db_id, "password": "******", "database": db_id}

    assign_fixture_credentials(
        db,
        drivername="mongodb",
        host=get_docker_host(),
        port=config["port"],
        database=db_id,
        username=db_id,
        password="******",
    )

    return db
    def _(_mysql_container):
        database_name = _create_clean_database()
        engine = get_sqlalchemy_engine(database_name)

        assign_fixture_credentials(
            engine,
            drivername="mysql+pymysql",
            host=get_docker_host(),
            port=config["port"],
            database=database_name,
            username="******",
            password="******",
        )

        engine_manager = EngineManager(engine, ordered_actions, tables=tables)
        for engine in engine_manager.manage(session=session):
            yield engine
    def _(_postgres_container):
        database_name = _create_clean_database()
        engine = get_sqlalchemy_engine(database_name)

        assign_fixture_credentials(
            engine,
            drivername="postgresql+psycopg2",
            host=get_docker_host(),
            port=config["port"],
            database=database_name,
            username="******",
            password="******",
        )

        engine_manager = EngineManager(engine,
                                       ordered_actions,
                                       tables=tables,
                                       default_schema="public")
        for engine in engine_manager.manage(session=session):
            yield engine
Exemple #8
0
def get_sqlalchemy_engine(database_name, isolation_level=None):
    DB_URI = str(URL(
        "mysql+pymysql",
        username=config["username"],
        password=config["password"],
        host=get_docker_host(),
        port=config["port"],
        database=database_name
    ))

    options = {}
    if isolation_level:
        options["isolation_level"] = isolation_level

    from pytest_mock_resources.compat import pymysql

    pymysql.connect

    engine = sqlalchemy.create_engine(DB_URI, **options)

    # Verify engine is connected
    engine.connect()

    return engine
Exemple #9
0
def get_pymongo_client():
    uri = "mongodb://{}:{}".format(get_docker_host(), config["port"])

    return pymongo.MongoClient(uri)