From 34609c574a366cc0b3052d7e069997646f4b16a7 Mon Sep 17 00:00:00 2001 From: JieguangZhou Date: Fri, 23 Feb 2024 12:02:24 +0800 Subject: [PATCH] Add integration test for mongodb saving files and folders --- test/integration/artifacts/__init__.py | 0 test/integration/artifacts/test_mongodb.py | 69 ++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 test/integration/artifacts/__init__.py create mode 100644 test/integration/artifacts/test_mongodb.py diff --git a/test/integration/artifacts/__init__.py b/test/integration/artifacts/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/integration/artifacts/test_mongodb.py b/test/integration/artifacts/test_mongodb.py new file mode 100644 index 0000000000..563e2a1273 --- /dev/null +++ b/test/integration/artifacts/test_mongodb.py @@ -0,0 +1,69 @@ +import dataclasses as dc +import filecmp +import os +import typing as t + +import pytest + +from superduperdb.backends.local.artifacts import FileSystemArtifactStore +from superduperdb.components.component import Component +from superduperdb.components.datatype import ( + DataType, + file_serializer, + serializers, +) + + +@dc.dataclass(kw_only=True) +class TestComponent(Component): + path: str + type_id: t.ClassVar[str] = "TestComponent" + + _artifacts: t.ClassVar[t.Sequence[t.Tuple[str, "DataType"]]] = ( + ("path", file_serializer), + ) + + +@pytest.fixture +def artifact_strore(tmpdir) -> FileSystemArtifactStore: + artifact_strore = FileSystemArtifactStore(f"{tmpdir}") + artifact_strore._serializers = serializers + return artifact_strore + + +def test_save_and_load_directory(test_db): + # test save and load directory + directory = os.path.join(os.getcwd(), "superduperdb") + test_component = TestComponent(path=directory, identifier="test") + test_db.add(test_component) + test_component_loaded = test_db.load("TestComponent", "test") + test_component_loaded.init() + # assert that the paths are different + assert test_component.path != test_component_loaded.path + # assert that the directory names are the same + assert ( + os.path.split(test_component.path)[-1] + == os.path.split(test_component_loaded.path)[-1] + ) + # assert that the directory sizes are the same + assert os.path.getsize(test_component.path) == os.path.getsize( + test_component_loaded.path + ) + + +def test_save_and_load_file(test_db): + # test save and load file + file = os.path.abspath(__file__) + test_component = TestComponent(path=file, identifier="test") + test_db.add(test_component) + test_component_loaded = test_db.load("TestComponent", "test") + test_component_loaded.init() + # assert that the paths are different + assert test_component.path != test_component_loaded.path + # assert that the file names are the same + assert ( + os.path.split(test_component.path)[-1] + == os.path.split(test_component_loaded.path)[-1] + ) + # assert that the file sizes are the same + assert filecmp.cmp(test_component.path, test_component_loaded.path)