diff options
| author | Claude Paroz <claude@2xlibre.net> | 2012-04-05 15:44:04 +0000 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2012-04-05 15:44:04 +0000 |
| commit | 5c954136eaef3d98d532368deec4c19cf892f664 (patch) | |
| tree | 2fb423b086e301350e7053340e05c8ccb7959b8f /tests/regressiontests | |
| parent | 5e047ed859251d8019a185262b8f5abf5966af09 (diff) | |
Fixed #15644 -- Improved Django File wrapper to support more file-like objects. Thanks nickname123 and Michael Palumbo for working on the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17871 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/regressiontests')
| -rw-r--r-- | tests/regressiontests/file_storage/tests.py | 43 |
1 files changed, 41 insertions, 2 deletions
diff --git a/tests/regressiontests/file_storage/tests.py b/tests/regressiontests/file_storage/tests.py index f03a7a8eac..c28b019c84 100644 --- a/tests/regressiontests/file_storage/tests.py +++ b/tests/regressiontests/file_storage/tests.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +from __future__ import absolute_import + import errno import os import shutil @@ -17,12 +19,13 @@ except ImportError: from django.conf import settings from django.core.exceptions import SuspiciousOperation, ImproperlyConfigured -from django.core.files.base import ContentFile +from django.core.files.base import File, ContentFile from django.core.files.images import get_image_dimensions from django.core.files.storage import FileSystemStorage, get_storage_class from django.core.files.uploadedfile import UploadedFile from django.test import SimpleTestCase from django.utils import unittest +from ..servers.tests import LiveServerBase # Try to import PIL in either of the two ways it can end up installed. # Checking for the existence of Image is enough for CPython, but @@ -544,6 +547,42 @@ class ContentFileTestCase(unittest.TestCase): def test_content_file_default_name(self): self.assertEqual(ContentFile("content").name, None) - def test_content_file_custome_name(self): + def test_content_file_custom_name(self): name = "I can have a name too!" self.assertEqual(ContentFile("content", name=name).name, name) + +class NoNameFileTestCase(unittest.TestCase): + """ + Other examples of unnamed files may be tempfile.SpooledTemporaryFile or + urllib.urlopen() + """ + def test_noname_file_default_name(self): + self.assertEqual(File(StringIO('A file with no name')).name, None) + + def test_noname_file_get_size(self): + self.assertEqual(File(StringIO('A file with no name')).size, 19) + +class FileLikeObjectTestCase(LiveServerBase): + """ + Test file-like objects (#15644). + """ + def setUp(self): + self.temp_dir = tempfile.mkdtemp() + self.storage = FileSystemStorage(location=self.temp_dir) + + def tearDown(self): + shutil.rmtree(self.temp_dir) + + def test_urllib2_urlopen(self): + """ + Test the File storage API with a file like object coming from urllib2.urlopen() + """ + + file_like_object = self.urlopen('/example_view/') + f = File(file_like_object) + stored_filename = self.storage.save("remote_file.html", f) + + stored_file = self.storage.open(stored_filename) + remote_file = self.urlopen('/example_view/') + + self.assertEqual(stored_file.read(), remote_file.read()) |
