Unit test for Django Resource containing an ImageField
silviomoreto opened this issue · 1 comments
silviomoreto commented
Hi,
I am trying to write an unit test for my resource, but I am getting the error:
{"error": "Expecting property name enclosed in double quotes: line 1 column 2 (char 1)"}
Here is my model:
class MyModel(models.Model):
input_image = models.ImageField('Input image', upload_to='process')
My resource:
class MyModelResource(DjangoResource):
preparer = FieldsPreparer(fields={
'input_image': 'input_image_url',
'id': 'id',
})
def is_authenticated(self):
return True
@skip_prepare
def list(self):
return list(MyModel.objects.all().values('id'))
def update(self):
raise MethodNotAllowed()
def delete(self):
raise MethodNotAllowed()
def detail(self, pk):
return MyModel.objects.get(id=pk)
def create(self):
form = MyModelForm(self.request, self.data, self.request.FILES)
if form.is_valid():
obj = form.save()
My unit test
class CountProcessResourceTest(TestCase):
def setUp(self):
self.client = Client()
self.img_url = 'https://www.djangoproject.com/s/img/small-fundraising-heart.png'
image = urllib.urlopen(self.img_url)
self.image = SimpleUploadedFile(name='test_image.jpg', content=image.read(), content_type='image/png')
self.object = MyModel.objects.create(input_image=self.image)
def test_create(self):
api_url = reverse('api_mymodel_list')
t = {"input_image": self.image}
response = self.client.post(api_url, t, content_type='application/json')
print response.request
print response.content
What am I doing wrong?
guilhermetavares commented
Silvio,
probably is a Client problem
look at the ResourceTestCase from Tastypie or Restless tests
In model, tries to use @Property with dict, works for me, and facilitates the use of thumbnail.
class MyModel(models.Model):
input_image = models.ImageField('Input image', upload_to='process')
@property
def get_input_image(self):
if self.input_image:
return self.input_image.url
return None
@property
def get_dict_image(self):
if self.input_image:
return {
'url': self.input_image.url,
# 'width': ...
# 'heigth': ...
# 'format': ...
}
return None
att.