miki725 / django-rest-framework-bulk

Django REST Framework bulk CRUD view mixins
Other
521 stars 106 forks source link

testing with api client #37

Open Jcbobo opened 9 years ago

Jcbobo commented 9 years ago

I'm trying to wrote test for my application ( I have bulk creation on one api ) but iI'm blocked because the rest_framework api client does not allow me to execute post with a list as data error:

Traceback (most recent call last):
  File "/Users/andrea/Documents/workspace/MakrShakrPortbl/ingredient/tests.py", line 79, in test_buld_ingridient_operation
    creation_response = self.client.post(reverse(self.ingredienttListViewName),self.ingredient_data_list)
  File "/Users/andrea/Documents/workspace/MakrShakrPortbl/venv/lib/python2.7/site-packages/rest_framework/test.py", line 168, in post
    path, data=data, format=format, content_type=content_type, **extra)
  File "/Users/andrea/Documents/workspace/MakrShakrPortbl/venv/lib/python2.7/site-packages/rest_framework/test.py", line 89, in post
    data, content_type = self._encode_data(data, format, content_type)
  File "/Users/andrea/Documents/workspace/MakrShakrPortbl/venv/lib/python2.7/site-packages/rest_framework/test.py", line 64, in _encode_data
    ret = renderer.render(data)
  File "/Users/andrea/Documents/workspace/MakrShakrPortbl/venv/lib/python2.7/site-packages/rest_framework/renderers.py", line 678, in render
    return encode_multipart(self.BOUNDARY, data)
  File "/Users/andrea/Documents/workspace/MakrShakrPortbl/venv/lib/python2.7/site-packages/django/test/client.py", line 168, in encode_multipart
    for (key, value) in data.items():
AttributeError: 'list' object has no attribute 'items' 

I'm working on osx yosemite 10.10.3 pip freeze with virtualenv actived:

Django==1.8.2 -e https://github.com/umutbozkurt/django-rest-framework-mongoengine#egg=django_rest_framework_mongoengine-master djangorestframework==3.1.3 djangorestframework-bulk==0.2.1 mongoengine==0.9.0 pymongo==2.8.1 wsgiref==0.1.2

no problem on creation,update reported

vegaelle commented 9 years ago

Have you tried to send your request with "format='json" parameter in client.post()?

miki725 commented 9 years ago

@Jcbobo88 can you post your test case?

jdsinh commented 8 years ago

I face the same isse while posting list of objects in "post" method. when

        data = [
            {
                'contact.created': "2016-10-12T12:31:08.553139Z",
                'contact.modified': "2016-10-12T12:31:08.566822Z",
                'contact.status': 1,
            },
            {
                'contact.created': "2016-10-12T12:31:08.553139Z",
                'contact.modified': "2016-10-12T12:31:08.566822Z",
                'contact.status': 1,
            },

        ]

to

response = self.c.post(self._url, data, format='json') AttributeError: 'list' object has no attribute 'items'

lucabezerra commented 6 years ago

To whoever finds this issue, passing it as a JSONified string solved it for me:

import json
# [...]
self.client.post(self.view_url, json.dumps(data), content_type='application/json')

Keep in mind that I'm not using this framework, I'm doing bulk actions manually in my code, but the error and the situation were the same, so I believe it should work.