API

Fixture Functions

pytest_mock_resources.create_mongo_fixture(scope='function')

Produce a mongo fixture.

Any number of fixture functions can be created. Under the hood they will all share the same database server.

Parameters

scope – Passthrough pytest’s fixture scope.

pytest_mock_resources.create_mysql_fixture(*ordered_actions, scope='function', tables=None, session=None)

Produce a MySQL fixture.

Any number of fixture functions can be created. Under the hood they will all share the same database server.

Parameters
  • ordered_actions – Any number of ordered actions to be run on test setup.

  • scope – Passthrough pytest’s fixture scope.

  • tables – Subsets the tables created by ordered_actions. This is generally most useful when a model-base was specified in ordered_actions.

  • session – Whether to return a session instead of an engine directly. This can either be a bool or a callable capable of producing a session.

pytest_mock_resources.create_postgres_fixture(*ordered_actions, scope='function', tables=None, session=None, async_=False, createdb_template='template1', engine_kwargs=None)

Produce a Postgres fixture.

Any number of fixture functions can be created. Under the hood they will all share the same database server.

Parameters
  • ordered_actions – Any number of ordered actions to be run on test setup.

  • scope – Passthrough pytest’s fixture scope.

  • tables – Subsets the tables created by ordered_actions. This is generally most useful when a model-base was specified in ordered_actions.

  • session – Whether to return a session instead of an engine directly. This can either be a bool or a callable capable of producing a session.

  • async – Whether to return an async fixture/client.

  • createdb_template – The template database used to create sub-databases. “template1” is the default chosen when no template is specified.

  • engine_kwargs – Optional set of kwargs to send into the engine on creation.

pytest_mock_resources.create_redis_fixture(scope='function')

Produce a Redis fixture.

Any number of fixture functions can be created. Under the hood they will all share the same database server.

Note

If running tests in parallel, the implementation fans out to different redis “database”s, up to a 16 (which is the default container fixed limite). This means you can only run up to 16 simultaneous tests.

Additionally, any calls to flushall or any other cross-database calls will still represent cross-test state.

Finally, the above notes are purely describing the current implementation, and should not be assumed. In the future, the current database selection mechanism may change, or databases may not be used altogether.

Parameters

scope (str) – The scope of the fixture can be specified by the user, defaults to “function”.

Raises

KeyError – If any additional arguments are provided to the function than what is necessary.

pytest_mock_resources.create_redshift_fixture(*ordered_actions, scope='function', tables=None, session=None, async_=False, createdb_template='template1', engine_kwargs=None)

Produce a Redshift fixture.

Any number of fixture functions can be created. Under the hood they will all share the same database server.

Note that, by default, redshift uses a postgres container as the database server and attempts to reintroduce appoximations of Redshift features, such as S3 COPY/UNLOAD, redshift-specific functions, and other specific behaviors.

Parameters
  • ordered_actions – Any number of ordered actions to be run on test setup.

  • scope – Passthrough pytest’s fixture scope.

  • tables – Subsets the tables created by ordered_actions. This is generally most useful when a model-base was specified in ordered_actions.

  • session – Whether to return a session instead of an engine directly. This can either be a bool or a callable capable of producing a session.

  • async – Whether to return an async fixture/client.

  • createdb_template – The template database used to create sub-databases. “template1” is the default chosen when no template is specified.

  • engine_kwargs – Optional set of kwargs to send into the engine on creation.

pytest_mock_resources.create_sqlite_fixture(*ordered_actions, scope='function', tables=None, session=None, decimal_warnings=False, postgres_like=True)

Produce a SQLite fixture.

Any number of fixture functions can be created. Under the hood they will all share the same database server.

Parameters
  • ordered_actions – Any number of ordered actions to be run on test setup.

  • scope – Passthrough pytest’s fixture scope.

  • tables – Subsets the tables created by ordered_actions. This is generally most useful when a model-base was specified in ordered_actions.

  • session – Whether to return a session instead of an engine directly. This can either be a bool or a callable capable of producing a session.

  • decimal_warnings – Whether to show sqlalchemy decimal warnings related to precision loss. The default False suppresses these warnings.

  • postgres_like – Whether to add extra SQLite features which attempt to mimic postgres enough to stand in for it for testing.

class pytest_mock_resources.fixture.database.generic.Credentials(drivername, host, port, database, username, password)

Return as pmr_credentials attribute on supported docker-based fixtures.

Examples

It’s also directly dict-able. >>> creds = Credentials(‘d’, ‘l’, ‘p’, ‘baz’, ‘user’, ‘pass’) >>> dict_creds = dict(creds)

as_mongo_kwargs()

Return the valid arguments to a mongo client.

as_psycopg2_kwargs()

Return the valid arguments to sqlalchemy psycopg2.connect.

as_redis_kwargs()

Return the valid arguments to a redis client.

as_sqlalchemy_url()

Return a sqlalchemy sqlalchemy.engine.url.URL.

as_sqlalchemy_url_kwargs()

Return the valid arguments to sqlalchemy sqlalchemy.engine.url.URL.

as_url()

Return a stringified dbapi URL string.

Fixture Config

class pytest_mock_resources.MongoConfig(**kwargs)

Define the configuration object for mongo.

Parameters
  • image (str) – The docker image:tag specifier to use for mongo containers. Defaults to "mongo:3.6".

  • host (str) – The hostname under which a mounted port will be available. Defaults to "localhost".

  • port (int) – The port to bind the container to. Defaults to 28017.

  • ci_port (int) – The port to bind the container to when a CI environment is detected. Defaults to 27017.

  • root_database (str) – The name of the root mongo database to create. Defaults to "dev-mongo".

class pytest_mock_resources.MysqlConfig(**kwargs)

Define the configuration object for MySql.

Parameters
  • image (str) – The docker image:tag specifier to use for mysql containers. Defaults to "mysql:5.6".

  • host (str) – The hostname under which a mounted port will be available. Defaults to "localhost".

  • port (int) – The port to bind the container to. Defaults to 5532.

  • ci_port (int) – The port to bind the container to when a CI environment is detected. Defaults to 5432.

  • username (str) – The username of the root user Defaults to "user".

  • password (str) – The password of the root password Defaults to "password".

  • root_database (str) – The name of the root database to create. Defaults to "dev".

class pytest_mock_resources.PostgresConfig(**kwargs)

Define the configuration object for postgres.

Parameters
  • image (str) – The docker image:tag specifier to use for postgres containers. Defaults to "postgres:9.6.10-alpine".

  • host (str) – The hostname under which a mounted port will be available. Defaults to "localhost".

  • port (int) – The port to bind the container to. Defaults to 5532.

  • ci_port (int) – The port to bind the container to when a CI environment is detected. Defaults to 5432.

  • username (str) – The username of the root postgres user Defaults to "user".

  • password (str) – The password of the root postgres password Defaults to "password".

  • root_database (str) – The name of the root postgres database to create. Defaults to "dev".

class pytest_mock_resources.RedisConfig(**kwargs)

Define the configuration object for redis.

Parameters
  • image (str) – The docker image:tag specifier to use for redis containers. Defaults to "redis:5.0.7".

  • host (str) – The hostname under which a mounted port will be available. Defaults to "localhost".

  • port (int) – The port to bind the container to. Defaults to 6380.

  • ci_port (int) – The port to bind the container to when a CI environment is detected. Defaults to 6379.

pytest_mock_resources.pmr_mongo_config()

Override this fixture with a MongoConfig instance to specify different defaults.

Examples

>>> @pytest.fixture(scope='session')
... def pmr_mongo_config():
...     return MongoConfig(image="mongo:3.4", root_database="foo")
pytest_mock_resources.pmr_mysql_config()

Override this fixture with a MysqlConfig instance to specify different defaults.

Examples

>>> @pytest.fixture(scope='session')
... def pmr_mysql_config():
...     return MysqlConfig(image="mysql:5.2", root_database="foo")
pytest_mock_resources.pmr_postgres_config()

Override this fixture with a PostgresConfig instance to specify different defaults.

Examples

>>> @pytest.fixture(scope='session')
... def pmr_postgres_config():
...     return PostgresConfig(image="postgres:9.6.10", root_database="foo")
pytest_mock_resources.pmr_redis_config()

Override this fixture with a RedisConfig instance to specify different defaults.

Examples

>>> @pytest.fixture(scope='session')
... def pmr_redis_config():
...     return RedisConfig(image="redis:6.0")