OrderedDict in Django tests

I have such Django tests

queryset = SomeModel.objects.all()
response = self.client.get("some_url")
serializer = SomeSerializer(
    instance=list(queryset[:10]), many=True
)
self.assertEqual(
    serializer.data,
    response.json()["results"],
)

And got such error

E   AssertionError: [OrderedDict([('uuid', 'e58fecc0-d559-41af[23258 chars]))])] != [{'uuid': 'e58fecc0-d559-41af-993b-9519309[17732 chars]0.0}]

How can be OrderedDict be removed or transformed?

Obviously:

self.assertEqual(dict(serializer.data), response.json()["results"], msg="Please dont forget an error message")

But it is not so obviously:

AssretEqual falls if the compared dictionaries are with different order. And in your case - you should check the value of uuid, not the dictionaries themselves.

self.assertEqual(serializer.data['uuid'], response.json()["results"]['uuid'], msg="Please dont forget an error message")

Answer was even simpler. Instead of response.json() response.data should be used, because response.data returns same as serializer.data

The final result will be

queryset = SomeModel.objects.all()
response = self.client.get("some_url")
serializer = SomeSerializer(
    instance=list(queryset[:10]), many=True
)
self.assertEqual(
    serializer.data,
    response.data["results"],
)

And test passes.

Back to Top