django-rest-framework-bulk icon indicating copy to clipboard operation
django-rest-framework-bulk copied to clipboard

testing with api client

Open Jcbobo opened this issue 10 years ago • 4 comments

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

Jcbobo avatar Jul 03 '15 16:07 Jcbobo

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

vegaelle avatar Sep 10 '15 14:09 vegaelle

@Jcbobo88 can you post your test case?

miki725 avatar Sep 10 '15 18:09 miki725

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'

jdsinh avatar Oct 14 '16 10:10 jdsinh

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.

lucabezerra avatar May 14 '18 21:05 lucabezerra