mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 00:04:16 +03:00
Added a test for the FileField.
This commit is contained in:
parent
e112a806d8
commit
69a01d7125
|
@ -1,34 +1,39 @@
|
||||||
# from django.test import TestCase
|
import StringIO
|
||||||
# from django import forms
|
import datetime
|
||||||
|
|
||||||
# from django.test.client import RequestFactory
|
from django.test import TestCase
|
||||||
# from rest_framework.views import View
|
|
||||||
# from rest_framework.response import Response
|
|
||||||
|
|
||||||
# import StringIO
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
|
||||||
# class UploadFilesTests(TestCase):
|
class UploadedFile(object):
|
||||||
# """Check uploading of files"""
|
def __init__(self, file, created=None):
|
||||||
# def setUp(self):
|
self.file = file
|
||||||
# self.factory = RequestFactory()
|
self.created = created or datetime.datetime.now()
|
||||||
|
|
||||||
# def test_upload_file(self):
|
|
||||||
|
|
||||||
# class FileForm(forms.Form):
|
class UploadedFileSerializer(serializers.Serializer):
|
||||||
# file = forms.FileField()
|
file = serializers.FileField()
|
||||||
|
created = serializers.DateTimeField()
|
||||||
|
|
||||||
# class MockView(View):
|
def restore_object(self, attrs, instance=None):
|
||||||
# permissions = ()
|
if instance:
|
||||||
# form = FileForm
|
instance.file = attrs['file']
|
||||||
|
instance.created = attrs['created']
|
||||||
|
return instance
|
||||||
|
return UploadedFile(**attrs)
|
||||||
|
|
||||||
# def post(self, request, *args, **kwargs):
|
|
||||||
# return Response({'FILE_NAME': self.CONTENT['file'].name,
|
|
||||||
# 'FILE_CONTENT': self.CONTENT['file'].read()})
|
|
||||||
|
|
||||||
# file = StringIO.StringIO('stuff')
|
class FileSerializerTests(TestCase):
|
||||||
# file.name = 'stuff.txt'
|
|
||||||
# request = self.factory.post('/', {'file': file})
|
def test_create(self):
|
||||||
# view = MockView.as_view()
|
now = datetime.datetime.now()
|
||||||
# response = view(request)
|
file = StringIO.StringIO('stuff')
|
||||||
# self.assertEquals(response.raw_content, {"FILE_CONTENT": "stuff", "FILE_NAME": "stuff.txt"})
|
file.name = 'stuff.txt'
|
||||||
|
file.size = file.len
|
||||||
|
serializer = UploadedFileSerializer(data={'created': now}, files={'file': file})
|
||||||
|
uploaded_file = UploadedFile(file=file, created=now)
|
||||||
|
self.assertTrue(serializer.is_valid())
|
||||||
|
self.assertEquals(serializer.object.created, uploaded_file.created)
|
||||||
|
self.assertEquals(serializer.object.file, uploaded_file.file)
|
||||||
|
self.assertFalse(serializer.object is uploaded_file)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user