diff options
| author | Adam Johnson <me@adamj.eu> | 2025-06-03 23:11:05 +0100 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2025-06-05 13:49:39 +0200 |
| commit | 9a3f3b84999878e22021c92f9102707ba2648209 (patch) | |
| tree | 3616e05d410bc7c17f8ec13c58f516d34200a7a2 /tests/test_utils | |
| parent | 68c9f7e0b79168007e6ba0139fd315d7c44ca8c9 (diff) | |
Refs #36435 -- Tidied tests for CaptureQueriesContext and assertNumQueries.
Avoided repeatedly templating the URL and hoisted Person creation to
setUpTestData in AssertNumQueriesContextManagerTests to repeat the same
pattern as in CaptureQueriesContextManagerTests.
Diffstat (limited to 'tests/test_utils')
| -rw-r--r-- | tests/test_utils/tests.py | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/tests/test_utils/tests.py b/tests/test_utils/tests.py index fd0c47a42c..ae123920e0 100644 --- a/tests/test_utils/tests.py +++ b/tests/test_utils/tests.py @@ -386,6 +386,7 @@ class CaptureQueriesContextManagerTests(TestCase): @classmethod def setUpTestData(cls): cls.person_pk = str(Person.objects.create(name="test").pk) + cls.url = f"/test_utils/get_person/{cls.person_pk}/" def test_simple(self): with CaptureQueriesContext(connection) as captured_queries: @@ -418,18 +419,18 @@ class CaptureQueriesContextManagerTests(TestCase): def test_with_client(self): with CaptureQueriesContext(connection) as captured_queries: - self.client.get("/test_utils/get_person/%s/" % self.person_pk) + self.client.get(self.url) self.assertEqual(len(captured_queries), 1) self.assertIn(self.person_pk, captured_queries[0]["sql"]) with CaptureQueriesContext(connection) as captured_queries: - self.client.get("/test_utils/get_person/%s/" % self.person_pk) + self.client.get(self.url) self.assertEqual(len(captured_queries), 1) self.assertIn(self.person_pk, captured_queries[0]["sql"]) with CaptureQueriesContext(connection) as captured_queries: - self.client.get("/test_utils/get_person/%s/" % self.person_pk) - self.client.get("/test_utils/get_person/%s/" % self.person_pk) + self.client.get(self.url) + self.client.get(self.url) self.assertEqual(len(captured_queries), 2) self.assertIn(self.person_pk, captured_queries[0]["sql"]) self.assertIn(self.person_pk, captured_queries[1]["sql"]) @@ -437,6 +438,11 @@ class CaptureQueriesContextManagerTests(TestCase): @override_settings(ROOT_URLCONF="test_utils.urls") class AssertNumQueriesContextManagerTests(TestCase): + @classmethod + def setUpTestData(cls): + cls.person_pk = str(Person.objects.create(name="test").pk) + cls.url = f"/test_utils/get_person/{cls.person_pk}/" + def test_simple(self): with self.assertNumQueries(0): pass @@ -459,17 +465,15 @@ class AssertNumQueriesContextManagerTests(TestCase): raise TypeError def test_with_client(self): - person = Person.objects.create(name="test") - with self.assertNumQueries(1): - self.client.get("/test_utils/get_person/%s/" % person.pk) + self.client.get(self.url) with self.assertNumQueries(1): - self.client.get("/test_utils/get_person/%s/" % person.pk) + self.client.get(self.url) with self.assertNumQueries(2): - self.client.get("/test_utils/get_person/%s/" % person.pk) - self.client.get("/test_utils/get_person/%s/" % person.pk) + self.client.get(self.url) + self.client.get(self.url) @override_settings(ROOT_URLCONF="test_utils.urls") |
