Skip to content

Commit

Permalink
add unit test to LocalGetLinks iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
marcocapozzoli committed Feb 27, 2024
1 parent b2e4986 commit 27d8c81
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
2 changes: 1 addition & 1 deletion hyperon_das/traverse_engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def get_links(self, **kwargs) -> QueryAnswerIterator:
no_iterator=False,
targets_document=True,
cursor=0,
chunk_size=kwargs.get('chunk_size', 500)
chunk_size=kwargs.get('chunk_size', 500),
)
return TraverseLinksIterator(source=incoming_links, cursor=self._cursor['handle'], **kwargs)

Expand Down
76 changes: 75 additions & 1 deletion tests/unit/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

from hyperon_das import DistributedAtomSpace
from hyperon_das.cache import ListIterator, LocalIncomingLinks, ProductIterator
from hyperon_das.cache import ListIterator, LocalGetLinks, LocalIncomingLinks, ProductIterator
from hyperon_das.utils import Assignment
from tests.unit.mock import up_knowledge_base_animals, up_n_links_in_database

Expand Down Expand Up @@ -227,3 +227,77 @@ def mock_get_incoming_links_redis_mongo(atom_handle: str, **kwargs):
with pytest.raises(StopIteration):
it.get()
_db_down()

def test_local_get_links_ram_only_iterator(self):
das = DistributedAtomSpace()
up_knowledge_base_animals(das)
link_type = 'Inheritance'
answer = das.query_engine._get_related_links(link_type)
it = LocalGetLinks(ListIterator(answer), backend=das.query_engine, link_type=link_type)
current_value = it.get()
assert isinstance(current_value, dict)
assert current_value == next(it)
assert it.is_empty() is False
[i for i in it]
assert it.is_empty() is True
with pytest.raises(StopIteration):
it.get()
answer = das.query_engine._get_related_links('Fake')
it = LocalGetLinks(ListIterator(answer), backend=das.query_engine, link_type=link_type)
assert it.is_empty() is True
with pytest.raises(StopIteration):
it.get()
assert [i for i in it] == []

def test_local_get_links_redis_mongo_iterator(self):
from tests.integration.test_local_redis_mongo import (
_db_down,
_db_up,
mongo_port,
redis_port,
)

_db_up()

das = DistributedAtomSpace(
query_engine='local',
atomdb='redis_mongo',
mongo_port=mongo_port,
mongo_username='dbadmin',
mongo_password='dassecret',
redis_port=redis_port,
redis_cluster=False,
redis_ssl=False,
)
up_knowledge_base_animals(das)
das.commit_changes()

link_type = 'Similarity'
cursor, answer = das.query_engine._get_related_links(link_type, cursor=0)
it = LocalGetLinks(
ListIterator(answer), backend=das.query_engine, link_type=link_type, cursor=cursor
)
current_value = it.get()
assert isinstance(current_value, dict)
assert current_value == next(it)
assert it.is_empty() is False
[i for i in it]
assert it.is_empty() is True
with pytest.raises(StopIteration):
it.get()

up_n_links_in_database(das, 2000)
das.commit_changes()
link_type = 'Inheritance'
cursor, answer = das.query_engine._get_related_links(link_type, cursor=0, chunk_size=500)
it = LocalGetLinks(
ListIterator(answer), backend=das.query_engine, link_type=link_type, cursor=cursor
)
assert it.is_empty() is False
result = [i for i in it]
assert it.is_empty() is True
with pytest.raises(StopIteration):
it.get()
assert len(result) >= 1000

_db_down()

0 comments on commit 27d8c81

Please sign in to comment.