"""Util for testing the logger.""" from owslogger import constants from owslogger import logger def patch(mocker): """Patch the logger's methods. Args: mocker (Mocker): the pytest-mock object. """ mocker.patch.object(logger.OwsLoggingAdapter, 'info') mocker.patch.object(logger.OwsLoggingAdapter, 'warn') mocker.patch.object(logger.OwsLoggingAdapter, 'error') def assert_logger_calls( status, verb, path, resources=None, extra_message=None): """Assert calls to the logger. This util verify if any of the calls are matching the ones in the logger, and not just the last one. Args: status (int): the status of the request. verb (str): the verb of the request. path (str): the path of the request. resources (dict): additional resources that should be present. extra_message (str): additional message to send to the logger. """ resources = resources or {} method = logger.OwsLoggingAdapter.info if 300 <= status < 500: method = logger.OwsLoggingAdapter.warn elif 500 <= status: method = logger.OwsLoggingAdapter.error message = constants.AUTOLOG_MESSAGE if extra_message: message = constants.AUTOLOG_MESSAGE_WITH_EXTRA from unittest.mock import ANY method.assert_any_call( message.format( status=status, verb=verb, resource=path, extra_message=None), resources=ANY)