def test_recursion(sheraf_temp_dir, sheraf_connection): class FileStorable(tests.UUIDAutoModel): my_file = sheraf.FileAttribute() m = FileStorable.create() m.my_file = sheraf.FileObject(stream=b"", extension="txt") m.save() m = FileStorable.read(m.id) m.delete() # il ne faut pas de recursion
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_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()
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_directory_removed(sheraf_temp_dir, sheraf_connection): class FileStorable(tests.UUIDAutoModel): attr = sheraf.SimpleAttribute() file = sheraf.FileAttribute() model = FileStorable.create() model.file = sheraf.FileObject(stream=b"hello", extension="txt") model.save() os.remove(model.file.absolute_path()) os.rmdir(os.path.dirname(model.file.absolute_path())) sheraf.FilesGarbageCollector.instance().clear()
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_copy(sheraf_temp_dir, sheraf_connection): class FileStorable(tests.UUIDAutoModel): logo = sheraf.FileAttribute() s1 = FileStorable.create() s1.logo = sheraf.FileObject(extension="EXT", stream=b"STREAM") s1.save() s2 = FileStorable.create() s2.logo = s1.logo s2.save() assert s1.logo.stream == s2.logo.stream assert s1.logo.relative_path() != s2.logo.relative_path()
def test_optional_files(sheraf_temp_dir, sheraf_connection): class FileStorable(tests.UUIDAutoModel): logo = sheraf.FileAttribute() _files_dir = sheraf.attributes.files.FILES_ROOT_DIR + FileStorable.table + "/logo/" s = FileStorable.create() s.logo = {"extension": "EXT", "stream": b"OPT_STREAM"} s.save() _s = FileStorable.read(s.id) assert _s.logo == sheraf.FileObject(extension="EXT", stream=b"OPT_STREAM") logo_path = _files_dir + s.id + ".EXT" assert os.path.exists(logo_path) with open(logo_path, "rb") as f: _content = f.read() assert b"OPT_STREAM" == _content
def test_delete_model_with_file_attribute_and_relative_root_file_dir( sheraf_temp_dir, sheraf_connection): class FileStorable(tests.UUIDAutoModel): my_file = sheraf.FileAttribute() s = FileStorable.create() s.my_file = sheraf.FileObject(stream=b"yolo", extension="txt") s.save() rel_path = os.path.join(FileStorable.table, "my_file", s.id + ".txt") abs_path = os.path.join(sheraf.attributes.files.FILES_ROOT_DIR, rel_path) assert os.path.exists(abs_path) s.delete() assert os.path.exists(abs_path) assert rel_path in sheraf.FilesGarbageCollector.instance() assert abs_path in sheraf.FilesGarbageCollector.instance() assert (rel_path in sheraf.Database.current_connection().root() ["__sheraf_collected_files_to_remove"]) shutil.rmtree(sheraf.attributes.files.FILES_ROOT_DIR)
def test_not_model(sheraf_temp_dir, sheraf_connection): file = sheraf.FileObject(stream=b"STREAM", extension="txt") assert file.relative_path() is None
def test_attributes(sheraf_temp_dir, sheraf_connection): _file = sheraf.FileObject(stream=b"STREAM", extension="txt") assert b"STREAM" == _file.stream assert "txt" == _file.extension
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()