def test_adding_extzipfile(self): # ZipExtFile advertises seek() but can raise UnsupportedOperation with ZipFile(os.path.join(DIR, "fixtures/test.zip")) as testzip: for filename in testzip.namelist(): with testzip.open(filename) as testfile: o = Thing() o.upload = DjangoFile(testfile) o.save() with testzip.open(filename) as testfile: uploaded = File.objects.get(name="i/special/" + filename) self.assertEqual(uploaded.size, testzip.getinfo(filename).file_size) self.assertEqual( BytesIO(uploaded.content).read(), testfile.read())
def test_adding_file(self): # Create default thing storing reference to file # in the local media directory. test_fqfn = os.path.join(self.media_dir, "test.txt") open(test_fqfn, "w").write("hello there") o1 = o = Thing() test_fn = "i/special/test.txt" o.upload = test_fn o.save() obj_id = o.id # Confirm thing was saved. Thing.objects.update() q = Thing.objects.all() self.assertEqual(q.count(), 1) self.assertEqual(q[0].upload.name, test_fn) # Confirm the file only exists on the file system # and hasn't been loaded into the database. q = File.objects.all() self.assertEqual(q.count(), 0) # Verify that the storage reports that the file exists self.assertTrue(o.upload.storage.exists(o.upload.name)) # Verify we can read the contents of thing. o = Thing.objects.get(id=obj_id) self.assertEqual(o.upload.read(), b"hello there") # Verify that by attempting to read the file, we've automatically # loaded it into the database. File.objects.update() q = File.objects.all() self.assertEqual(q.count(), 1) self.assertEqual(BytesIO(q.first().content).getvalue(), b"hello there") # Load a dynamically created file outside /media. test_file = files.temp.NamedTemporaryFile( suffix=".txt", # Django>=1.10 no longer allows accessing files outside of MEDIA_ROOT... dir=os.path.join(settings.PROJECT_DIR, "media"), ) data0 = b"1234567890" test_file.write(data0) test_file.seek(0) t = Thing.objects.create(upload=files.File(test_file), ) self.assertEqual(File.objects.count(), 2) t = Thing.objects.get(pk=t.pk) self.assertEqual(t.upload.file.size, 10) self.assertEqual(t.upload.file.name[-4:], ".txt") self.assertEqual(t.upload.file.read(), data0) t.upload.delete() self.assertEqual(File.objects.count(), 1) # Delete file from local filesystem and re-export it from the database. self.assertEqual(os.path.isfile(test_fqfn), True) os.remove(test_fqfn) self.assertEqual(os.path.isfile(test_fqfn), False) # Verify that the storage still reports that the file exists self.assertTrue(o1.upload.storage.exists(o1.upload.name)) o1.upload.read() # This forces the re-export to the filesystem. self.assertEqual(os.path.isfile(test_fqfn), True) # This dumps all files to the filesystem. File.dump_files() # Confirm when delete a file from the database, we also delete it from # the filesystem. self.assertEqual(default_storage.exists("i/special/test.txt"), True) default_storage.delete("i/special/test.txt") self.assertEqual(default_storage.exists("i/special/test.txt"), False) self.assertEqual(os.path.isfile(test_fqfn), False)
class Meta: managed = False db_table = Thing()._meta.db_table