1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Elegant way to add a list view test case in drf

Discussão em 'Python' iniciado por Stack, Outubro 7, 2024 às 05:12.

  1. Stack

    Stack Membro Participativo

    I have one concern in writing the test code for list view.

    For DRF's list view, it responds to multiple data.

    What should I do if I assume that the response.data object is a model with many fields?

    response.data --> [{"id": 1, "field1": "xxx", "field2": "yyy"....}, {"id": 2, ...}, ...]


    Too much input is required to compare with static data when a returns a lot of field data.

    def test_xx(self):
    self.assertEqual(response.data, [{"id": 1, "field1": "xxx", ..., "field12": "ddd", ...}, ...])


    Currently, the way I came up with is as follows.

    I created an example model called Dog.

    class Dog(models.Model):
    ...
    hungry = models.BooleanField(default=False)
    hurt = models.BooleanField(default=False)


    As an example, let's say there is a class called Dog and there is logic in View to get a list of hungry dogs. I'm going to make a test about the logic of get a list of hungry dogs.

    class ...
    def test_dog_hungry_list(self):
    response = client.get("/api/dog/hungry/")
    expected_response = Dog.objects.filter(hungry=True)
    self.assertEqual(response.data, expected_response)


    The two values that I want to compare with assertEqual, response and expected_response, are of different types.

    • response --> Response
    • expected_response --> Queryset

    In the case of response.data, Serializer is applied, so when it's in dict form, I want to compare it with Queryset type expected_response.

    I thought of two ways as follows.

    1. Apply serializer to Queryset for comparison. --> (Serializer(expected_response, many=True))
    2. Compare the primary key values.

    number 2 example.

    expected_response = Dog.objects.filter(hungry=True).values("id")
    self.assertEqual(
    [data["id"] for data in response.data],
    [data["id"] for data in expected_response]
    )


    But I'm not sure if the two methods are good in comparison.

    Is there a way to make the data smart to compare when the test code returns two or three data in the form of a large dict?

    Continue reading...

Compartilhe esta Página