How do I compare a response.data serialized object using Python / Django when unittesting?
Question: This is my view:

    
    
    class ChatViewSet(viewsets.ModelViewSet):
        serializer_class = ChatSerializer
    
        def get_queryset(self):
            return Chat.objects.filter(users__in=[self.request.user])
    

This is my Serializer:

    
    
    class ChatSerializer(serializers.ModelSerializer):
    
        class Meta:
            model = Chat
    
        def validate_users(self, value):
            for user in value:
                if user in self.context['request'].user.userextended.follow.all() or user == self.context['request'].user:
                    pass
                else:
                    raise serializers.ValidationError('You cannot chat with a user you are not following.')
    
            if self.context['request'].user not in value:
                value.append(self.context['request'].user)
            return value
    

This is my model:

    
    
    class Chat(models.Model):
        users = models.ManyToManyField(User)
    

This is my unittest:

    
    
    from django.test import TestCase
    
    # Create your tests here.
    
    # Importing this from the DRF example of APIClient unittesting.
    from django.core.urlresolvers import reverse
    
    from rest_framework import status
    
    from rest_framework.test import APITestCase
    
    from django.contrib.auth.models import User
    from CMSApp.models import Chat
    from CMSApp.serializers import ChatSerializer
    
    class ChatTests(APITestCase):
        def setUp(self):
            User.objects.create_user(username='a', password='a', email='a@hotmail.com')
    
    def test_get_chat_list(self):
        """
        Ensure only authenticated users can get their own chat list.
        """
        a = User.objects.get()
        url = reverse('chat-list')
        self.client.login(username='a', password='a')
        Chat.objects.create()
        Chat.objects.get().users.add(a)
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        # Now, I want to either see if the id of resopnse.data is 1
        # or somehow verify that the chat which was received / created 
        # == the first chat created (chat who's pk / id  is 1). How
        # Would I do this?
    

I tried:

    
    
        self.assertEqual(response.data.id, 1)
    

But got this error:

    
    
    AttributeError: 'ReturnList' object has no attribute 'id'
    

When I do `print(response.data)` I get:

    
    
    [OrderedDict([('id', 1), ('users', [1])])]
    

I also tried this:

    
    
    self.assertEqual(response.data, ChatSerializer(Chat.objects.get()))
    

but got this error:

    
    
    self.assertEqual(response.data, ChatSerializer(Chat.objects.get()))
    AssertionError: [OrderedDict([('id', 1), ('users', [1])])] != ChatSerializer(<Chat: Chat object>):
        [134 chars]ll())
    

Any idea how I can accomplish what I want?


Answer: As you noticed the returned datatype is list of Orderdict so to access the
same you've to use index. :)

    
    
    response_data = simplejson.loads(response.content)
    expected_keys = set(['id', 'user'])
    # Here first we test if the keys are same in response
    # Notice we are checking the 0 index
    self.assertEqual(expected_keys, set(response_data[0].keys()))
    # To match the value
    self.assertEqual(response_data[0]['id'], 1)
    

