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.pipeline(),
            call.pipeline().incr(second, n),
            call.pipeline().incr(minute, n),
            call.pipeline().incr(hour, n),
            call.pipeline().incr(day, n),
            call.pipeline().incr(week, n),
            call.pipeline().incr(month, n),
            call.pipeline().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)
    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.pipeline(),
            call.pipeline().incr(second, n),
            call.pipeline().incr(minute, n),
            call.pipeline().incr(hour, n),
            call.pipeline().incr(day, n),
            call.pipeline().incr(week, n),
            call.pipeline().incr(month, n),
            call.pipeline().incr(year, n),
        ])

        # Expiration should not have gotten called
        self.assertFalse(self.redis.expire.called)
    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), call.pipeline()]
        for k in keys:
            call_list.append(call.pipeline().incr(k, n))
            call_list.append(call.pipeline().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)