Пример #1
0
    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)
Пример #2
0
    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)
Пример #3
0
    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)
Пример #4
0
    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)
Пример #5
0
    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)