trolldb.test_utils.mongodb_instance module

The module which defines functionalities to run a MongoDB instance which is to be used in the testing environment.

class trolldb.test_utils.mongodb_instance.TestMongoInstance[source]

Bases: object

A static class to enclose functionalities for running a MongoDB instance.

log_dir: ClassVar[str] = '/tmp/tmpdnij1dvl__pytroll_db_temp_test_log'

Temp directory for logging messages by the MongoDB instance.

Warning

The value of this attribute as shown above is just an example and will change in an unpredictable (secure) way every time!

storage_dir: ClassVar[str] = '/tmp/tmprl75scxb__pytroll_db_temp_test_storage'

Temp directory for storing database files by the MongoDB instance.

Warning

The value of this attribute as shown above is just an example and will change in an unpredictable (secure) way every time!

port: ClassVar[int] = 28017

The port on which the instance will run.

Warning

This must be always hard-coded.

process: ClassVar[Popen | None] = None

The process which is used to run the MongoDB instance.

classmethod __prepare_dir(directory: str) None

An auxiliary function to prepare a single directory.

It creates a directory if it does not exist, or removes it first if it exists and then recreates it.

classmethod __remove_dir(directory: str) None

An auxiliary function to remove a directory and all its content recursively.

classmethod run_subprocess(args: list[str], wait=True) tuple[AnyStr, AnyStr] | None[source]

Runs the subprocess in shell given its arguments.

classmethod mongodb_exists() bool[source]

Checks if mongod command exists.

classmethod prepare_dirs() None[source]

Prepares the temp directories.

classmethod run_instance() None[source]

Runs the MongoDB instance and does not wait for it, i.e. the process runs in the background.

classmethod shutdown_instance() None[source]

Shuts down the MongoDB instance by terminating its process.

trolldb.test_utils.mongodb_instance.mongodb_instance_server_process_context(database_config: DatabaseConfig = pydantic.BaseModel.database, startup_time: pydantic.PositiveFloat = 2) Generator[Any, Any, None][source]

A synchronous context manager to run the MongoDB instance in a separate process (non-blocking).

It uses the subprocess package. The main use case is envisaged to be in testing environments.

Parameters:
  • database_config – The configuration of the database.

  • startup_time – The overall time in seconds that is expected for the MongoDB server instance to run before the database content can be accessed.

trolldb.test_utils.mongodb_instance.running_prepared_database_context() Generator[Any, Any, None][source]

A synchronous context manager to start and prepare a database instance for tests.