Refactored file_storage tests to avoid depending on servers tests.

This commit is contained in:
Aymeric Augustin 2013-06-04 07:55:20 +02:00
parent 96c71d423d
commit 7a65c95d42
2 changed files with 18 additions and 7 deletions

View File

@ -7,6 +7,10 @@ import shutil
import sys import sys
import tempfile import tempfile
import time import time
try:
from urllib.request import urlopen
except ImportError: # Python 2
from urllib2 import urlopen
import zlib import zlib
from datetime import datetime, timedelta from datetime import datetime, timedelta
from io import BytesIO from io import BytesIO
@ -22,12 +26,11 @@ from django.core.files.base import File, ContentFile
from django.core.files.images import get_image_dimensions from django.core.files.images import get_image_dimensions
from django.core.files.storage import FileSystemStorage, get_storage_class from django.core.files.storage import FileSystemStorage, get_storage_class
from django.core.files.uploadedfile import UploadedFile from django.core.files.uploadedfile import UploadedFile
from django.test import SimpleTestCase from django.test import LiveServerTestCase, SimpleTestCase
from django.utils import six from django.utils import six
from django.utils import unittest from django.utils import unittest
from django.utils._os import upath from django.utils._os import upath
from django.test.utils import override_settings from django.test.utils import override_settings
from servers.tests import LiveServerBase
try: try:
from django.utils.image import Image from django.utils.image import Image
@ -613,10 +616,13 @@ class NoNameFileTestCase(unittest.TestCase):
def test_noname_file_get_size(self): def test_noname_file_get_size(self):
self.assertEqual(File(BytesIO(b'A file with no name')).size, 19) self.assertEqual(File(BytesIO(b'A file with no name')).size, 19)
class FileLikeObjectTestCase(LiveServerBase):
class FileLikeObjectTestCase(LiveServerTestCase):
""" """
Test file-like objects (#15644). Test file-like objects (#15644).
""" """
urls = 'file_storage.urls'
def setUp(self): def setUp(self):
self.temp_dir = tempfile.mkdtemp() self.temp_dir = tempfile.mkdtemp()
self.storage = FileSystemStorage(location=self.temp_dir) self.storage = FileSystemStorage(location=self.temp_dir)
@ -628,12 +634,10 @@ class FileLikeObjectTestCase(LiveServerBase):
""" """
Test the File storage API with a file like object coming from urllib2.urlopen() Test the File storage API with a file like object coming from urllib2.urlopen()
""" """
file_like_object = urlopen(self.live_server_url + '/')
file_like_object = self.urlopen('/example_view/')
f = File(file_like_object) f = File(file_like_object)
stored_filename = self.storage.save("remote_file.html", f) stored_filename = self.storage.save("remote_file.html", f)
remote_file = self.urlopen('/example_view/') remote_file = urlopen(self.live_server_url + '/')
with self.storage.open(stored_filename) as stored_file: with self.storage.open(stored_filename) as stored_file:
self.assertEqual(stored_file.read(), remote_file.read()) self.assertEqual(stored_file.read(), remote_file.read())

View File

@ -0,0 +1,7 @@
from django.conf.urls import patterns, url
from django.http import HttpResponse
urlpatterns = patterns('',
url(r'^$', lambda req: HttpResponse('example view')),
)