samedi 27 février 2016

How to post a list with django-restframework testclient

I am writing unit-tests for my api views. Right now I'm facing the problem that I cannot post lists using the testclient provided by django-restframwork:

My view looks like this:

@detail_route(methods=['post'])
def dosomestuff(self, request, slug, *args, **kwargs):
    watchlist = request.data.get('watchlist', [])
    sortedoutlist = request.data.get('sortedoutlist', [])
    # create some objects
    ...

    return Response('success'), status=status.HTTP_201_CREATED)

The json objects I post to this endpoint normally look like this:

{"watchlist": [32, 12, 23], "sortedoutlist": [1, 2,3 ]}

Now I set up a testcase for this view:

class MyTestCase(APITestCase):

    def test_dosomestuff(self):
        url = reverse('dosomestuff')
        data = {'watchlist': [32, 12, 23],
                'sortedoutlist': [1, 2,3 ]}
        r = self.client.post(url, data)
        self.assertEqual(r.status_code, status.HTTP_201_CREATED)

The problem is that the watchlist and sortedoutlist recieved in the view are no lists, but int containing the last value of the sent list (watchlist is 23 and sortedoutlist is 3.

What am I doing wrong here? How can I post lists in the testcase?

Aucun commentaire:

Enregistrer un commentaire