Caco
Caco

Reputation: 1654

Testing with Django: how to display all characters when using assertEqual with json object?

I'm testing django-rest-framework. When I POST some content in json format and want to test if it was posted correctly I try to get the object posted with

resp_get = self.client.get(self.url)

and test with assertEqual (json format):

self.assertEqual(
            json.loads(resp_get.content.decode('utf8')),
            [
                {
                    'id': researcher.id,
                    'first_name': researcher.first_name,
                    'surname': researcher.surname,
                    'email': researcher.email,
                    'studies': [],
                    'nes_id': researcher.nes_id,
                    'owner': researcher.owner.username
                }
            ]
        )

As comparing results false, I get an AssertionError with following message

AssertionError: {'id': 1, 'first_name': 'João Maria', 'su[102 chars]ab1'} != [{'id': 1, 'first_name': 'João Maria', 's[104 chars]b1'}]

I'd like to know if is someway possible to get entire json object compared in both sides, as AssertionError returns the object compacted: su[102 chars]ab1, and s[104 chars]b1.

Upvotes: 1

Views: 158

Answers (1)

aliva
aliva

Reputation: 5720

use assertDictEqual

self.assertDictEqual(item1, item2)

Upvotes: 2

Related Questions