mirror of https://github.com/django/django.git
Made a set of small test changes to avoid leaving temp files hanging around after running the test suite. First, fixed a couple of places where temp dirs were (or could be) created without later being deleted. Second, added a missing close() before unlink() since Windows raises an error on an attempt to remove an open file. Finally, in the file_uploads tests, avoided opening-by-name temporary files that we already have a descriptor for. Doing additional opens seems to run afoul of the Windows issue with deleting open files, so it generally works better to just seek back to 0 instead of calling open multiple times.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10406 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
6d5492630d
commit
d0c897d660
|
@ -4,7 +4,8 @@ from django.db import models
|
||||||
from django.core.files.storage import FileSystemStorage
|
from django.core.files.storage import FileSystemStorage
|
||||||
from django.forms import ModelForm
|
from django.forms import ModelForm
|
||||||
|
|
||||||
temp_storage = FileSystemStorage(tempfile.gettempdir())
|
temp_storage_dir = tempfile.mkdtemp()
|
||||||
|
temp_storage = FileSystemStorage(temp_storage_dir)
|
||||||
|
|
||||||
class Photo(models.Model):
|
class Photo(models.Model):
|
||||||
title = models.CharField(max_length=30)
|
title = models.CharField(max_length=30)
|
||||||
|
|
|
@ -5,10 +5,11 @@ ModelForm's save() method causes Model.save() to be called more than once.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import shutil
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from django.core.files.uploadedfile import SimpleUploadedFile
|
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||||
from regressiontests.bug639.models import Photo, PhotoForm
|
from regressiontests.bug639.models import Photo, PhotoForm, temp_storage_dir
|
||||||
|
|
||||||
class Bug639Test(unittest.TestCase):
|
class Bug639Test(unittest.TestCase):
|
||||||
|
|
||||||
|
@ -37,3 +38,4 @@ class Bug639Test(unittest.TestCase):
|
||||||
"""
|
"""
|
||||||
p = Photo.objects.get()
|
p = Photo.objects.get()
|
||||||
p.image.delete(save=False)
|
p.image.delete(save=False)
|
||||||
|
shutil.rmtree(temp_storage_dir)
|
||||||
|
|
|
@ -5,9 +5,6 @@ from django.db import models
|
||||||
from django.core.files.storage import FileSystemStorage
|
from django.core.files.storage import FileSystemStorage
|
||||||
from django.core.files.base import ContentFile
|
from django.core.files.base import ContentFile
|
||||||
|
|
||||||
temp_storage_dir = tempfile.mkdtemp()
|
|
||||||
temp_storage = FileSystemStorage(temp_storage_dir)
|
|
||||||
|
|
||||||
# Test for correct behavior of width_field/height_field.
|
# Test for correct behavior of width_field/height_field.
|
||||||
# Of course, we can't run this without PIL.
|
# Of course, we can't run this without PIL.
|
||||||
|
|
||||||
|
@ -20,6 +17,9 @@ except ImportError:
|
||||||
|
|
||||||
# If we have PIL, do these tests
|
# If we have PIL, do these tests
|
||||||
if Image:
|
if Image:
|
||||||
|
temp_storage_dir = tempfile.mkdtemp()
|
||||||
|
temp_storage = FileSystemStorage(temp_storage_dir)
|
||||||
|
|
||||||
class Person(models.Model):
|
class Person(models.Model):
|
||||||
name = models.CharField(max_length=50)
|
name = models.CharField(max_length=50)
|
||||||
mugshot = models.ImageField(storage=temp_storage, upload_to='tests',
|
mugshot = models.ImageField(storage=temp_storage, upload_to='tests',
|
||||||
|
|
|
@ -37,8 +37,8 @@ class FileUploadTests(TestCase):
|
||||||
|
|
||||||
post_data = {
|
post_data = {
|
||||||
'name': 'Ringo',
|
'name': 'Ringo',
|
||||||
'file_field1': open(file1.name),
|
'file_field1': file1,
|
||||||
'file_field2': open(file2.name),
|
'file_field2': file2,
|
||||||
}
|
}
|
||||||
|
|
||||||
for key in post_data.keys():
|
for key in post_data.keys():
|
||||||
|
@ -66,6 +66,7 @@ class FileUploadTests(TestCase):
|
||||||
|
|
||||||
response = self.client.post('/file_uploads/unicode_name/', post_data)
|
response = self.client.post('/file_uploads/unicode_name/', post_data)
|
||||||
|
|
||||||
|
file1.close()
|
||||||
try:
|
try:
|
||||||
os.unlink(file1.name)
|
os.unlink(file1.name)
|
||||||
except:
|
except:
|
||||||
|
@ -150,51 +151,57 @@ class FileUploadTests(TestCase):
|
||||||
# A small file (under the 5M quota)
|
# A small file (under the 5M quota)
|
||||||
smallfile = tempfile.NamedTemporaryFile()
|
smallfile = tempfile.NamedTemporaryFile()
|
||||||
smallfile.write('a' * (2 ** 21))
|
smallfile.write('a' * (2 ** 21))
|
||||||
|
smallfile.seek(0)
|
||||||
|
|
||||||
# A big file (over the quota)
|
# A big file (over the quota)
|
||||||
bigfile = tempfile.NamedTemporaryFile()
|
bigfile = tempfile.NamedTemporaryFile()
|
||||||
bigfile.write('a' * (10 * 2 ** 20))
|
bigfile.write('a' * (10 * 2 ** 20))
|
||||||
|
bigfile.seek(0)
|
||||||
|
|
||||||
# Small file posting should work.
|
# Small file posting should work.
|
||||||
response = self.client.post('/file_uploads/quota/', {'f': open(smallfile.name)})
|
response = self.client.post('/file_uploads/quota/', {'f': smallfile})
|
||||||
got = simplejson.loads(response.content)
|
got = simplejson.loads(response.content)
|
||||||
self.assert_('f' in got)
|
self.assert_('f' in got)
|
||||||
|
|
||||||
# Large files don't go through.
|
# Large files don't go through.
|
||||||
response = self.client.post("/file_uploads/quota/", {'f': open(bigfile.name)})
|
response = self.client.post("/file_uploads/quota/", {'f': bigfile})
|
||||||
got = simplejson.loads(response.content)
|
got = simplejson.loads(response.content)
|
||||||
self.assert_('f' not in got)
|
self.assert_('f' not in got)
|
||||||
|
|
||||||
def test_broken_custom_upload_handler(self):
|
def test_broken_custom_upload_handler(self):
|
||||||
f = tempfile.NamedTemporaryFile()
|
f = tempfile.NamedTemporaryFile()
|
||||||
f.write('a' * (2 ** 21))
|
f.write('a' * (2 ** 21))
|
||||||
|
f.seek(0)
|
||||||
|
|
||||||
# AttributeError: You cannot alter upload handlers after the upload has been processed.
|
# AttributeError: You cannot alter upload handlers after the upload has been processed.
|
||||||
self.assertRaises(
|
self.assertRaises(
|
||||||
AttributeError,
|
AttributeError,
|
||||||
self.client.post,
|
self.client.post,
|
||||||
'/file_uploads/quota/broken/',
|
'/file_uploads/quota/broken/',
|
||||||
{'f': open(f.name)}
|
{'f': f}
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_fileupload_getlist(self):
|
def test_fileupload_getlist(self):
|
||||||
file1 = tempfile.NamedTemporaryFile()
|
file1 = tempfile.NamedTemporaryFile()
|
||||||
file1.write('a' * (2 ** 23))
|
file1.write('a' * (2 ** 23))
|
||||||
|
file1.seek(0)
|
||||||
|
|
||||||
file2 = tempfile.NamedTemporaryFile()
|
file2 = tempfile.NamedTemporaryFile()
|
||||||
file2.write('a' * (2 * 2 ** 18))
|
file2.write('a' * (2 * 2 ** 18))
|
||||||
|
file2.seek(0)
|
||||||
|
|
||||||
file2a = tempfile.NamedTemporaryFile()
|
file2a = tempfile.NamedTemporaryFile()
|
||||||
file2a.write('a' * (5 * 2 ** 20))
|
file2a.write('a' * (5 * 2 ** 20))
|
||||||
|
file2a.seek(0)
|
||||||
|
|
||||||
response = self.client.post('/file_uploads/getlist_count/', {
|
response = self.client.post('/file_uploads/getlist_count/', {
|
||||||
'file1': open(file1.name),
|
'file1': file1,
|
||||||
'field1': u'test',
|
'field1': u'test',
|
||||||
'field2': u'test3',
|
'field2': u'test3',
|
||||||
'field3': u'test5',
|
'field3': u'test5',
|
||||||
'field4': u'test6',
|
'field4': u'test6',
|
||||||
'field5': u'test7',
|
'field5': u'test7',
|
||||||
'file2': (open(file2.name), open(file2a.name))
|
'file2': (file2, file2a)
|
||||||
})
|
})
|
||||||
got = simplejson.loads(response.content)
|
got = simplejson.loads(response.content)
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import datetime
|
import datetime
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import shutil
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
# Can't import as "forms" due to implementation details in the test suite (the
|
# Can't import as "forms" due to implementation details in the test suite (the
|
||||||
|
@ -41,6 +42,12 @@ True
|
||||||
{'file1': <SimpleUploadedFile: 我隻氣墊船裝滿晒鱔.txt (text/plain)>}
|
{'file1': <SimpleUploadedFile: 我隻氣墊船裝滿晒鱔.txt (text/plain)>}
|
||||||
>>> m = FileModel.objects.create(file=f.cleaned_data['file1'])
|
>>> m = FileModel.objects.create(file=f.cleaned_data['file1'])
|
||||||
|
|
||||||
|
# It's enough that m gets created without error. Preservation of the exotic name is checked
|
||||||
|
# in a file_uploads test; it's hard to do that correctly with doctest's unicode issues. So
|
||||||
|
# we create and then immediately delete m so as to not leave the exotically named file around
|
||||||
|
# for shutil.rmtree (on Windows) to have trouble with later.
|
||||||
|
>>> m.delete()
|
||||||
|
|
||||||
# Boundary conditions on a PostitiveIntegerField #########################
|
# Boundary conditions on a PostitiveIntegerField #########################
|
||||||
>>> class BoundaryForm(ModelForm):
|
>>> class BoundaryForm(ModelForm):
|
||||||
... class Meta:
|
... class Meta:
|
||||||
|
@ -78,4 +85,5 @@ u'instance value'
|
||||||
datetime.date(1969, 4, 4)
|
datetime.date(1969, 4, 4)
|
||||||
>>> instance_form.initial['value']
|
>>> instance_form.initial['value']
|
||||||
12
|
12
|
||||||
|
>>> shutil.rmtree(temp_storage_location)
|
||||||
"""}
|
"""}
|
||||||
|
|
Loading…
Reference in New Issue