def test_pyfs_delete(app, db, dummy_location): """Test init of files.""" testurl = join(dummy_location.uri, 'subpath/data') s = PyFSFileStorage(testurl) s.initialize(size=100) assert exists(testurl) s.delete() assert not exists(testurl) s = PyFSFileStorage(join(dummy_location.uri, 'anotherpath/data')) pytest.raises(ResourceNotFoundError, s.delete)
def put(self, **kwargs): """Create a uuid and return a links dict. file upload step is below ② upload file to server. :returns: Json Response have a links dict. """ fn = request.view_args['key'] # if ".xsd" not in fn: # abort(405, "Xsd File only !!") pid = request.view_args['pid_value'] furl = self.xsd_location_folder + 'tmp/' + pid + '/' + fn # size = len(request.data) try: fs = PyFSFileStorage(furl) fileurl, bytes_written, checksum = fs.save(request.stream) except Exception: raise InvalidDataRESTError() else: pass jd = { 'key': fn, 'mimetype': request.mimetype, 'links': {}, 'size': bytes_written } data = dict(key=fn, mimetype='text/plain') response = current_app.response_class(json.dumps(jd), mimetype='application/json') response.status_code = 200 return response
def test_pyfs_copy(pyfs, dummy_location): """Test send file.""" s = PyFSFileStorage(join(dummy_location.uri, 'anotherpath/data')) s.save(BytesIO(b'otherdata')) pyfs.copy(s) fp = pyfs.open() assert fp.read() == b'otherdata'
def test_pyfs_checksum_fail(): """Test fixity problems.""" # Raise an error during checksum calculation def callback(total, size): raise OSError(errno.EPERM, "Permission") s = PyFSFileStorage('LICENSE', size=getsize('LICENSE')) pytest.raises(StorageError, s.checksum, progress_callback=callback)
def test_pyfs_checksum(get_md5): """Test fixity.""" # Compute checksum of license file/ with open('LICENSE', 'rb') as fp: data = fp.read() checksum = get_md5(data) counter = dict(size=0) def callback(total, size): counter['size'] = size # Now do it with storage interface s = PyFSFileStorage('LICENSE', size=getsize('LICENSE')) assert checksum == s.checksum(chunk_size=2, progress_callback=callback) assert counter['size'] == getsize('LICENSE') # No size provided, means progress callback isn't called counter['size'] = 0 s = PyFSFileStorage('LICENSE') assert checksum == s.checksum(chunk_size=2, progress_callback=callback) assert counter['size'] == 0
def test_copy(s3_bucket, s3fs): """Test copy file.""" data = b'test' s3fs.save(BytesIO(data)) s3_copy_path = 's3://{}/path/to/copy/data'.format(s3_bucket.name) s3fs_copy = S3FSFileStorage(s3_copy_path) s3fs_copy.copy(s3fs) assert s3fs_copy.open().read() == data tmppath = tempfile.mkdtemp() s = PyFSFileStorage(os.path.join(tmppath, 'anotherpath/data')) data = b'othertest' s.save(BytesIO(data)) s3fs_copy.copy(s) assert s3fs_copy.open().read() == data shutil.rmtree(tmppath)
def pyfs(dummy_location, pyfs_testpath): """Instance of PyFSFileStorage.""" return PyFSFileStorage(pyfs_testpath)