Strange failing test

17 views
Skip to first unread message

Gergely Polonkai

unread,
Aug 24, 2016, 6:11:18 AM8/24/16
to Django users
Hello,

I have a test that fetches some JSON data from my API and compares it with the expected result, which is generated like this:

        expected_json = {
            'status': 'ok',
            'invalid-fields': {},
            'updated-fields': cloned_build.groupings.get(group=group1)
            .field_values
            .exclude(template=approvable_template)
            .values_list('id', flat=True)[:],
        }

At the end, I get this error:

AssertionError: {u'status': u'ok', u'invalid-fields': {}, u'updated-fields': [13, 14, 15]} != {u'status': u'ok', u'invalid-fields': {}, u'updated-fields': [13, 14, 15]}
  {u'invalid-fields': {}, u'status': u'ok', u'updated-fields': [13, 14, 15]}


It seems the two dicts are equal, but assertEquals thinks they are not. Am I missing something here?

Best,
Gergely

Derek

unread,
Aug 24, 2016, 10:03:26 AM8/24/16
to Django users
Assuming that the long function you have embedded inside your dictionary simply generates a list i.e.
   cloned_build.groupings.get(
group=group1)
   .field_values
   .exclude(template=approvable_template)
   .values_list('id', flat=True)[:]

results in:
   [1,2,3]

Then some examples of possible tests:


expected_json = {
            'status': 'ok',
            'invalid-fields': {},
            'updated-fields': [1,2,3]
        }
actual_json = {
            'invalid-fields': {},
            'status': 'ok',
            'updated-fields': [1,2,3]
        }
invalid_json = {
            'invalid-fields': {},
            'status': 'ok',
            'updated-fields': [2,3]
        }

assert cmp(expected_json, actual_json) == 0  # same
assert cmp(expected_json, invalid_json) == -1  # different

Hope this helps.  For more background & discussion, see:

http://stackoverflow.com/questions/4527942/comparing-two-dictionaries-in-python

Gergely Polonkai

unread,
Aug 24, 2016, 10:27:37 AM8/24/16
to Django users
It just turned out that value_list(…, flat=True) returns a QuerySet, not a list. So expected_json couldn’t be equal to the actual result. It took a bit of debugging as repr() displays [1, 2, 3], not QuerySet(1, 2, 3) or something.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ac51f934-5320-45c1-8dc8-aca658fde881%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tim Graham

unread,
Aug 24, 2016, 10:59:16 AM8/24/16
to Django users

Gergely Polonkai

unread,
Aug 24, 2016, 11:04:13 AM8/24/16
to Django users
That’s indeed great news. Too bad I’m rooted with 1.9 for a while, as 1.10 doesn’t play nice with Python 2.7.8. I hope I can upgrade both Python and Django to as recent as possible soon. Thank you for the info anyways!

Reply all
Reply to author
Forward
0 new messages