2013-02-05 00:55:35 +04:00
|
|
|
from __future__ import unicode_literals
|
2012-11-15 02:04:46 +04:00
|
|
|
from django.test import TestCase
|
|
|
|
from rest_framework import serializers
|
2013-01-03 14:41:07 +04:00
|
|
|
from rest_framework.compat import BytesIO
|
|
|
|
from rest_framework.compat import six
|
2013-02-05 00:55:35 +04:00
|
|
|
import datetime
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
2012-11-15 02:04:46 +04:00
|
|
|
class UploadedFile(object):
|
2013-07-25 01:24:29 +04:00
|
|
|
def __init__(self, file=None, created=None):
|
2012-11-15 02:04:46 +04:00
|
|
|
self.file = file
|
|
|
|
self.created = created or datetime.datetime.now()
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
2012-11-15 02:04:46 +04:00
|
|
|
class UploadedFileSerializer(serializers.Serializer):
|
2013-07-25 01:24:29 +04:00
|
|
|
file = serializers.FileField(required=False)
|
2012-11-15 02:04:46 +04:00
|
|
|
created = serializers.DateTimeField()
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2012-11-15 02:04:46 +04:00
|
|
|
def restore_object(self, attrs, instance=None):
|
|
|
|
if instance:
|
|
|
|
instance.file = attrs['file']
|
|
|
|
instance.created = attrs['created']
|
|
|
|
return instance
|
|
|
|
return UploadedFile(**attrs)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
2012-11-15 02:04:46 +04:00
|
|
|
class FileSerializerTests(TestCase):
|
|
|
|
def test_create(self):
|
|
|
|
now = datetime.datetime.now()
|
2012-11-25 02:19:03 +04:00
|
|
|
file = BytesIO(six.b('stuff'))
|
2012-11-15 02:04:46 +04:00
|
|
|
file.name = 'stuff.txt'
|
2012-11-23 04:12:33 +04:00
|
|
|
file.size = len(file.getvalue())
|
2012-11-15 02:04:46 +04:00
|
|
|
serializer = UploadedFileSerializer(data={'created': now}, files={'file': file})
|
|
|
|
uploaded_file = UploadedFile(file=file, created=now)
|
|
|
|
self.assertTrue(serializer.is_valid())
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.object.created, uploaded_file.created)
|
|
|
|
self.assertEqual(serializer.object.file, uploaded_file.file)
|
2012-11-15 02:04:46 +04:00
|
|
|
self.assertFalse(serializer.object is uploaded_file)
|
2013-01-03 00:07:23 +04:00
|
|
|
|
|
|
|
def test_creation_failure(self):
|
|
|
|
"""
|
|
|
|
Passing files=None should result in an ValidationError
|
|
|
|
|
|
|
|
Regression test for:
|
|
|
|
https://github.com/tomchristie/django-rest-framework/issues/542
|
|
|
|
"""
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
|
2013-01-03 10:28:17 +04:00
|
|
|
serializer = UploadedFileSerializer(data={'created': now})
|
2013-07-25 01:24:29 +04:00
|
|
|
self.assertTrue(serializer.is_valid())
|
|
|
|
self.assertEqual(serializer.object.created, now)
|
|
|
|
self.assertIsNone(serializer.object.file)
|
|
|
|
|
|
|
|
def test_remove_with_empty_string(self):
|
|
|
|
"""
|
|
|
|
Passing empty string as data should cause file to be removed
|
|
|
|
|
|
|
|
Test for:
|
|
|
|
https://github.com/tomchristie/django-rest-framework/issues/937
|
|
|
|
"""
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
file = BytesIO(six.b('stuff'))
|
|
|
|
file.name = 'stuff.txt'
|
|
|
|
file.size = len(file.getvalue())
|
|
|
|
|
|
|
|
uploaded_file = UploadedFile(file=file, created=now)
|
|
|
|
|
|
|
|
serializer = UploadedFileSerializer(instance=uploaded_file, data={'created': now, 'file': ''})
|
|
|
|
self.assertTrue(serializer.is_valid())
|
|
|
|
self.assertEqual(serializer.object.created, uploaded_file.created)
|
|
|
|
self.assertIsNone(serializer.object.file)
|
2013-08-23 15:03:54 +04:00
|
|
|
|
|
|
|
def test_validation_error_with_non_file(self):
|
|
|
|
"""
|
|
|
|
Passing non-files should raise a validation error.
|
|
|
|
"""
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
errmsg = 'No file was submitted. Check the encoding type on the form.'
|
|
|
|
|
|
|
|
serializer = UploadedFileSerializer(data={'created': now, 'file': 'abc'})
|
|
|
|
self.assertFalse(serializer.is_valid())
|
|
|
|
self.assertEqual(serializer.errors, {'file': [errmsg]})
|
2013-09-12 19:03:20 +04:00
|
|
|
|
|
|
|
def test_validation_with_no_data(self):
|
|
|
|
"""
|
|
|
|
Validation should still function when no data dictionary is provided.
|
|
|
|
"""
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
file = BytesIO(six.b('stuff'))
|
|
|
|
file.name = 'stuff.txt'
|
|
|
|
file.size = len(file.getvalue())
|
|
|
|
uploaded_file = UploadedFile(file=file, created=now)
|
|
|
|
|
|
|
|
serializer = UploadedFileSerializer(files={'file': file})
|
2013-09-12 19:04:33 +04:00
|
|
|
self.assertFalse(serializer.is_valid())
|