def test_save(sheraf_temp_dir, sheraf_connection): FileStorable = get_file_storable(sheraf.FileAttribute()) _model = FileStorable.create() _model.file = {"stream": b"STREAM", "extension": "txt"} _model.save() _path = _model.file.relative_path() assert "file_storable_{}/file/{}.txt".format( sheraf.FileAttribute(), _model.id == _path, )
def test_already_removed(sheraf_temp_dir, sheraf_connection): FileStorable = get_file_storable(sheraf.FileAttribute()) model = FileStorable.create() model.file = sheraf.FileObject(stream=b"hello", extension="txt") model.save() model.file.delete() os.remove(model.file.absolute_path()) sheraf.FilesGarbageCollector.instance().clear()
def test_save_file(sheraf_temp_dir, sheraf_connection): FileStorable = get_file_storable(sheraf.FileAttribute()) _model = FileStorable.create() _model.file = sheraf.FileObject(stream=b"STREAM", extension="txt") _model.save() _model_from_base = FileStorable.read(_model.id) assert _model.file == _model_from_base.file
def test_retrocompatibility(sheraf_temp_dir, sheraf_connection): FileStorable = get_file_storable(sheraf.FileAttribute()) _model = FileStorable.create() _model.file = {"stream": b"STREAM", "extension": "txt"} _model.save() assert sheraf.FileObject(stream=b"STREAM", extension="txt") == _model.file _model.file["extension"] = "png" _model.file["stream"] = b"STREAM2" assert "png" == _model.file["extension"] assert b"STREAM2" == _model.file["stream"]
def test_path_in_db_but_file_not_exists(sheraf_temp_dir, sheraf_connection): FileStorable = get_file_storable(sheraf.FileAttribute()) s = FileStorable.create() file_path = os.path.join(FileStorable.table, "file", "file.txt") s.mapping["file"] = file_path assert s.file.relative_path() == file_path assert s.file.extension == "txt" assert not s.file.exists() with pytest.raises(IOError): assert s.file.stream s2 = FileStorable.create() with pytest.raises(IOError): s2.file = s.file
def test_readmapping_mapping(sheraf_temp_dir, sheraf_connection): FileStorable = get_file_storable(sheraf.FileAttribute()) s = FileStorable.create() directory = os.path.join(sheraf.attributes.files.FILES_ROOT_DIR, FileStorable.table, "file") os.makedirs(directory) file_path = os.path.join(directory, "file.txt") with open(file_path, "wb") as f: f.write(b"content") s.mapping["file"] = os.path.relpath(file_path, sheraf.attributes.files.FILES_ROOT_DIR) assert s.file.stream == b"content" assert s.file.extension == "txt"
def test_cler_files_garbage_collector_mixin(sheraf_temp_dir, sheraf_connection): FileStorable = get_file_storable(sheraf.FileAttribute()) model = FileStorable.create() model.file = sheraf.FileObject(stream=b"hello", extension="txt") model.save() model.file.delete() other = FileStorable.create() other.file = sheraf.FileObject(stream=b"world", extension="txt") other.save() sheraf.FilesGarbageCollector.instance().clear() assert not os.path.exists(model.file.absolute_path()) assert os.path.exists(other.file.absolute_path()) root = sheraf.Database.current_connection().root() assert not root["__sheraf_collected_files_to_remove"] assert not sheraf.FilesGarbageCollector.instance()
class FileStorable(tests.UUIDAutoModel): logo = sheraf.FileAttribute()
class FileStorable(tests.UUIDAutoModel): attr = sheraf.SimpleAttribute() file = sheraf.FileAttribute()
def test_savemapping_mapping(sheraf_temp_dir, sheraf_connection): FileStorable = get_file_storable(sheraf.FileAttribute()) s = FileStorable.create() s.file = sheraf.FileObject(stream=b"content", extension="txt") s.save() assert s.mapping["file"] == s.file.relative_path()