"""This file tests logging function of an app.""" from unittest.mock import patch from notifications_delivery.connectors import loggly def clear_handlers(): """Remove all handlers from apps logger.""" for h in loggly.app_logger.handlers: loggly.app_logger.removeHandler(h) @patch('uuid.uuid1', return_value='TEST') def test_current_logger(uuid_mock): """Test to see if loggly generate correlation id if it wasn't provided.""" current_logger = loggly.get_current_logger() assert 'correlation_id' in current_logger.extra uuid_mock.assert_called_once() assert current_logger.extra['correlation_id'] == 'TEST' def test_current_logger_correlation_id(): """Test correlation_id link with logging adapter. Test whether correlation_id is assigned to logging adapter, when using set_correlation_id(correlation_id) context manager. """ correlation_id = 'test_correlation_id' with loggly.set_correlation_id(correlation_id): current_logger = loggly.get_current_logger() assert 'correlation_id' in current_logger.extra assert current_logger.extra['correlation_id'] == correlation_id def teardown_module(module): """Clear all handlers when app tears down.""" clear_handlers()