# Path: django_prometheus/testutils.py
# class PrometheusTestCaseMixin:
#     """A collection of utilities that make it easier to write test cases
#     that interact with metrics.
#     """
# 
#     def saveRegistry(self, registry=REGISTRY):
#         """Freezes a registry. This lets a user test changes to a metric
#         instead of testing the absolute value. A typical use case looks like:
# 
#           registry = self.saveRegistry()
#           doStuff()
#           self.assertMetricDiff(registry, 1, 'stuff_done_total')
#         """
#         return copy.deepcopy(list(registry.collect()))
# 
#     def getMetricFromFrozenRegistry(self, metric_name, frozen_registry, **labels):
#         """Gets a single metric from a frozen registry."""
#         for metric in frozen_registry:
#             for sample in metric.samples:
#                 if sample[0] == metric_name and sample[1] == labels:
#                     return sample[2]
# 
#     def getMetric(self, metric_name, registry=REGISTRY, **labels):
#         """Gets a single metric."""
#         return self.getMetricFromFrozenRegistry(
#             metric_name, registry.collect(), **labels
#         )
# 
#     def getMetricVectorFromFrozenRegistry(self, metric_name, frozen_registry):
#         """Like getMetricVector, but from a frozen registry."""
#         output = []
#         for metric in frozen_registry:
#             for sample in metric.samples:
#                 if sample[0] == metric_name:
#                     output.append((sample[1], sample[2]))
#         return output
# 
#     def getMetricVector(self, metric_name, registry=REGISTRY):
#         """Returns the values for all labels of a given metric.
# 
#         The result is returned as a list of (labels, value) tuples,
#         where `labels` is a dict.
# 
#         This is quite a hack since it relies on the internal
#         representation of the prometheus_client, and it should
#         probably be provided as a function there instead.
#         """
#         return self.getMetricVectorFromFrozenRegistry(metric_name, registry.collect())
# 
#     def formatLabels(self, labels):
#         """Format a set of labels to Prometheus representation.
# 
#         In:
#           {'method': 'GET', 'port': '80'}
# 
#         Out:
#           '{method="GET",port="80"}'
#         """
#         return "{%s}" % ",".join([f'{k}="{v}"' for k, v in labels.items()])
# 
#     def formatVector(self, vector):
#         """Formats a list of (labels, value) where labels is a dict into a
#         human-readable representation.
#         """
#         return "\n".join(
#             [
#                 "{} = {}".format(self.formatLabels(labels), value)
#                 for labels, value in vector
#             ]
#         )
# 
#     def assertMetricEquals(
#         self, expected_value, metric_name, registry=REGISTRY, **labels
#     ):
#         """Asserts that metric_name{**labels} == expected_value."""
#         value = self.getMetric(metric_name, registry=registry, **labels)
#         self.assertEqual(
#             expected_value,
#             value,
#             METRIC_EQUALS_ERR_EXPLANATION
#             % (
#                 metric_name,
#                 self.formatLabels(labels),
#                 value,
#                 expected_value,
#                 metric_name,
#                 self.formatVector(self.getMetricVector(metric_name)),
#             ),
#         )
# 
#     def assertMetricDiff(
#         self, frozen_registry, expected_diff, metric_name, registry=REGISTRY, **labels
#     ):
#         """Asserts that metric_name{**labels} changed by expected_diff between
#         the frozen registry and now. A frozen registry can be obtained
#         by calling saveRegistry, typically at the beginning of a test
#         case.
#         """
#         saved_value = self.getMetricFromFrozenRegistry(
#             metric_name, frozen_registry, **labels
#         )
#         current_value = self.getMetric(metric_name, registry=registry, **labels)
#         assert current_value is not None, METRIC_DIFF_ERR_NONE_EXPLANATION % (
#             metric_name,
#             self.formatLabels(labels),
#             saved_value,
#             current_value,
#         )
#         diff = current_value - (saved_value or 0.0)
#         self.assertEqual(
#             expected_diff,
#             diff,
#             METRIC_DIFF_ERR_EXPLANATION
#             % (
#                 metric_name,
#                 self.formatLabels(labels),
#                 diff,
#                 expected_diff,
#                 saved_value,
#                 current_value,
#             ),
#         )
# 
#     def assertMetricCompare(
#         self, frozen_registry, predicate, metric_name, registry=REGISTRY, **labels
#     ):
#         """Asserts that metric_name{**labels} changed according to a provided
#         predicate function between the frozen registry and now. A
#         frozen registry can be obtained by calling saveRegistry,
#         typically at the beginning of a test case.
#         """
#         saved_value = self.getMetricFromFrozenRegistry(
#             metric_name, frozen_registry, **labels
#         )
#         current_value = self.getMetric(metric_name, registry=registry, **labels)
#         assert current_value is not None, METRIC_DIFF_ERR_NONE_EXPLANATION % (
#             metric_name,
#             self.formatLabels(labels),
#             saved_value,
#             current_value,
#         )
#         assert (
#             predicate(saved_value, current_value) is True
#         ), METRIC_COMPARE_ERR_EXPLANATION % (
#             metric_name,
#             self.formatLabels(labels),
#             saved_value,
#             current_value,
#         )


from django.test import TestCase
from testapp.models import Dog, Lawn
from django_prometheus.testutils import PrometheusTestCaseMixin



def M(metric_name):
    """Make a full metric name from a short metric name.

    This is just intended to help keep the lines shorter in test
    cases.
    """
    return "django_model_%s" % metric_name



# Path: django_prometheus/testutils.py
# class PrometheusTestCaseMixin:
#     """A collection of utilities that make it easier to write test cases
#     that interact with metrics.
#     """
# 
#     def saveRegistry(self, registry=REGISTRY):
#         """Freezes a registry. This lets a user test changes to a metric
#         instead of testing the absolute value. A typical use case looks like:
# 
#           registry = self.saveRegistry()
#           doStuff()
#           self.assertMetricDiff(registry, 1, 'stuff_done_total')
#         """
#         return copy.deepcopy(list(registry.collect()))
# 
#     def getMetricFromFrozenRegistry(self, metric_name, frozen_registry, **labels):
#         """Gets a single metric from a frozen registry."""
#         for metric in frozen_registry:
#             for sample in metric.samples:
#                 if sample[0] == metric_name and sample[1] == labels:
#                     return sample[2]
# 
#     def getMetric(self, metric_name, registry=REGISTRY, **labels):
#         """Gets a single metric."""
#         return self.getMetricFromFrozenRegistry(
#             metric_name, registry.collect(), **labels
#         )
# 
#     def getMetricVectorFromFrozenRegistry(self, metric_name, frozen_registry):
#         """Like getMetricVector, but from a frozen registry."""
#         output = []
#         for metric in frozen_registry:
#             for sample in metric.samples:
#                 if sample[0] == metric_name:
#                     output.append((sample[1], sample[2]))
#         return output
# 
#     def getMetricVector(self, metric_name, registry=REGISTRY):
#         """Returns the values for all labels of a given metric.
# 
#         The result is returned as a list of (labels, value) tuples,
#         where `labels` is a dict.
# 
#         This is quite a hack since it relies on the internal
#         representation of the prometheus_client, and it should
#         probably be provided as a function there instead.
#         """
#         return self.getMetricVectorFromFrozenRegistry(metric_name, registry.collect())
# 
#     def formatLabels(self, labels):
#         """Format a set of labels to Prometheus representation.
# 
#         In:
#           {'method': 'GET', 'port': '80'}
# 
#         Out:
#           '{method="GET",port="80"}'
#         """
#         return "{%s}" % ",".join([f'{k}="{v}"' for k, v in labels.items()])
# 
#     def formatVector(self, vector):
#         """Formats a list of (labels, value) where labels is a dict into a
#         human-readable representation.
#         """
#         return "\n".join(
#             [
#                 "{} = {}".format(self.formatLabels(labels), value)
#                 for labels, value in vector
#             ]
#         )
# 
#     def assertMetricEquals(
#         self, expected_value, metric_name, registry=REGISTRY, **labels
#     ):
#         """Asserts that metric_name{**labels} == expected_value."""
#         value = self.getMetric(metric_name, registry=registry, **labels)
#         self.assertEqual(
#             expected_value,
#             value,
#             METRIC_EQUALS_ERR_EXPLANATION
#             % (
#                 metric_name,
#                 self.formatLabels(labels),
#                 value,
#                 expected_value,
#                 metric_name,
#                 self.formatVector(self.getMetricVector(metric_name)),
#             ),
#         )
# 
#     def assertMetricDiff(
#         self, frozen_registry, expected_diff, metric_name, registry=REGISTRY, **labels
#     ):
#         """Asserts that metric_name{**labels} changed by expected_diff between
#         the frozen registry and now. A frozen registry can be obtained
#         by calling saveRegistry, typically at the beginning of a test
#         case.
#         """
#         saved_value = self.getMetricFromFrozenRegistry(
#             metric_name, frozen_registry, **labels
#         )
#         current_value = self.getMetric(metric_name, registry=registry, **labels)
#         assert current_value is not None, METRIC_DIFF_ERR_NONE_EXPLANATION % (
#             metric_name,
#             self.formatLabels(labels),
#             saved_value,
#             current_value,
#         )
#         diff = current_value - (saved_value or 0.0)
#         self.assertEqual(
#             expected_diff,
#             diff,
#             METRIC_DIFF_ERR_EXPLANATION
#             % (
#                 metric_name,
#                 self.formatLabels(labels),
#                 diff,
#                 expected_diff,
#                 saved_value,
#                 current_value,
#             ),
#         )
# 
#     def assertMetricCompare(
#         self, frozen_registry, predicate, metric_name, registry=REGISTRY, **labels
#     ):
#         """Asserts that metric_name{**labels} changed according to a provided
#         predicate function between the frozen registry and now. A
#         frozen registry can be obtained by calling saveRegistry,
#         typically at the beginning of a test case.
#         """
#         saved_value = self.getMetricFromFrozenRegistry(
#             metric_name, frozen_registry, **labels
#         )
#         current_value = self.getMetric(metric_name, registry=registry, **labels)
#         assert current_value is not None, METRIC_DIFF_ERR_NONE_EXPLANATION % (
#             metric_name,
#             self.formatLabels(labels),
#             saved_value,
#             current_value,
#         )
#         assert (
#             predicate(saved_value, current_value) is True
#         ), METRIC_COMPARE_ERR_EXPLANATION % (
#             metric_name,
#             self.formatLabels(labels),
#             saved_value,
#             current_value,
#         )


# Path: django_prometheus/tests/end2end/testapp/test_models.py
from django.test import TestCase
from testapp.models import Dog, Lawn
from django_prometheus.testutils import PrometheusTestCaseMixin




def M(metric_name):
    """Make a full metric name from a short metric name.

    This is just intended to help keep the lines shorter in test
    cases.
    """
    return "django_model_%s" % metric_name



class TestModelMetrics(PrometheusTestCaseMixin, TestCase):