Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.2.X] Migrate the files doctests. Thanks to Alex Gaynor.

Backport of r13781 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@13798 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit ae26534567ac638e08fb7e69ff3661db3288e02a 1 parent 106600e
Russell Keith-Magee authored September 12, 2010
124  tests/modeltests/files/models.py
@@ -7,10 +7,12 @@
7 7
 
8 8
 import random
9 9
 import tempfile
  10
+
10 11
 from django.db import models
11 12
 from django.core.files.base import ContentFile
12 13
 from django.core.files.storage import FileSystemStorage
13 14
 
  15
+
14 16
 temp_storage_location = tempfile.mkdtemp()
15 17
 temp_storage = FileSystemStorage(location=temp_storage_location)
16 18
 
@@ -30,125 +32,3 @@ def random_upload_to(self, filename):
30 32
     custom = models.FileField(storage=temp_storage, upload_to=custom_upload_to)
31 33
     random = models.FileField(storage=temp_storage, upload_to=random_upload_to)
32 34
     default = models.FileField(storage=temp_storage, upload_to='tests', default='tests/default.txt')
33  
-
34  
-__test__ = {'API_TESTS':"""
35  
-# Attempting to access a FileField from the class raises a descriptive error
36  
->>> Storage.normal
37  
-Traceback (most recent call last):
38  
-...
39  
-AttributeError: The 'normal' attribute can only be accessed from Storage instances.
40  
-
41  
-# An object without a file has limited functionality.
42  
-
43  
->>> obj1 = Storage()
44  
->>> obj1.normal
45  
-<FieldFile: None>
46  
->>> obj1.normal.size
47  
-Traceback (most recent call last):
48  
-...
49  
-ValueError: The 'normal' attribute has no file associated with it.
50  
-
51  
-# Saving a file enables full functionality.
52  
-
53  
->>> obj1.normal.save('django_test.txt', ContentFile('content'))
54  
->>> obj1.normal
55  
-<FieldFile: tests/django_test.txt>
56  
->>> obj1.normal.size
57  
-7
58  
->>> obj1.normal.read()
59  
-'content'
60  
-
61  
-# File objects can be assigned to FileField attributes,  but shouldn't get
62  
-# committed until the model it's attached to is saved.
63  
-
64  
->>> from django.core.files.uploadedfile import SimpleUploadedFile
65  
->>> obj1.normal = SimpleUploadedFile('assignment.txt', 'content')
66  
->>> dirs, files = temp_storage.listdir('tests')
67  
->>> dirs
68  
-[]
69  
->>> files.sort()
70  
->>> files == ['default.txt', 'django_test.txt']
71  
-True
72  
-
73  
->>> obj1.save()
74  
->>> dirs, files = temp_storage.listdir('tests')
75  
->>> files.sort()
76  
->>> files == ['assignment.txt', 'default.txt', 'django_test.txt']
77  
-True
78  
-
79  
-# Files can be read in a little at a time, if necessary.
80  
-
81  
->>> obj1.normal.open()
82  
->>> obj1.normal.read(3)
83  
-'con'
84  
->>> obj1.normal.read()
85  
-'tent'
86  
->>> '-'.join(obj1.normal.chunks(chunk_size=2))
87  
-'co-nt-en-t'
88  
-
89  
-# Save another file with the same name.
90  
-
91  
->>> obj2 = Storage()
92  
->>> obj2.normal.save('django_test.txt', ContentFile('more content'))
93  
->>> obj2.normal
94  
-<FieldFile: tests/django_test_1.txt>
95  
->>> obj2.normal.size
96  
-12
97  
-
98  
-# Push the objects into the cache to make sure they pickle properly
99  
-
100  
->>> from django.core.cache import cache
101  
->>> cache.set('obj1', obj1)
102  
->>> cache.set('obj2', obj2)
103  
->>> cache.get('obj2').normal
104  
-<FieldFile: tests/django_test_1.txt>
105  
-
106  
-# Deleting an object deletes the file it uses, if there are no other objects
107  
-# still using that file.
108  
-
109  
->>> obj2.delete()
110  
->>> obj2.normal.save('django_test.txt', ContentFile('more content'))
111  
->>> obj2.normal
112  
-<FieldFile: tests/django_test_1.txt>
113  
-
114  
-# Multiple files with the same name get _N appended to them.
115  
-
116  
->>> objs = [Storage() for i in range(3)]
117  
->>> for o in objs:
118  
-...     o.normal.save('multiple_files.txt', ContentFile('Same Content'))
119  
->>> [o.normal for o in objs]
120  
-[<FieldFile: tests/multiple_files.txt>, <FieldFile: tests/multiple_files_1.txt>, <FieldFile: tests/multiple_files_2.txt>]
121  
->>> for o in objs:
122  
-...     o.delete()
123  
-
124  
-# Default values allow an object to access a single file.
125  
-
126  
->>> obj3 = Storage.objects.create()
127  
->>> obj3.default
128  
-<FieldFile: tests/default.txt>
129  
->>> obj3.default.read()
130  
-'default content'
131  
-
132  
-# But it shouldn't be deleted, even if there are no more objects using it.
133  
-
134  
->>> obj3.delete()
135  
->>> obj3 = Storage()
136  
->>> obj3.default.read()
137  
-'default content'
138  
-
139  
-# Verify the fix for #5655, making sure the directory is only determined once.
140  
-
141  
->>> obj4 = Storage()
142  
->>> obj4.random.save('random_file', ContentFile('random content'))
143  
->>> obj4.random
144  
-<FieldFile: .../random_file>
145  
-
146  
-# Clean up the temporary files and dir.
147  
->>> obj1.normal.delete()
148  
->>> obj2.normal.delete()
149  
->>> obj3.default.delete()
150  
->>> obj4.random.delete()
151  
-
152  
->>> import shutil
153  
->>> shutil.rmtree(temp_storage_location)
154  
-"""}
100  tests/modeltests/files/tests.py
... ...
@@ -0,0 +1,100 @@
  1
+import shutil
  2
+
  3
+from django.core.cache import cache
  4
+from django.core.files.base import ContentFile
  5
+from django.core.files.uploadedfile import SimpleUploadedFile
  6
+from django.test import TestCase
  7
+
  8
+from models import Storage, temp_storage, temp_storage_location
  9
+
  10
+
  11
+class FileTests(TestCase):
  12
+    def tearDown(self):
  13
+        shutil.rmtree(temp_storage_location)
  14
+
  15
+    def test_files(self):
  16
+        # Attempting to access a FileField from the class raises a descriptive
  17
+        # error
  18
+        self.assertRaises(AttributeError, lambda: Storage.normal)
  19
+
  20
+        # An object without a file has limited functionality.
  21
+        obj1 = Storage()
  22
+        self.assertEqual(obj1.normal.name, "")
  23
+        self.assertRaises(ValueError, lambda: obj1.normal.size)
  24
+
  25
+        # Saving a file enables full functionality.
  26
+        obj1.normal.save("django_test.txt", ContentFile("content"))
  27
+        self.assertEqual(obj1.normal.name, "tests/django_test.txt")
  28
+        self.assertEqual(obj1.normal.size, 7)
  29
+        self.assertEqual(obj1.normal.read(), "content")
  30
+
  31
+        # File objects can be assigned to FileField attributes, but shouldn't
  32
+        # get committed until the model it's attached to is saved.
  33
+        obj1.normal = SimpleUploadedFile("assignment.txt", "content")
  34
+        dirs, files = temp_storage.listdir("tests")
  35
+        self.assertEqual(dirs, [])
  36
+        self.assertEqual(sorted(files), ["default.txt", "django_test.txt"])
  37
+
  38
+        obj1.save()
  39
+        dirs, files = temp_storage.listdir("tests")
  40
+        self.assertEqual(
  41
+            sorted(files), ["assignment.txt", "default.txt", "django_test.txt"]
  42
+        )
  43
+
  44
+        # Files can be read in a little at a time, if necessary.
  45
+        obj1.normal.open()
  46
+        self.assertEqual(obj1.normal.read(3), "con")
  47
+        self.assertEqual(obj1.normal.read(), "tent")
  48
+        self.assertEqual(list(obj1.normal.chunks(chunk_size=2)), ["co", "nt", "en", "t"])
  49
+
  50
+        # Save another file with the same name.
  51
+        obj2 = Storage()
  52
+        obj2.normal.save("django_test.txt", ContentFile("more content"))
  53
+        self.assertEqual(obj2.normal.name, "tests/django_test_1.txt")
  54
+        self.assertEqual(obj2.normal.size, 12)
  55
+
  56
+        # Push the objects into the cache to make sure they pickle properly
  57
+        cache.set("obj1", obj1)
  58
+        cache.set("obj2", obj2)
  59
+        self.assertEqual(cache.get("obj2").normal.name, "tests/django_test_1.txt")
  60
+
  61
+        # Deleting an object deletes the file it uses, if there are no other
  62
+        # objects still using that file.
  63
+        obj2.delete()
  64
+        obj2.normal.save("django_test.txt", ContentFile("more content"))
  65
+        self.assertEqual(obj2.normal.name, "tests/django_test_1.txt")
  66
+
  67
+        # Multiple files with the same name get _N appended to them.
  68
+        objs = [Storage() for i in range(3)]
  69
+        for o in objs:
  70
+            o.normal.save("multiple_files.txt", ContentFile("Same Content"))
  71
+        self.assertEqual(
  72
+            [o.normal.name for o in objs],
  73
+            ["tests/multiple_files.txt", "tests/multiple_files_1.txt", "tests/multiple_files_2.txt"]
  74
+        )
  75
+        for o in objs:
  76
+            o.delete()
  77
+
  78
+        # Default values allow an object to access a single file.
  79
+        obj3 = Storage.objects.create()
  80
+        self.assertEqual(obj3.default.name, "tests/default.txt")
  81
+        self.assertEqual(obj3.default.read(), "default content")
  82
+
  83
+        # But it shouldn't be deleted, even if there are no more objects using
  84
+        # it.
  85
+        obj3.delete()
  86
+        obj3 = Storage()
  87
+        self.assertEqual(obj3.default.read(), "default content")
  88
+
  89
+        # Verify the fix for #5655, making sure the directory is only
  90
+        # determined once.
  91
+        obj4 = Storage()
  92
+        obj4.random.save("random_file", ContentFile("random content"))
  93
+        self.assertTrue(obj4.random.name.endswith("/random_file"))
  94
+
  95
+        # Clean up the temporary files and dir.
  96
+        obj1.normal.delete()
  97
+        obj2.normal.delete()
  98
+        obj3.default.delete()
  99
+        obj4.random.delete()
  100
+

0 notes on commit ae26534

Please sign in to comment.
Something went wrong with that request. Please try again.