#36308: Parallel testing fails reporting unit test combining subTest and
assertNumQueries
-------------------------------------+-------------------------------------
     Reporter:  Jean Bvt             |                    Owner:  (none)
         Type:  Bug                  |                   Status:  closed
    Component:  Testing framework    |                  Version:  5.2
     Severity:  Normal               |               Resolution:  needsinfo
     Keywords:  test assertNumQuery  |             Triage Stage:
  reporting                          |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Comment (by Jean Bvt):

 Replying to [comment:2 Sarah Boyce]:
 > I cannot reproduce, but I think you would need to share your view here.
 > Does the view raise an error on it's own?
 >
 > I tried:
 > {{{#!diff
 > diff --git a/tests/test_utils/tests.py b/tests/test_utils/tests.py
 > index 16692500e3..2b2c3cf2bf 100644
 > --- a/tests/test_utils/tests.py
 > +++ b/tests/test_utils/tests.py
 > @@ -237,6 +237,12 @@ class AssertNumQueriesTests(TestCase):
 >
 >          self.assertNumQueries(2, test_func)
 >
 > +    def test_assert_num_queries_with_client_and_subtest(self):
 > +        person = Person.objects.create(name="test")
 > +        with self.subTest("sub test"):
 > +            with self.assertNumQueries(2):
 > +                self.client.get("/test_utils/get_person/%s/" %
 person.pk)
 > +
 >
 >  class AssertNumQueriesUponConnectionTests(TransactionTestCase):
 > }}}
 > And running `./runtests.py test_utils --parallel 2` but this failed in
 an expected manner.
 >

 The view does not raise an exception, the raised exception contains a
 message related to AssertNumQueries:

 >  (<class 'AssertionError'>, AssertionError('3 != 2 : 3 queries executed,
 2 expected\nCaptured queries were:\n1. SELECT "app_tokenapi"."id",
 "app_tokenapi"."user_id", "app_tokenapi"."key", "app_tokenapi"."name",
 "app_tokenapi"."scope_level", "app_tokenapi"."created_at",
 "app_tokenapi"."expired_at", "app_tokenapi"."expire [...]

 making me believe its the reporting that failed.

 Anyway, here is my view (and my serializer since I am using DRF)
 {{{
 @extend_schema(tags=["entite", "v2"])
 class EntitesView(ListAPIView):
     permission_classes = [AllowAny]
     queryset =
 Entite.objects.prefetch_related("parent").select_related("type")
     serializer_class = EntiteSerializer
     logger_instance = logging.getLogger("entites.entites")
     filter_backends = (DjangoFilterBackend,)
     filterset_class = EntitesFilter
 }}}
 {{{
 from rest_framework.serializers import ModelSerializer
 from rest_framework.relations import SlugRelatedField

 from my_models.models.entite import Entite, EntiteType
 from rest_framework import serializers


 class EntiteSerializer(ModelSerializer):
     parent = SlugRelatedField(
         slug_field="uness_id",
         queryset=Entite.objects.all(),
         error_messages={
             "does_not_exist": "L'identifiant {value} ne correspond à
 aucune entité."
         },
         required=False,
     )

     type = SlugRelatedField(
         slug_field="name",
         queryset=EntiteType.objects.all(),
         error_messages={
             "does_not_exist": "Le type {value} ne correspond à aucun type
 d'entité."
         },
     )
     allow_externe_id = serializers.SerializerMethodField()

     def get_allow_externe_id(self, obj):
         return obj.allow_externe_id

     class Meta:
         model = Entite
         fields = (
             "allow_externe_id",
             "code",
             "json",
             "libelle_court",
             "libelle",
             "parent",
             "type",
             "uness_id",
             "id",
         )
         read_only_fields = [
             "code",
             "libelle_court",
             "libelle",
             "parent",
             "type",
             "uness_id",
             "id",
         ]

 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36308#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/01070196196ab79e-f4aac01d-55d9-4930-b44a-a210e610a4f6-000000%40eu-central-1.amazonses.com.

Reply via email to