trolldb.test_utils.common module
Common functionalities for testing, shared between tests and other test utility modules.
- trolldb.test_utils.common.make_test_app_config_as_dict(subscriber_address: pydantic.FilePath | None = None) dict[str, dict][source]
Makes the app configuration (as a dictionary) when used in testing.
- Parameters:
subscriber_address – The address of the subscriber if it is of type
FilePath. Otherwise, if it isNonethesubscriberconfig will be an empty dictionary.- Returns:
A dictionary with a structure similar to that of an
AppConfigobject.
Warning
The return value of this function is a dictionary and not accepted as a valid input argument for
trolldb.database.mongodb.MongoDB.initialize(). As a result, one must cast it to the valid type by e.g.AppConfig(**make_test_app_config_as_dict()).
- trolldb.test_utils.common.test_app_config
The app configs for testing purposes assuming an empty configuration for the subscriber.
- trolldb.test_utils.common.create_config_file(config_path: pydantic.FilePath) pydantic.FilePath[source]
Creates a config file for tests.
- trolldb.test_utils.common.http_get(route: str = '', root: pydantic.AnyUrl = pydantic.BaseModel.api_server.url) BaseHTTPResponse[source]
An auxiliary function to make a GET request using
urllib.request().- Parameters:
route – The desired route (excluding the root URL) which can include a query string as well.
root (Optional, default
test_app_config.api_server.url) – The root to which the given route will be added to make the complete URL.
- Returns:
The response from the GET request.
- trolldb.test_utils.common.compare_by_operator_name(operator: str, left: Any, right: Any) Any[source]
Compares two operands given the binary operator name in a string format.
- Parameters:
operator – Any of
["$gte", "$gt", "$lte", "$lt", "$eq"]. These match the MongoDB comparison operators described here.left – The left operand
right – The right operand
- Returns:
The result of the comparison operation, i.e.
<left> <operator> <right>.- Raises:
ValueError – If the operator name is not valid.
- trolldb.test_utils.common.api_server_process_context(config: AppConfig = pydantic.BaseModel, startup_time: pydantic.PositiveFloat = 2) Generator[Process, Any, None][source]
A synchronous context manager to run the API server in a separate process (non-blocking).
It uses the multiprocessing package. The main use case is envisaged to be in TESTING environments.
- Parameters:
config – Same as
configargument forrun_server().startup_time – The overall time in seconds that is expected for the server and the database connections to be established before actual requests can be sent to the server. For testing purposes ensure that this is sufficiently large so that the tests will not time out.