Repository: django/django
File: tests/test_utils/tests.py (Python)

Code before:

    def test_failure(self):
        with self.assertRaises(TypeError):
            with CaptureQueriesContext(connection):
                raise TypeError

    def test_with_client(self):
        with CaptureQueriesContext(connection) as captured_queries:
            self.client.get("/test_utils/get_person/%s/" % self.person_pk)
        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.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.assertEqual(len(captured_queries), 2)
        self.assertIn(self.person_pk, captured_queries[0]["sql"])
        self.assertIn(self.person_pk, captured_queries[1]["sql"])


@override_settings(ROOT_URLCONF="test_utils.urls")
class AssertNumQueriesContextManagerTests(TestCase):
    def test_simple(self):
        with self.assertNumQueries(0):
            pass

        with self.assertNumQueries(1):
            Person.objects.count()

        with self.assertNumQueries(2):
            Person.objects.count()
            Person.objects.count()

    def test_failure(self):
        msg = "1 != 2 : 1 queries executed, 2 expected\nCaptured queries were:\n1."
        with self.assertRaisesMessage(AssertionError, msg):
            with self.assertNumQueries(2):
                Person.objects.count()

        with self.assertRaises(TypeError):
            with self.assertNumQueries(4000):
                raise TypeError

    def test_with_client(self):
        person = Person.objects.create(name="test")


Review comment (suggestion):
For me, when I revert the code changes this test and the other test (`AssertNumQueriesContextManagerTests.test_with_client_nested`) still pass :thinking: 

Code after:
        self.assertEqual(2, len(captured_queries))

    def test_failure(self):
        with self.assertRaises(TypeError):
            with CaptureQueriesContext(connection):
                raise TypeError

    def test_with_client(self):
        with CaptureQueriesContext(connection) as captured_queries:
            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(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(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"])

    def test_with_client_nested(self):
        with CaptureQueriesContext(connection) as captured_queries:
            Person.objects.count()
            with CaptureQueriesContext(connection):
                pass
            self.client.get(self.url)
        self.assertEqual(2, len(captured_queries))


@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

        with self.assertNumQueries(1):
            Person.objects.count()

        with self.assertNumQueries(2):
            Person.objects.count()
            Person.objects.count()