vendredi 30 janvier 2015

Testing sending file to REST API

I'm building a REST API with Django REST Framework.


I have my models as follows:



class Mentor(Model):
name = CharField(max_length=100, verbose_name=u'Nome')
area = CharField(max_length=100, verbose_name=u'Área de Atuação')
website = URLField(max_length=500, verbose_name=u'Website')
description = TextField(verbose_name=u'Descrição')
picture = ImageField(upload_to=r'mentors', verbose_name=u'Foto')

class Meta:
verbose_name = u'Mentor'
verbose_name_plural = u'Mentores'


class NationalMentor(Mentor):
class Meta:
verbose_name = u'Mentor Nacional'
verbose_name_plural = u'Mentores Nacionais'


And the serializer is simple enough:



class MentorSerializer(ModelSerializer):
class Meta:
model = Mentor


class NationalMentorSerializer(ModelSerializer):
class Meta:
model = NationalMentor


I would like to test the API generated by this view:



class NationalMentorViewSet(ModelViewSet):
queryset = NationalMentor.objects.all()
serializer_class = NationalMentorSerializer


And this router:



router = DefaultRouter()
router.register(r'national-mentors', NationalMentorViewSet)

urlpatterns = router.urls


I'm trying this:



@override_settings(MEDIA_URL=r'/media/')
class NationalMentorTestCase(APISimpleTestCase):
def setUp(self):
NationalMentor.objects.create(id=1, name=u'Osvaldo', area=u'Educação',
website=r'http://ift.tt/1uIGEwv',
description=u'Descrição do Osvaldo.', picture=r'osvaldo.png')
NationalMentor.objects.create(id=2, name=u'Alice', area=u'Inovação',
website=r'http://www.alice.com.br',
description=u'Descrição da Alice.', picture=r'alice.gif')

def tearDown(self):
NationalMentor.objects.all().delete()

def test_create(self):
url = reverse(r'nationalmentor-list')
response = self.client.post(url,
{'name': u'Rafael', r'area': u'Desenvolvimento',
r'website': r'http://ift.tt/1veGliP',
r'description': u'Descrição do Rafael.',
r'picture': r'rafael.jpg'})
self.assertEqual(url, r'/national-mentors/')
self.assertEqual(response.data,
{r'id': 3, r'name': u'Rafael', r'area': u'Desenvolvimento',
r'website': r'http://ift.tt/1veGliP',
r'description': u'Descrição do Rafael.',
r'picture': r'http://testserver/media/rafael.jpg'})
self.assertEqual(response.status_code, status.HTTP_201_CREATED)


But I'm getting this error:



======================================================================
FAIL: test_create (main.tests.test_national_mentor.NationalMentorTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/jpmelos/devel/ideation_admin/ideation_admin/main/tests/test_national_mentor.py", line 34, in test_create
r'picture': r'http://testserver/media/rafael.jpg'})
AssertionError: {'picture': [u'The submitted data was not a file. Check the encoding type on the [truncated]... != {'website': 'http://ift.tt/1veGliP', 'picture': 'http://testserver/media/rafa [truncated]...
- {'picture': [u'The submitted data was not a file. Check the encoding type on the form.']}
+ {'area': u'Desenvolvimento',
+ 'description': u'Descri\xe7\xe3o do Rafael.',
+ 'id': 3,
+ 'name': u'Rafael',
+ 'picture': 'http://testserver/media/rafael.jpg',
+ 'website': 'http://ift.tt/1veGliP'}

----------------------------------------------------------------------


I tried everything I could think of for last few hours, but couldn't find out how to test this API. How can I simulate a POST on this endpoint with the picture file for this NationalMentor and all the other data for creation of the instance?


Aucun commentaire:

Enregistrer un commentaire