示例#1
0
    def test_metric(self):
        """Test setting metrics using ``R.metric``."""

        slug = 'test-metric'
        n = 1

        # get the keys used for the metric, so we can check for the appropriate
        # calls
        keys = self.r._build_keys(slug)
        second, minute, hour, day, week, month, year = keys
        self.r.metric(slug, num=n)

        # Verify that setting a metric adds the appropriate slugs to the keys
        # set and then incrememts each key
        self.redis.assert_has_calls([
            call.sadd(self.r._metric_slugs_key, slug),
            call.incr(second, n),
            call.incr(minute, n),
            call.incr(hour, n),
            call.incr(day, n),
            call.incr(week, n),
            call.incr(month, n),
            call.incr(year, n),
        ])

        # Expiration should not have gotten called
        self.assertFalse(self.redis.expire.called)
示例#2
0
    def test_metric_with_category(self, mock_categorize):
        """The ``metric`` method should call ``_categorize`` if passed a
        ``category`` argument."""
        category = "Some Category"
        slug = 'categorized-metric'
        n = 1

        # get the keys used for the metric, so we can check for calls
        keys = self.r._build_keys(slug)
        second, minute, hour, day, week, month, year = keys
        self.r.metric(slug, num=n, category=category)

        # Verify that setting a metric adds the appropriate slugs to the keys
        # set and then incrememts each key
        self.redis.assert_has_calls([
            call.sadd(self.r._metric_slugs_key, slug),
            call.incr(second, n),
            call.incr(minute, n),
            call.incr(hour, n),
            call.incr(day, n),
            call.incr(week, n),
            call.incr(month, n),
            call.incr(year, n),
        ])

        # Make sure this gets categorized.
        mock_categorize.assert_called_once_with(slug, category)

        # Expiration should not have gotten called
        self.assertFalse(self.redis.expire.called)
示例#3
0
    def test__categorize(self, mock_category_slugs):
        """Categorizing a slug should add the correct key/values to Redis,
        and it should store the Category in a redis set."""

        # Sample category and metric slug
        cat = "Sample Category"
        cat_key = "c:Sample Category"
        slug = "sample-slug"

        with patch('redis_metrics.models.redis.StrictRedis') as mock_redis:
            redis_instance = mock_redis.return_value
            r = R()

            r._categorize(slug, cat)
            calls = [call.sadd(cat_key, slug), call.sadd("categories", cat)]
            redis_instance.assert_has_calls(calls, any_order=True)
    def test_metric_with_expiration(self, mock_categorize):
        """The ``metric`` method should call the redis ``expire`` method if
        passed an ``expire`` argument."""

        slug = 'categorized-metric'
        n = 1

        # get the keys used for the metric, so we can check for calls
        day, week, month, year = self.r._build_keys(slug)
        self.r.metric(slug, num=n, expire=3600)

        # Verify that setting a metric adds the appropriate slugs to the keys
        # set and then incrememts each key
        self.redis.assert_has_calls([
            call.sadd(self.r._metric_slugs_key, day, week, month, year),
            call.incr(day, n),
            call.incr(week, n),
            call.incr(month, n),
            call.incr(year, n),
        ])

        # Make sure nothing was categorized.
        self.assertFalse(mock_categorize.called)

        # Expiration should have gotten called
        self.redis.expire.assert_has_calls([
            call.expire(day, 3600),
            call.expire(week, 3600),
            call.expire(month, 3600),
            call.expire(year, 3600),
        ])
示例#5
0
 def test_gauge(self):
     """Tests setting a gauge with ``R.gauge``. Verifies that the gauge slug
     is added to the set of gauge slugs and that the value gets set."""
     self.r.gauge('test-gauge', 9000)
     self.redis.assert_has_calls([
         call.sadd(self.r._gauge_slugs_key, 'test-gauge'),
         call.set('g:test-gauge', 9000),
     ])
示例#6
0
    def test_metric_with_overridden_granularities(self):
        slug = 'test-metric'
        n = 1

        # get the keys used for the metric, so we can check for the appropriate
        # calls
        daily, weekly = self.r._build_keys(slug)
        self.r.metric(slug, num=n)

        # Verify that setting a metric adds the appropriate slugs to the keys
        # set and then incrememts each key
        self.redis.assert_has_calls([
            call.sadd(self.r._metric_slugs_key, slug),
            call.incr(daily, n),
            call.incr(weekly, n),
        ])

        # Expiration should not have gotten called
        self.assertFalse(self.redis.expire.called)
示例#7
0
    def test_metric(self):
        """Test setting metrics using ``R.metric``."""

        slug = 'test-metric'
        n = 1

        # get the keys used for the metric, so we can check for the appropriate
        # calls
        day, week, month, year = self.r._build_keys(slug)
        self.r.metric(slug, num=n)

        # Verify that setting a metric adds the appropriate slugs to the keys
        # set and then incrememts each key
        self.redis.assert_has_calls([
            call.sadd(self.r._metric_slugs_key, day, week, month, year),
            call.incr(day, n),
            call.incr(week, n),
            call.incr(month, n),
            call.incr(year, n),
        ])
示例#8
0
    def test_metric_with_expiration(self, mock_categorize):
        """The ``metric`` method should call the redis ``expire`` method if
        passed an ``expire`` argument."""

        slug = 'categorized-metric'
        n = 1

        # get the keys used for the metric, so we can check for calls
        keys = self.r._build_keys(slug)
        self.r.metric(slug, num=n, expire=3600)

        # Verify that setting a metric adds the appropriate slugs to the keys
        # set and then incrememts each key
        call_list = [call.sadd(self.r._metric_slugs_key, slug)]
        for k in keys:
            call_list.append(call.incr(k, n))
            call_list.append(call.expire(k, 3600))

        self.redis.assert_has_calls(call_list)

        # Make sure nothing was categorized.
        self.assertFalse(mock_categorize.called)
    def test_metric_with_overridden_granularities(self):
        test_settings = TEST_SETTINGS.copy()
        test_settings['MIN_GRANULARITY'] = 'daily'
        test_settings['MAX_GRANULARITY'] = 'weekly'
        with override_settings(REDIS_METRICS=test_settings):
            slug = 'test-metric'
            n = 1

            # get the keys used for the metric, so we can check for the appropriate
            # calls
            daily, weekly = self.r._build_keys(slug)
            self.r.metric(slug, num=n)

            # Verify that setting a metric adds the appropriate slugs to the keys
            # set and then incrememts each key
            self.redis.assert_has_calls([
                call.sadd(self.r._metric_slugs_key, slug),
                call.pipeline(),
                call.pipeline().incr(daily, n),
                call.pipeline().incr(weekly, n),
            ])

            # Expiration should not have gotten called
            self.assertFalse(self.redis.expire.called)