Skip to content

Commit

Permalink
fixing some small things and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloprobst committed Sep 24, 2024
1 parent 5610706 commit a3ca6d8
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 25 deletions.
13 changes: 8 additions & 5 deletions hyperon_das_atomdb/adapters/ram_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand All @@ -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:
"""
Expand Down Expand Up @@ -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."
)
Expand Down Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions hyperon_das_atomdb/adapters/redis_mongo_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions hyperon_das_atomdb/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/adapters/test_ram_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 4 additions & 13 deletions tests/unit/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit a3ca6d8

Please sign in to comment.