def test_create_auto_crop_from_filename(self): """ Creating auto crop resize from filename """ uploads = self.upload_path path = self.path self.prepare_uploads() filename = 'original_vertical.jpg' src = os.path.join(uploads, filename) backend = BackendLocal(path) storage = Storage(backend, secret_key=self.config.SECRET_KEY, local_temp=self.config.LOCAL_TEMP) id = storage.put(src) resize_url = storage.get_auto_crop_url(id, '100x200', 'fill') storage.create_resize(resize_url) resize_filename = resize_url.split('/')[-1] tmp_path = os.path.join(self.tmp_path, id) tmp_original = os.path.join(tmp_path, filename) tmp_resize = os.path.join(tmp_path, resize_filename) parts = backend.id_to_path(id) storage_resize = os.path.join(path, *parts, resize_filename) # assert tmp stuff deleted self.assertFalse(os.path.exists(tmp_original)) self.assertFalse(os.path.exists(tmp_resize)) self.assertFalse(os.path.exists(tmp_path)) # assert put to storage self.assertTrue(os.path.exists(storage_resize))
def test_convert_id_to_path(self): """ Converting id to path """ backend = BackendLocal(self.path) filename = 'demo-test.tar.gz' id = utils.generate_id(filename) parts = backend.id_to_path(id) self.assertEquals(6, len(parts)) self.assertEquals(filename, parts[5])
def test_lowercase_filename_when_putting(self): """REGRESSEION (#9): Convert file names to lowercase """ self.prepare_uploads() backend = BackendLocal(self.path) uploads = self.upload_path src = os.path.join(uploads, 'MiXeDcAsE.jpg') id = utils.generate_id('MiXeDcAsE.jpg') result = backend.put(src, id) current = self.path for dir in id.split('-')[0:5]: current = os.path.join(current, dir) full_file_path = os.path.join(current, 'MiXeDcAsE.jpg') self.assertTrue(os.path.exists(full_file_path.lower()))
def test_retrieve_original_to_temp(self): """ Retrieving from backend to local temp """ # put file self.prepare_uploads() backend = BackendLocal(self.path) src = os.path.join(self.upload_path, 'demo-test.tar.gz') id = utils.generate_id('demo-test.tar.gz') backend.put(src, id) # retrieve file result = backend.retrieve_original(id, self.tmp_path) expected_dst = os.path.join(self.tmp_path, id, 'demo-test.tar.gz') self.assertEquals(expected_dst, result) self.assertTrue(os.path.exists(expected_dst))
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_file(self): """ Put file to storage """ 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(src, id) # assert directories created current = self.path for dir in id.split('-')[0:5]: 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_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_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_get_original_url(self): """ Generating original url """ path = self.path base_url = 'http://test.url' backend = BackendLocal(path, base_url) storage = Storage(backend, secret_key=self.config.SECRET_KEY, local_temp=self.config.LOCAL_TEMP) filename = 'example-object.tar.gz' id = utils.generate_id(filename) url = storage.get_original_url(id) self.assertTrue(url.startswith(base_url)) self.assertTrue(url.endswith(filename + '/' + filename))
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_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_get_manual_crop_url(self): """ Generating manual crop url """ path = self.path base_url = 'http://test.url' backend = BackendLocal(path, base_url) storage = Storage(backend, secret_key=self.config.SECRET_KEY, local_temp=self.config.LOCAL_TEMP) filename = 'example-object.tar.gz' id = utils.generate_id(filename) url = storage.get_manual_crop_url(id=id, sample_size='200x400', target_size='100x200', output_format='gif', upscale=True, quality=80) self.assertTrue(url.startswith(base_url)) url = url.replace(base_url, '').strip('/').split('/') self.assertEquals(filename, url[5]) self.assertTrue(url[6].startswith('100x200-200x400-80-upscale')) self.assertTrue(url[6].endswith('.gif'))
def test_parse_url(self): """ Parsing object url into id and filename """ filename = 'demo-file.tar.gz' backend = BackendLocal(self.path) pb = PathBuilder('123456') base_url = backend.get_url() id = utils.generate_id(filename) parts = backend.id_to_path(id) path = '/'.join(parts) object_url = base_url + '/' + path + '/' original = object_url + filename crop_filename = pb.get_auto_crop_filename(id, '100x100', 'fit', 'jpg') resize = object_url + crop_filename result1 = backend.parse_url(original) result2 = backend.parse_url(resize) self.assertEquals(id, result1[0]) self.assertEquals(filename, result1[1]) self.assertEquals(id, result2[0]) self.assertEquals(crop_filename, result2[1])
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')
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_can_instantiate_backend(self): """ Can instantiate backend """ backend = BackendLocal(self.path) self.assertIsInstance(backend, BackendLocal)
def test_getting_path_creates_directory(self): """ Can create local path upon getting """ self.assertFalse(os.path.exists(self.path)) backend = BackendLocal(self.path) path = backend.path self.assertTrue(os.path.exists(path))