1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
import pytest
from django.conf import settings
from django.db import connections
from django_prometheus.testutils import (
assert_metric_compare,
assert_metric_diff,
assert_metric_equal,
get_metric,
save_registry,
)
# @pytest.fixture(autouse=True)
# def enable_db_access_for_all_tests(db):
# pass
@pytest.mark.django_db(databases=list(settings.DATABASES.keys()))
class BaseDBTest:
pass
@pytest.mark.skipif(connections["test_db_1"].vendor != "sqlite", reason="Skipped unless test_db_1 uses sqlite")
class TestDbMetrics(BaseDBTest):
"""Test django_prometheus.db metrics.
Note regarding the values of metrics: many tests interact with the
database, and the test runner itself does. As such, tests that
require that a metric has a specific value are at best very
fragile. Consider asserting that the value exceeds a certain
threshold, or check by how much it increased during the test.
"""
def test_config_has_expected_databases(self):
"""Not a real unit test: ensures that testapp.settings contains the
databases this test expects.
"""
assert "default" in connections.databases.keys()
assert "test_db_1" in connections.databases.keys()
assert "test_db_2" in connections.databases.keys()
def test_counters(self):
cursor_db1 = connections["test_db_1"].cursor()
cursor_db2 = connections["test_db_2"].cursor()
cursor_db1.execute("SELECT 1")
for _ in range(200):
cursor_db2.execute("SELECT 2")
cursor_db1.execute("SELECT 3")
try:
cursor_db1.execute("this is clearly not valid SQL")
except Exception:
pass
assert_metric_equal(
1,
"django_db_errors_total",
alias="test_db_1",
vendor="sqlite",
type="OperationalError",
)
assert get_metric("django_db_execute_total", alias="test_db_1", vendor="sqlite") > 0
assert get_metric("django_db_execute_total", alias="test_db_2", vendor="sqlite") >= 200
def test_histograms(self):
cursor_db1 = connections["test_db_1"].cursor()
cursor_db2 = connections["test_db_2"].cursor()
cursor_db1.execute("SELECT 1")
for _ in range(200):
cursor_db2.execute("SELECT 2")
assert (
get_metric(
"django_db_query_duration_seconds_count",
alias="test_db_1",
vendor="sqlite",
)
> 0
)
assert (
get_metric(
"django_db_query_duration_seconds_count",
alias="test_db_2",
vendor="sqlite",
)
>= 200
)
def test_execute_many(self):
registry = save_registry()
cursor_db1 = connections["test_db_1"].cursor()
cursor_db1.executemany(
"INSERT INTO testapp_lawn(location) VALUES (?)",
[("Paris",), ("New York",), ("Berlin",), ("San Francisco",)],
)
assert_metric_diff(
registry,
4,
"django_db_execute_many_total",
alias="test_db_1",
vendor="sqlite",
)
@pytest.mark.skipif("postgresql" not in connections, reason="Skipped unless postgresql database is enabled")
class TestPostgresDbMetrics(BaseDBTest):
"""Test django_prometheus.db metrics for postgres backend.
Note regarding the values of metrics: many tests interact with the
database, and the test runner itself does. As such, tests that
require that a metric has a specific value are at best very
fragile. Consider asserting that the value exceeds a certain
threshold, or check by how much it increased during the test.
"""
def test_counters(self):
registry = save_registry()
cursor = connections["postgresql"].cursor()
for _ in range(20):
cursor.execute("SELECT 1")
assert_metric_compare(
registry,
lambda a, b: a + 20 <= b < a + 25,
"django_db_execute_total",
alias="postgresql",
vendor="postgresql",
)
@pytest.mark.skipif("mysql" not in connections, reason="Skipped unless mysql database is enabled")
class TestMysDbMetrics(BaseDBTest):
"""Test django_prometheus.db metrics for mys backend.
Note regarding the values of metrics: many tests interact with the
database, and the test runner itself does. As such, tests that
require that a metric has a specific value are at best very
fragile. Consider asserting that the value exceeds a certain
threshold, or check by how much it increased during the test.
"""
def test_counters(self):
registry = save_registry()
cursor = connections["mysql"].cursor()
for _ in range(20):
cursor.execute("SELECT 1")
assert_metric_compare(
registry,
lambda a, b: a + 20 <= b < a + 25,
"django_db_execute_total",
alias="mysql",
vendor="mysql",
)
@pytest.mark.skipif("postgis" not in connections, reason="Skipped unless postgis database is enabled")
class TestPostgisDbMetrics(BaseDBTest):
"""Test django_prometheus.db metrics for postgis backend.
Note regarding the values of metrics: many tests interact with the
database, and the test runner itself does. As such, tests that
require that a metric has a specific value are at best very
fragile. Consider asserting that the value exceeds a certain
threshold, or check by how much it increased during the test.
"""
def test_counters(self):
r = save_registry()
cursor = connections["postgis"].cursor()
for _ in range(20):
cursor.execute("SELECT 1")
assert_metric_compare(
r,
lambda a, b: a + 20 <= b < a + 25,
"django_db_execute_total",
alias="postgis",
vendor="postgresql",
)
@pytest.mark.skipif("spatialite" not in connections, reason="Skipped unless spatialite database is enabled")
class TestSpatialiteDbMetrics(BaseDBTest):
"""Test django_prometheus.db metrics for spatialite backend.
Note regarding the values of metrics: many tests interact with the
database, and the test runner itself does. As such, tests that
require that a metric has a specific value are at best very
fragile. Consider asserting that the value exceeds a certain
threshold, or check by how much it increased during the test.
"""
def test_counters(self):
r = save_registry()
connection = connections["spatialite"]
# Make sure the extension is loaded and geospatial tables are created
connection.prepare_database()
cursor = connection.cursor()
for _ in range(20):
cursor.execute("SELECT 1")
assert_metric_compare(
r,
lambda a, b: a + 20 <= b < a + 25,
"django_db_execute_total",
alias="spatialite",
vendor="sqlite",
)
|