11from django .core .exceptions import ValidationError
22from django .core .files .base import ContentFile
3+ from django .core .files .storage .memory import InMemoryFileNode
34from django .db import DataError , models , transaction
45from django .test import TestCase , override_settings
56from django .utils .translation import override
@@ -144,12 +145,10 @@ def test_fallback_getting_FileField(self):
144145 post = Post .objects .create (title = "Test Post" , is_published = True )
145146 sample_file = ContentFile ("sample content" , name = "sample-en.txt" )
146147 attachment = Attachment .objects .create (post = post , file = sample_file )
147- default_file_name = attachment .file .name
148- assert default_file_name
149148
150149 with override ("fr" ):
151150 self .assertIsInstance (attachment .file_i18n , models .fields .files .FieldFile )
152- self .assertEqual (attachment .file_i18n .name , default_file_name )
151+ self .assertIsInstance (attachment .file_i18n .file , InMemoryFileNode )
153152
154153 def test_set_FileField (self ):
155154 post = Post .objects .create (title = "Test Post" , is_published = True )
@@ -162,14 +161,58 @@ def test_set_FileField(self):
162161 attachment = Attachment .objects .create (
163162 post = post , file = sample_file_en , file_fr = sample_file_fr
164163 )
164+ self .assertIsInstance (attachment .file_fr , models .fields .files .FieldFile )
165+
166+ self .assertIsInstance (attachment .file .file , InMemoryFileNode )
167+ self .assertIsInstance (attachment .file_fr .file , InMemoryFileNode )
165168
166169 saved_fr_content = attachment .file_fr .read ().decode ("utf-8" )
167170 self .assertEqual (saved_fr_content , fr_content )
168- self .assertIsInstance (attachment .file_fr , models .fields .files .FieldFile )
169171
170172 with override ("fr" ):
171173 self .assertEqual (attachment .file_i18n , attachment .file_fr )
172174
175+ def test_FileField_getter (self ):
176+ post = Post .objects .create (title = "Test Post" , is_published = True )
177+
178+ fr_content = "exemple de contenu 2"
179+ sample_file_fr = ContentFile (fr_content , name = "sample-fr-2.txt" )
180+
181+ attachment = Attachment (post = post , file_fr = sample_file_fr )
182+ # prior to invoking save, the file_fr should be an instance of FieldFile
183+ self .assertIsInstance (attachment .file_fr , models .fields .files .FieldFile )
184+ # but the file object should be an instance of ContentFile
185+ self .assertIsInstance (attachment .file_fr .file , ContentFile )
186+ attachment .save ()
187+
188+ # After saving, the file object should be the default storage class
189+ self .assertIsInstance (attachment .file_fr .file , InMemoryFileNode )
190+
191+ # Retreiving the instance from the database, file and file_fr
192+ # should return the same kind of interface (a FieldFile instance)
193+ attachment = Attachment .objects .get (pk = attachment .pk )
194+ self .assertIsInstance (attachment .file , models .fields .files .FieldFile )
195+ self .assertIsInstance (attachment .file_fr , models .fields .files .FieldFile )
196+
197+ # Test that we can overwrite those with new content
198+ new_content = "new content"
199+ new_fr_content = "new French content"
200+ attachment .file = ContentFile (new_content , name = "content-new.txt" )
201+ attachment .file_fr = ContentFile (new_fr_content , name = "content-new-fr.txt" )
202+ self .assertIsInstance (attachment .file , models .fields .files .FieldFile )
203+ self .assertIsInstance (attachment .file_fr , models .fields .files .FieldFile )
204+
205+ attachment .save ()
206+
207+ self .assertIsInstance (attachment .file_fr , models .fields .files .FieldFile )
208+ self .assertIsInstance (attachment .file , models .fields .files .FieldFile )
209+
210+ with attachment .file .open () as f :
211+ self .assertEqual (f .read ().decode ("utf-8" ), new_content )
212+
213+ with attachment .file_fr .open () as f :
214+ self .assertEqual (f .read ().decode ("utf-8" ), new_fr_content )
215+
173216 def test_creating_using_virtual_default_language_field (self ):
174217 m = Blog .objects .create (title_en = "Falcon" )
175218
0 commit comments