@@ -386,6 +386,7 @@ class CaptureQueriesContextManagerTests(TestCase):
386386 @classmethod
387387 def setUpTestData (cls ):
388388 cls .person_pk = str (Person .objects .create (name = "test" ).pk )
389+ cls .url = f"/test_utils/get_person/{ cls .person_pk } /"
389390
390391 def test_simple (self ):
391392 with CaptureQueriesContext (connection ) as captured_queries :
@@ -418,25 +419,38 @@ def test_failure(self):
418419
419420 def test_with_client (self ):
420421 with CaptureQueriesContext (connection ) as captured_queries :
421- self .client .get ("/test_utils/get_person/%s/" % self .person_pk )
422+ self .client .get (self .url )
422423 self .assertEqual (len (captured_queries ), 1 )
423424 self .assertIn (self .person_pk , captured_queries [0 ]["sql" ])
424425
425426 with CaptureQueriesContext (connection ) as captured_queries :
426- self .client .get ("/test_utils/get_person/%s/" % self .person_pk )
427+ self .client .get (self .url )
427428 self .assertEqual (len (captured_queries ), 1 )
428429 self .assertIn (self .person_pk , captured_queries [0 ]["sql" ])
429430
430431 with CaptureQueriesContext (connection ) as captured_queries :
431- self .client .get ("/test_utils/get_person/%s/" % self .person_pk )
432- self .client .get ("/test_utils/get_person/%s/" % self .person_pk )
432+ self .client .get (self .url )
433+ self .client .get (self .url )
433434 self .assertEqual (len (captured_queries ), 2 )
434435 self .assertIn (self .person_pk , captured_queries [0 ]["sql" ])
435436 self .assertIn (self .person_pk , captured_queries [1 ]["sql" ])
436437
438+ def test_with_client_nested (self ):
439+ with CaptureQueriesContext (connection ) as captured_queries :
440+ Person .objects .count ()
441+ with CaptureQueriesContext (connection ):
442+ pass
443+ self .client .get (self .url )
444+ self .assertEqual (2 , len (captured_queries ))
445+
437446
438447@override_settings (ROOT_URLCONF = "test_utils.urls" )
439448class AssertNumQueriesContextManagerTests (TestCase ):
449+ @classmethod
450+ def setUpTestData (cls ):
451+ cls .person_pk = str (Person .objects .create (name = "test" ).pk )
452+ cls .url = f"/test_utils/get_person/{ cls .person_pk } /"
453+
440454 def test_simple (self ):
441455 with self .assertNumQueries (0 ):
442456 pass
@@ -459,17 +473,22 @@ def test_failure(self):
459473 raise TypeError
460474
461475 def test_with_client (self ):
462- person = Person .objects .create (name = "test" )
463-
464476 with self .assertNumQueries (1 ):
465- self .client .get ("/test_utils/get_person/%s/" % person . pk )
477+ self .client .get (self . url )
466478
467479 with self .assertNumQueries (1 ):
468- self .client .get ("/test_utils/get_person/%s/" % person . pk )
480+ self .client .get (self . url )
469481
470482 with self .assertNumQueries (2 ):
471- self .client .get ("/test_utils/get_person/%s/" % person .pk )
472- self .client .get ("/test_utils/get_person/%s/" % person .pk )
483+ self .client .get (self .url )
484+ self .client .get (self .url )
485+
486+ def test_with_client_nested (self ):
487+ with self .assertNumQueries (2 ):
488+ Person .objects .count ()
489+ with self .assertNumQueries (0 ):
490+ pass
491+ self .client .get (self .url )
473492
474493
475494@override_settings (ROOT_URLCONF = "test_utils.urls" )
0 commit comments