samedi 16 mai 2015

How to send multiple files using rest_framework.test.APITestCase

I'm trying to send a couple of files to my backend:

class AccountsImporterTestCase(APITestCase):

    def test(self):
        data = [open('accounts/importer/accounts.csv'), open('accounts/importer/apartments.csv')]
        response = self.client.post('/api/v1/accounts/import/', data, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)

But I'm getting an error:

Error
Traceback (most recent call last):
  File "/vagrant/conjuntos/accounts/tests/cases.py", line 128, in test
    response = self.client.post('/api/v1/accounts/import/', data, format='multipart')
  File "/vagrant/venv/lib/python3.4/site-packages/rest_framework/test.py", line 168, in post
    path, data=data, format=format, content_type=content_type, **extra)
  File "/vagrant/venv/lib/python3.4/site-packages/rest_framework/test.py", line 89, in post
    data, content_type = self._encode_data(data, format, content_type)
  File "/vagrant/venv/lib/python3.4/site-packages/rest_framework/test.py", line 64, in _encode_data
    ret = renderer.render(data)
  File "/vagrant/venv/lib/python3.4/site-packages/rest_framework/renderers.py", line 757, in render
    return encode_multipart(self.BOUNDARY, data)
  File "/vagrant/venv/lib/python3.4/site-packages/django/test/client.py", line 156, in encode_multipart
    for (key, value) in data.items():
AttributeError: 'list' object has no attribute 'items'

I know I'm not preparing the data correctly but is it possible to do it?, how?. Thanks!

Aucun commentaire:

Enregistrer un commentaire