def test_save_safe_storage(self): StorageClass = RandomFilenameMetaStorage(storage_class=StubSafeStorage) storage = StorageClass(tries=3) stub_random_string.count = 0 with patch(django_randomfilenamestorage.storage, random_string=stub_random_string): # stub_random_string() is called 4 times, when attempting # to save three times. self.assertEqual(storage._save('name.txt'), '2.txt')
def test_save(self): with media_root(): storage = SafeFileSystemStorage() # Save one copy content = 'Hello world!' name = storage.save('hello.txt', ContentFile(content)) self.assertEqual(name, 'hello.txt') self.assertEqual(open(storage.path(name)).read(), content) # Save another, which should be renamed content = 'Hello.' name = storage._save('hello.txt', ContentFile(content)) self.assertTrue(name in ('hello_.txt', 'hello_1.txt'), "%r is not 'hello_.txt' or 'hello_1.txt'" % name) self.assertEqual(open(storage.path(name)).read(), content)
def test_save_broken_retry(self): StorageClass = RandomFilenameMetaStorage(storage_class=StubSafeStorage) class BrokenStorage(StorageClass): def get_available_name(self, name): return super(BrokenStorage, self).get_available_name(name) storage = BrokenStorage(tries=3) stub_random_string.count = 0 with patch(django_randomfilenamestorage.storage, random_string=stub_random_string): with catch_warnings(): warnings.simplefilter('ignore') # stub_random_string() is called four times, when attempting # to save three times self.assertEqual(storage._save('name.txt'), '4.txt')