From a3ca6d804fb40543803977cde335ad468569c3d1 Mon Sep 17 00:00:00 2001 From: Angelo Probst Date: Tue, 24 Sep 2024 17:37:37 -0300 Subject: [PATCH] fixing some small things and tests --- hyperon_das_atomdb/adapters/ram_only.py | 13 ++++++++----- hyperon_das_atomdb/adapters/redis_mongo_db.py | 12 +++++++++--- hyperon_das_atomdb/database.py | 4 ++++ tests/unit/adapters/test_ram_only.py | 8 ++++---- tests/unit/test_database.py | 17 ++++------------- 5 files changed, 29 insertions(+), 25 deletions(-) diff --git a/hyperon_das_atomdb/adapters/ram_only.py b/hyperon_das_atomdb/adapters/ram_only.py index 80f67fc4..67047ee1 100644 --- a/hyperon_das_atomdb/adapters/ram_only.py +++ b/hyperon_das_atomdb/adapters/ram_only.py @@ -180,7 +180,7 @@ def _add_outgoing_set(self, key: str, targets_hash: list[str]) -> None: key (str): The key for the outgoing set. targets_hash (list[str]): A list of target hashes to be added to the outgoing set. """ - self.db.outgoing_set[key] = targets_hash + self.db.outgoing_set[key] = set(targets_hash) def _get_and_delete_outgoing_set(self, handle: str) -> list[str] | None: """ @@ -192,7 +192,7 @@ def _get_and_delete_outgoing_set(self, handle: str) -> list[str] | None: Returns: list[str] | None: The outgoing set if found and deleted, otherwise None. """ - return self.db.outgoing_set.pop(handle, None) + return list(self.db.outgoing_set.pop(handle)) if handle in self.db.outgoing_set else None def _add_incoming_set(self, key: str, targets_hash: list[str]) -> None: """ @@ -512,7 +512,7 @@ def get_link_type(self, link_handle: str) -> str | None: def get_link_targets(self, link_handle: str) -> list[str]: answer = self.db.outgoing_set.get(link_handle) if answer is not None: - return answer + return list(answer) logger().error( f"Failed to retrieve link targets for {link_handle}. This link may not exist." ) @@ -639,8 +639,11 @@ def add_node(self, node_params: NodeParamsT) -> NodeT | None: self._update_index(node) return node - def add_link(self, link_params: LinkParamsT, toplevel: bool = True) -> LinkT: - handle, link, _ = self._build_link(link_params, toplevel) + def add_link(self, link_params: LinkParamsT, toplevel: bool = True) -> LinkT | None: + r = self._build_link(link_params, toplevel) + if r is None: + return None + handle, link, _ = r self.db.link[handle] = link self._update_index(link) return link diff --git a/hyperon_das_atomdb/adapters/redis_mongo_db.py b/hyperon_das_atomdb/adapters/redis_mongo_db.py index dd474ffc..908f7281 100644 --- a/hyperon_das_atomdb/adapters/redis_mongo_db.py +++ b/hyperon_das_atomdb/adapters/redis_mongo_db.py @@ -719,7 +719,10 @@ def get_link_type(self, link_handle: str) -> str | None: return document[FieldNames.TYPE_NAME] def _get_atom(self, handle: str) -> AtomT | None: - return self.get_atom_as_dict(handle) + try: + return self.get_atom_as_dict(handle) + except AtomDoesNotExist: + return None def get_atom_type(self, handle: str) -> str | None: atom = self._retrieve_document(handle) @@ -737,8 +740,11 @@ def get_atom_as_dict(self, handle: str, arity: int | None = 0) -> AtomT: else: document["name"] = document["name"] return document - else: - return None + logger().error(f"Failed to retrieve atom for handle: {handle}. This link may not exist.") + raise AtomDoesNotExist( + message="Nonexistent atom", + details=f"handle: {handle}", + ) def count_atoms(self, parameters: dict[str, Any] | None = None) -> dict[str, int]: atom_count = self.mongo_atoms_collection.estimated_document_count() diff --git a/hyperon_das_atomdb/database.py b/hyperon_das_atomdb/database.py index 783cf5fc..a8e5be12 100644 --- a/hyperon_das_atomdb/database.py +++ b/hyperon_das_atomdb/database.py @@ -251,10 +251,14 @@ def _build_link( raise ValueError("The target must be a dictionary") if "targets" not in target: atom = self.add_node(target) + if atom is None: + return None atom_hash = atom["composite_type_hash"] composite_type.append(atom_hash) else: atom = self.add_link(target, toplevel=False) + if atom is None: + return None atom_hash = atom["composite_type_hash"] composite_type.append(atom["composite_type"]) composite_type_hash.append(atom_hash) diff --git a/tests/unit/adapters/test_ram_only.py b/tests/unit/adapters/test_ram_only.py index 3a8f7592..b4df70d1 100644 --- a/tests/unit/adapters/test_ram_only.py +++ b/tests/unit/adapters/test_ram_only.py @@ -760,8 +760,8 @@ def test_delete_atom(self): }, } assert db.db.outgoing_set == { - inheritance_dog_mammal_handle: [dog_handle, mammal_handle], - inheritance_cat_mammal_handle: [cat_handle, mammal_handle], + inheritance_dog_mammal_handle: {dog_handle, mammal_handle}, + inheritance_cat_mammal_handle: {cat_handle, mammal_handle}, } assert db.db.templates == { "41c082428b28d7e9ea96160f7fd614ad": { @@ -904,7 +904,7 @@ def test_delete_atom(self): dog_handle: {inheritance_dog_mammal_handle}, mammal_handle: {inheritance_dog_mammal_handle}, } - assert db.db.outgoing_set == {inheritance_dog_mammal_handle: [dog_handle, mammal_handle]} + assert db.db.outgoing_set == {inheritance_dog_mammal_handle: {dog_handle, mammal_handle}} assert db.db.templates == { "41c082428b28d7e9ea96160f7fd614ad": { inheritance_dog_mammal_handle, @@ -956,7 +956,7 @@ def test_delete_atom(self): cat_handle: {inheritance_cat_mammal_handle}, mammal_handle: {inheritance_cat_mammal_handle}, } - assert db.db.outgoing_set == {inheritance_cat_mammal_handle: [cat_handle, mammal_handle]} + assert db.db.outgoing_set == {inheritance_cat_mammal_handle: {cat_handle, mammal_handle}} assert db.db.templates == { "41c082428b28d7e9ea96160f7fd614ad": { inheritance_cat_mammal_handle, diff --git a/tests/unit/test_database.py b/tests/unit/test_database.py index 7afff599..39e7a097 100644 --- a/tests/unit/test_database.py +++ b/tests/unit/test_database.py @@ -3,6 +3,7 @@ import pytest from hyperon_das_atomdb.database import AtomDB +from hyperon_das_atomdb.exceptions import AtomDoesNotExist from .fixtures import in_memory_db, redis_mongo_db # noqa: F401 @@ -704,9 +705,7 @@ def test__get_atom(self, database, request): def test__get_atom_none(self, database, request): db: AtomDB = request.getfixturevalue(database) node = db._get_atom("handle") - link = db._get_atom("handle") assert node is None - assert link is None @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_atom_type(self, database, request): @@ -738,18 +737,10 @@ def test_get_atom_as_dict(self, database, request): assert isinstance(atom_link, dict) @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) - def test_get_atom_as_dict_none(self, database, request): - if database == "in_memory_db": - # TODO: fix this - pytest.skip( - "ERROR in_memory raises exception, they should return the same result/exception. " - "See https://github.com/singnet/das-atom-db/issues/210" - ) + def test_get_atom_as_dict_exception(self, database, request): db: AtomDB = request.getfixturevalue(database) - atom_node = db.get_atom_as_dict("handle") - atom_link = db.get_atom_as_dict("handle") - assert atom_node is None - assert atom_link is None + with pytest.raises(AtomDoesNotExist, match="Nonexistent atom"): + db.get_atom_as_dict("handle") @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_atom_as_dict_exceptions(self, database, request):