From 25dc5b22006c30087a89c1e189b3e85c3bc383ff Mon Sep 17 00:00:00 2001 From: dogeystamp Date: Sun, 9 Apr 2023 15:04:22 -0400 Subject: [PATCH] storage: added tests for renaming --- sachet/storage/filesystem.py | 8 ++++---- tests/conftest.py | 25 ++++++++++++++----------- tests/test_storage.py | 36 +++++++++++++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 16 deletions(-) diff --git a/sachet/storage/filesystem.py b/sachet/storage/filesystem.py index 219cac4..a41dd25 100644 --- a/sachet/storage/filesystem.py +++ b/sachet/storage/filesystem.py @@ -78,14 +78,14 @@ class FileSystem(Storage): if not path.exists(): raise OSError(f"Path {path} does not exist.") new_path = self._get_path(new_name) - if path.exists(): + if new_path.exists(): raise OSError(f"Path {path} already exists.") meta_path = self._get_meta_path(name) - if not path.exists(): + if not meta_path.exists(): raise OSError(f"Path {meta_path} does not exist.") - new_meta_path = self._get_meta_path(name) - if path.exists(): + new_meta_path = self._get_meta_path(new_name) + if new_meta_path.exists(): raise OSError(f"Path {path} already exists.") path.rename(new_path) diff --git a/tests/conftest.py b/tests/conftest.py index e4b215d..f021635 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -20,6 +20,18 @@ def rand(): r.seed(0) return r +def clear_filesystem(): + if app.config["SACHET_STORAGE"] == "filesystem": + for file in itertools.chain( + storage._meta_directory.iterdir(), + storage._files_directory.iterdir(), + ): + if file.is_relative_to(Path(app.instance_path)) and file.is_file(): + file.unlink() + else: + raise OSError( + f"Attempted to delete {file}: please delete it yourself." + ) @pytest.fixture def client(config={}): @@ -32,20 +44,11 @@ def client(config={}): db.drop_all() db.create_all() db.session.commit() + clear_filesystem() yield client + clear_filesystem() db.session.remove() db.drop_all() - if app.config["SACHET_STORAGE"] == "filesystem": - for file in itertools.chain( - storage._meta_directory.iterdir(), - storage._files_directory.iterdir(), - ): - if file.is_relative_to(Path(app.instance_path)) and file.is_file(): - file.unlink() - else: - raise OSError( - f"Attempted to delete {file}: please delete it yourself." - ) @pytest.fixture diff --git a/tests/test_storage.py b/tests/test_storage.py index aa9f4e7..78c1ba4 100644 --- a/tests/test_storage.py +++ b/tests/test_storage.py @@ -10,7 +10,7 @@ from uuid import UUID @pytest.mark.parametrize("client", [{"SACHET_STORAGE": "filesystem"}], indirect=True) class TestSuite: def test_creation(self, client, rand): - """Test the process of creating, writing, then reading files with metadata.""" + """Test the process of creating, writing, then reading files with metadata, and also listing files.""" files = [ dict( @@ -38,3 +38,37 @@ class TestSuite: assert saved_data == file["data"] saved_meta = storage.read_metadata(file["name"]) assert saved_meta == file["metadata"] + + assert sorted([f.name for f in storage.list_files()]) == sorted( + [f["name"] for f in files] + ) + + def test_rename(self, client, rand): + files = [ + dict( + name=str(UUID(bytes=rand.randbytes(16))), + new_name=str(UUID(bytes=rand.randbytes(16))), + data=rand.randbytes(4000), + metadata=dict( + sdljkf=dict(abc="def", aaa="bbb"), + lkdsjf=dict(ld="sdlfj", sdljf="sdlkjf"), + sdlfkj="sjdlkfsldk", + ssjdklf=rand.randint(-1000, 1000), + ), + ) + for i in range(25) + ] + + for file in files: + storage.create(file["name"]) + with storage.open(file["name"], mode="wb") as f: + f.write(file["data"]) + storage.write_metadata(file["name"], file["metadata"]) + storage.rename(file["name"], file["new_name"]) + + for file in files: + with storage.open(file["new_name"], mode="rb") as f: + saved_data = f.read() + assert saved_data == file["data"] + saved_meta = storage.read_metadata(file["new_name"]) + assert saved_meta == file["metadata"]