Add missing tests for top-level

This commit is contained in:
Dave Shawley 2019-09-02 21:31:18 -04:00
parent fe4f4a7e38
commit 3c71ae0313

View file

@ -7,6 +7,7 @@ import os
import json import json
import time import time
import unittest import unittest
import uuid
from tornado import concurrent, httpserver, httputil, ioloop, testing, web from tornado import concurrent, httpserver, httputil, ioloop, testing, web
@ -55,16 +56,19 @@ class MockHelper(unittest.TestCase):
@contextlib.contextmanager @contextlib.contextmanager
def override_environment_variable(name, value): def override_environment_variable(**env_vars):
stash = os.environ.pop(name, None) stash = {}
if value is not None: for name, value in env_vars.items():
os.environ[name] = value stash[name] = os.environ.pop(name, None)
if value is not None:
os.environ[name] = value
try: try:
yield yield
finally: finally:
os.environ.pop(name, None) for name, value in stash.items():
if stash is not None: os.environ.pop(name, None)
os.environ[name] = stash if value is not None:
os.environ[name] = value
class ErrorLoggerTests(testing.AsyncHTTPTestCase): class ErrorLoggerTests(testing.AsyncHTTPTestCase):
@ -240,21 +244,21 @@ class RunTests(MockHelper, unittest.TestCase):
def test_that_debug_envvar_enables_debug_flag(self): def test_that_debug_envvar_enables_debug_flag(self):
create_app = mock.Mock() create_app = mock.Mock()
with override_environment_variable('DEBUG', '1'): with override_environment_variable(DEBUG='1'):
sprockets.http.run(create_app) sprockets.http.run(create_app)
create_app.assert_called_once_with(debug=True) create_app.assert_called_once_with(debug=True)
self.get_logging_config.assert_called_once_with(True) self.get_logging_config.assert_called_once_with(True)
def test_that_false_debug_envvar_disables_debug_flag(self): def test_that_false_debug_envvar_disables_debug_flag(self):
create_app = mock.Mock() create_app = mock.Mock()
with override_environment_variable('DEBUG', '0'): with override_environment_variable(DEBUG='0'):
sprockets.http.run(create_app) sprockets.http.run(create_app)
create_app.assert_called_once_with(debug=False) create_app.assert_called_once_with(debug=False)
self.get_logging_config.assert_called_once_with(False) self.get_logging_config.assert_called_once_with(False)
def test_that_unset_debug_envvar_disables_debug_flag(self): def test_that_unset_debug_envvar_disables_debug_flag(self):
create_app = mock.Mock() create_app = mock.Mock()
with override_environment_variable('DEBUG', None): with override_environment_variable(DEBUG=None):
sprockets.http.run(create_app) sprockets.http.run(create_app)
create_app.assert_called_once_with(debug=False) create_app.assert_called_once_with(debug=False)
self.get_logging_config.assert_called_once_with(False) self.get_logging_config.assert_called_once_with(False)
@ -264,7 +268,7 @@ class RunTests(MockHelper, unittest.TestCase):
self.runner_instance.run.assert_called_once_with(8000, mock.ANY) self.runner_instance.run.assert_called_once_with(8000, mock.ANY)
def test_that_port_envvar_sets_port_number(self): def test_that_port_envvar_sets_port_number(self):
with override_environment_variable('PORT', '8888'): with override_environment_variable(PORT='8888'):
sprockets.http.run(mock.Mock()) sprockets.http.run(mock.Mock())
self.runner_instance.run.assert_called_once_with(8888, mock.ANY) self.runner_instance.run.assert_called_once_with(8888, mock.ANY)
@ -642,3 +646,41 @@ class TestCaseTests(unittest.TestCase):
test_case.app.stop.assert_called_once_with( test_case.app.stop.assert_called_once_with(
test_case.io_loop, test_case.shutdown_limit, test_case.io_loop, test_case.shutdown_limit,
test_case.wait_timeout) test_case.wait_timeout)
class CorrelationFilterTests(unittest.TestCase):
def setUp(self):
super(CorrelationFilterTests, self).setUp()
self.logger = logging.getLogger()
self.record = self.logger.makeRecord(
'name', logging.INFO, 'functionName', 42, 'hello %s',
tuple(['world']), (None, None, None))
self.filter = sprockets.http._CorrelationFilter()
def test_that_correlation_filter_adds_correlation_id(self):
self.filter.filter(self.record)
self.assertTrue(hasattr(self.record, 'correlation-id'))
def test_that_correlation_filter_does_not_overwrite_correlation_id(self):
some_value = str(uuid.uuid4())
setattr(self.record, 'correlation-id', some_value)
self.filter.filter(self.record)
self.assertEqual(getattr(self.record, 'correlation-id'), some_value)
class LoggingConfigurationTests(unittest.TestCase):
def test_that_debug_sets_log_level_to_debug(self):
config = sprockets.http._get_logging_config(True)
self.assertEqual(config['root']['level'], 'DEBUG')
def test_that_not_debug_sets_log_level_to_info(self):
config = sprockets.http._get_logging_config(False)
self.assertEqual(config['root']['level'], 'INFO')
def test_that_format_includes_sd_when_service_and_env_are_set(self):
with override_environment_variable(SERVICE='service',
ENVIRONMENT='whatever'):
config = sprockets.http._get_logging_config(False)
fmt_name = list(config['formatters'].keys())[0]
self.assertIn('service="service" environment="whatever"',
config['formatters'][fmt_name]['format'])