def test_delete_file(self): """ Deleting file from local storage """ # put file self.prepare_uploads() backend = BackendLocal(self.path) uploads = self.upload_path src = os.path.join(uploads, 'test.tar.gz') id1 = utils.generate_id('test.tar.gz') # regression testing id2 = id1.split('-') id2[4] += 'ZZZ' id2 = '-'.join(id2) backend.put_variant(src, id1, 'original.tar.gz') backend.put_variant(src, id2, 'original.tar.gz') backend.delete(id1) path1 = os.path.join(self.path, *id1.split('-')[0:6], 'original.tar.gz') self.assertFalse(os.path.exists(path1)) # assume only proper file deleted path2 = os.path.join(self.path, *id2.split('-')[0:6], 'original.tar.gz') self.assertTrue(os.path.exists(path2))
def test_put_raises_on_overwriting(self): """ Put raises exception on attempt to overwrite existing path """ self.prepare_uploads() backend = BackendLocal(self.path) uploads = self.upload_path src1 = os.path.join(uploads, 'demo-test.tar.gz') src2 = os.path.join(uploads, 'test.jpg') id = utils.generate_id('demo-test.tar.gz') backend.put_variant(src1, id, 'demo-test.tar.gz') with assert_raises(x.FileExists): backend.put_variant(src2, id, 'demo-test.tar.gz')
def test_force_put_to_overwrite_existing(self): """ Using force option to overwrite existing file """ self.prepare_uploads() backend = BackendLocal(self.path) uploads = self.upload_path filename = 'demo-test.tar.gz' src1 = os.path.join(uploads, filename) src2 = os.path.join(uploads, 'test.jpg') id = utils.generate_id(filename) backend.put_variant(src1, id, filename) backend.put_variant(src2, id, filename, True) path = os.path.join(backend.path, *backend.id_to_path(id), filename) # assert overwritten with src2 self.assertEquals(os.path.getsize(path), os.path.getsize(src2))
def test_put_with_sequential_ids(self): """ Putting two items in sequence""" filename = 'demo-test.tar.gz' base_id = utils.generate_id(filename).replace('-' + filename, '') id1 = base_id + '1-' + filename id2 = base_id + '2-' + filename self.prepare_uploads() backend = BackendLocal(self.path) uploads = self.upload_path src = os.path.join(uploads, 'demo-test.tar.gz') backend.put_variant(src, id1, 'demo-test.tar.gz') backend.put_variant(src, id2, 'demo-test.tar.gz') path1 = os.path.join(backend.path, *backend.id_to_path(id1), filename) path2 = os.path.join(backend.path, *backend.id_to_path(id2), filename) self.assertTrue(os.path.exists(path1)) self.assertTrue(os.path.exists(path2))
def test_put_variant(self): """ Put file variant to storage by filename""" self.prepare_uploads() backend = BackendLocal(self.path) uploads = self.upload_path src = os.path.join(uploads, 'demo-test.tar.gz') id = utils.generate_id('demo-test.tar.gz') backend.put_variant(src, id, 'demo-test.tar.gz') # assert directories created current = self.path for dir in backend.id_to_path(id): current = os.path.join(current, dir) self.assertTrue(os.path.exists(current)) # assert file put full_file_path = os.path.join(current, 'demo-test.tar.gz') self.assertTrue(os.path.exists(full_file_path))
def test_clear_variants(self): """ Clearing generated variants""" self.prepare_uploads() backend = BackendLocal(self.path) src1 = os.path.join(self.upload_path, 'demo-test.tar.gz') id1 = utils.generate_id('demo-test.tar.gz') backend.put(src1, id1) backend.put_variant(src1, id1, 'variant1.tar.gz') backend.put_variant(src1, id1, 'variant2.tar.gz') src2 = os.path.join(self.upload_path, 'demo-test.tar.gz') id2 = utils.generate_id('demo-test.tar.gz') backend.put(src2, id2) backend.put_variant(src2, id2, 'variant3.jpg') backend.put_variant(src2, id2, 'variant4.jpg') backend.clear_variants() path1 = os.path.join(self.path, *backend.id_to_path(id1)) original1 = path1 + '/demo-test.tar.gz' variant1 = path1 + '/variant1.tar.gz' variant2 = path1 + '/variant2.tar.gz' path2 = os.path.join(self.path, *backend.id_to_path(id2)) original2 = path2 + '/demo-test.tar.gz' variant3 = path2 + '/variant3.tar.gz' variant4 = path2 + '/variant4.tar.gz' self.assertTrue(os.path.exists(original1)) self.assertFalse(os.path.exists(variant1)) self.assertFalse(os.path.exists(variant2)) self.assertTrue(os.path.exists(original2)) self.assertFalse(os.path.exists(variant3)) self.assertFalse(os.path.exists(variant4))
def test_put_raises_on_nonexistent_file(self): """ Put raises exception if source file does not exist """ backend = BackendLocal(self.path) id = utils.generate_id('test.tar.gz') with assert_raises(x.LocalFileNotFound): backend.put_variant('nonexistent', id, 'random.tar.gz')