swh.core.db.pytest_plugin module#

class swh.core.db.pytest_plugin.SWHDatabaseJanitor(user: str, host: str, port: int, dbname: str, version: Union[str, float], password: Optional[str] = None, isolation_level: Optional[int] = None, connection_timeout: int = 60, dump_files: Optional[Union[str, Sequence[str]]] = None, no_truncate_tables: Set[str] = {}, no_db_drop: bool = False)[source]#

Bases: DatabaseJanitor

SWH database janitor implementation with a a different setup/teardown policy than than the stock one. Instead of dropping, creating and initializing the database for each test, it creates and initializes the db once, then truncates the tables (and sequences) in between tests.

This is needed to have acceptable test performances.

Initialize janitor.

Parameters:
  • user – postgresql username

  • host – postgresql host

  • port – postgresql port

  • dbname – database name

  • version – postgresql version number

  • password – optional postgresql password

  • isolation_level – optional postgresql isolation level defaults to server’s default

  • connection_timeout – how long to retry connection before raising a TimeoutError

psql_exec(fname: str) None[source]#
db_reset() None[source]#

Truncate tables (all but self.no_truncate_tables set) and sequences

init() None[source]#

Create database in postgresql out of a template it if it exists, bare creation otherwise.

drop() None[source]#

Drop database in postgresql.

swh.core.db.pytest_plugin.postgresql_fact(process_fixture_name: str, dbname: Optional[str] = None, load: Optional[Sequence[Union[Callable, str]]] = None, isolation_level: Optional[int] = None, modname: Optional[str] = None, dump_files: Optional[Union[str, List[str]]] = None, no_truncate_tables: Set[str] = {'dbversion'}, no_db_drop: bool = False) Callable[[FixtureRequest], Iterator[connection]][source]#

Return connection fixture factory for PostgreSQL.

Parameters:
  • process_fixture_name – name of the process fixture

  • dbname – database name

  • load – SQL, function or function import paths to automatically load into our test database

  • isolation_level – optional postgresql isolation level defaults to server’s default

  • modname – (swh) module name for which the database is created

Dump_files:

(deprecated, use load instead) list of sql script files to execute after the database has been created

No_truncate_tables:

list of table not to truncate between tests (only used when no_db_drop is True)

No_db_drop:

if True, keep the database between tests; in which case, the database is reset (see SWHDatabaseJanitor.db_reset()) by truncating most of the tables. Note that this makes de facto tests (potentially) interdependent, use with extra caution.

Returns:

function which makes a connection to postgresql

swh.core.db.pytest_plugin.gen_dump_files(dump_files: Union[str, Iterable[str]]) Iterator[str][source]#

Generate files potentially resolving glob patterns if any