API

pytest_pymysql_autorecord

class pytest_pymysql_autorecord.DatabaseMock(mode: Mode, db_data_dir: Optional[Path], request: FixtureRequest)[source]

Properties and methods for the database mock fixture.

Parameters:
  • mode (Mode) – The mode in which the fixture is used.

  • db_data_dir (Path) – Directory for storing the recorded data files.

  • request (FixtureRequest) – pytest request fixture.

mode

The mode in which the fixture is used.

Type:

Mode

user_value(value: Any) Any[source]

Mock a user-supplied value.

Some database tests may generate random values, store these in the database and use these random values in assertions. This poses a problem for mocking as the value will be different for each test run, so that the current and the previously stored version differs. Hence such tests will fail when run with the stored data.

The solution is to wrap random values with this method. For example:

import uuid

random_value = database_mock.user_value(str(uuid.uuid4())

When database data is stored, value is stored along with the data, and it is returned as the return value. When the database is mocked, the previously stored stored value is returned. Otherwise the method just returns value.

It must be possible to pickle the passed value

Parameters:

value (any) – Value. The value is ignored when the database is mocked.

Returns:

Either the previously stored value (when the database is being mocked) or the passed value (otherwise).

Return type:

any

class pytest_pymysql_autorecord.Mode(value)[source]

An enumeration of the available modes.

The available modes are STORE_DATA, MOCK and NORMAL.

pytest_pymysql_autorecord.skip_for_db_mocking() None[source]

Skip a test if this plugin is used.

Call this function from a test if you want to skip it whenever database data is stored or mocked, i.e. if the --store-db-data or --mock-db-data command line options are used. You might want to do this to avoid storing confidential data or to avoid tests failing because of non-deterministic database access.