Skip to content

Commit

Permalink
Move assert_metric_equal out of base class (#390)
Browse files Browse the repository at this point in the history
  • Loading branch information
asherf authored Apr 28, 2023
1 parent 1b5cf42 commit a04eabc
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 73 deletions.
26 changes: 13 additions & 13 deletions django_prometheus/tests/end2end/testapp/test_caches.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from django.test import TestCase
from redis import RedisError

from django_prometheus.testutils import PrometheusTestCaseMixin, get_metric
from django_prometheus.testutils import assert_metric_equal, get_metric


class TestCachesMetrics(PrometheusTestCaseMixin, TestCase):
class TestCachesMetrics(TestCase):
"""Test django_prometheus.caches metrics."""

def test_counters(self):
Expand Down Expand Up @@ -35,9 +35,9 @@ def test_counters(self):

assert result == "default"

self.assertMetricEquals(total_before + 4, "django_cache_get_total", backend=backend)
self.assertMetricEquals(hit_before + 2, "django_cache_get_hits_total", backend=backend)
self.assertMetricEquals(
assert_metric_equal(total_before + 4, "django_cache_get_total", backend=backend)
assert_metric_equal(hit_before + 2, "django_cache_get_hits_total", backend=backend)
assert_metric_equal(
miss_before + 2,
"django_cache_get_misses_total",
backend=backend,
Expand All @@ -55,19 +55,19 @@ def test_redis_cache_fail(self):
tested_cache = caches["stopped_redis_ignore_exception"]
tested_cache.get("foo1")

self.assertMetricEquals(hit_before, "django_cache_get_hits_total", backend=supported_cache)
self.assertMetricEquals(miss_before, "django_cache_get_misses_total", backend=supported_cache)
self.assertMetricEquals(total_before + 1, "django_cache_get_total", backend=supported_cache)
self.assertMetricEquals(fail_before + 1, "django_cache_get_fail_total", backend=supported_cache)
assert_metric_equal(hit_before, "django_cache_get_hits_total", backend=supported_cache)
assert_metric_equal(miss_before, "django_cache_get_misses_total", backend=supported_cache)
assert_metric_equal(total_before + 1, "django_cache_get_total", backend=supported_cache)
assert_metric_equal(fail_before + 1, "django_cache_get_fail_total", backend=supported_cache)

tested_cache = caches["stopped_redis"]
with self.assertRaises(RedisError):
tested_cache.get("foo1")

self.assertMetricEquals(hit_before, "django_cache_get_hits_total", backend=supported_cache)
self.assertMetricEquals(miss_before, "django_cache_get_misses_total", backend=supported_cache)
self.assertMetricEquals(total_before + 2, "django_cache_get_total", backend=supported_cache)
self.assertMetricEquals(fail_before + 2, "django_cache_get_fail_total", backend=supported_cache)
assert_metric_equal(hit_before, "django_cache_get_hits_total", backend=supported_cache)
assert_metric_equal(miss_before, "django_cache_get_misses_total", backend=supported_cache)
assert_metric_equal(total_before + 2, "django_cache_get_total", backend=supported_cache)
assert_metric_equal(fail_before + 2, "django_cache_get_fail_total", backend=supported_cache)

def test_cache_version_support(self):
supported_caches = [
Expand Down
6 changes: 3 additions & 3 deletions django_prometheus/tests/end2end/testapp/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
from django.test import TestCase

from django_prometheus.testutils import (
PrometheusTestCaseMixin,
assert_metric_compare,
assert_metric_diff,
assert_metric_equal,
get_metric,
save_registry,
)


class BaseDbMetricTest(PrometheusTestCaseMixin, TestCase):
class BaseDbMetricTest(TestCase):
# https://docs.djangoproject.com/en/2.2/topics/testing/tools/#django.test.SimpleTestCase.databases
databases = "__all__"

Expand Down Expand Up @@ -47,7 +47,7 @@ def test_counters(self):
except Exception:
pass

self.assertMetricEquals(
assert_metric_equal(
1,
"django_db_errors_total",
alias="test_db_1",
Expand Down
6 changes: 3 additions & 3 deletions django_prometheus/tests/end2end/testapp/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from testapp.views import ObjectionException

from django_prometheus.testutils import (
PrometheusTestCaseMixin,
assert_metric_diff,
assert_metric_equal,
save_registry,
)

Expand All @@ -25,7 +25,7 @@ def T(metric_name):


@override_settings(PROMETHEUS_LATENCY_BUCKETS=(0.05, 1.0, 2.0, 4.0, 5.0, 10.0, float("inf")))
class TestMiddlewareMetrics(PrometheusTestCaseMixin, SimpleTestCase):
class TestMiddlewareMetrics(SimpleTestCase):
"""Test django_prometheus.middleware.
Note that counters related to exceptions can't be tested as
Expand Down Expand Up @@ -74,7 +74,7 @@ def test_request_counters(self):
# <=128 bytes bodies.
assert_metric_diff(registry, 3, M("requests_body_total_bytes_bucket"), le="0.0")
assert_metric_diff(registry, 4, M("requests_body_total_bytes_bucket"), le="128.0")
self.assertMetricEquals(None, M("responses_total_by_templatename"), templatename="help.html")
assert_metric_equal(None, M("responses_total_by_templatename"), templatename="help.html")
assert_metric_diff(registry, 3, T("responses_total_by_templatename"), templatename="index.html")
assert_metric_diff(registry, 4, T("responses_total_by_status"), status="200")
assert_metric_diff(registry, 0, M("responses_body_total_bytes_bucket"), le="0.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
PrometheusAfterMiddleware,
PrometheusBeforeMiddleware,
)
from django_prometheus.testutils import (
PrometheusTestCaseMixin,
assert_metric_diff,
save_registry,
)
from django_prometheus.testutils import assert_metric_diff, save_registry

EXTENDED_METRICS = [
M("requests_latency_seconds_by_view_method"),
Expand Down Expand Up @@ -49,7 +45,7 @@ def label_metric(self, metric, request, response=None, **labels):
"testapp.test_middleware_custom_labels.AppMetricsAfterMiddleware",
)
)
class TestMiddlewareMetricsWithCustomLabels(PrometheusTestCaseMixin, SimpleTestCase):
class TestMiddlewareMetricsWithCustomLabels(SimpleTestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
Expand Down
12 changes: 6 additions & 6 deletions django_prometheus/tests/end2end/testapp/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.test import SimpleTestCase

from django_prometheus.migrations import ExportMigrationsForDatabase
from django_prometheus.testutils import PrometheusTestCaseMixin
from django_prometheus.testutils import assert_metric_equal


def M(metric_name):
Expand All @@ -15,7 +15,7 @@ def M(metric_name):
return "django_migrations_%s" % metric_name


class TestMigrations(PrometheusTestCaseMixin, SimpleTestCase):
class TestMigrations(SimpleTestCase):
"""Test migration counters."""

def test_counters(self):
Expand All @@ -30,7 +30,7 @@ def test_counters(self):
executor.loader.applied_migrations = {"b", "c"}
ExportMigrationsForDatabase("fakedb2", executor)

self.assertMetricEquals(3, M("applied_total"), connection="fakedb1")
self.assertMetricEquals(0, M("unapplied_total"), connection="fakedb1")
self.assertMetricEquals(2, M("applied_total"), connection="fakedb2")
self.assertMetricEquals(1, M("unapplied_total"), connection="fakedb2")
assert_metric_equal(3, M("applied_total"), connection="fakedb1")
assert_metric_equal(0, M("unapplied_total"), connection="fakedb1")
assert_metric_equal(2, M("applied_total"), connection="fakedb2")
assert_metric_equal(1, M("unapplied_total"), connection="fakedb2")
8 changes: 2 additions & 6 deletions django_prometheus/tests/end2end/testapp/test_models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
from django.test import TestCase
from testapp.models import Dog, Lawn

from django_prometheus.testutils import (
PrometheusTestCaseMixin,
assert_metric_diff,
save_registry,
)
from django_prometheus.testutils import assert_metric_diff, save_registry


def M(metric_name):
Expand All @@ -17,7 +13,7 @@ def M(metric_name):
return "django_model_%s" % metric_name


class TestModelMetrics(PrometheusTestCaseMixin, TestCase):
class TestModelMetrics(TestCase):
"""Test django_prometheus.models."""

def test_counters(self):
Expand Down
28 changes: 8 additions & 20 deletions django_prometheus/tests/test_testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import prometheus_client

from django_prometheus.testutils import (
PrometheusTestCaseMixin,
assert_metric_diff,
assert_metric_equal,
assert_metric_no_diff,
assert_metric_not_equal,
get_metric,
Expand All @@ -16,17 +16,6 @@
)


class SomeTestCase(PrometheusTestCaseMixin):
"""A class that pretends to be a unit test."""

def __init__(self):
self.passes = True
super().__init__()

def assertEqual(self, left, right, *args, **kwargs):
self.passes = self.passes and (left == right)


class PrometheusTestCaseMixinTest(unittest.TestCase):
def setUp(self):
self.registry = prometheus_client.CollectorRegistry()
Expand All @@ -42,7 +31,6 @@ def setUp(self):
self.some_labelled_gauge.labels("pink", "royal").set(2)
self.some_labelled_gauge.labels("carmin", "indigo").set(3)
self.some_labelled_gauge.labels("carmin", "royal").set(4)
self.test_case = SomeTestCase()

def test_get_metric(self):
"""Tests get_metric."""
Expand Down Expand Up @@ -71,25 +59,25 @@ def test_get_metrics_vector(self):
key=itemgetter(1),
) == sorted(vector, key=itemgetter(1))

def testAssertMetricEquals(self):
"""Tests assertMetricEquals."""
def test_assert_metric_equal(self):
"""Tests assert_metric_equal."""
# First we test that a scalar metric can be tested.
self.test_case.assertMetricEquals(42, "some_gauge", registry=self.registry)
assert self.test_case.passes is True
assert_metric_equal(42, "some_gauge", registry=self.registry)

assert_metric_not_equal(43, "some_gauge", registry=self.registry)

# Here we test that assertMetricEquals fails on nonexistent gauges.
# Here we test that assert_metric_equal fails on nonexistent gauges.
assert_metric_not_equal(42, "some_nonexistent_gauge", registry=self.registry)

# Here we test that labelled metrics can be tested.
self.test_case.assertMetricEquals(
assert_metric_equal(
1,
"some_labelled_gauge",
registry=self.registry,
labelred="pink",
labelblue="indigo",
)
assert self.test_case.passes is True

assert_metric_not_equal(
1,
"some_labelled_gauge",
Expand Down
32 changes: 16 additions & 16 deletions django_prometheus/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@
"""


class PrometheusTestCaseMixin:
"""A collection of utilities that make it easier to write test cases
that interact with metrics.
"""
"""A collection of utilities that make it easier to write test cases
that interact with metrics.
"""

def assertMetricEquals(self, expected_value, metric_name, registry=REGISTRY, **labels):
"""Asserts that metric_name{**labels} == expected_value."""
value = get_metric(metric_name, registry=registry, **labels)
assert_err = METRIC_EQUALS_ERR_EXPLANATION % (
metric_name,
format_labels(labels),
value,
expected_value,
metric_name,
format_vector(get_metrics_vector(metric_name)),
)
assert expected_value == value, assert_err

def assert_metric_equal(expected_value, metric_name, registry=REGISTRY, **labels):
"""Asserts that metric_name{**labels} == expected_value."""
value = get_metric(metric_name, registry=registry, **labels)
assert_err = METRIC_EQUALS_ERR_EXPLANATION % (
metric_name,
format_labels(labels),
value,
expected_value,
metric_name,
format_vector(get_metrics_vector(metric_name)),
)
assert expected_value == value, assert_err


def assert_metric_diff(frozen_registry, expected_diff, metric_name, registry=REGISTRY, **labels):
Expand Down

0 comments on commit a04eabc

Please sign in to comment.