def test_compute_hash(self): with media_root(): storage = HashedFilenameFileSystemStorage() self.assertEqual( storage._compute_hash(content=ContentFile(self.CONTENT)), self.SHA1SUM ) self.assertEqual( storage._compute_hash(content=ContentFile(self.CONTENT), chunk_size=1), self.SHA1SUM )
def test_get_bytes_content_name(self): with media_root(): storage = HashedFilenameFileSystemStorage() self.assertEqual( storage._get_content_name(name='', content=ContentFile(self.BCONTENT)), '%s' % self.SHA1SUM) self.assertEqual( storage._get_content_name(name='', content=ContentFile(self.BCONTENT), chunk_size=1), '%s' % self.SHA1SUM) self.assertEqual( storage._get_content_name(name='foo', content=ContentFile(self.BCONTENT)), '%s' % self.SHA1SUM) self.assertEqual( storage._get_content_name(name='foo.txt', content=ContentFile(self.BCONTENT)), '%s.txt' % self.SHA1SUM) self.assertEqual( storage._get_content_name(name='foo/bar', content=ContentFile(self.BCONTENT)), 'foo/%s' % self.SHA1SUM) self.assertEqual( storage._get_content_name(name='foo/bar.txt', content=ContentFile(self.BCONTENT)), 'foo/%s.txt' % self.SHA1SUM)
def test_save_bytes_content(self): with media_root(): storage = HashedFilenameFileSystemStorage() name1 = storage.save('foo/bar.txt', ContentFile(self.BCONTENT)) self.assertEqual(name1, 'foo/%s.txt' % self.SHA1SUM) self.assertEqual(storage.open(name1, 'rb').read(), self.BCONTENT) storage.delete(name1) name2 = storage.save('foo/bar.txt', ContentFile(self.BCONTENT)) self.assertEqual(name2, name1) self.assertEqual(storage.open(name2, 'rb').read(), self.BCONTENT) name3 = storage.save('foo/another.txt', ContentFile(self.BCONTENT)) self.assertEqual(name3, name1) self.assertEqual(storage.open(name3, 'rb').read(), self.BCONTENT)
def test_get_content_name(self): with media_root(): storage = HashedFilenameFileSystemStorage() self.assertEqual( storage._get_content_name(name='', content=ContentFile(self.CONTENT)), '%s' % self.SHA1SUM ) self.assertEqual( storage._get_content_name(name='', content=ContentFile(self.CONTENT), chunk_size=1), '%s' % self.SHA1SUM ) self.assertEqual( storage._get_content_name(name='foo', content=ContentFile(self.CONTENT)), '%s' % self.SHA1SUM ) self.assertEqual( storage._get_content_name(name='foo.txt', content=ContentFile(self.CONTENT)), '%s.txt' % self.SHA1SUM ) self.assertEqual( storage._get_content_name(name='foo/bar', content=ContentFile(self.CONTENT)), 'foo/%s' % self.SHA1SUM ) self.assertEqual( storage._get_content_name(name='foo/bar.txt', content=ContentFile(self.CONTENT)), 'foo/%s.txt' % self.SHA1SUM )
def test_get_available_name_overridden_on_save(self): with media_root(): storage = HashedFilenameFileSystemStorage() # get_available_name returns the filename given, but is overridden # on save self.assertEqual(storage.get_available_name('foo/bar.txt'), 'foo/bar.txt') name1 = storage.save('foo/bar.txt', ContentFile(self.CONTENT)) self.assertEqual(name1, 'foo/%s.txt' % self.SHA1SUM) self.assertEqual(storage.open(name1, 'rt').read(), self.CONTENT) self.assertTrue( os.path.exists( os.path.join(settings.MEDIA_ROOT, 'foo/%s.txt' % self.SHA1SUM))) self.assertFalse( os.path.exists(os.path.join(settings.MEDIA_ROOT, 'foo/bar.txt')))
def test_get_available_name_overridden_on_save(self): with media_root(): storage = HashedFilenameFileSystemStorage() # get_available_name returns the filename given, but is overridden # on save self.assertEqual( storage.get_available_name('foo/bar.txt'), 'foo/bar.txt' ) name1 = storage.save('foo/bar.txt', ContentFile(self.CONTENT)) self.assertEqual(name1, 'foo/%s.txt' % self.SHA1SUM) self.assertEqual(storage.open(name1, 'rt').read(), self.CONTENT) self.assertTrue( os.path.exists( os.path.join(settings.MEDIA_ROOT, 'foo/%s.txt' % self.SHA1SUM) ) ) self.assertFalse( os.path.exists(os.path.join(settings.MEDIA_ROOT, 'foo/bar.txt')) )
def test_get_available_name(self): with media_root(): storage = HashedFilenameFileSystemStorage() self.assertEqual(storage.get_available_name('foo.txt'), 'foo.txt')
def test_get_available_name(self): with media_root(): storage = HashedFilenameFileSystemStorage() self.assertRaises(NoAvailableName, storage.get_available_name, '')