def test_on_finish(self): """ on_finish() calls send_stats() """ with patch( 'krux.tornado.handlers.RequestHandler.send_stats', autospec = True, ) as mock_stats: handler = RequestHandler(self.app, self.request) handler.on_finish() mock_stats.assert_called_once_with(handler)
def test_get_stats_key_with_args(self): """ get_stats_key() with path arguments """ self.request.path = '/test/banana/apple' expected = 'test' handler = RequestHandler(self.app, self.request) handler.path_args = ['banana', 'apple'] handler.path_kwargs = {} key = handler.get_stats_key() assert_equal(key, expected)
def test_get_stats_key_no_args(self): """ get_stats_key() with no path arguments """ self.request.path = '/test' expected = 'test' handler = RequestHandler(self.app, self.request) handler.path_args = [] handler.path_kwargs = {} key = handler.get_stats_key() assert_equal(key, expected)
def test_get_stats_key_complex_path(self): """ get_stats_key() with a multi-level path """ self.request.path = '/test/banana/apple' expected = 'test.banana.apple' handler = RequestHandler(self.app, self.request) handler.path_args = [] handler.path_kwargs = {} key = handler.get_stats_key() assert_equal(key, expected)
def test_prepare(self): """ prepare() starts the timer for the request """ start_time = time.time() handler = RequestHandler(self.app, self.request) with patch( 'krux.tornado.handlers.time.time', autospec = True, return_value = start_time ) as mock_time: handler.prepare() mock_time.assert_called_once_with() assert_equal(handler.start_time, start_time)
def test_get_stats_key_http_error(self): """ get_stats_key() with an HTTP error """ self.request.path = '/test/banana/apple' expected = 'test' mock_status = MagicMock(return_value = 404) handler = RequestHandler(self.app, self.request) handler.path_args = [] handler.path_kwargs = {} handler.get_status = mock_status key = handler.get_stats_key() mock_status.assert_called_once_with() assert_equal(key, expected)
def test_send_stats(self): """ send_stats() calls the correct stats methods """ key = 'test.banana' method = 'get' end_time = time.time() delta = .001 status = 200 expected_incr = '.'.join((key, method.upper(), str(status))) expected_timer = '.'.join((key, method.upper())) expected_delta = delta * 1000 mock_get_key = MagicMock(return_value = key) mock_get_status = MagicMock(return_value = status) mock_incr = MagicMock() mock_timer = MagicMock() mock_stats = MagicMock() mock_stats.incr = mock_incr mock_stats.timing = mock_timer self.app.endpoint_stats = mock_stats self.request.method = method handler = RequestHandler(self.app, self.request) handler.get_stats_key = mock_get_key handler.start_time = end_time - delta with patch( 'krux.tornado.handlers.time.time', autospec = True, return_value = end_time ) as mock_time: handler.send_stats() mock_get_key.assert_called_once_with() mock_incr.assert_called_once_with(expected_incr) mock_timer.assert_called_once_with(expected_timer, expected_delta)