From a020cdab03eee50573fe0de1bcf33992cbf97ef1 Mon Sep 17 00:00:00 2001 From: Edgar Brissow Date: Tue, 3 Sep 2024 17:24:04 -0300 Subject: [PATCH 1/6] Added mongomock --- hyperon_das_atomdb/database.py | 4 ---- hyperon_das_atomdb/utils/expression_hasher.py | 1 + pyproject.toml | 1 + 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/hyperon_das_atomdb/database.py b/hyperon_das_atomdb/database.py index 42ee3187..d1269d40 100644 --- a/hyperon_das_atomdb/database.py +++ b/hyperon_das_atomdb/database.py @@ -257,14 +257,10 @@ 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/hyperon_das_atomdb/utils/expression_hasher.py b/hyperon_das_atomdb/utils/expression_hasher.py index e4720f40..c6ed3537 100644 --- a/hyperon_das_atomdb/utils/expression_hasher.py +++ b/hyperon_das_atomdb/utils/expression_hasher.py @@ -112,6 +112,7 @@ def composite_hash(hash_base: str | list[str]) -> str: return ExpressionHasher._compute_hash( ExpressionHasher.compound_separator.join(hash_base) ) + # TODO unreachable else: raise ValueError( "Invalid base to compute composite hash: " f"{type(hash_base)}: {hash_base}" diff --git a/pyproject.toml b/pyproject.toml index de36c43f..a2516edc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,6 +18,7 @@ pymongo = "^4.5.0" python-dotenv = "^1.0.0" mongomock = "^4.1.2" setuptools = "^70.2.0" +fakeredis = "^2.24.1" [tool.poetry.group.dev.dependencies] flake8 = "^6.1.0" From fac7ca0ad43ac67617999e80dae1e049cc38f996 Mon Sep 17 00:00:00 2001 From: Edgar Brissow Date: Thu, 5 Sep 2024 15:09:50 -0300 Subject: [PATCH 2/6] Cover more tests --- hyperon_das_atomdb/adapters/ram_only.py | 67 +- hyperon_das_atomdb/adapters/redis_mongo_db.py | 55 +- tests/unit/adapters/test_ram_only_extra.py | 33 + tests/unit/adapters/test_redis_mongo_extra.py | 38 + tests/unit/fixtures.py | 143 +++ tests/unit/test_database.py | 940 ++++++++++++++++++ 6 files changed, 1190 insertions(+), 86 deletions(-) create mode 100644 tests/unit/adapters/test_ram_only_extra.py create mode 100644 tests/unit/adapters/test_redis_mongo_extra.py create mode 100644 tests/unit/fixtures.py create mode 100644 tests/unit/test_database.py diff --git a/hyperon_das_atomdb/adapters/ram_only.py b/hyperon_das_atomdb/adapters/ram_only.py index ccd69ea8..cbf58c45 100644 --- a/hyperon_das_atomdb/adapters/ram_only.py +++ b/hyperon_das_atomdb/adapters/ram_only.py @@ -220,11 +220,11 @@ def _delete_incoming_set(self, link_handle: str, atoms_handle: list[str]) -> Non handles.remove(link_handle) def _add_templates( - self, - composite_type_hash: str, - named_type_hash: str, - key: str, - targets_hash: list[str], + self, + composite_type_hash: str, + named_type_hash: str, + key: str, + targets_hash: list[str], ) -> None: """ Add templates to the database. @@ -473,7 +473,7 @@ def get_node_by_name(self, node_type: str, substring: str) -> list[str]: key for key, value in self.db.node.items() if substring in value[FieldNames.NODE_NAME] - and node_type_hash == value[FieldNames.COMPOSITE_TYPE_HASH] + and node_type_hash == value[FieldNames.COMPOSITE_TYPE_HASH] ] def get_all_nodes(self, node_type: str, names: bool = False) -> list[str]: @@ -549,7 +549,7 @@ def is_ordered(self, link_handle: str) -> bool: ) def get_matched_links( - self, link_type: str, target_handles: list[str], **kwargs + self, link_type: str, target_handles: list[str], **kwargs ) -> MatchedLinksResultT: if link_type != WILDCARD and WILDCARD not in target_handles: return kwargs.get("cursor"), [self.get_link_handle(link_type, target_handles)] @@ -557,8 +557,8 @@ def get_matched_links( link_type_hash = ( WILDCARD if link_type == WILDCARD else ExpressionHasher.named_type_hash(link_type) ) - - if link_type in UNORDERED_LINK_TYPES: + # NOTE unreachable + if link_type in UNORDERED_LINK_TYPES: # pragma: no cover logger().error( "Failed to get matched links: Queries with unordered links are not implemented. " f"link_type: {link_type}" @@ -598,27 +598,27 @@ def get_matched_type(self, link_type: str, **kwargs) -> MatchedTypesResultT: return kwargs.get("cursor"), self._filter_non_toplevel(templates_matched) return kwargs.get("cursor"), templates_matched - def get_atoms_by_field(self, query: list[OrderedDict[str, str]]) -> list[str]: + def get_atoms_by_field(self, query: list[OrderedDict[str, str]]) -> list[str]: # pragma: no cover raise NotImplementedError() def get_atoms_by_index( - self, - index_id: str, - query: list[OrderedDict[str, str]], - cursor: int = 0, - chunk_size: int = 500, - ) -> tuple[int, list[AtomT]]: + self, + index_id: str, + query: list[OrderedDict[str, str]], + cursor: int = 0, + chunk_size: int = 500, + ) -> tuple[int, list[AtomT]]: # pragma: no cover raise NotImplementedError() def get_atoms_by_text_field( - self, - text_value: str, - field: str | None = None, - text_index_id: str | None = None, - ) -> list[str]: + self, + text_value: str, + field: str | None = None, + text_index_id: str | None = None, + ) -> list[str]: # pragma: no cover raise NotImplementedError() - def get_node_by_name_starting_with(self, node_type: str, startswith: str) -> list[str]: + def get_node_by_name_starting_with(self, node_type: str, startswith: str) -> list[str]: # pragma: no cover raise NotImplementedError() def _get_atom(self, handle: str) -> AtomT | None: @@ -672,7 +672,8 @@ def add_node(self, node_params: NodeParamsT) -> NodeT | None: def add_link(self, link_params: LinkParamsT, toplevel: bool = True) -> LinkT | None: result = self._build_link(link_params, toplevel) - if result is None: + # NOTE unreachable + if result is None: # pragma: no cover return None handle, link, _ = result self.db.link[handle] = link @@ -680,8 +681,8 @@ def add_link(self, link_params: LinkParamsT, toplevel: bool = True) -> LinkT | N return link def reindex( - self, pattern_index_templates: dict[str, list[dict[str, Any]]] | None = None - ) -> None: + self, pattern_index_templates: dict[str, list[dict[str, Any]]] | None = None + ) -> None: # pragma: no cover raise NotImplementedError() def delete_atom(self, handle: str, **kwargs) -> None: @@ -704,13 +705,13 @@ def delete_atom(self, handle: str, **kwargs) -> None: raise ex def create_field_index( - self, - atom_type: str, - fields: list[str], - named_type: str | None = None, - composite_type: list[Any] | None = None, - index_type: FieldIndexType | None = None, - ) -> str: + self, + atom_type: str, + fields: list[str], + named_type: str | None = None, + composite_type: list[Any] | None = None, + index_type: FieldIndexType | None = None, + ) -> str: # pragma: no cover raise NotImplementedError() def bulk_insert(self, documents: list[AtomT]) -> None: @@ -732,5 +733,5 @@ def retrieve_all_atoms(self) -> list[AtomT]: logger().error(f"Error retrieving all atoms: {str(e)}") raise e - def commit(self, **kwargs) -> None: + def commit(self, **kwargs) -> None: # pragma: no cover raise NotImplementedError() diff --git a/hyperon_das_atomdb/adapters/redis_mongo_db.py b/hyperon_das_atomdb/adapters/redis_mongo_db.py index e1a75d42..64615a21 100644 --- a/hyperon_das_atomdb/adapters/redis_mongo_db.py +++ b/hyperon_das_atomdb/adapters/redis_mongo_db.py @@ -89,59 +89,6 @@ class MongoIndexType(str, Enum): TEXT = "text" -class NodeDocuments: - """Class for managing node documents in MongoDB.""" - - def __init__(self, collection: Collection) -> None: - """ - Initialize the NodeDocuments class with a MongoDB collection. - - Args: - collection (Collection): The MongoDB collection to manage node documents. - """ - self.mongo_collection = collection - self.cached_nodes: dict[str, Any] = {} - self.count = 0 - - def add(self) -> None: - """Increment the count of node documents managed by this instance.""" - self.count += 1 - - def get(self, handle: str, default_value: Any = None) -> Any: - """ - Retrieve a node document from the MongoDB collection using the given handle. - - Args: - handle (str): The unique identifier for the node document. - default_value (Any): The value to return if the node document is not found. - Defaults to None. - - Returns: - The node document if found, otherwise the default value. - """ - mongo_filter = {FieldNames.ID_HASH: handle} - node = self.mongo_collection.find_one(mongo_filter) - return node if node else default_value - - def size(self) -> int: - """ - Return the count of node documents managed by this instance. - - Returns: - int: The count of node documents. - """ - return self.count - - def values(self) -> Iterable[dict[str, Any]]: - """ - Yield all node documents from the MongoDB collection. - - Returns: - generator: A generator yielding each document in the MongoDB collection. - """ - for document in self.mongo_collection.find(): - yield document - class _HashableDocument: """Class for making documents hashable.""" @@ -763,6 +710,7 @@ def get_matched_links( link_type_hash = WILDCARD if link_type == WILDCARD else self._get_atom_type_hash(link_type) + # NOTE unreachable if link_type in UNORDERED_LINK_TYPES: target_handles = sorted(target_handles) @@ -861,6 +809,7 @@ def commit(self, **kwargs) -> None: {id_tag: document[id_tag]}, document, upsert=True ) self._update_atom_indexes([document]) + except Exception as e: logger().error(f"Failed to commit buffer - Details: {str(e)}") raise e diff --git a/tests/unit/adapters/test_ram_only_extra.py b/tests/unit/adapters/test_ram_only_extra.py new file mode 100644 index 00000000..c3190dee --- /dev/null +++ b/tests/unit/adapters/test_ram_only_extra.py @@ -0,0 +1,33 @@ +import pytest +from tests.unit.fixtures import in_memory_db +from tests.unit.test_database import _check_handle +from hyperon_das_atomdb.adapters.ram_only import InMemoryDB + +class TestRamOnlyExtra: + + def test__build_atom_type_key_hash(self, in_memory_db): + db: InMemoryDB = in_memory_db + hash = db._build_atom_type_key_hash("A") + assert _check_handle(hash) + assert hash == "2c832bdcd9d74bf961205676d861540a" + + def test__delete_atom_type(self, in_memory_db): + db: InMemoryDB = in_memory_db + node = db.add_node({"name": "A", "type": "A"}) + assert len(db.all_named_types) == 1 + assert node['named_type'] in db.all_named_types + db._delete_atom_type("A") + assert len(db.all_named_types) == 0 + assert node['named_type'] not in db.all_named_types + + + def test__update_atom_indexes(self, in_memory_db): + db: InMemoryDB = in_memory_db + node = db.add_node({"name": "A", "type": "A"}) + db._update_atom_indexes([node]) + assert len(db.all_named_types) == 1 + + + + + diff --git a/tests/unit/adapters/test_redis_mongo_extra.py b/tests/unit/adapters/test_redis_mongo_extra.py new file mode 100644 index 00000000..d2912855 --- /dev/null +++ b/tests/unit/adapters/test_redis_mongo_extra.py @@ -0,0 +1,38 @@ +from multiprocessing.managers import Value + +import pytest +from tests.unit.fixtures import redis_mongo_db +from tests.unit.test_database import _check_handle +from hyperon_das_atomdb.adapters.redis_mongo_db import RedisMongoDB, _HashableDocument, MongoDBIndex +from unittest import mock + + +class TestRedisMongoExtra: + + def test_hashable_document_str(self, redis_mongo_db): + db = redis_mongo_db + node = db._build_node({"type": "A", "name": "A"}) + hashable = _HashableDocument(node) + str_hashable = str(hashable) + assert isinstance(str_hashable, str) + assert hashable + assert str(node) == str_hashable + + @pytest.mark.parametrize("params", [ + {"atom_type": "A", "fields": []}, + {"atom_type": "A", "fields": None}, + ]) + def test_index_create_exceptions(self, params, request): + db = request.getfixturevalue("redis_mongo_db") + mi = MongoDBIndex(db.mongo_db) + with pytest.raises(ValueError): + mi.create(**params) + + @mock.patch("hyperon_das_atomdb.adapters.redis_mongo_db.MongoClient", return_value=mock.MagicMock()) + @mock.patch("hyperon_das_atomdb.adapters.redis_mongo_db.Redis", return_value=mock.MagicMock()) + @mock.patch("hyperon_das_atomdb.adapters.redis_mongo_db.RedisCluster", return_value=mock.MagicMock()) + def test_create_db_connection_mongo(self, mock_mongo, mock_redis, mock_redis_cluster): + RedisMongoDB(mongo_tls_ca_file="/tmp/mock", redis_password="12", redis_username="A") + RedisMongoDB(redis_cluster=False) + + diff --git a/tests/unit/fixtures.py b/tests/unit/fixtures.py new file mode 100644 index 00000000..f50967ab --- /dev/null +++ b/tests/unit/fixtures.py @@ -0,0 +1,143 @@ +import fakeredis +import pytest +import mongomock +from unittest import mock +from hyperon_das_atomdb.adapters.redis_mongo_db import RedisMongoDB, MongoCollectionNames +from hyperon_das_atomdb.adapters.ram_only import InMemoryDB + +class MockRedis: + def __init__(self, cache=dict()): + self.cache = cache + + def get(self, key): + if key in self.cache: + return self.cache[key] + return None # return nil + + def set(self, key, value, *args, **kwargs): + if self.cache: + self.cache[key] = value + return "OK" + return None # return nil in case of some issue + + def hget(self, hash, key): + if hash in self.cache: + if key in self.cache[hash]: + return self.cache[hash][key] + return None # return nil + + def hset(self, hash, key, value, *args, **kwargs): + if self.cache: + self.cache[hash][key] = value + return 1 + return None # return nil in case of some issue + + def exists(self, key): + if key in self.cache: + return 1 + return 0 + + def cache_overwrite(self, cache=dict()): + self.cache = cache + + def sadd(self, key, *members): + # Simulate the sadd method of Redis + if key not in self.cache: + self.cache[key] = set() + before_count = len(self.cache[key]) + self.cache[key].update(members) + after_count = len(self.cache[key]) + return after_count - before_count + + def smembers(self, key): + # Simulate the smembers method of Redis + if key in self.cache: + return self.cache[key] + return set() + + def flushall(self): + # Simulate the flushall method of Redis + self.cache.clear() + + def delete(self, *keys): + # Simulate the delete method of Redis + deleted_count = 0 + for key in keys: + if key in self.cache: + del self.cache[key] + deleted_count += 1 + return deleted_count + + def getdel(self, key): + print("Cccc") + + # Simulate the getdel method of Redis + value = self.cache.get(key) + if key in self.cache: + del self.cache[key] + return value + + def srem(self, key, *members): + print("Bbbb") + + # Simulate the srem method of Redis + if key not in self.cache: + return 0 + initial_count = len(self.cache[key]) + self.cache[key].difference_update(members) + removed_count = initial_count - len(self.cache[key]) + return removed_count + + def sscan(self, name, cursor=0, match=None, count=None): + # Simulate the sscan method of Redis + print("Aaaa") + key = name + if key not in self.cache: + return (0, []) + + elements = list(self.cache[key]) + if match: + elements = [e for e in elements if match in e] + + # Cursor-based iteration simulation + start = cursor + end = min(start + (count if count else len(elements)), len(elements)) + new_cursor = end if end < len(elements) else 0 + + return (new_cursor, elements[start:end]) + + +@pytest.fixture +def redis_mongo_db(): + # import fakeredis + mongo_db = mongomock.MongoClient().db + # redis_db = fakeredis.FakeRedis() + redis_db = MockRedis() + with mock.patch( + "hyperon_das_atomdb.adapters.redis_mongo_db.RedisMongoDB._connection_mongo_db", + return_value=mongo_db, + ), mock.patch( + "hyperon_das_atomdb.adapters.redis_mongo_db.RedisMongoDB._connection_redis", + # return_value=fakeredis.FakeRedis(), + return_value=redis_db, + ): + db = RedisMongoDB() + db.mongo_atoms_collection = mongo_db.collection + db.mongo_types_collection = mongo_db.collection + + # db.mongo_atoms_collection.insert_many(loader("atom_collection_data.json")) + db.all_mongo_collections = [ + (MongoCollectionNames.ATOMS, db.mongo_atoms_collection), + (MongoCollectionNames.ATOM_TYPES, db.mongo_types_collection), + ] + db.mongo_bulk_insertion_buffer = { + MongoCollectionNames.ATOMS: tuple([db.mongo_atoms_collection, set()]), + MongoCollectionNames.ATOM_TYPES: tuple([db.mongo_types_collection, set()]), + } + + yield db + +@pytest.fixture +def in_memory_db(): + db = InMemoryDB() + yield db diff --git a/tests/unit/test_database.py b/tests/unit/test_database.py new file mode 100644 index 00000000..acc0dc7e --- /dev/null +++ b/tests/unit/test_database.py @@ -0,0 +1,940 @@ +import pytest +from .fixtures import redis_mongo_db, in_memory_db +from hyperon_das_atomdb.database import AtomDB +from unittest import mock + + +def _check_handle(handle): + return all((isinstance(handle, str), len(handle) == 32, int(handle, 16))) + + +class TestDatabase: + + @staticmethod + def _add_node(db: AtomDB, node_name, node_type, adapter, extra_fields=None): + node_dict = {'name': node_name, 'type': node_type} + node_dict.update(extra_fields or {}) + node = db.add_node(node_dict) + if adapter == 'redis_mongo_db': + db.commit() + return node + + @staticmethod + def _add_link(db: AtomDB, link_type, dict_targets, adapter, is_top_level=True): + link = db.add_link({'type': link_type, 'targets': dict_targets}, toplevel=is_top_level) + if adapter != "in_memory_db": + db.commit() + return link + + @pytest.mark.parametrize("database,expected", [ + ("redis_mongo_db", "1fd600f0fd8a1fab79546a4fc3612df3"), # Concept, Human + ("in_memory_db", "1fd600f0fd8a1fab79546a4fc3612df3") # Concept, Human + ]) + def test_node_handle(self, database, expected, request): + db: AtomDB = request.getfixturevalue(database) + handle = db.node_handle("Concept", "Human") + incorrect_handle = db.node_handle("Concept", "human") + assert handle, incorrect_handle + assert handle != incorrect_handle + assert handle == expected + assert _check_handle(handle) + + @pytest.mark.parametrize("database,expected", [ + ("redis_mongo_db", "1fd600f0fd8a1fab79546a4fc3612df3"), # Concept, Human + ("in_memory_db", "1fd600f0fd8a1fab79546a4fc3612df3") # Concept, Human + ]) + def test_node_handle_exceptions(self, database, expected, request): + db: AtomDB = request.getfixturevalue(database) + # NOTE Should raise ValueError + with pytest.raises(TypeError): + db.node_handle([], []) + + @pytest.mark.parametrize("database,expected", [ + ("redis_mongo_db", "a9dea78180588431ec64d6bc4872fdbc"), # Similarity + ("in_memory_db", "a9dea78180588431ec64d6bc4872fdbc") # Similarity + ]) + def test_link_handle(self, database, expected, request): + db: AtomDB = request.getfixturevalue(database) + handle = db.link_handle("Similarity", []) + assert len(set([db.link_handle("Similarity", f) for f in [[], [], ""]])) == 1 + assert handle + assert _check_handle(handle) + + @pytest.mark.parametrize("database,expected", [ + ("redis_mongo_db", "a9dea78180588431ec64d6bc4872fdbc"), # Similarity + ("in_memory_db", "a9dea78180588431ec64d6bc4872fdbc") # Similarity + ]) + def test_link_handle_exceptions(self, database, expected, request): + db: AtomDB = request.getfixturevalue(database) + with pytest.raises(TypeError): + db.link_handle("Similarity", None) + # NOTE Unreachable + # with pytest.raises(ValueError): + # db.link_handle("Similarity", set()) + + @pytest.mark.parametrize("database,kwlist", [ + ("redis_mongo_db", ["targets_document", "deep_representation"]), + ("in_memory_db", ["targets_document", "deep_representation"]) + ]) + def test_reformat_document(self, database, kwlist, request): + db: AtomDB = request.getfixturevalue(database) + node_handle = db.add_node({'name': 'A', 'type': 'Test'}).get('handle') + if database != "in_memory_db": + db.commit() + link = {'name': 'A', 'targets': [node_handle]} + for kw in kwlist: + answer = db._reformat_document(link, **{kw: True}) + assert set(answer.keys()) == {'name', 'targets', 'targets_document'} + assert len(answer['targets']) == 1 + assert len(answer['targets_document']) == 1 + assert answer['name'] == "A" + assert isinstance(answer['targets'][0], (str if kw == "targets_document" else dict)) + + @pytest.mark.parametrize("database,kwlist", [ + ("redis_mongo_db", ["targets_document", "deep_representation"]), + ("in_memory_db", ["targets_document", "deep_representation"]) + ]) + def test_reformat_document_exceptions(self, database, kwlist, request): + db: AtomDB = request.getfixturevalue(database) + link = {'name': 'A', 'targets': ['test']} + for kw in kwlist: + with pytest.raises(Exception, match='Nonexistent atom'): + db._reformat_document(link, **{kw: True}) + + @pytest.mark.parametrize("database,expected_fields, expected_handle", [ + ("redis_mongo_db", ['handle', '_id', 'composite_type_hash', 'name', 'named_type'], + '180fed764dbd593f1ea45b63b13d7e69'), + ("in_memory_db", ['handle', '_id', 'composite_type_hash', 'name', 'named_type'], + '180fed764dbd593f1ea45b63b13d7e69') + ]) + def test_build_node(self, database, expected_fields, expected_handle, request): + db: AtomDB = request.getfixturevalue(database) + handle, node = db._build_node({'type': 'Test', 'name': 'test'}) + assert node + assert handle == expected_handle + assert all([k in node for k in expected_fields]) + assert isinstance(node, dict) + assert _check_handle(handle) + + # Test exception + with pytest.raises(Exception, match='The "name" and "type" fields must be sent'): + db._build_node({}) + + @pytest.mark.parametrize("database,expected_fields, expected_handle,is_top_level", [ + ("redis_mongo_db", + ['handle', '_id', 'composite_type_hash', 'named_type_hash', 'named_type', 'is_toplevel', 'targets'], + '180fed764dbd593f1ea45b63b13d7e69', True), + ("redis_mongo_db", + ['handle', '_id', 'composite_type_hash', 'named_type_hash', 'named_type', 'is_toplevel', 'targets'], + '180fed764dbd593f1ea45b63b13d7e69', False), + ("in_memory_db", + ['handle', '_id', 'composite_type_hash', 'named_type_hash', 'named_type', 'is_toplevel', 'targets'], + '180fed764dbd593f1ea45b63b13d7e69', True), + ("in_memory_db", + ['handle', '_id', 'composite_type_hash', 'named_type_hash', 'named_type', 'is_toplevel', 'targets'], + '180fed764dbd593f1ea45b63b13d7e69', False) + + ]) + def test_build_link(self, database, expected_fields, expected_handle, is_top_level, request): + db: AtomDB = request.getfixturevalue(database) + handle, link, targets = db._build_link({'type': 'Test', 'targets': [{'type': 'Test', 'name': 'test'}]}, + is_top_level) + assert expected_handle in targets + assert all([k in link for k in expected_fields]) + assert link['is_toplevel'] == is_top_level + assert _check_handle(handle) + assert isinstance(link, dict) + assert isinstance(targets, list) + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_build_link_exceptions(self, database, request): + db: AtomDB = request.getfixturevalue(database) + with pytest.raises(ValueError, match='The target must be a dictionary'): + db._build_link({'type': 'Test', 'targets': ['']}) + with pytest.raises(Exception, match='The "type" and "targets" fields must be sent'): + db._build_link({'type': 'Test', 'targets': None}) + with pytest.raises(Exception, match='The "type" and "targets" fields must be sent'): + db._build_link({'type': None, 'targets': []}) + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_node_exists(self, database, request): + db: AtomDB = request.getfixturevalue(database) + db.add_node({'name': 'A', 'type': 'Test'}) + if database != "in_memory_db": + db.commit() + no_exists = db.node_exists('Test', 'B') + exists = db.node_exists('Test', 'A') + assert isinstance(no_exists, bool) + assert isinstance(exists, bool) + assert not no_exists + assert exists + + @pytest.mark.parametrize("database,targets", [ + ("redis_mongo_db", ['180fed764dbd593f1ea45b63b13d7e69']), + ("redis_mongo_db", []), + ("in_memory_db", ['180fed764dbd593f1ea45b63b13d7e69']), + ("in_memory_db", []) + + ]) + def test_link_exists(self, database, targets, request): + db: AtomDB = request.getfixturevalue(database) + dict_targets = [{'type': 'Test', 'name': 'test'}] if targets else [] + link = {'type': 'Test', 'targets': dict_targets} + db.add_link(link) + if database != "in_memory_db": + db.commit() + no_exists = db.link_exists('Tes', []) + exists = db.link_exists('Test', targets) + + assert isinstance(no_exists, bool) + assert isinstance(exists, bool) + assert not no_exists + assert exists + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_get_node_handle(self, database, request): + db: AtomDB = request.getfixturevalue(database) + expected_node = self._add_node(db, 'A', 'Test', database) + node = db.get_node_handle('Test', 'A') + assert node == expected_node['handle'] + assert _check_handle(node) + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_get_node_handle_exceptions(self, database, request): + db: AtomDB = request.getfixturevalue(database) + with pytest.raises(Exception, match='Nonexistent atom'): + db.get_node_handle('Test', 'A') + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_get_node_name(self, database, request): + db: AtomDB = request.getfixturevalue(database) + expected_node = self._add_node(db, 'A', 'Test', database) + name = db.get_node_name(expected_node['handle']) + # NOTE all adapters must return the same type + assert isinstance(name, str) + assert name == expected_node['name'] + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_get_node_name_exceptions(self, database, request): + if database == "redis_mongo_db": + pytest.skip( + "ERROR in_memory returns a AtomDoesNotExist exception, redis_mongo returns ValueError. " + "See https://github.com/singnet/das-atom-db/issues/210") + db: AtomDB = request.getfixturevalue(database) + # in memory returns a AtomDoesNotExist exception, redis_mongo returns ValueError + with pytest.raises(Exception, match='Nonexistent atom'): + db.get_node_name('error') + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_get_node_type(self, database, request): + db: AtomDB = request.getfixturevalue(database) + expected_node = self._add_node(db, 'A', 'Test', database) + node_type = db.get_node_type(expected_node['handle']) + assert isinstance(node_type, str) + assert node_type == expected_node['named_type'] + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_get_node_type_exceptions(self, database, request): + db: AtomDB = request.getfixturevalue(database) + with pytest.raises(Exception, match='Nonexistent atom'): + db.get_node_type('test') + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_get_node_by_name(self, database, request): + db: AtomDB = request.getfixturevalue(database) + expected_nodes = [self._add_node(db, n, 'Test', database) for n in {'A', 'Aa', 'Ac'}] + not_expected_nodes = [self._add_node(db, n, 'Test', database) for n in {'B', 'Ba', 'Bc'}] + nodes = db.get_node_by_name('Test', 'A') + not_nodes = db.get_node_by_name('Test', 'C') + assert not_nodes == [] + assert isinstance(nodes, list) + assert len(nodes) == 3 + assert all(_check_handle(node) for node in nodes) + assert all(n['handle'] in nodes for n in expected_nodes) + assert not any(n['handle'] in nodes for n in not_expected_nodes) + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_get_atoms_by_field(self, database, request): + if database == "in_memory_db": + pytest.skip("ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210") + db: AtomDB = request.getfixturevalue(database) + expected_node = self._add_node(db, 'Ac', 'Test', database) + expected_link = self._add_link(db, 'Ac', [expected_node], database) + nodes = db.get_atoms_by_field([{'field': 'name', 'value': 'Ac'}]) + links = db.get_atoms_by_field([{'field': 'named_type', 'value': 'Ac'}]) + assert isinstance(nodes, list) + assert isinstance(links, list) + assert all(_check_handle(node) for node in nodes) + assert all(_check_handle(link) for link in links) + assert nodes[0] == expected_node['handle'] + assert links[0] == expected_link['handle'] + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_get_atoms_by_index(self, database, request): + if database == "in_memory_db": + pytest.skip("ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210") + db: AtomDB = request.getfixturevalue(database) + self._add_node(db, "A", "Test", database, {"value": 3}) + index_id = db.create_field_index(atom_type="node", fields=["value"], named_type="Test") + cursor, atoms = db.get_atoms_by_index(index_id, [{"field": "value", "value": 3}]) + assert isinstance(cursor, int) + assert isinstance(atoms, list) + assert cursor == 0 + assert len(atoms) == 1 + assert all(isinstance(a, dict) for a in atoms) + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_get_atoms_by_index_exceptions(self, database, request): + if database == "in_memory_db": + pytest.skip("ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210") + db: AtomDB = request.getfixturevalue(database) + with pytest.raises(Exception): + db.get_atoms_by_index("", []) + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_get_atoms_by_text_field_regex(self, database, request): + if database == "in_memory_db": + pytest.skip("ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210") + db: AtomDB = request.getfixturevalue(database) + self._add_node(db, "A", "Test", database, {"value": "Test sentence"}) + self._add_link(db, 'Test', [], database) + index_id = db.create_field_index(atom_type="node", fields=["value"], named_type="Test", + index_type="token_inverted_list") + atoms = db.get_atoms_by_text_field("Test", "value", text_index_id=index_id) + assert isinstance(atoms, list) + assert all(_check_handle(a) for a in atoms) + assert all(isinstance(a, str) for a in atoms) + assert len(atoms) == 1 + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_get_atoms_by_text_field_text(self, database, request): + if database == "in_memory_db": + pytest.skip("ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210") + db: AtomDB = request.getfixturevalue(database) + self._add_node(db, "A", "Test", database, {"value": "Test sentence"}) + index_id = db.create_field_index(atom_type="node", fields=["value"], named_type="Test", + index_type="token_inverted_list") + with mock.patch( + "mongomock.filtering._Filterer.apply", + return_value=['815212e3d7ac246e70c1744d14a8c402'] + ): + atoms = db.get_atoms_by_text_field("Test", text_index_id=index_id) + assert isinstance(atoms, list) + assert all(_check_handle(a) for a in atoms) + assert all(isinstance(a, str) for a in atoms) + assert len(atoms) == 1 + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_get_node_by_name_starting_with(self, database, request): + if database == "in_memory_db": + pytest.skip("ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210") + db: AtomDB = request.getfixturevalue(database) + node_a = self._add_node(db, "Aaa", "Test", database) + node_b = self._add_node(db, "Abb", "Test", database) + self._add_node(db, "Bbb", "Test", database) + nodes = db.get_node_by_name_starting_with("Test", "A") + assert isinstance(nodes, list) + assert all(_check_handle(n) for n in nodes) + assert all(isinstance(n, str) for n in nodes) + assert all(handle in nodes for handle in [node_a['handle'], node_b['handle']]) + assert len(nodes) == 2 + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_get_all_nodes(self, database, request): + db: AtomDB = request.getfixturevalue(database) + self._add_node(db, "Aaa", "Test", database) + self._add_node(db, "Abb", "Test", database) + self._add_node(db, "Bbb", "Test", database) + nodes = db.get_all_nodes("Test") + assert isinstance(nodes, list) + assert all(_check_handle(n) for n in nodes) + assert all(isinstance(n, str) for n in nodes) + assert len(nodes) == 3 + + @pytest.mark.parametrize("database,params,links_len,cursor_value", [ + ("redis_mongo_db", {"link_type": "Ac"}, 3, 0), + ("redis_mongo_db", {"link_type": "Ac", "names": True, "chunk_size": 1}, 3, 0), + ("redis_mongo_db", {"link_type": "Ac", "names": True, "cursor": 0}, 3, 0), + ("redis_mongo_db", {"link_type": "Z", "names": True, "cursor": 1}, 0, 0), + ("redis_mongo_db", {"link_type": "Ac", "chunk_size": 1, "cursor": 0}, 1, 1), + ("in_memory_db", {"link_type": "Ac"}, 3, None), + # NOTE should return the same value for the cursor + ("in_memory_db", {"link_type": "Ac", "names": True, "cursor": 0}, 3, 0), + ("in_memory_db", {"link_type": "Ac", "names": True, "cursor": 0}, 3, 0), + ("in_memory_db", {"link_type": "Z", "names": True, "cursor": 1}, 0, 1), + ("in_memory_db", {"link_type": "Ac", "chunk_size": 1, "cursor": 0}, 3, 0), + ]) + def test_get_all_links(self, database, params, links_len, cursor_value, request): + db: AtomDB = request.getfixturevalue(database) + self._add_link(db, 'Ac', [{"name": "A", "type": "A"}], database) + self._add_link(db, 'Ac', [{"name": "B", "type": "B"}], database) + self._add_link(db, 'Ac', [{"name": "C", "type": "C"}], database) + cursor, links = db.get_all_links(**params) + assert cursor == cursor_value + assert isinstance(links, list) + assert all(_check_handle(l) for l in links) + assert all(isinstance(link, str) for link in links) + assert len(links) == links_len + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_get_link_handle(self, database, request): + db: AtomDB = request.getfixturevalue(database) + link = self._add_link(db, 'Ac', [{"name": "A", "type": "A"}], database) + handle = db.get_link_handle(link['type'], link['targets']) + assert _check_handle(handle) + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_get_link_handle_exceptions(self, database, request): + db: AtomDB = request.getfixturevalue(database) + with pytest.raises(Exception, match="Nonexistent atom"): + db.get_link_handle("A", []) + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_get_link_type(self, database, request): + db: AtomDB = request.getfixturevalue(database) + link_a = self._add_link(db, 'Ac', [{"name": "A", "type": "A"}], database) + self._add_link(db, 'Bc', [{"name": "A", "type": "A"}], database) + link_type = db.get_link_type(link_a['handle']) + assert link_type + assert isinstance(link_type, str) + assert link_type == link_a['type'] + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_get_link_type_exceptions(self, database, request): + db: AtomDB = request.getfixturevalue(database) + with pytest.raises(Exception, match="Nonexistent atom"): + db.get_link_type("") + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_get_link_targets(self, database, request): + db: AtomDB = request.getfixturevalue(database) + link_a = self._add_link(db, 'Ac', [{"name": "A", "type": "A"}], database) + targets = db.get_link_targets(link_a['handle']) + assert isinstance(targets, list) + assert len(targets) == 1 + assert all(_check_handle(t) for t in targets) + assert all(isinstance(t, str) for t in targets) + assert targets == link_a['targets'] + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_is_ordered(self, database, request): + db: AtomDB = request.getfixturevalue(database) + link_a = self._add_link(db, 'Ac', [{"name": "A", "type": "A"}], database) + # NOTE just retrieves the link ... + assert db.is_ordered(link_a['handle']) + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_is_ordered_no_handle(self, database, request): + if database == "redis_mongo_db": + pytest.skip( + "ERROR redis_mongo_db is raising ValueError exception, should be AtomDoesNotExist. " + "See https://github.com/singnet/das-atom-db/issues/210") + db: AtomDB = request.getfixturevalue(database) + with pytest.raises(Exception, match="Nonexistent atom"): + db.is_ordered("handle") + + @pytest.mark.parametrize("database,params,links_len,cursor_value", [ + ("redis_mongo_db", {}, 3, None), + ("redis_mongo_db", {"handles_only": True}, 3, None), + ("redis_mongo_db", {"no_target_format": True}, 3, None), + ("redis_mongo_db", {"cursor": 0, "chunk_size": 1}, 1, 1), + # NOTE should return None on all cases + ("in_memory_db", {}, 3, None), + ("in_memory_db", {"handles_only": True}, 3, None), + ("in_memory_db", {"no_target_format": True}, 3, None), + ("in_memory_db", {"cursor": 0, "chunk_size": 1}, 3, 0), + ]) + def test_get_incoming_links(self, database, params, links_len, cursor_value, request): + db: AtomDB = request.getfixturevalue(database) + node_a = self._add_node(db, "Aaa", "Test", database) + self._add_link(db, 'Aa', [node_a], database) + self._add_link(db, 'Ab', [node_a], database) + self._add_link(db, 'Ac', [node_a], database) + cursor, links = db.get_incoming_links(node_a['handle'], **params) + assert cursor == cursor_value + assert len(links) == links_len + assert all([_check_handle(l if params.get('handles_only') else l["handle"]) for l in links]) + + @pytest.mark.parametrize("database,params,links_len,cursor_value", [ + ("redis_mongo_db", {}, 1, None), + ("redis_mongo_db", {"toplevel_only": True}, 1, None), + # ("redis_mongo_db", {"link_type": "NoTopLevel" , "toplevel_only": True}, 0, None), # doesn't work + # Note returning different values + # ("in_memory_db", {"link_type": "*", "toplevel_only": True}, 3, None), + # ("redis_mongo_db", {"link_type": "*", "toplevel_only": True}, 0, None), + # ("redis_mongo_db", {"link_type": "*"}, 3, None), # should return 3 + ("redis_mongo_db", {"target_handles": "*"}, 1, None), + ("redis_mongo_db", {"handles_only": True}, 1, None), + ("redis_mongo_db", {"no_target_format": True}, 1, None), + ("redis_mongo_db", {"cursor": 0, "chunk_size": 1}, 1, None), + ("in_memory_db", {}, 1, None), + ("in_memory_db", {"toplevel_only": True}, 1, None), + # ("in_memory_db", {"link_type": "NoTopLevel", "toplevel_only": True}, 0, None), # doesn't work + ("in_memory_db", {"link_type": "*"}, 3, None), + ("in_memory_db", {"toplevel_only": True}, 1, None), + ("in_memory_db", {"target_handles": "*"}, 1, None), + ("in_memory_db", {"handles_only": True}, 1, None), + ("in_memory_db", {"no_target_format": True}, 1, None), + ("in_memory_db", {"cursor": 0, "chunk_size": 1}, 1, 0), + ]) + def test_get_matched_links(self, database, params, links_len, cursor_value, request): + db: AtomDB = request.getfixturevalue(database) + node_a = self._add_node(db, "Aaa", "Test", database) + link_a = self._add_link(db, 'Aa', [node_a], database) + _ = self._add_link(db, 'NoTopLevel', [node_a], database, is_top_level=False) + _ = self._add_link(db, 'Ac', [node_a], database) + params['link_type'] = link_a['type'] if not params.get('link_type') else params['link_type'] + params['target_handles'] = link_a['targets'] if not params.get('target_handles') else params['target_handles'] + cursor, links = db.get_matched_links(**params) + assert cursor == cursor_value + print(links) + print(node_a["handle"]) + assert len(links) == links_len + if all(isinstance(l, tuple) for l in links): + for link in links: + while link: + assert _check_handle(link[0]) + link = link[1] if len(link) > 1 else None + else: + assert all([_check_handle(l) for l in links]) + + @pytest.mark.parametrize("database,params,links_len,cursor_value", [ + ("redis_mongo_db", {"link_type": "Z", "target_handles": []}, 0, None), + # ("redis_mongo_db", {"link_type": "*", "target_handles": ["*", "*"], "toplevel_only": True}, 0, None), + # ("in_memory_db", {"link_type": "*", "target_handles": ["*", "*"]}, 0, None), + # ("in_memory_db", {"link_type": "*", "target_handles": ["*", "*"], "toplevel_only": True}, 0, None), + ]) + def test_get_matched_no_links(self, database, params, links_len, cursor_value, request): + db: AtomDB = request.getfixturevalue(database) + self._add_node(db, "Aaa", "Test", database) + cursor, links = db.get_matched_links(**params) + assert cursor == cursor_value + assert len(links) == links_len + + @pytest.mark.parametrize("database,params,links_len,cursor_value,is_top_level", [ + ("redis_mongo_db", {}, 1, None, True), + ("redis_mongo_db", {}, 1, None, False), + ("redis_mongo_db", {"toplevel_only": True}, 0, None, False), + ("redis_mongo_db", {"cursor": 0, "chunk_size": 1}, 1, 0, False), + ("in_memory_db", {}, 1, None, True), + ("in_memory_db", {}, 1, None, False), + ("in_memory_db", {"toplevel_only": True}, 0, None, False), + # NOTE should return None or same as redis_mongo + ("in_memory_db", {"cursor": 0, "chunk_size": 1}, 1, 0, False), + ]) + def test_get_matched_type_template(self, database, params, links_len, cursor_value, is_top_level, request): + db: AtomDB = request.getfixturevalue(database) + node_a = self._add_node(db, "Aaa", "Test", database) + node_b = self._add_node(db, "Bbb", "Test", database) + link_a = self._add_link(db, 'Aa', [node_a, node_b], database, is_top_level=is_top_level) + cursor, links = db.get_matched_type_template(["Aa", "Test", "Test"], **params) + assert cursor == cursor_value + assert len(links) == links_len + if len(links) > 0: + for link in links: + assert _check_handle(link[0]) + assert link[0] == link_a['handle'] + assert all(t in link[1] for t in link_a['targets']) + assert all(_check_handle(t) for t in link[1]) + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_get_matched_type(self, database, request): + if database == "redis_mongo_db": + pytest.skip( + "ERROR redis_mongo_db is returning more values. " + "See https://github.com/singnet/das-atom-db/issues/210") + db: AtomDB = request.getfixturevalue(database) + link_a = self._add_link(db, 'Aa', [], database) + self._add_link(db, 'Ab', [], database) + cursor, links = db.get_matched_type(link_a['type']) + assert cursor is None + assert len(links) == 1 + if len(links) > 0: + for link in links: + assert _check_handle(link[0]) + assert link[0] == link_a['handle'] + assert all(t in link[1] for t in link_a['targets']) + assert all(_check_handle(t) for t in link[1]) + + @pytest.mark.parametrize("database,params,top_level,n_links,n_nodes", [ + ("redis_mongo_db", {}, True, 1, 1), + ("redis_mongo_db", {"no_target_format": True}, False, 1, 1), + # ("redis_mongo_db", {"targets_document": True}, False, 1, 1),# breaks when is a node + ("redis_mongo_db", {"deep_representation": True}, False, 1, 1), + ("in_memory_db", {}, True, 1, 1), + ("in_memory_db", {"no_target_format": True}, False, 0, 1), + # ("in_memory_db", {"targets_document": True}, False, 0, 1),# breaks when is a node + ("in_memory_db", {"deep_representation": True}, False, 0, 1), + + ]) + def test_get_atom_node(self, database, params, top_level, n_links, n_nodes, request): + db: AtomDB = request.getfixturevalue(database) + node_a = self._add_node(db, "Aaa", "Test", database) + atom_n = db.get_atom(node_a['handle'], **params) + assert atom_n + assert atom_n['handle'] == node_a['handle'] + assert _check_handle(atom_n['handle']) + + @pytest.mark.parametrize("database,params,top_level,n_links,n_nodes", [ + ("redis_mongo_db", {}, True, 1, 1), + ("redis_mongo_db", {"no_target_format": True}, False, 1, 1), + ("redis_mongo_db", {"targets_document": True}, False, 1, 1), + ("redis_mongo_db", {"deep_representation": True}, False, 1, 1), + ("in_memory_db", {}, True, 1, 1), + ("in_memory_db", {"no_target_format": True}, False, 0, 1), + ("in_memory_db", {"targets_document": True}, False, 0, 1), + ("in_memory_db", {"deep_representation": True}, False, 0, 1), + + ]) + def test_get_atom_link(self, database, params, top_level, n_links, n_nodes, request): + db: AtomDB = request.getfixturevalue(database) + link_a = self._add_link(db, 'Aa', [], database, is_top_level=top_level) + atom_l = db.get_atom(link_a['handle'], **params) + assert atom_l + assert atom_l['handle'] == link_a['handle'] + assert _check_handle(atom_l['handle']) + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test__get_atom(self, database, request): + db: AtomDB = request.getfixturevalue(database) + node_a = self._add_node(db, "Aaa", "Test", database) + link_a = self._add_link(db, 'Aa', [], database) + node = db._get_atom(node_a['handle']) + link = db._get_atom(link_a['handle']) + assert node, link + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + 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): + db: AtomDB = request.getfixturevalue(database) + node_a = self._add_node(db, "Aaa", "Test", database) + link_a = self._add_link(db, 'Test', [], database) + atom_type_node = db.get_atom_type(node_a["handle"]) + atom_type_link = db.get_atom_type(link_a["handle"]) + assert isinstance(atom_type_node, str) + assert isinstance(atom_type_link, str) + assert atom_type_node == atom_type_link + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_get_atom_type_none(self, database, request): + db: AtomDB = request.getfixturevalue(database) + atom_type_node = db.get_atom_type("handle") + atom_type_link = db.get_atom_type("handle") + assert atom_type_node is None + assert atom_type_link is None + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_get_atom_as_dict(self, database, request): + db: AtomDB = request.getfixturevalue(database) + node_a = self._add_node(db, "Aaa", "Test", database) + link_a = self._add_link(db, 'Test', [], database) + atom_node = db.get_atom_as_dict(node_a["handle"]) + atom_link = db.get_atom_as_dict(link_a["handle"]) + assert isinstance(atom_node, dict) + 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": + pytest.skip( + "ERROR in_memory raises exception, they should return the same result/exception. " + "See https://github.com/singnet/das-atom-db/issues/210") + db: AtomDB = request.getfixturevalue(database) + atom_node = db.get_atom_as_dict("handle") + atom_link = db.get_atom_as_dict("handle") + assert atom_node == {} + assert atom_link == {} + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_get_atom_as_dict_exceptions(self, database, request): + if database == "redis_mongo_db": + pytest.skip( + "ERROR redis_mongo_db doesn't raises exception, they should return the same result/exception. " + "See https://github.com/singnet/das-atom-db/issues/210") + db: AtomDB = request.getfixturevalue(database) + with pytest.raises(Exception, match="Nonexistent atom"): + db.get_atom_as_dict("handle") + with pytest.raises(Exception, match="Nonexistent atom"): + db.get_atom_as_dict("handle") + + @pytest.mark.parametrize("database,params", [ + ("redis_mongo_db", {}), + ("redis_mongo_db", {"precise": True}), + ("redis_mongo_db", {"precise": False}), + # NOTE should return the same value if the arg precise is set + ("in_memory_db", {}), + ("in_memory_db", {"precise": True}), + ("in_memory_db", {"precise": False}), + ]) + def test_count_atoms(self, database, params, request): + db: AtomDB = request.getfixturevalue(database) + self._add_node(db, "Aaa", "Test", database) + self._add_link(db, 'Test', [], database) + atoms_count = db.count_atoms(params) + assert atoms_count + assert isinstance(atoms_count, dict) + assert isinstance(atoms_count['atom_count'], int) + assert atoms_count['atom_count'] == 2 + if params.get('precise', False): + assert isinstance(atoms_count['node_count'], int) + assert isinstance(atoms_count['link_count'], int) + assert atoms_count['node_count'] == 1 + assert atoms_count['link_count'] == 1 + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_clear_database(self, database, request): + db: AtomDB = request.getfixturevalue(database) + self._add_node(db, "Aaa", "Test", database) + self._add_link(db, 'Test', [], database) + assert db.count_atoms()['atom_count'] == 2 + db.clear_database() + assert db.count_atoms()['atom_count'] == 0 + + @pytest.mark.parametrize("database,node", [ + ("redis_mongo_db", {"name": "A", "type": "A"}), + ("in_memory_db", {"name": "A", "type": "A"}), + ]) + def test_add_node(self, database, node, request): + db: AtomDB = request.getfixturevalue(database) + if database == "redis_mongo_db": + db.mongo_bulk_insertion_limit = 1 + node = db.add_node(node) + count = db.count_atoms() + assert node + assert count['atom_count'] == 1 + assert isinstance(node, dict) + + @pytest.mark.parametrize("database,node", [ + ("redis_mongo_db", {"name": "AAAA", "type": "A"}), + ]) + def test_add_node_discard(self, database, node, request): + db: AtomDB = request.getfixturevalue(database) + db.mongo_bulk_insertion_limit = 1 + db.max_mongo_db_document_size = 1 + node = db.add_node(node) + count = db.count_atoms() + assert node is None + assert count['atom_count'] == 0 + + @pytest.mark.parametrize("database,node", [ + ("redis_mongo_db", {}), + # NOTE it's not breaking, should break? + # ("redis_mongo_db", {"name": "A", "type": "A", "handle": ""}), + # ("redis_mongo_db", {"name": "A", "type": "A", "_id": ""}), + # ("redis_mongo_db", {"name": "A", "type": "A", "composite_type_hash": ""}), + # ("redis_mongo_db", {"name": "A", "type": "A", "named_type": ""}), + ("in_memory_db", {}), + # NOTE it's not breaking, should break? + # ("in_memory_db", {"name": "A", "type": "A", "handle": ""}), + # ("in_memory_db", {"name": "A", "type": "A", "_id": ""}), + # ("in_memory_db", {"name": "A", "type": "A", "composite_type_hash": ""}), + # ("in_memory_db", {"name": "A", "type": "A", "named_type": ""}), + ]) + def test_add_node_exceptions(self, database, node, request): + db: AtomDB = request.getfixturevalue(database) + with pytest.raises(Exception): + db.add_node(node) + + @pytest.mark.parametrize("database,params,expected_count,top_level", [ + ("redis_mongo_db", {'type': "A", 'targets': [{"name": "A", "type": "A"}]}, 2, True), + ("redis_mongo_db", {'type': "A", 'targets': []}, 1, True), + ("in_memory_db", {'type': "A", 'targets': [{"name": "A", "type": "A"}]}, 2, True), + ("in_memory_db", {'type': "A", 'targets': []}, 1, True), + ]) + def test_add_link(self, database, params, expected_count, top_level, request): + db: AtomDB = request.getfixturevalue(database) + if database == "redis_mongo_db": + db.mongo_bulk_insertion_limit = 1 + link = db.add_link(params, top_level) + count = db.count_atoms() + assert link + assert count['atom_count'] == expected_count + assert isinstance(link, dict) + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_reindex(self, database, request): + if database == "in_memory_db": + pytest.skip("ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210") + db: AtomDB = request.getfixturevalue(database) + node_a = self._add_node(db, "Aaa", "Test", database) + link_a = self._add_link(db, 'Test', [], database) + db.reindex() + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_delete_atom(self, database, request): + if database == "in_memory_db": + pytest.skip("ERROR Atom not in incoming_set. See https://github.com/singnet/das-atom-db/issues/210") + db: AtomDB = request.getfixturevalue(database) + node_a = self._add_node(db, "Aaa", "Test", database) + link_a = self._add_link(db, 'Test', [], database) + count = db.count_atoms({"precise": True}) + assert count['atom_count'] == 2 + assert count['node_count'] == 1 + assert count['link_count'] == 1 + db.delete_atom(node_a['handle']) + count = db.count_atoms({"precise": True}) + assert count['atom_count'] == 1 + assert count['node_count'] == 0 + assert count['link_count'] == 1 + db.delete_atom(link_a['handle']) + count = db.count_atoms({"precise": True}) + assert count['atom_count'] == 0 + assert count['node_count'] == 0 + assert count['link_count'] == 0 + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_delete_atom_exceptions(self, database, request): + if database == "in_memory_db": + pytest.skip("ERROR Atom not in incoming_set. See https://github.com/singnet/das-atom-db/issues/210") + db: AtomDB = request.getfixturevalue(database) + with pytest.raises(Exception): + db.delete_atom("handle") + + @pytest.mark.parametrize("database,params", [ + ("redis_mongo_db", {"atom_type": "A", "fields": ["value"]}), + ("redis_mongo_db", {"atom_type": "A", "fields": ["value"], "named_type": "A"}), + ("redis_mongo_db", {"atom_type": "A", "fields": ["value"], "composite_type": ["value", "side"]}), + ("redis_mongo_db", {"atom_type": "A", "fields": ["value"], "named_type": "A", "index_type": "binary_tree"}), + ("redis_mongo_db", {"atom_type": "A", "fields": ["value", "side"], "composite_type": ["value", "side"], + "index_type": "binary_tree"}), + ("redis_mongo_db", {"atom_type": "A", "fields": ["value", "side"], "composite_type": ["value", "side"], + "index_type": "token_inverted_list"}), + ("redis_mongo_db", + {"atom_type": "A", "fields": ["value"], "named_type": "A", "index_type": "token_inverted_list"}), + ("redis_mongo_db", {"atom_type": "A", "fields": ["value"], "named_type": "A", "index_type": "binary_tree"}), + ("redis_mongo_db", {"atom_type": "A", "fields": ["value"], "composite_type": ["value", "side"], + "index_type": "token_inverted_list"}), + ("redis_mongo_db", + {"atom_type": "A", "fields": ["value"], "composite_type": ["value", "side"], "index_type": "binary_tree"}), + ("in_memory_db", {}) + ]) + def test_create_field_index(self, database, params, request): + if database == "in_memory_db": + pytest.skip("ERROR Not implemented on in_memory_db. See https://github.com/singnet/das-atom-db/issues/210") + db: AtomDB = request.getfixturevalue(database) + index_id = db.create_field_index(**params) + assert index_id + assert isinstance(index_id, str) + # check if creating a duplicated index breaks + index_id2 = db.create_field_index(**params) + assert isinstance(index_id2, str) + assert index_id2 == index_id + + @pytest.mark.parametrize("database,params", [ + ("redis_mongo_db", {"atom_type": "A", "fields": []}), + ("redis_mongo_db", + {"atom_type": "A", "fields": ["value"], "named_type": "A", "composite_type": ["value", "side"]}), + ("in_memory_db", {}) + ]) + def test_create_field_index_value_error(self, database, params, request): + if database == "in_memory_db": + pytest.skip("ERROR Not implemented on in_memory_db. See https://github.com/singnet/das-atom-db/issues/210") + db: AtomDB = request.getfixturevalue(database) + with pytest.raises(ValueError): + db.create_field_index(**params) + + # + # @pytest.mark.parametrize("database,params", [ + # ("redis_mongo_db", {"atom_type": "A", "fields": ["side"], "index_type": "wrong_type"}), + # ("in_memory_db", {}) + # ]) + # def test_create_field_index_mongo_error(self, database, params, request): + # if database == "in_memory_db": + # pytest.skip("ERROR Not implemented on in_memory_db. See https://github.com/singnet/das-atom-db/issues/210") + # + # db: AtomDB = request.getfixturevalue(database) + # # with pytest.raises(ValueError): + # db.create_field_index(**params) + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_bulk_insert(self, database, request): + if database == "redis_mongo_db": + pytest.skip( + "ERROR redis_mongo_db is not updating targets. See https://github.com/singnet/das-atom-db/issues/210") + db: AtomDB = request.getfixturevalue(database) + node_a = self._add_node(db, "Aaa", "Test", database) + link_a = self._add_link(db, 'Test', [{"name": "A", "type": "A"}], database) + node_a['name'] = "B" + link_a["targets"] = [node_a["handle"]] + db.bulk_insert([node_a, link_a]) + count = db.count_atoms({"precise": True}) + n = db.get_atom(node_a['handle']) + l = db.get_atom(link_a['handle']) + assert count['atom_count'] == 3 + assert count['node_count'] == 2 + assert count['link_count'] == 1 + assert n["name"] == "B" + assert l["targets"] == [node_a["handle"]] + + # Note no exception is raised if error + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_bulk_insert_exceptions(self, database, request): + db: AtomDB = request.getfixturevalue(database) + node_a = db._build_node({"name": "A", "type": "A"}) + link_a = db._build_link({"targets": [], "type": "A"}) + with pytest.raises(Exception): + db.bulk_insert([node_a, link_a]) + pytest.skip("ERROR should raise an exception. See https://github.com/singnet/das-atom-db/issues/210") + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_retrieve_all_atoms(self, database, request): + db: AtomDB = request.getfixturevalue(database) + node_a = self._add_node(db, "Aaa", "Test", database) + link_a = self._add_link(db, 'Test', [{"name": "A", "type": "A"}], database) + node_b = db.get_atom(db.get_node_handle(node_type="A", node_name="A")) + atoms = db.retrieve_all_atoms() + assert isinstance(atoms, list) + assert len(atoms) == 3 + assert all(a in atoms for a in [node_a, link_a, node_b]) + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_commit(self, database, request): + if database == "in_memory_db": + pytest.skip("ERROR Not implemented on in_memory_db. See https://github.com/singnet/das-atom-db/issues/210") + db: AtomDB = request.getfixturevalue(database) + db.add_node({'name': 'A', 'type': 'Test'}) + db.add_link({'type': 'Test', 'targets': []}) + db.commit() + count = db.count_atoms({"precise": True}) + assert count['atom_count'] == 2 + assert count['node_count'] == 1 + assert count['link_count'] == 1 + + @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) + def test_commit_buffer(self, database, request): + if database == "in_memory_db": + pytest.skip( + "ERROR Not implemented on in_memory_db. See https://github.com/singnet/das-atom-db/issues/210") + db: AtomDB = request.getfixturevalue(database) + node_a = self._add_node(db, "Aaa", "Test", database) + link_a = self._add_link(db, 'Test', [{"name": "A", "type": "A"}], database) + node_a['name'] = "B" + link_a["targets"] = [node_a["handle"]] + db.commit(buffer=[node_a, link_a]) + count = db.count_atoms({"precise": True}) + assert count['atom_count'] == 3 + assert count['node_count'] == 2 + assert count['link_count'] == 1 + + @pytest.mark.parametrize("database", ["redis_mongo_db"]) + def test_commit_buffer_exception(self, database, request): + + db: AtomDB = request.getfixturevalue(database) + with pytest.raises(Exception): + db.commit(buffer=[{"name": "A", "type": "A"}]) + + with pytest.raises(Exception, match="Failed to commit Atom Types"): + db.mongo_bulk_insertion_buffer = {"atom_types": ("a", "a")} + db.commit() + + + + + From 4017dffef6548922b1a201683ae93846173a774e Mon Sep 17 00:00:00 2001 From: Edgar Brissow Date: Thu, 5 Sep 2024 15:14:27 -0300 Subject: [PATCH 3/6] Cover more tests --- tests/unit/adapters/test_ram_only_extra.py | 5 +- tests/unit/adapters/test_redis_mongo_extra.py | 3 - tests/unit/fixtures.py | 3 - tests/unit/test_database.py | 342 +++++++++--------- 4 files changed, 172 insertions(+), 181 deletions(-) diff --git a/tests/unit/adapters/test_ram_only_extra.py b/tests/unit/adapters/test_ram_only_extra.py index c3190dee..48b668bd 100644 --- a/tests/unit/adapters/test_ram_only_extra.py +++ b/tests/unit/adapters/test_ram_only_extra.py @@ -1,4 +1,3 @@ -import pytest from tests.unit.fixtures import in_memory_db from tests.unit.test_database import _check_handle from hyperon_das_atomdb.adapters.ram_only import InMemoryDB @@ -15,10 +14,10 @@ def test__delete_atom_type(self, in_memory_db): db: InMemoryDB = in_memory_db node = db.add_node({"name": "A", "type": "A"}) assert len(db.all_named_types) == 1 - assert node['named_type'] in db.all_named_types + assert node["named_type"] in db.all_named_types db._delete_atom_type("A") assert len(db.all_named_types) == 0 - assert node['named_type'] not in db.all_named_types + assert node["named_type"] not in db.all_named_types def test__update_atom_indexes(self, in_memory_db): diff --git a/tests/unit/adapters/test_redis_mongo_extra.py b/tests/unit/adapters/test_redis_mongo_extra.py index d2912855..ea3c3c99 100644 --- a/tests/unit/adapters/test_redis_mongo_extra.py +++ b/tests/unit/adapters/test_redis_mongo_extra.py @@ -1,8 +1,5 @@ -from multiprocessing.managers import Value - import pytest from tests.unit.fixtures import redis_mongo_db -from tests.unit.test_database import _check_handle from hyperon_das_atomdb.adapters.redis_mongo_db import RedisMongoDB, _HashableDocument, MongoDBIndex from unittest import mock diff --git a/tests/unit/fixtures.py b/tests/unit/fixtures.py index f50967ab..726f7d00 100644 --- a/tests/unit/fixtures.py +++ b/tests/unit/fixtures.py @@ -69,7 +69,6 @@ def delete(self, *keys): return deleted_count def getdel(self, key): - print("Cccc") # Simulate the getdel method of Redis value = self.cache.get(key) @@ -78,7 +77,6 @@ def getdel(self, key): return value def srem(self, key, *members): - print("Bbbb") # Simulate the srem method of Redis if key not in self.cache: @@ -90,7 +88,6 @@ def srem(self, key, *members): def sscan(self, name, cursor=0, match=None, count=None): # Simulate the sscan method of Redis - print("Aaaa") key = name if key not in self.cache: return (0, []) diff --git a/tests/unit/test_database.py b/tests/unit/test_database.py index acc0dc7e..15aaff40 100644 --- a/tests/unit/test_database.py +++ b/tests/unit/test_database.py @@ -12,16 +12,16 @@ class TestDatabase: @staticmethod def _add_node(db: AtomDB, node_name, node_type, adapter, extra_fields=None): - node_dict = {'name': node_name, 'type': node_type} + node_dict = {"name": node_name, "type": node_type} node_dict.update(extra_fields or {}) node = db.add_node(node_dict) - if adapter == 'redis_mongo_db': + if adapter == "redis_mongo_db": db.commit() return node @staticmethod def _add_link(db: AtomDB, link_type, dict_targets, adapter, is_top_level=True): - link = db.add_link({'type': link_type, 'targets': dict_targets}, toplevel=is_top_level) + link = db.add_link({"type": link_type, "targets": dict_targets}, toplevel=is_top_level) if adapter != "in_memory_db": db.commit() return link @@ -78,17 +78,17 @@ def test_link_handle_exceptions(self, database, expected, request): ]) def test_reformat_document(self, database, kwlist, request): db: AtomDB = request.getfixturevalue(database) - node_handle = db.add_node({'name': 'A', 'type': 'Test'}).get('handle') + node_handle = db.add_node({"name": "A", "type": "Test"}).get("handle") if database != "in_memory_db": db.commit() - link = {'name': 'A', 'targets': [node_handle]} + link = {"name": "A", "targets": [node_handle]} for kw in kwlist: answer = db._reformat_document(link, **{kw: True}) - assert set(answer.keys()) == {'name', 'targets', 'targets_document'} - assert len(answer['targets']) == 1 - assert len(answer['targets_document']) == 1 - assert answer['name'] == "A" - assert isinstance(answer['targets'][0], (str if kw == "targets_document" else dict)) + assert set(answer.keys()) == {"name", "targets", "targets_document"} + assert len(answer["targets"]) == 1 + assert len(answer["targets_document"]) == 1 + assert answer["name"] == "A" + assert isinstance(answer["targets"][0], (str if kw == "targets_document" else dict)) @pytest.mark.parametrize("database,kwlist", [ ("redis_mongo_db", ["targets_document", "deep_representation"]), @@ -96,20 +96,20 @@ def test_reformat_document(self, database, kwlist, request): ]) def test_reformat_document_exceptions(self, database, kwlist, request): db: AtomDB = request.getfixturevalue(database) - link = {'name': 'A', 'targets': ['test']} + link = {"name": "A", "targets": ["test"]} for kw in kwlist: - with pytest.raises(Exception, match='Nonexistent atom'): + with pytest.raises(Exception, match="Nonexistent atom"): db._reformat_document(link, **{kw: True}) @pytest.mark.parametrize("database,expected_fields, expected_handle", [ - ("redis_mongo_db", ['handle', '_id', 'composite_type_hash', 'name', 'named_type'], - '180fed764dbd593f1ea45b63b13d7e69'), - ("in_memory_db", ['handle', '_id', 'composite_type_hash', 'name', 'named_type'], - '180fed764dbd593f1ea45b63b13d7e69') + ("redis_mongo_db", ["handle", "_id", "composite_type_hash", "name", "named_type"], + "180fed764dbd593f1ea45b63b13d7e69"), + ("in_memory_db", ["handle", "_id", "composite_type_hash", "name", "named_type"], + "180fed764dbd593f1ea45b63b13d7e69") ]) def test_build_node(self, database, expected_fields, expected_handle, request): db: AtomDB = request.getfixturevalue(database) - handle, node = db._build_node({'type': 'Test', 'name': 'test'}) + handle, node = db._build_node({"type": "Test", "name": "test"}) assert node assert handle == expected_handle assert all([k in node for k in expected_fields]) @@ -117,31 +117,31 @@ def test_build_node(self, database, expected_fields, expected_handle, request): assert _check_handle(handle) # Test exception - with pytest.raises(Exception, match='The "name" and "type" fields must be sent'): + with pytest.raises(Exception, match="The \"name\" and \"type\" fields must be sent"): db._build_node({}) @pytest.mark.parametrize("database,expected_fields, expected_handle,is_top_level", [ ("redis_mongo_db", - ['handle', '_id', 'composite_type_hash', 'named_type_hash', 'named_type', 'is_toplevel', 'targets'], - '180fed764dbd593f1ea45b63b13d7e69', True), + ["handle", "_id", "composite_type_hash", "named_type_hash", "named_type", "is_toplevel", "targets"], + "180fed764dbd593f1ea45b63b13d7e69", True), ("redis_mongo_db", - ['handle', '_id', 'composite_type_hash', 'named_type_hash', 'named_type', 'is_toplevel', 'targets'], - '180fed764dbd593f1ea45b63b13d7e69', False), + ["handle", "_id", "composite_type_hash", "named_type_hash", "named_type", "is_toplevel", "targets"], + "180fed764dbd593f1ea45b63b13d7e69", False), ("in_memory_db", - ['handle', '_id', 'composite_type_hash', 'named_type_hash', 'named_type', 'is_toplevel', 'targets'], - '180fed764dbd593f1ea45b63b13d7e69', True), + ["handle", "_id", "composite_type_hash", "named_type_hash", "named_type", "is_toplevel", "targets"], + "180fed764dbd593f1ea45b63b13d7e69", True), ("in_memory_db", - ['handle', '_id', 'composite_type_hash', 'named_type_hash', 'named_type', 'is_toplevel', 'targets'], - '180fed764dbd593f1ea45b63b13d7e69', False) + ["handle", "_id", "composite_type_hash", "named_type_hash", "named_type", "is_toplevel", "targets"], + "180fed764dbd593f1ea45b63b13d7e69", False) ]) def test_build_link(self, database, expected_fields, expected_handle, is_top_level, request): db: AtomDB = request.getfixturevalue(database) - handle, link, targets = db._build_link({'type': 'Test', 'targets': [{'type': 'Test', 'name': 'test'}]}, + handle, link, targets = db._build_link({"type": "Test", "targets": [{"type": "Test", "name": "test"}]}, is_top_level) assert expected_handle in targets assert all([k in link for k in expected_fields]) - assert link['is_toplevel'] == is_top_level + assert link["is_toplevel"] == is_top_level assert _check_handle(handle) assert isinstance(link, dict) assert isinstance(targets, list) @@ -149,42 +149,42 @@ def test_build_link(self, database, expected_fields, expected_handle, is_top_lev @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_build_link_exceptions(self, database, request): db: AtomDB = request.getfixturevalue(database) - with pytest.raises(ValueError, match='The target must be a dictionary'): - db._build_link({'type': 'Test', 'targets': ['']}) - with pytest.raises(Exception, match='The "type" and "targets" fields must be sent'): - db._build_link({'type': 'Test', 'targets': None}) - with pytest.raises(Exception, match='The "type" and "targets" fields must be sent'): - db._build_link({'type': None, 'targets': []}) + with pytest.raises(ValueError, match="The target must be a dictionary"): + db._build_link({"type": "Test", "targets": [""]}) + with pytest.raises(Exception, match="The \"type\" and \"targets\" fields must be sent"): + db._build_link({"type": "Test", "targets": None}) + with pytest.raises(Exception, match="The \"type\" and \"targets\" fields must be sent"): + db._build_link({"type": None, "targets": []}) @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_node_exists(self, database, request): db: AtomDB = request.getfixturevalue(database) - db.add_node({'name': 'A', 'type': 'Test'}) + db.add_node({"name": "A", "type": "Test"}) if database != "in_memory_db": db.commit() - no_exists = db.node_exists('Test', 'B') - exists = db.node_exists('Test', 'A') + no_exists = db.node_exists("Test", "B") + exists = db.node_exists("Test", "A") assert isinstance(no_exists, bool) assert isinstance(exists, bool) assert not no_exists assert exists @pytest.mark.parametrize("database,targets", [ - ("redis_mongo_db", ['180fed764dbd593f1ea45b63b13d7e69']), + ("redis_mongo_db", ["180fed764dbd593f1ea45b63b13d7e69"]), ("redis_mongo_db", []), - ("in_memory_db", ['180fed764dbd593f1ea45b63b13d7e69']), + ("in_memory_db", ["180fed764dbd593f1ea45b63b13d7e69"]), ("in_memory_db", []) ]) def test_link_exists(self, database, targets, request): db: AtomDB = request.getfixturevalue(database) - dict_targets = [{'type': 'Test', 'name': 'test'}] if targets else [] - link = {'type': 'Test', 'targets': dict_targets} + dict_targets = [{"type": "Test", "name": "test"}] if targets else [] + link = {"type": "Test", "targets": dict_targets} db.add_link(link) if database != "in_memory_db": db.commit() - no_exists = db.link_exists('Tes', []) - exists = db.link_exists('Test', targets) + no_exists = db.link_exists("Tes", []) + exists = db.link_exists("Test", targets) assert isinstance(no_exists, bool) assert isinstance(exists, bool) @@ -194,25 +194,25 @@ def test_link_exists(self, database, targets, request): @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_node_handle(self, database, request): db: AtomDB = request.getfixturevalue(database) - expected_node = self._add_node(db, 'A', 'Test', database) - node = db.get_node_handle('Test', 'A') - assert node == expected_node['handle'] + expected_node = self._add_node(db, "A", "Test", database) + node = db.get_node_handle("Test", "A") + assert node == expected_node["handle"] assert _check_handle(node) @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_node_handle_exceptions(self, database, request): db: AtomDB = request.getfixturevalue(database) - with pytest.raises(Exception, match='Nonexistent atom'): - db.get_node_handle('Test', 'A') + with pytest.raises(Exception, match="Nonexistent atom"): + db.get_node_handle("Test", "A") @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_node_name(self, database, request): db: AtomDB = request.getfixturevalue(database) - expected_node = self._add_node(db, 'A', 'Test', database) - name = db.get_node_name(expected_node['handle']) + expected_node = self._add_node(db, "A", "Test", database) + name = db.get_node_name(expected_node["handle"]) # NOTE all adapters must return the same type assert isinstance(name, str) - assert name == expected_node['name'] + assert name == expected_node["name"] @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_node_name_exceptions(self, database, request): @@ -222,52 +222,52 @@ def test_get_node_name_exceptions(self, database, request): "See https://github.com/singnet/das-atom-db/issues/210") db: AtomDB = request.getfixturevalue(database) # in memory returns a AtomDoesNotExist exception, redis_mongo returns ValueError - with pytest.raises(Exception, match='Nonexistent atom'): - db.get_node_name('error') + with pytest.raises(Exception, match="Nonexistent atom"): + db.get_node_name("error") @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_node_type(self, database, request): db: AtomDB = request.getfixturevalue(database) - expected_node = self._add_node(db, 'A', 'Test', database) - node_type = db.get_node_type(expected_node['handle']) + expected_node = self._add_node(db, "A", "Test", database) + node_type = db.get_node_type(expected_node["handle"]) assert isinstance(node_type, str) - assert node_type == expected_node['named_type'] + assert node_type == expected_node["named_type"] @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_node_type_exceptions(self, database, request): db: AtomDB = request.getfixturevalue(database) - with pytest.raises(Exception, match='Nonexistent atom'): - db.get_node_type('test') + with pytest.raises(Exception, match="Nonexistent atom"): + db.get_node_type("test") @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_node_by_name(self, database, request): db: AtomDB = request.getfixturevalue(database) - expected_nodes = [self._add_node(db, n, 'Test', database) for n in {'A', 'Aa', 'Ac'}] - not_expected_nodes = [self._add_node(db, n, 'Test', database) for n in {'B', 'Ba', 'Bc'}] - nodes = db.get_node_by_name('Test', 'A') - not_nodes = db.get_node_by_name('Test', 'C') + expected_nodes = [self._add_node(db, n, "Test", database) for n in {"A", "Aa", "Ac"}] + not_expected_nodes = [self._add_node(db, n, "Test", database) for n in {"B", "Ba", "Bc"}] + nodes = db.get_node_by_name("Test", "A") + not_nodes = db.get_node_by_name("Test", "C") assert not_nodes == [] assert isinstance(nodes, list) assert len(nodes) == 3 assert all(_check_handle(node) for node in nodes) - assert all(n['handle'] in nodes for n in expected_nodes) - assert not any(n['handle'] in nodes for n in not_expected_nodes) + assert all(n["handle"] in nodes for n in expected_nodes) + assert not any(n["handle"] in nodes for n in not_expected_nodes) @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_atoms_by_field(self, database, request): if database == "in_memory_db": pytest.skip("ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210") db: AtomDB = request.getfixturevalue(database) - expected_node = self._add_node(db, 'Ac', 'Test', database) - expected_link = self._add_link(db, 'Ac', [expected_node], database) - nodes = db.get_atoms_by_field([{'field': 'name', 'value': 'Ac'}]) - links = db.get_atoms_by_field([{'field': 'named_type', 'value': 'Ac'}]) + expected_node = self._add_node(db, "Ac", "Test", database) + expected_link = self._add_link(db, "Ac", [expected_node], database) + nodes = db.get_atoms_by_field([{"field": "name", "value": "Ac"}]) + links = db.get_atoms_by_field([{"field": "named_type", "value": "Ac"}]) assert isinstance(nodes, list) assert isinstance(links, list) assert all(_check_handle(node) for node in nodes) assert all(_check_handle(link) for link in links) - assert nodes[0] == expected_node['handle'] - assert links[0] == expected_link['handle'] + assert nodes[0] == expected_node["handle"] + assert links[0] == expected_link["handle"] @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_atoms_by_index(self, database, request): @@ -297,7 +297,7 @@ def test_get_atoms_by_text_field_regex(self, database, request): pytest.skip("ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210") db: AtomDB = request.getfixturevalue(database) self._add_node(db, "A", "Test", database, {"value": "Test sentence"}) - self._add_link(db, 'Test', [], database) + self._add_link(db, "Test", [], database) index_id = db.create_field_index(atom_type="node", fields=["value"], named_type="Test", index_type="token_inverted_list") atoms = db.get_atoms_by_text_field("Test", "value", text_index_id=index_id) @@ -316,7 +316,7 @@ def test_get_atoms_by_text_field_text(self, database, request): index_type="token_inverted_list") with mock.patch( "mongomock.filtering._Filterer.apply", - return_value=['815212e3d7ac246e70c1744d14a8c402'] + return_value=["815212e3d7ac246e70c1744d14a8c402"] ): atoms = db.get_atoms_by_text_field("Test", text_index_id=index_id) assert isinstance(atoms, list) @@ -336,7 +336,7 @@ def test_get_node_by_name_starting_with(self, database, request): assert isinstance(nodes, list) assert all(_check_handle(n) for n in nodes) assert all(isinstance(n, str) for n in nodes) - assert all(handle in nodes for handle in [node_a['handle'], node_b['handle']]) + assert all(handle in nodes for handle in [node_a["handle"], node_b["handle"]]) assert len(nodes) == 2 @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) @@ -366,9 +366,9 @@ def test_get_all_nodes(self, database, request): ]) def test_get_all_links(self, database, params, links_len, cursor_value, request): db: AtomDB = request.getfixturevalue(database) - self._add_link(db, 'Ac', [{"name": "A", "type": "A"}], database) - self._add_link(db, 'Ac', [{"name": "B", "type": "B"}], database) - self._add_link(db, 'Ac', [{"name": "C", "type": "C"}], database) + self._add_link(db, "Ac", [{"name": "A", "type": "A"}], database) + self._add_link(db, "Ac", [{"name": "B", "type": "B"}], database) + self._add_link(db, "Ac", [{"name": "C", "type": "C"}], database) cursor, links = db.get_all_links(**params) assert cursor == cursor_value assert isinstance(links, list) @@ -379,8 +379,8 @@ def test_get_all_links(self, database, params, links_len, cursor_value, request) @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_link_handle(self, database, request): db: AtomDB = request.getfixturevalue(database) - link = self._add_link(db, 'Ac', [{"name": "A", "type": "A"}], database) - handle = db.get_link_handle(link['type'], link['targets']) + link = self._add_link(db, "Ac", [{"name": "A", "type": "A"}], database) + handle = db.get_link_handle(link["type"], link["targets"]) assert _check_handle(handle) @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) @@ -392,12 +392,12 @@ def test_get_link_handle_exceptions(self, database, request): @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_link_type(self, database, request): db: AtomDB = request.getfixturevalue(database) - link_a = self._add_link(db, 'Ac', [{"name": "A", "type": "A"}], database) - self._add_link(db, 'Bc', [{"name": "A", "type": "A"}], database) - link_type = db.get_link_type(link_a['handle']) + link_a = self._add_link(db, "Ac", [{"name": "A", "type": "A"}], database) + self._add_link(db, "Bc", [{"name": "A", "type": "A"}], database) + link_type = db.get_link_type(link_a["handle"]) assert link_type assert isinstance(link_type, str) - assert link_type == link_a['type'] + assert link_type == link_a["type"] @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_link_type_exceptions(self, database, request): @@ -408,20 +408,20 @@ def test_get_link_type_exceptions(self, database, request): @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_link_targets(self, database, request): db: AtomDB = request.getfixturevalue(database) - link_a = self._add_link(db, 'Ac', [{"name": "A", "type": "A"}], database) - targets = db.get_link_targets(link_a['handle']) + link_a = self._add_link(db, "Ac", [{"name": "A", "type": "A"}], database) + targets = db.get_link_targets(link_a["handle"]) assert isinstance(targets, list) assert len(targets) == 1 assert all(_check_handle(t) for t in targets) assert all(isinstance(t, str) for t in targets) - assert targets == link_a['targets'] + assert targets == link_a["targets"] @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_is_ordered(self, database, request): db: AtomDB = request.getfixturevalue(database) - link_a = self._add_link(db, 'Ac', [{"name": "A", "type": "A"}], database) + link_a = self._add_link(db, "Ac", [{"name": "A", "type": "A"}], database) # NOTE just retrieves the link ... - assert db.is_ordered(link_a['handle']) + assert db.is_ordered(link_a["handle"]) @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_is_ordered_no_handle(self, database, request): @@ -447,18 +447,18 @@ def test_is_ordered_no_handle(self, database, request): def test_get_incoming_links(self, database, params, links_len, cursor_value, request): db: AtomDB = request.getfixturevalue(database) node_a = self._add_node(db, "Aaa", "Test", database) - self._add_link(db, 'Aa', [node_a], database) - self._add_link(db, 'Ab', [node_a], database) - self._add_link(db, 'Ac', [node_a], database) - cursor, links = db.get_incoming_links(node_a['handle'], **params) + self._add_link(db, "Aa", [node_a], database) + self._add_link(db, "Ab", [node_a], database) + self._add_link(db, "Ac", [node_a], database) + cursor, links = db.get_incoming_links(node_a["handle"], **params) assert cursor == cursor_value assert len(links) == links_len - assert all([_check_handle(l if params.get('handles_only') else l["handle"]) for l in links]) + assert all([_check_handle(l if params.get("handles_only") else l["handle"]) for l in links]) @pytest.mark.parametrize("database,params,links_len,cursor_value", [ ("redis_mongo_db", {}, 1, None), ("redis_mongo_db", {"toplevel_only": True}, 1, None), - # ("redis_mongo_db", {"link_type": "NoTopLevel" , "toplevel_only": True}, 0, None), # doesn't work + # ("redis_mongo_db", {"link_type": "NoTopLevel" , "toplevel_only": True}, 0, None), # doesn"t work # Note returning different values # ("in_memory_db", {"link_type": "*", "toplevel_only": True}, 3, None), # ("redis_mongo_db", {"link_type": "*", "toplevel_only": True}, 0, None), @@ -469,7 +469,7 @@ def test_get_incoming_links(self, database, params, links_len, cursor_value, req ("redis_mongo_db", {"cursor": 0, "chunk_size": 1}, 1, None), ("in_memory_db", {}, 1, None), ("in_memory_db", {"toplevel_only": True}, 1, None), - # ("in_memory_db", {"link_type": "NoTopLevel", "toplevel_only": True}, 0, None), # doesn't work + # ("in_memory_db", {"link_type": "NoTopLevel", "toplevel_only": True}, 0, None), # doesn"t work ("in_memory_db", {"link_type": "*"}, 3, None), ("in_memory_db", {"toplevel_only": True}, 1, None), ("in_memory_db", {"target_handles": "*"}, 1, None), @@ -480,15 +480,13 @@ def test_get_incoming_links(self, database, params, links_len, cursor_value, req def test_get_matched_links(self, database, params, links_len, cursor_value, request): db: AtomDB = request.getfixturevalue(database) node_a = self._add_node(db, "Aaa", "Test", database) - link_a = self._add_link(db, 'Aa', [node_a], database) - _ = self._add_link(db, 'NoTopLevel', [node_a], database, is_top_level=False) - _ = self._add_link(db, 'Ac', [node_a], database) - params['link_type'] = link_a['type'] if not params.get('link_type') else params['link_type'] - params['target_handles'] = link_a['targets'] if not params.get('target_handles') else params['target_handles'] + link_a = self._add_link(db, "Aa", [node_a], database) + _ = self._add_link(db, "NoTopLevel", [node_a], database, is_top_level=False) + _ = self._add_link(db, "Ac", [node_a], database) + params["link_type"] = link_a["type"] if not params.get("link_type") else params["link_type"] + params["target_handles"] = link_a["targets"] if not params.get("target_handles") else params["target_handles"] cursor, links = db.get_matched_links(**params) assert cursor == cursor_value - print(links) - print(node_a["handle"]) assert len(links) == links_len if all(isinstance(l, tuple) for l in links): for link in links: @@ -526,15 +524,15 @@ def test_get_matched_type_template(self, database, params, links_len, cursor_val db: AtomDB = request.getfixturevalue(database) node_a = self._add_node(db, "Aaa", "Test", database) node_b = self._add_node(db, "Bbb", "Test", database) - link_a = self._add_link(db, 'Aa', [node_a, node_b], database, is_top_level=is_top_level) + link_a = self._add_link(db, "Aa", [node_a, node_b], database, is_top_level=is_top_level) cursor, links = db.get_matched_type_template(["Aa", "Test", "Test"], **params) assert cursor == cursor_value assert len(links) == links_len if len(links) > 0: for link in links: assert _check_handle(link[0]) - assert link[0] == link_a['handle'] - assert all(t in link[1] for t in link_a['targets']) + assert link[0] == link_a["handle"] + assert all(t in link[1] for t in link_a["targets"]) assert all(_check_handle(t) for t in link[1]) @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) @@ -544,16 +542,16 @@ def test_get_matched_type(self, database, request): "ERROR redis_mongo_db is returning more values. " "See https://github.com/singnet/das-atom-db/issues/210") db: AtomDB = request.getfixturevalue(database) - link_a = self._add_link(db, 'Aa', [], database) - self._add_link(db, 'Ab', [], database) - cursor, links = db.get_matched_type(link_a['type']) + link_a = self._add_link(db, "Aa", [], database) + self._add_link(db, "Ab", [], database) + cursor, links = db.get_matched_type(link_a["type"]) assert cursor is None assert len(links) == 1 if len(links) > 0: for link in links: assert _check_handle(link[0]) - assert link[0] == link_a['handle'] - assert all(t in link[1] for t in link_a['targets']) + assert link[0] == link_a["handle"] + assert all(t in link[1] for t in link_a["targets"]) assert all(_check_handle(t) for t in link[1]) @pytest.mark.parametrize("database,params,top_level,n_links,n_nodes", [ @@ -570,10 +568,10 @@ def test_get_matched_type(self, database, request): def test_get_atom_node(self, database, params, top_level, n_links, n_nodes, request): db: AtomDB = request.getfixturevalue(database) node_a = self._add_node(db, "Aaa", "Test", database) - atom_n = db.get_atom(node_a['handle'], **params) + atom_n = db.get_atom(node_a["handle"], **params) assert atom_n - assert atom_n['handle'] == node_a['handle'] - assert _check_handle(atom_n['handle']) + assert atom_n["handle"] == node_a["handle"] + assert _check_handle(atom_n["handle"]) @pytest.mark.parametrize("database,params,top_level,n_links,n_nodes", [ ("redis_mongo_db", {}, True, 1, 1), @@ -588,19 +586,19 @@ def test_get_atom_node(self, database, params, top_level, n_links, n_nodes, requ ]) def test_get_atom_link(self, database, params, top_level, n_links, n_nodes, request): db: AtomDB = request.getfixturevalue(database) - link_a = self._add_link(db, 'Aa', [], database, is_top_level=top_level) - atom_l = db.get_atom(link_a['handle'], **params) + link_a = self._add_link(db, "Aa", [], database, is_top_level=top_level) + atom_l = db.get_atom(link_a["handle"], **params) assert atom_l - assert atom_l['handle'] == link_a['handle'] - assert _check_handle(atom_l['handle']) + assert atom_l["handle"] == link_a["handle"] + assert _check_handle(atom_l["handle"]) @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test__get_atom(self, database, request): db: AtomDB = request.getfixturevalue(database) node_a = self._add_node(db, "Aaa", "Test", database) - link_a = self._add_link(db, 'Aa', [], database) - node = db._get_atom(node_a['handle']) - link = db._get_atom(link_a['handle']) + link_a = self._add_link(db, "Aa", [], database) + node = db._get_atom(node_a["handle"]) + link = db._get_atom(link_a["handle"]) assert node, link @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) @@ -615,7 +613,7 @@ def test__get_atom_none(self, database, request): def test_get_atom_type(self, database, request): db: AtomDB = request.getfixturevalue(database) node_a = self._add_node(db, "Aaa", "Test", database) - link_a = self._add_link(db, 'Test', [], database) + link_a = self._add_link(db, "Test", [], database) atom_type_node = db.get_atom_type(node_a["handle"]) atom_type_link = db.get_atom_type(link_a["handle"]) assert isinstance(atom_type_node, str) @@ -634,7 +632,7 @@ def test_get_atom_type_none(self, database, request): def test_get_atom_as_dict(self, database, request): db: AtomDB = request.getfixturevalue(database) node_a = self._add_node(db, "Aaa", "Test", database) - link_a = self._add_link(db, 'Test', [], database) + link_a = self._add_link(db, "Test", [], database) atom_node = db.get_atom_as_dict(node_a["handle"]) atom_link = db.get_atom_as_dict(link_a["handle"]) assert isinstance(atom_node, dict) @@ -676,26 +674,26 @@ def test_get_atom_as_dict_exceptions(self, database, request): def test_count_atoms(self, database, params, request): db: AtomDB = request.getfixturevalue(database) self._add_node(db, "Aaa", "Test", database) - self._add_link(db, 'Test', [], database) + self._add_link(db, "Test", [], database) atoms_count = db.count_atoms(params) assert atoms_count assert isinstance(atoms_count, dict) - assert isinstance(atoms_count['atom_count'], int) - assert atoms_count['atom_count'] == 2 - if params.get('precise', False): - assert isinstance(atoms_count['node_count'], int) - assert isinstance(atoms_count['link_count'], int) - assert atoms_count['node_count'] == 1 - assert atoms_count['link_count'] == 1 + assert isinstance(atoms_count["atom_count"], int) + assert atoms_count["atom_count"] == 2 + if params.get("precise", False): + assert isinstance(atoms_count["node_count"], int) + assert isinstance(atoms_count["link_count"], int) + assert atoms_count["node_count"] == 1 + assert atoms_count["link_count"] == 1 @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_clear_database(self, database, request): db: AtomDB = request.getfixturevalue(database) self._add_node(db, "Aaa", "Test", database) - self._add_link(db, 'Test', [], database) - assert db.count_atoms()['atom_count'] == 2 + self._add_link(db, "Test", [], database) + assert db.count_atoms()["atom_count"] == 2 db.clear_database() - assert db.count_atoms()['atom_count'] == 0 + assert db.count_atoms()["atom_count"] == 0 @pytest.mark.parametrize("database,node", [ ("redis_mongo_db", {"name": "A", "type": "A"}), @@ -708,7 +706,7 @@ def test_add_node(self, database, node, request): node = db.add_node(node) count = db.count_atoms() assert node - assert count['atom_count'] == 1 + assert count["atom_count"] == 1 assert isinstance(node, dict) @pytest.mark.parametrize("database,node", [ @@ -721,17 +719,17 @@ def test_add_node_discard(self, database, node, request): node = db.add_node(node) count = db.count_atoms() assert node is None - assert count['atom_count'] == 0 + assert count["atom_count"] == 0 @pytest.mark.parametrize("database,node", [ ("redis_mongo_db", {}), - # NOTE it's not breaking, should break? + # NOTE it"s not breaking, should break? # ("redis_mongo_db", {"name": "A", "type": "A", "handle": ""}), # ("redis_mongo_db", {"name": "A", "type": "A", "_id": ""}), # ("redis_mongo_db", {"name": "A", "type": "A", "composite_type_hash": ""}), # ("redis_mongo_db", {"name": "A", "type": "A", "named_type": ""}), ("in_memory_db", {}), - # NOTE it's not breaking, should break? + # NOTE it"s not breaking, should break? # ("in_memory_db", {"name": "A", "type": "A", "handle": ""}), # ("in_memory_db", {"name": "A", "type": "A", "_id": ""}), # ("in_memory_db", {"name": "A", "type": "A", "composite_type_hash": ""}), @@ -743,10 +741,10 @@ def test_add_node_exceptions(self, database, node, request): db.add_node(node) @pytest.mark.parametrize("database,params,expected_count,top_level", [ - ("redis_mongo_db", {'type': "A", 'targets': [{"name": "A", "type": "A"}]}, 2, True), - ("redis_mongo_db", {'type': "A", 'targets': []}, 1, True), - ("in_memory_db", {'type': "A", 'targets': [{"name": "A", "type": "A"}]}, 2, True), - ("in_memory_db", {'type': "A", 'targets': []}, 1, True), + ("redis_mongo_db", {"type": "A", "targets": [{"name": "A", "type": "A"}]}, 2, True), + ("redis_mongo_db", {"type": "A", "targets": []}, 1, True), + ("in_memory_db", {"type": "A", "targets": [{"name": "A", "type": "A"}]}, 2, True), + ("in_memory_db", {"type": "A", "targets": []}, 1, True), ]) def test_add_link(self, database, params, expected_count, top_level, request): db: AtomDB = request.getfixturevalue(database) @@ -755,7 +753,7 @@ def test_add_link(self, database, params, expected_count, top_level, request): link = db.add_link(params, top_level) count = db.count_atoms() assert link - assert count['atom_count'] == expected_count + assert count["atom_count"] == expected_count assert isinstance(link, dict) @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) @@ -764,7 +762,7 @@ def test_reindex(self, database, request): pytest.skip("ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210") db: AtomDB = request.getfixturevalue(database) node_a = self._add_node(db, "Aaa", "Test", database) - link_a = self._add_link(db, 'Test', [], database) + link_a = self._add_link(db, "Test", [], database) db.reindex() @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) @@ -773,21 +771,21 @@ def test_delete_atom(self, database, request): pytest.skip("ERROR Atom not in incoming_set. See https://github.com/singnet/das-atom-db/issues/210") db: AtomDB = request.getfixturevalue(database) node_a = self._add_node(db, "Aaa", "Test", database) - link_a = self._add_link(db, 'Test', [], database) + link_a = self._add_link(db, "Test", [], database) count = db.count_atoms({"precise": True}) - assert count['atom_count'] == 2 - assert count['node_count'] == 1 - assert count['link_count'] == 1 - db.delete_atom(node_a['handle']) + assert count["atom_count"] == 2 + assert count["node_count"] == 1 + assert count["link_count"] == 1 + db.delete_atom(node_a["handle"]) count = db.count_atoms({"precise": True}) - assert count['atom_count'] == 1 - assert count['node_count'] == 0 - assert count['link_count'] == 1 - db.delete_atom(link_a['handle']) + assert count["atom_count"] == 1 + assert count["node_count"] == 0 + assert count["link_count"] == 1 + db.delete_atom(link_a["handle"]) count = db.count_atoms({"precise": True}) - assert count['atom_count'] == 0 - assert count['node_count'] == 0 - assert count['link_count'] == 0 + assert count["atom_count"] == 0 + assert count["node_count"] == 0 + assert count["link_count"] == 0 @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_delete_atom_exceptions(self, database, request): @@ -860,16 +858,16 @@ def test_bulk_insert(self, database, request): "ERROR redis_mongo_db is not updating targets. See https://github.com/singnet/das-atom-db/issues/210") db: AtomDB = request.getfixturevalue(database) node_a = self._add_node(db, "Aaa", "Test", database) - link_a = self._add_link(db, 'Test', [{"name": "A", "type": "A"}], database) - node_a['name'] = "B" + link_a = self._add_link(db, "Test", [{"name": "A", "type": "A"}], database) + node_a["name"] = "B" link_a["targets"] = [node_a["handle"]] db.bulk_insert([node_a, link_a]) count = db.count_atoms({"precise": True}) - n = db.get_atom(node_a['handle']) - l = db.get_atom(link_a['handle']) - assert count['atom_count'] == 3 - assert count['node_count'] == 2 - assert count['link_count'] == 1 + n = db.get_atom(node_a["handle"]) + l = db.get_atom(link_a["handle"]) + assert count["atom_count"] == 3 + assert count["node_count"] == 2 + assert count["link_count"] == 1 assert n["name"] == "B" assert l["targets"] == [node_a["handle"]] @@ -887,7 +885,7 @@ def test_bulk_insert_exceptions(self, database, request): def test_retrieve_all_atoms(self, database, request): db: AtomDB = request.getfixturevalue(database) node_a = self._add_node(db, "Aaa", "Test", database) - link_a = self._add_link(db, 'Test', [{"name": "A", "type": "A"}], database) + link_a = self._add_link(db, "Test", [{"name": "A", "type": "A"}], database) node_b = db.get_atom(db.get_node_handle(node_type="A", node_name="A")) atoms = db.retrieve_all_atoms() assert isinstance(atoms, list) @@ -899,13 +897,13 @@ def test_commit(self, database, request): if database == "in_memory_db": pytest.skip("ERROR Not implemented on in_memory_db. See https://github.com/singnet/das-atom-db/issues/210") db: AtomDB = request.getfixturevalue(database) - db.add_node({'name': 'A', 'type': 'Test'}) - db.add_link({'type': 'Test', 'targets': []}) + db.add_node({"name": "A", "type": "Test"}) + db.add_link({"type": "Test", "targets": []}) db.commit() count = db.count_atoms({"precise": True}) - assert count['atom_count'] == 2 - assert count['node_count'] == 1 - assert count['link_count'] == 1 + assert count["atom_count"] == 2 + assert count["node_count"] == 1 + assert count["link_count"] == 1 @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_commit_buffer(self, database, request): @@ -914,14 +912,14 @@ def test_commit_buffer(self, database, request): "ERROR Not implemented on in_memory_db. See https://github.com/singnet/das-atom-db/issues/210") db: AtomDB = request.getfixturevalue(database) node_a = self._add_node(db, "Aaa", "Test", database) - link_a = self._add_link(db, 'Test', [{"name": "A", "type": "A"}], database) - node_a['name'] = "B" + link_a = self._add_link(db, "Test", [{"name": "A", "type": "A"}], database) + node_a["name"] = "B" link_a["targets"] = [node_a["handle"]] db.commit(buffer=[node_a, link_a]) count = db.count_atoms({"precise": True}) - assert count['atom_count'] == 3 - assert count['node_count'] == 2 - assert count['link_count'] == 1 + assert count["atom_count"] == 3 + assert count["node_count"] == 2 + assert count["link_count"] == 1 @pytest.mark.parametrize("database", ["redis_mongo_db"]) def test_commit_buffer_exception(self, database, request): From e1d7712b75c112e9b17825979d41c6cf91f70a2d Mon Sep 17 00:00:00 2001 From: Edgar Brissow Date: Thu, 5 Sep 2024 15:33:33 -0300 Subject: [PATCH 4/6] fix flake8 --- hyperon_das_atomdb/adapters/ram_only.py | 62 +- hyperon_das_atomdb/adapters/redis_mongo_db.py | 1 - tests/unit/adapters/test_ram_only_extra.py | 20 +- tests/unit/adapters/test_redis_mongo_extra.py | 32 +- tests/unit/fixtures.py | 27 +- tests/unit/test_database.py | 712 ++++++++++++------ 6 files changed, 537 insertions(+), 317 deletions(-) diff --git a/hyperon_das_atomdb/adapters/ram_only.py b/hyperon_das_atomdb/adapters/ram_only.py index cbf58c45..2e044e3f 100644 --- a/hyperon_das_atomdb/adapters/ram_only.py +++ b/hyperon_das_atomdb/adapters/ram_only.py @@ -220,11 +220,11 @@ def _delete_incoming_set(self, link_handle: str, atoms_handle: list[str]) -> Non handles.remove(link_handle) def _add_templates( - self, - composite_type_hash: str, - named_type_hash: str, - key: str, - targets_hash: list[str], + self, + composite_type_hash: str, + named_type_hash: str, + key: str, + targets_hash: list[str], ) -> None: """ Add templates to the database. @@ -473,7 +473,7 @@ def get_node_by_name(self, node_type: str, substring: str) -> list[str]: key for key, value in self.db.node.items() if substring in value[FieldNames.NODE_NAME] - and node_type_hash == value[FieldNames.COMPOSITE_TYPE_HASH] + and node_type_hash == value[FieldNames.COMPOSITE_TYPE_HASH] ] def get_all_nodes(self, node_type: str, names: bool = False) -> list[str]: @@ -549,7 +549,7 @@ def is_ordered(self, link_handle: str) -> bool: ) def get_matched_links( - self, link_type: str, target_handles: list[str], **kwargs + self, link_type: str, target_handles: list[str], **kwargs ) -> MatchedLinksResultT: if link_type != WILDCARD and WILDCARD not in target_handles: return kwargs.get("cursor"), [self.get_link_handle(link_type, target_handles)] @@ -598,27 +598,31 @@ def get_matched_type(self, link_type: str, **kwargs) -> MatchedTypesResultT: return kwargs.get("cursor"), self._filter_non_toplevel(templates_matched) return kwargs.get("cursor"), templates_matched - def get_atoms_by_field(self, query: list[OrderedDict[str, str]]) -> list[str]: # pragma: no cover + def get_atoms_by_field( + self, query: list[OrderedDict[str, str]] + ) -> list[str]: # pragma: no cover raise NotImplementedError() def get_atoms_by_index( - self, - index_id: str, - query: list[OrderedDict[str, str]], - cursor: int = 0, - chunk_size: int = 500, + self, + index_id: str, + query: list[OrderedDict[str, str]], + cursor: int = 0, + chunk_size: int = 500, ) -> tuple[int, list[AtomT]]: # pragma: no cover raise NotImplementedError() def get_atoms_by_text_field( - self, - text_value: str, - field: str | None = None, - text_index_id: str | None = None, + self, + text_value: str, + field: str | None = None, + text_index_id: str | None = None, ) -> list[str]: # pragma: no cover raise NotImplementedError() - def get_node_by_name_starting_with(self, node_type: str, startswith: str) -> list[str]: # pragma: no cover + def get_node_by_name_starting_with( + self, node_type: str, startswith: str + ) -> list[str]: # pragma: no cover raise NotImplementedError() def _get_atom(self, handle: str) -> AtomT | None: @@ -673,7 +677,7 @@ def add_node(self, node_params: NodeParamsT) -> NodeT | None: def add_link(self, link_params: LinkParamsT, toplevel: bool = True) -> LinkT | None: result = self._build_link(link_params, toplevel) # NOTE unreachable - if result is None: # pragma: no cover + if result is None: # pragma: no cover return None handle, link, _ = result self.db.link[handle] = link @@ -681,8 +685,8 @@ def add_link(self, link_params: LinkParamsT, toplevel: bool = True) -> LinkT | N return link def reindex( - self, pattern_index_templates: dict[str, list[dict[str, Any]]] | None = None - ) -> None: # pragma: no cover + self, pattern_index_templates: dict[str, list[dict[str, Any]]] | None = None + ) -> None: # pragma: no cover raise NotImplementedError() def delete_atom(self, handle: str, **kwargs) -> None: @@ -705,13 +709,13 @@ def delete_atom(self, handle: str, **kwargs) -> None: raise ex def create_field_index( - self, - atom_type: str, - fields: list[str], - named_type: str | None = None, - composite_type: list[Any] | None = None, - index_type: FieldIndexType | None = None, - ) -> str: # pragma: no cover + self, + atom_type: str, + fields: list[str], + named_type: str | None = None, + composite_type: list[Any] | None = None, + index_type: FieldIndexType | None = None, + ) -> str: # pragma: no cover raise NotImplementedError() def bulk_insert(self, documents: list[AtomT]) -> None: @@ -733,5 +737,5 @@ def retrieve_all_atoms(self) -> list[AtomT]: logger().error(f"Error retrieving all atoms: {str(e)}") raise e - def commit(self, **kwargs) -> None: # pragma: no cover + def commit(self, **kwargs) -> None: # pragma: no cover raise NotImplementedError() diff --git a/hyperon_das_atomdb/adapters/redis_mongo_db.py b/hyperon_das_atomdb/adapters/redis_mongo_db.py index 64615a21..a58099e6 100644 --- a/hyperon_das_atomdb/adapters/redis_mongo_db.py +++ b/hyperon_das_atomdb/adapters/redis_mongo_db.py @@ -89,7 +89,6 @@ class MongoIndexType(str, Enum): TEXT = "text" - class _HashableDocument: """Class for making documents hashable.""" diff --git a/tests/unit/adapters/test_ram_only_extra.py b/tests/unit/adapters/test_ram_only_extra.py index 48b668bd..eb766620 100644 --- a/tests/unit/adapters/test_ram_only_extra.py +++ b/tests/unit/adapters/test_ram_only_extra.py @@ -1,32 +1,26 @@ -from tests.unit.fixtures import in_memory_db -from tests.unit.test_database import _check_handle from hyperon_das_atomdb.adapters.ram_only import InMemoryDB +from tests.unit.fixtures import in_memory_db # noqa: F401 +from tests.unit.test_database import _check_handle -class TestRamOnlyExtra: - def test__build_atom_type_key_hash(self, in_memory_db): +class TestRamOnlyExtra: + def test__build_atom_type_key_hash(self, in_memory_db): # noqa: F811 db: InMemoryDB = in_memory_db hash = db._build_atom_type_key_hash("A") assert _check_handle(hash) assert hash == "2c832bdcd9d74bf961205676d861540a" - def test__delete_atom_type(self, in_memory_db): + def test__delete_atom_type(self, in_memory_db): # noqa: F811 db: InMemoryDB = in_memory_db node = db.add_node({"name": "A", "type": "A"}) - assert len(db.all_named_types) == 1 + assert len(db.all_named_types) == 1 assert node["named_type"] in db.all_named_types db._delete_atom_type("A") assert len(db.all_named_types) == 0 assert node["named_type"] not in db.all_named_types - - def test__update_atom_indexes(self, in_memory_db): + def test__update_atom_indexes(self, in_memory_db): # noqa: F811 db: InMemoryDB = in_memory_db node = db.add_node({"name": "A", "type": "A"}) db._update_atom_indexes([node]) assert len(db.all_named_types) == 1 - - - - - diff --git a/tests/unit/adapters/test_redis_mongo_extra.py b/tests/unit/adapters/test_redis_mongo_extra.py index ea3c3c99..d4edbb76 100644 --- a/tests/unit/adapters/test_redis_mongo_extra.py +++ b/tests/unit/adapters/test_redis_mongo_extra.py @@ -1,12 +1,13 @@ -import pytest -from tests.unit.fixtures import redis_mongo_db -from hyperon_das_atomdb.adapters.redis_mongo_db import RedisMongoDB, _HashableDocument, MongoDBIndex from unittest import mock +import pytest + +from hyperon_das_atomdb.adapters.redis_mongo_db import MongoDBIndex, RedisMongoDB, _HashableDocument +from tests.unit.fixtures import redis_mongo_db # noqa: F401 -class TestRedisMongoExtra: - def test_hashable_document_str(self, redis_mongo_db): +class TestRedisMongoExtra: + def test_hashable_document_str(self, redis_mongo_db): # noqa: F811 db = redis_mongo_db node = db._build_node({"type": "A", "name": "A"}) hashable = _HashableDocument(node) @@ -15,21 +16,26 @@ def test_hashable_document_str(self, redis_mongo_db): assert hashable assert str(node) == str_hashable - @pytest.mark.parametrize("params", [ - {"atom_type": "A", "fields": []}, - {"atom_type": "A", "fields": None}, - ]) + @pytest.mark.parametrize( + "params", + [ + {"atom_type": "A", "fields": []}, + {"atom_type": "A", "fields": None}, + ], + ) def test_index_create_exceptions(self, params, request): db = request.getfixturevalue("redis_mongo_db") mi = MongoDBIndex(db.mongo_db) with pytest.raises(ValueError): mi.create(**params) - @mock.patch("hyperon_das_atomdb.adapters.redis_mongo_db.MongoClient", return_value=mock.MagicMock()) + @mock.patch( + "hyperon_das_atomdb.adapters.redis_mongo_db.MongoClient", return_value=mock.MagicMock() + ) @mock.patch("hyperon_das_atomdb.adapters.redis_mongo_db.Redis", return_value=mock.MagicMock()) - @mock.patch("hyperon_das_atomdb.adapters.redis_mongo_db.RedisCluster", return_value=mock.MagicMock()) + @mock.patch( + "hyperon_das_atomdb.adapters.redis_mongo_db.RedisCluster", return_value=mock.MagicMock() + ) def test_create_db_connection_mongo(self, mock_mongo, mock_redis, mock_redis_cluster): RedisMongoDB(mongo_tls_ca_file="/tmp/mock", redis_password="12", redis_username="A") RedisMongoDB(redis_cluster=False) - - diff --git a/tests/unit/fixtures.py b/tests/unit/fixtures.py index 726f7d00..1c488ed9 100644 --- a/tests/unit/fixtures.py +++ b/tests/unit/fixtures.py @@ -1,9 +1,11 @@ -import fakeredis -import pytest -import mongomock from unittest import mock -from hyperon_das_atomdb.adapters.redis_mongo_db import RedisMongoDB, MongoCollectionNames + +import mongomock +import pytest + from hyperon_das_atomdb.adapters.ram_only import InMemoryDB +from hyperon_das_atomdb.adapters.redis_mongo_db import MongoCollectionNames, RedisMongoDB + class MockRedis: def __init__(self, cache=dict()): @@ -16,8 +18,8 @@ def get(self, key): def set(self, key, value, *args, **kwargs): if self.cache: - self.cache[key] = value - return "OK" + self.cache[key] = value + return "OK" return None # return nil in case of some issue def hget(self, hash, key): @@ -28,8 +30,8 @@ def hget(self, hash, key): def hset(self, hash, key, value, *args, **kwargs): if self.cache: - self.cache[hash][key] = value - return 1 + self.cache[hash][key] = value + return 1 return None # return nil in case of some issue def exists(self, key): @@ -69,7 +71,6 @@ def delete(self, *keys): return deleted_count def getdel(self, key): - # Simulate the getdel method of Redis value = self.cache.get(key) if key in self.cache: @@ -77,7 +78,6 @@ def getdel(self, key): return value def srem(self, key, *members): - # Simulate the srem method of Redis if key not in self.cache: return 0 @@ -107,12 +107,12 @@ def sscan(self, name, cursor=0, match=None, count=None): @pytest.fixture def redis_mongo_db(): # import fakeredis - mongo_db = mongomock.MongoClient().db + mongo_db = mongomock.MongoClient().db # redis_db = fakeredis.FakeRedis() redis_db = MockRedis() with mock.patch( - "hyperon_das_atomdb.adapters.redis_mongo_db.RedisMongoDB._connection_mongo_db", - return_value=mongo_db, + "hyperon_das_atomdb.adapters.redis_mongo_db.RedisMongoDB._connection_mongo_db", + return_value=mongo_db, ), mock.patch( "hyperon_das_atomdb.adapters.redis_mongo_db.RedisMongoDB._connection_redis", # return_value=fakeredis.FakeRedis(), @@ -134,6 +134,7 @@ def redis_mongo_db(): yield db + @pytest.fixture def in_memory_db(): db = InMemoryDB() diff --git a/tests/unit/test_database.py b/tests/unit/test_database.py index 15aaff40..d4780d50 100644 --- a/tests/unit/test_database.py +++ b/tests/unit/test_database.py @@ -1,7 +1,10 @@ +from unittest import mock + import pytest -from .fixtures import redis_mongo_db, in_memory_db + from hyperon_das_atomdb.database import AtomDB -from unittest import mock + +from .fixtures import in_memory_db, redis_mongo_db # noqa: F401 def _check_handle(handle): @@ -9,7 +12,6 @@ def _check_handle(handle): class TestDatabase: - @staticmethod def _add_node(db: AtomDB, node_name, node_type, adapter, extra_fields=None): node_dict = {"name": node_name, "type": node_type} @@ -26,10 +28,13 @@ def _add_link(db: AtomDB, link_type, dict_targets, adapter, is_top_level=True): db.commit() return link - @pytest.mark.parametrize("database,expected", [ - ("redis_mongo_db", "1fd600f0fd8a1fab79546a4fc3612df3"), # Concept, Human - ("in_memory_db", "1fd600f0fd8a1fab79546a4fc3612df3") # Concept, Human - ]) + @pytest.mark.parametrize( + "database,expected", + [ + ("redis_mongo_db", "1fd600f0fd8a1fab79546a4fc3612df3"), # Concept, Human + ("in_memory_db", "1fd600f0fd8a1fab79546a4fc3612df3"), # Concept, Human + ], + ) def test_node_handle(self, database, expected, request): db: AtomDB = request.getfixturevalue(database) handle = db.node_handle("Concept", "Human") @@ -39,20 +44,26 @@ def test_node_handle(self, database, expected, request): assert handle == expected assert _check_handle(handle) - @pytest.mark.parametrize("database,expected", [ - ("redis_mongo_db", "1fd600f0fd8a1fab79546a4fc3612df3"), # Concept, Human - ("in_memory_db", "1fd600f0fd8a1fab79546a4fc3612df3") # Concept, Human - ]) + @pytest.mark.parametrize( + "database,expected", + [ + ("redis_mongo_db", "1fd600f0fd8a1fab79546a4fc3612df3"), # Concept, Human + ("in_memory_db", "1fd600f0fd8a1fab79546a4fc3612df3"), # Concept, Human + ], + ) def test_node_handle_exceptions(self, database, expected, request): db: AtomDB = request.getfixturevalue(database) # NOTE Should raise ValueError with pytest.raises(TypeError): db.node_handle([], []) - @pytest.mark.parametrize("database,expected", [ - ("redis_mongo_db", "a9dea78180588431ec64d6bc4872fdbc"), # Similarity - ("in_memory_db", "a9dea78180588431ec64d6bc4872fdbc") # Similarity - ]) + @pytest.mark.parametrize( + "database,expected", + [ + ("redis_mongo_db", "a9dea78180588431ec64d6bc4872fdbc"), # Similarity + ("in_memory_db", "a9dea78180588431ec64d6bc4872fdbc"), # Similarity + ], + ) def test_link_handle(self, database, expected, request): db: AtomDB = request.getfixturevalue(database) handle = db.link_handle("Similarity", []) @@ -60,10 +71,13 @@ def test_link_handle(self, database, expected, request): assert handle assert _check_handle(handle) - @pytest.mark.parametrize("database,expected", [ - ("redis_mongo_db", "a9dea78180588431ec64d6bc4872fdbc"), # Similarity - ("in_memory_db", "a9dea78180588431ec64d6bc4872fdbc") # Similarity - ]) + @pytest.mark.parametrize( + "database,expected", + [ + ("redis_mongo_db", "a9dea78180588431ec64d6bc4872fdbc"), # Similarity + ("in_memory_db", "a9dea78180588431ec64d6bc4872fdbc"), # Similarity + ], + ) def test_link_handle_exceptions(self, database, expected, request): db: AtomDB = request.getfixturevalue(database) with pytest.raises(TypeError): @@ -72,10 +86,13 @@ def test_link_handle_exceptions(self, database, expected, request): # with pytest.raises(ValueError): # db.link_handle("Similarity", set()) - @pytest.mark.parametrize("database,kwlist", [ - ("redis_mongo_db", ["targets_document", "deep_representation"]), - ("in_memory_db", ["targets_document", "deep_representation"]) - ]) + @pytest.mark.parametrize( + "database,kwlist", + [ + ("redis_mongo_db", ["targets_document", "deep_representation"]), + ("in_memory_db", ["targets_document", "deep_representation"]), + ], + ) def test_reformat_document(self, database, kwlist, request): db: AtomDB = request.getfixturevalue(database) node_handle = db.add_node({"name": "A", "type": "Test"}).get("handle") @@ -90,10 +107,13 @@ def test_reformat_document(self, database, kwlist, request): assert answer["name"] == "A" assert isinstance(answer["targets"][0], (str if kw == "targets_document" else dict)) - @pytest.mark.parametrize("database,kwlist", [ - ("redis_mongo_db", ["targets_document", "deep_representation"]), - ("in_memory_db", ["targets_document", "deep_representation"]) - ]) + @pytest.mark.parametrize( + "database,kwlist", + [ + ("redis_mongo_db", ["targets_document", "deep_representation"]), + ("in_memory_db", ["targets_document", "deep_representation"]), + ], + ) def test_reformat_document_exceptions(self, database, kwlist, request): db: AtomDB = request.getfixturevalue(database) link = {"name": "A", "targets": ["test"]} @@ -101,12 +121,21 @@ def test_reformat_document_exceptions(self, database, kwlist, request): with pytest.raises(Exception, match="Nonexistent atom"): db._reformat_document(link, **{kw: True}) - @pytest.mark.parametrize("database,expected_fields, expected_handle", [ - ("redis_mongo_db", ["handle", "_id", "composite_type_hash", "name", "named_type"], - "180fed764dbd593f1ea45b63b13d7e69"), - ("in_memory_db", ["handle", "_id", "composite_type_hash", "name", "named_type"], - "180fed764dbd593f1ea45b63b13d7e69") - ]) + @pytest.mark.parametrize( + "database,expected_fields, expected_handle", + [ + ( + "redis_mongo_db", + ["handle", "_id", "composite_type_hash", "name", "named_type"], + "180fed764dbd593f1ea45b63b13d7e69", + ), + ( + "in_memory_db", + ["handle", "_id", "composite_type_hash", "name", "named_type"], + "180fed764dbd593f1ea45b63b13d7e69", + ), + ], + ) def test_build_node(self, database, expected_fields, expected_handle, request): db: AtomDB = request.getfixturevalue(database) handle, node = db._build_node({"type": "Test", "name": "test"}) @@ -120,25 +149,72 @@ def test_build_node(self, database, expected_fields, expected_handle, request): with pytest.raises(Exception, match="The \"name\" and \"type\" fields must be sent"): db._build_node({}) - @pytest.mark.parametrize("database,expected_fields, expected_handle,is_top_level", [ - ("redis_mongo_db", - ["handle", "_id", "composite_type_hash", "named_type_hash", "named_type", "is_toplevel", "targets"], - "180fed764dbd593f1ea45b63b13d7e69", True), - ("redis_mongo_db", - ["handle", "_id", "composite_type_hash", "named_type_hash", "named_type", "is_toplevel", "targets"], - "180fed764dbd593f1ea45b63b13d7e69", False), - ("in_memory_db", - ["handle", "_id", "composite_type_hash", "named_type_hash", "named_type", "is_toplevel", "targets"], - "180fed764dbd593f1ea45b63b13d7e69", True), - ("in_memory_db", - ["handle", "_id", "composite_type_hash", "named_type_hash", "named_type", "is_toplevel", "targets"], - "180fed764dbd593f1ea45b63b13d7e69", False) - - ]) + @pytest.mark.parametrize( + "database,expected_fields, expected_handle,is_top_level", + [ + ( + "redis_mongo_db", + [ + "handle", + "_id", + "composite_type_hash", + "named_type_hash", + "named_type", + "is_toplevel", + "targets", + ], + "180fed764dbd593f1ea45b63b13d7e69", + True, + ), + ( + "redis_mongo_db", + [ + "handle", + "_id", + "composite_type_hash", + "named_type_hash", + "named_type", + "is_toplevel", + "targets", + ], + "180fed764dbd593f1ea45b63b13d7e69", + False, + ), + ( + "in_memory_db", + [ + "handle", + "_id", + "composite_type_hash", + "named_type_hash", + "named_type", + "is_toplevel", + "targets", + ], + "180fed764dbd593f1ea45b63b13d7e69", + True, + ), + ( + "in_memory_db", + [ + "handle", + "_id", + "composite_type_hash", + "named_type_hash", + "named_type", + "is_toplevel", + "targets", + ], + "180fed764dbd593f1ea45b63b13d7e69", + False, + ), + ], + ) def test_build_link(self, database, expected_fields, expected_handle, is_top_level, request): db: AtomDB = request.getfixturevalue(database) - handle, link, targets = db._build_link({"type": "Test", "targets": [{"type": "Test", "name": "test"}]}, - is_top_level) + handle, link, targets = db._build_link( + {"type": "Test", "targets": [{"type": "Test", "name": "test"}]}, is_top_level + ) assert expected_handle in targets assert all([k in link for k in expected_fields]) assert link["is_toplevel"] == is_top_level @@ -169,13 +245,15 @@ def test_node_exists(self, database, request): assert not no_exists assert exists - @pytest.mark.parametrize("database,targets", [ - ("redis_mongo_db", ["180fed764dbd593f1ea45b63b13d7e69"]), - ("redis_mongo_db", []), - ("in_memory_db", ["180fed764dbd593f1ea45b63b13d7e69"]), - ("in_memory_db", []) - - ]) + @pytest.mark.parametrize( + "database,targets", + [ + ("redis_mongo_db", ["180fed764dbd593f1ea45b63b13d7e69"]), + ("redis_mongo_db", []), + ("in_memory_db", ["180fed764dbd593f1ea45b63b13d7e69"]), + ("in_memory_db", []), + ], + ) def test_link_exists(self, database, targets, request): db: AtomDB = request.getfixturevalue(database) dict_targets = [{"type": "Test", "name": "test"}] if targets else [] @@ -219,7 +297,8 @@ def test_get_node_name_exceptions(self, database, request): if database == "redis_mongo_db": pytest.skip( "ERROR in_memory returns a AtomDoesNotExist exception, redis_mongo returns ValueError. " - "See https://github.com/singnet/das-atom-db/issues/210") + "See https://github.com/singnet/das-atom-db/issues/210" + ) db: AtomDB = request.getfixturevalue(database) # in memory returns a AtomDoesNotExist exception, redis_mongo returns ValueError with pytest.raises(Exception, match="Nonexistent atom"): @@ -256,7 +335,9 @@ def test_get_node_by_name(self, database, request): @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_atoms_by_field(self, database, request): if database == "in_memory_db": - pytest.skip("ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210") + pytest.skip( + "ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210" + ) db: AtomDB = request.getfixturevalue(database) expected_node = self._add_node(db, "Ac", "Test", database) expected_link = self._add_link(db, "Ac", [expected_node], database) @@ -272,7 +353,9 @@ def test_get_atoms_by_field(self, database, request): @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_atoms_by_index(self, database, request): if database == "in_memory_db": - pytest.skip("ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210") + pytest.skip( + "ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210" + ) db: AtomDB = request.getfixturevalue(database) self._add_node(db, "A", "Test", database, {"value": 3}) index_id = db.create_field_index(atom_type="node", fields=["value"], named_type="Test") @@ -286,7 +369,9 @@ def test_get_atoms_by_index(self, database, request): @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_atoms_by_index_exceptions(self, database, request): if database == "in_memory_db": - pytest.skip("ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210") + pytest.skip( + "ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210" + ) db: AtomDB = request.getfixturevalue(database) with pytest.raises(Exception): db.get_atoms_by_index("", []) @@ -294,12 +379,15 @@ def test_get_atoms_by_index_exceptions(self, database, request): @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_atoms_by_text_field_regex(self, database, request): if database == "in_memory_db": - pytest.skip("ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210") + pytest.skip( + "ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210" + ) db: AtomDB = request.getfixturevalue(database) self._add_node(db, "A", "Test", database, {"value": "Test sentence"}) self._add_link(db, "Test", [], database) - index_id = db.create_field_index(atom_type="node", fields=["value"], named_type="Test", - index_type="token_inverted_list") + index_id = db.create_field_index( + atom_type="node", fields=["value"], named_type="Test", index_type="token_inverted_list" + ) atoms = db.get_atoms_by_text_field("Test", "value", text_index_id=index_id) assert isinstance(atoms, list) assert all(_check_handle(a) for a in atoms) @@ -309,14 +397,16 @@ def test_get_atoms_by_text_field_regex(self, database, request): @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_atoms_by_text_field_text(self, database, request): if database == "in_memory_db": - pytest.skip("ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210") + pytest.skip( + "ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210" + ) db: AtomDB = request.getfixturevalue(database) self._add_node(db, "A", "Test", database, {"value": "Test sentence"}) - index_id = db.create_field_index(atom_type="node", fields=["value"], named_type="Test", - index_type="token_inverted_list") + index_id = db.create_field_index( + atom_type="node", fields=["value"], named_type="Test", index_type="token_inverted_list" + ) with mock.patch( - "mongomock.filtering._Filterer.apply", - return_value=["815212e3d7ac246e70c1744d14a8c402"] + "mongomock.filtering._Filterer.apply", return_value=["815212e3d7ac246e70c1744d14a8c402"] ): atoms = db.get_atoms_by_text_field("Test", text_index_id=index_id) assert isinstance(atoms, list) @@ -327,7 +417,9 @@ def test_get_atoms_by_text_field_text(self, database, request): @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_node_by_name_starting_with(self, database, request): if database == "in_memory_db": - pytest.skip("ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210") + pytest.skip( + "ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210" + ) db: AtomDB = request.getfixturevalue(database) node_a = self._add_node(db, "Aaa", "Test", database) node_b = self._add_node(db, "Abb", "Test", database) @@ -351,19 +443,22 @@ def test_get_all_nodes(self, database, request): assert all(isinstance(n, str) for n in nodes) assert len(nodes) == 3 - @pytest.mark.parametrize("database,params,links_len,cursor_value", [ - ("redis_mongo_db", {"link_type": "Ac"}, 3, 0), - ("redis_mongo_db", {"link_type": "Ac", "names": True, "chunk_size": 1}, 3, 0), - ("redis_mongo_db", {"link_type": "Ac", "names": True, "cursor": 0}, 3, 0), - ("redis_mongo_db", {"link_type": "Z", "names": True, "cursor": 1}, 0, 0), - ("redis_mongo_db", {"link_type": "Ac", "chunk_size": 1, "cursor": 0}, 1, 1), - ("in_memory_db", {"link_type": "Ac"}, 3, None), - # NOTE should return the same value for the cursor - ("in_memory_db", {"link_type": "Ac", "names": True, "cursor": 0}, 3, 0), - ("in_memory_db", {"link_type": "Ac", "names": True, "cursor": 0}, 3, 0), - ("in_memory_db", {"link_type": "Z", "names": True, "cursor": 1}, 0, 1), - ("in_memory_db", {"link_type": "Ac", "chunk_size": 1, "cursor": 0}, 3, 0), - ]) + @pytest.mark.parametrize( + "database,params,links_len,cursor_value", + [ + ("redis_mongo_db", {"link_type": "Ac"}, 3, 0), + ("redis_mongo_db", {"link_type": "Ac", "names": True, "chunk_size": 1}, 3, 0), + ("redis_mongo_db", {"link_type": "Ac", "names": True, "cursor": 0}, 3, 0), + ("redis_mongo_db", {"link_type": "Z", "names": True, "cursor": 1}, 0, 0), + ("redis_mongo_db", {"link_type": "Ac", "chunk_size": 1, "cursor": 0}, 1, 1), + ("in_memory_db", {"link_type": "Ac"}, 3, None), + # NOTE should return the same value for the cursor + ("in_memory_db", {"link_type": "Ac", "names": True, "cursor": 0}, 3, 0), + ("in_memory_db", {"link_type": "Ac", "names": True, "cursor": 0}, 3, 0), + ("in_memory_db", {"link_type": "Z", "names": True, "cursor": 1}, 0, 1), + ("in_memory_db", {"link_type": "Ac", "chunk_size": 1, "cursor": 0}, 3, 0), + ], + ) def test_get_all_links(self, database, params, links_len, cursor_value, request): db: AtomDB = request.getfixturevalue(database) self._add_link(db, "Ac", [{"name": "A", "type": "A"}], database) @@ -372,7 +467,7 @@ def test_get_all_links(self, database, params, links_len, cursor_value, request) cursor, links = db.get_all_links(**params) assert cursor == cursor_value assert isinstance(links, list) - assert all(_check_handle(l) for l in links) + assert all(_check_handle(link) for link in links) assert all(isinstance(link, str) for link in links) assert len(links) == links_len @@ -428,22 +523,26 @@ def test_is_ordered_no_handle(self, database, request): if database == "redis_mongo_db": pytest.skip( "ERROR redis_mongo_db is raising ValueError exception, should be AtomDoesNotExist. " - "See https://github.com/singnet/das-atom-db/issues/210") + "See https://github.com/singnet/das-atom-db/issues/210" + ) db: AtomDB = request.getfixturevalue(database) with pytest.raises(Exception, match="Nonexistent atom"): db.is_ordered("handle") - @pytest.mark.parametrize("database,params,links_len,cursor_value", [ - ("redis_mongo_db", {}, 3, None), - ("redis_mongo_db", {"handles_only": True}, 3, None), - ("redis_mongo_db", {"no_target_format": True}, 3, None), - ("redis_mongo_db", {"cursor": 0, "chunk_size": 1}, 1, 1), - # NOTE should return None on all cases - ("in_memory_db", {}, 3, None), - ("in_memory_db", {"handles_only": True}, 3, None), - ("in_memory_db", {"no_target_format": True}, 3, None), - ("in_memory_db", {"cursor": 0, "chunk_size": 1}, 3, 0), - ]) + @pytest.mark.parametrize( + "database,params,links_len,cursor_value", + [ + ("redis_mongo_db", {}, 3, None), + ("redis_mongo_db", {"handles_only": True}, 3, None), + ("redis_mongo_db", {"no_target_format": True}, 3, None), + ("redis_mongo_db", {"cursor": 0, "chunk_size": 1}, 1, 1), + # NOTE should return None on all cases + ("in_memory_db", {}, 3, None), + ("in_memory_db", {"handles_only": True}, 3, None), + ("in_memory_db", {"no_target_format": True}, 3, None), + ("in_memory_db", {"cursor": 0, "chunk_size": 1}, 3, 0), + ], + ) def test_get_incoming_links(self, database, params, links_len, cursor_value, request): db: AtomDB = request.getfixturevalue(database) node_a = self._add_node(db, "Aaa", "Test", database) @@ -453,30 +552,38 @@ def test_get_incoming_links(self, database, params, links_len, cursor_value, req cursor, links = db.get_incoming_links(node_a["handle"], **params) assert cursor == cursor_value assert len(links) == links_len - assert all([_check_handle(l if params.get("handles_only") else l["handle"]) for l in links]) - - @pytest.mark.parametrize("database,params,links_len,cursor_value", [ - ("redis_mongo_db", {}, 1, None), - ("redis_mongo_db", {"toplevel_only": True}, 1, None), - # ("redis_mongo_db", {"link_type": "NoTopLevel" , "toplevel_only": True}, 0, None), # doesn"t work - # Note returning different values - # ("in_memory_db", {"link_type": "*", "toplevel_only": True}, 3, None), - # ("redis_mongo_db", {"link_type": "*", "toplevel_only": True}, 0, None), - # ("redis_mongo_db", {"link_type": "*"}, 3, None), # should return 3 - ("redis_mongo_db", {"target_handles": "*"}, 1, None), - ("redis_mongo_db", {"handles_only": True}, 1, None), - ("redis_mongo_db", {"no_target_format": True}, 1, None), - ("redis_mongo_db", {"cursor": 0, "chunk_size": 1}, 1, None), - ("in_memory_db", {}, 1, None), - ("in_memory_db", {"toplevel_only": True}, 1, None), - # ("in_memory_db", {"link_type": "NoTopLevel", "toplevel_only": True}, 0, None), # doesn"t work - ("in_memory_db", {"link_type": "*"}, 3, None), - ("in_memory_db", {"toplevel_only": True}, 1, None), - ("in_memory_db", {"target_handles": "*"}, 1, None), - ("in_memory_db", {"handles_only": True}, 1, None), - ("in_memory_db", {"no_target_format": True}, 1, None), - ("in_memory_db", {"cursor": 0, "chunk_size": 1}, 1, 0), - ]) + assert all( + [ + _check_handle(link if params.get("handles_only") else link["handle"]) + for link in links + ] + ) + + @pytest.mark.parametrize( + "database,params,links_len,cursor_value", + [ + ("redis_mongo_db", {}, 1, None), + ("redis_mongo_db", {"toplevel_only": True}, 1, None), + # ("redis_mongo_db", {"link_type": "NoTopLevel" , "toplevel_only": True}, 0, None), # doesn"t work + # Note returning different values + # ("in_memory_db", {"link_type": "*", "toplevel_only": True}, 3, None), + # ("redis_mongo_db", {"link_type": "*", "toplevel_only": True}, 0, None), + # ("redis_mongo_db", {"link_type": "*"}, 3, None), # should return 3 + ("redis_mongo_db", {"target_handles": "*"}, 1, None), + ("redis_mongo_db", {"handles_only": True}, 1, None), + ("redis_mongo_db", {"no_target_format": True}, 1, None), + ("redis_mongo_db", {"cursor": 0, "chunk_size": 1}, 1, None), + ("in_memory_db", {}, 1, None), + ("in_memory_db", {"toplevel_only": True}, 1, None), + # ("in_memory_db", {"link_type": "NoTopLevel", "toplevel_only": True}, 0, None), # doesn"t work + ("in_memory_db", {"link_type": "*"}, 3, None), + ("in_memory_db", {"toplevel_only": True}, 1, None), + ("in_memory_db", {"target_handles": "*"}, 1, None), + ("in_memory_db", {"handles_only": True}, 1, None), + ("in_memory_db", {"no_target_format": True}, 1, None), + ("in_memory_db", {"cursor": 0, "chunk_size": 1}, 1, 0), + ], + ) def test_get_matched_links(self, database, params, links_len, cursor_value, request): db: AtomDB = request.getfixturevalue(database) node_a = self._add_node(db, "Aaa", "Test", database) @@ -484,24 +591,29 @@ def test_get_matched_links(self, database, params, links_len, cursor_value, requ _ = self._add_link(db, "NoTopLevel", [node_a], database, is_top_level=False) _ = self._add_link(db, "Ac", [node_a], database) params["link_type"] = link_a["type"] if not params.get("link_type") else params["link_type"] - params["target_handles"] = link_a["targets"] if not params.get("target_handles") else params["target_handles"] + params["target_handles"] = ( + link_a["targets"] if not params.get("target_handles") else params["target_handles"] + ) cursor, links = db.get_matched_links(**params) assert cursor == cursor_value assert len(links) == links_len - if all(isinstance(l, tuple) for l in links): + if all(isinstance(link, tuple) for link in links): for link in links: while link: assert _check_handle(link[0]) link = link[1] if len(link) > 1 else None else: - assert all([_check_handle(l) for l in links]) - - @pytest.mark.parametrize("database,params,links_len,cursor_value", [ - ("redis_mongo_db", {"link_type": "Z", "target_handles": []}, 0, None), - # ("redis_mongo_db", {"link_type": "*", "target_handles": ["*", "*"], "toplevel_only": True}, 0, None), - # ("in_memory_db", {"link_type": "*", "target_handles": ["*", "*"]}, 0, None), - # ("in_memory_db", {"link_type": "*", "target_handles": ["*", "*"], "toplevel_only": True}, 0, None), - ]) + assert all([_check_handle(link) for link in links]) + + @pytest.mark.parametrize( + "database,params,links_len,cursor_value", + [ + ("redis_mongo_db", {"link_type": "Z", "target_handles": []}, 0, None), + # ("redis_mongo_db", {"link_type": "*", "target_handles": ["*", "*"], "toplevel_only": True}, 0, None), + # ("in_memory_db", {"link_type": "*", "target_handles": ["*", "*"]}, 0, None), + # ("in_memory_db", {"link_type": "*", "target_handles": ["*", "*"], "toplevel_only": True}, 0, None), + ], + ) def test_get_matched_no_links(self, database, params, links_len, cursor_value, request): db: AtomDB = request.getfixturevalue(database) self._add_node(db, "Aaa", "Test", database) @@ -509,18 +621,23 @@ def test_get_matched_no_links(self, database, params, links_len, cursor_value, r assert cursor == cursor_value assert len(links) == links_len - @pytest.mark.parametrize("database,params,links_len,cursor_value,is_top_level", [ - ("redis_mongo_db", {}, 1, None, True), - ("redis_mongo_db", {}, 1, None, False), - ("redis_mongo_db", {"toplevel_only": True}, 0, None, False), - ("redis_mongo_db", {"cursor": 0, "chunk_size": 1}, 1, 0, False), - ("in_memory_db", {}, 1, None, True), - ("in_memory_db", {}, 1, None, False), - ("in_memory_db", {"toplevel_only": True}, 0, None, False), - # NOTE should return None or same as redis_mongo - ("in_memory_db", {"cursor": 0, "chunk_size": 1}, 1, 0, False), - ]) - def test_get_matched_type_template(self, database, params, links_len, cursor_value, is_top_level, request): + @pytest.mark.parametrize( + "database,params,links_len,cursor_value,is_top_level", + [ + ("redis_mongo_db", {}, 1, None, True), + ("redis_mongo_db", {}, 1, None, False), + ("redis_mongo_db", {"toplevel_only": True}, 0, None, False), + ("redis_mongo_db", {"cursor": 0, "chunk_size": 1}, 1, 0, False), + ("in_memory_db", {}, 1, None, True), + ("in_memory_db", {}, 1, None, False), + ("in_memory_db", {"toplevel_only": True}, 0, None, False), + # NOTE should return None or same as redis_mongo + ("in_memory_db", {"cursor": 0, "chunk_size": 1}, 1, 0, False), + ], + ) + def test_get_matched_type_template( + self, database, params, links_len, cursor_value, is_top_level, request + ): db: AtomDB = request.getfixturevalue(database) node_a = self._add_node(db, "Aaa", "Test", database) node_b = self._add_node(db, "Bbb", "Test", database) @@ -540,7 +657,8 @@ def test_get_matched_type(self, database, request): if database == "redis_mongo_db": pytest.skip( "ERROR redis_mongo_db is returning more values. " - "See https://github.com/singnet/das-atom-db/issues/210") + "See https://github.com/singnet/das-atom-db/issues/210" + ) db: AtomDB = request.getfixturevalue(database) link_a = self._add_link(db, "Aa", [], database) self._add_link(db, "Ab", [], database) @@ -554,17 +672,19 @@ def test_get_matched_type(self, database, request): assert all(t in link[1] for t in link_a["targets"]) assert all(_check_handle(t) for t in link[1]) - @pytest.mark.parametrize("database,params,top_level,n_links,n_nodes", [ - ("redis_mongo_db", {}, True, 1, 1), - ("redis_mongo_db", {"no_target_format": True}, False, 1, 1), - # ("redis_mongo_db", {"targets_document": True}, False, 1, 1),# breaks when is a node - ("redis_mongo_db", {"deep_representation": True}, False, 1, 1), - ("in_memory_db", {}, True, 1, 1), - ("in_memory_db", {"no_target_format": True}, False, 0, 1), - # ("in_memory_db", {"targets_document": True}, False, 0, 1),# breaks when is a node - ("in_memory_db", {"deep_representation": True}, False, 0, 1), - - ]) + @pytest.mark.parametrize( + "database,params,top_level,n_links,n_nodes", + [ + ("redis_mongo_db", {}, True, 1, 1), + ("redis_mongo_db", {"no_target_format": True}, False, 1, 1), + # ("redis_mongo_db", {"targets_document": True}, False, 1, 1),# breaks when is a node + ("redis_mongo_db", {"deep_representation": True}, False, 1, 1), + ("in_memory_db", {}, True, 1, 1), + ("in_memory_db", {"no_target_format": True}, False, 0, 1), + # ("in_memory_db", {"targets_document": True}, False, 0, 1),# breaks when is a node + ("in_memory_db", {"deep_representation": True}, False, 0, 1), + ], + ) def test_get_atom_node(self, database, params, top_level, n_links, n_nodes, request): db: AtomDB = request.getfixturevalue(database) node_a = self._add_node(db, "Aaa", "Test", database) @@ -573,17 +693,19 @@ def test_get_atom_node(self, database, params, top_level, n_links, n_nodes, requ assert atom_n["handle"] == node_a["handle"] assert _check_handle(atom_n["handle"]) - @pytest.mark.parametrize("database,params,top_level,n_links,n_nodes", [ - ("redis_mongo_db", {}, True, 1, 1), - ("redis_mongo_db", {"no_target_format": True}, False, 1, 1), - ("redis_mongo_db", {"targets_document": True}, False, 1, 1), - ("redis_mongo_db", {"deep_representation": True}, False, 1, 1), - ("in_memory_db", {}, True, 1, 1), - ("in_memory_db", {"no_target_format": True}, False, 0, 1), - ("in_memory_db", {"targets_document": True}, False, 0, 1), - ("in_memory_db", {"deep_representation": True}, False, 0, 1), - - ]) + @pytest.mark.parametrize( + "database,params,top_level,n_links,n_nodes", + [ + ("redis_mongo_db", {}, True, 1, 1), + ("redis_mongo_db", {"no_target_format": True}, False, 1, 1), + ("redis_mongo_db", {"targets_document": True}, False, 1, 1), + ("redis_mongo_db", {"deep_representation": True}, False, 1, 1), + ("in_memory_db", {}, True, 1, 1), + ("in_memory_db", {"no_target_format": True}, False, 0, 1), + ("in_memory_db", {"targets_document": True}, False, 0, 1), + ("in_memory_db", {"deep_representation": True}, False, 0, 1), + ], + ) def test_get_atom_link(self, database, params, top_level, n_links, n_nodes, request): db: AtomDB = request.getfixturevalue(database) link_a = self._add_link(db, "Aa", [], database, is_top_level=top_level) @@ -643,7 +765,8 @@ def test_get_atom_as_dict_none(self, database, request): if database == "in_memory_db": pytest.skip( "ERROR in_memory raises exception, they should return the same result/exception. " - "See https://github.com/singnet/das-atom-db/issues/210") + "See https://github.com/singnet/das-atom-db/issues/210" + ) db: AtomDB = request.getfixturevalue(database) atom_node = db.get_atom_as_dict("handle") atom_link = db.get_atom_as_dict("handle") @@ -655,22 +778,26 @@ def test_get_atom_as_dict_exceptions(self, database, request): if database == "redis_mongo_db": pytest.skip( "ERROR redis_mongo_db doesn't raises exception, they should return the same result/exception. " - "See https://github.com/singnet/das-atom-db/issues/210") + "See https://github.com/singnet/das-atom-db/issues/210" + ) db: AtomDB = request.getfixturevalue(database) with pytest.raises(Exception, match="Nonexistent atom"): db.get_atom_as_dict("handle") with pytest.raises(Exception, match="Nonexistent atom"): db.get_atom_as_dict("handle") - @pytest.mark.parametrize("database,params", [ - ("redis_mongo_db", {}), - ("redis_mongo_db", {"precise": True}), - ("redis_mongo_db", {"precise": False}), - # NOTE should return the same value if the arg precise is set - ("in_memory_db", {}), - ("in_memory_db", {"precise": True}), - ("in_memory_db", {"precise": False}), - ]) + @pytest.mark.parametrize( + "database,params", + [ + ("redis_mongo_db", {}), + ("redis_mongo_db", {"precise": True}), + ("redis_mongo_db", {"precise": False}), + # NOTE should return the same value if the arg precise is set + ("in_memory_db", {}), + ("in_memory_db", {"precise": True}), + ("in_memory_db", {"precise": False}), + ], + ) def test_count_atoms(self, database, params, request): db: AtomDB = request.getfixturevalue(database) self._add_node(db, "Aaa", "Test", database) @@ -695,10 +822,13 @@ def test_clear_database(self, database, request): db.clear_database() assert db.count_atoms()["atom_count"] == 0 - @pytest.mark.parametrize("database,node", [ - ("redis_mongo_db", {"name": "A", "type": "A"}), - ("in_memory_db", {"name": "A", "type": "A"}), - ]) + @pytest.mark.parametrize( + "database,node", + [ + ("redis_mongo_db", {"name": "A", "type": "A"}), + ("in_memory_db", {"name": "A", "type": "A"}), + ], + ) def test_add_node(self, database, node, request): db: AtomDB = request.getfixturevalue(database) if database == "redis_mongo_db": @@ -709,9 +839,12 @@ def test_add_node(self, database, node, request): assert count["atom_count"] == 1 assert isinstance(node, dict) - @pytest.mark.parametrize("database,node", [ - ("redis_mongo_db", {"name": "AAAA", "type": "A"}), - ]) + @pytest.mark.parametrize( + "database,node", + [ + ("redis_mongo_db", {"name": "AAAA", "type": "A"}), + ], + ) def test_add_node_discard(self, database, node, request): db: AtomDB = request.getfixturevalue(database) db.mongo_bulk_insertion_limit = 1 @@ -721,31 +854,37 @@ def test_add_node_discard(self, database, node, request): assert node is None assert count["atom_count"] == 0 - @pytest.mark.parametrize("database,node", [ - ("redis_mongo_db", {}), - # NOTE it"s not breaking, should break? - # ("redis_mongo_db", {"name": "A", "type": "A", "handle": ""}), - # ("redis_mongo_db", {"name": "A", "type": "A", "_id": ""}), - # ("redis_mongo_db", {"name": "A", "type": "A", "composite_type_hash": ""}), - # ("redis_mongo_db", {"name": "A", "type": "A", "named_type": ""}), - ("in_memory_db", {}), - # NOTE it"s not breaking, should break? - # ("in_memory_db", {"name": "A", "type": "A", "handle": ""}), - # ("in_memory_db", {"name": "A", "type": "A", "_id": ""}), - # ("in_memory_db", {"name": "A", "type": "A", "composite_type_hash": ""}), - # ("in_memory_db", {"name": "A", "type": "A", "named_type": ""}), - ]) + @pytest.mark.parametrize( + "database,node", + [ + ("redis_mongo_db", {}), + # NOTE it"s not breaking, should break? + # ("redis_mongo_db", {"name": "A", "type": "A", "handle": ""}), + # ("redis_mongo_db", {"name": "A", "type": "A", "_id": ""}), + # ("redis_mongo_db", {"name": "A", "type": "A", "composite_type_hash": ""}), + # ("redis_mongo_db", {"name": "A", "type": "A", "named_type": ""}), + ("in_memory_db", {}), + # NOTE it"s not breaking, should break? + # ("in_memory_db", {"name": "A", "type": "A", "handle": ""}), + # ("in_memory_db", {"name": "A", "type": "A", "_id": ""}), + # ("in_memory_db", {"name": "A", "type": "A", "composite_type_hash": ""}), + # ("in_memory_db", {"name": "A", "type": "A", "named_type": ""}), + ], + ) def test_add_node_exceptions(self, database, node, request): db: AtomDB = request.getfixturevalue(database) with pytest.raises(Exception): db.add_node(node) - @pytest.mark.parametrize("database,params,expected_count,top_level", [ - ("redis_mongo_db", {"type": "A", "targets": [{"name": "A", "type": "A"}]}, 2, True), - ("redis_mongo_db", {"type": "A", "targets": []}, 1, True), - ("in_memory_db", {"type": "A", "targets": [{"name": "A", "type": "A"}]}, 2, True), - ("in_memory_db", {"type": "A", "targets": []}, 1, True), - ]) + @pytest.mark.parametrize( + "database,params,expected_count,top_level", + [ + ("redis_mongo_db", {"type": "A", "targets": [{"name": "A", "type": "A"}]}, 2, True), + ("redis_mongo_db", {"type": "A", "targets": []}, 1, True), + ("in_memory_db", {"type": "A", "targets": [{"name": "A", "type": "A"}]}, 2, True), + ("in_memory_db", {"type": "A", "targets": []}, 1, True), + ], + ) def test_add_link(self, database, params, expected_count, top_level, request): db: AtomDB = request.getfixturevalue(database) if database == "redis_mongo_db": @@ -759,16 +898,20 @@ def test_add_link(self, database, params, expected_count, top_level, request): @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_reindex(self, database, request): if database == "in_memory_db": - pytest.skip("ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210") + pytest.skip( + "ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210" + ) db: AtomDB = request.getfixturevalue(database) - node_a = self._add_node(db, "Aaa", "Test", database) - link_a = self._add_link(db, "Test", [], database) + self._add_node(db, "Aaa", "Test", database) + self._add_link(db, "Test", [], database) db.reindex() @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_delete_atom(self, database, request): if database == "in_memory_db": - pytest.skip("ERROR Atom not in incoming_set. See https://github.com/singnet/das-atom-db/issues/210") + pytest.skip( + "ERROR Atom not in incoming_set. See https://github.com/singnet/das-atom-db/issues/210" + ) db: AtomDB = request.getfixturevalue(database) node_a = self._add_node(db, "Aaa", "Test", database) link_a = self._add_link(db, "Test", [], database) @@ -790,32 +933,93 @@ def test_delete_atom(self, database, request): @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_delete_atom_exceptions(self, database, request): if database == "in_memory_db": - pytest.skip("ERROR Atom not in incoming_set. See https://github.com/singnet/das-atom-db/issues/210") + pytest.skip( + "ERROR Atom not in incoming_set. See https://github.com/singnet/das-atom-db/issues/210" + ) db: AtomDB = request.getfixturevalue(database) with pytest.raises(Exception): db.delete_atom("handle") - @pytest.mark.parametrize("database,params", [ - ("redis_mongo_db", {"atom_type": "A", "fields": ["value"]}), - ("redis_mongo_db", {"atom_type": "A", "fields": ["value"], "named_type": "A"}), - ("redis_mongo_db", {"atom_type": "A", "fields": ["value"], "composite_type": ["value", "side"]}), - ("redis_mongo_db", {"atom_type": "A", "fields": ["value"], "named_type": "A", "index_type": "binary_tree"}), - ("redis_mongo_db", {"atom_type": "A", "fields": ["value", "side"], "composite_type": ["value", "side"], - "index_type": "binary_tree"}), - ("redis_mongo_db", {"atom_type": "A", "fields": ["value", "side"], "composite_type": ["value", "side"], - "index_type": "token_inverted_list"}), - ("redis_mongo_db", - {"atom_type": "A", "fields": ["value"], "named_type": "A", "index_type": "token_inverted_list"}), - ("redis_mongo_db", {"atom_type": "A", "fields": ["value"], "named_type": "A", "index_type": "binary_tree"}), - ("redis_mongo_db", {"atom_type": "A", "fields": ["value"], "composite_type": ["value", "side"], - "index_type": "token_inverted_list"}), - ("redis_mongo_db", - {"atom_type": "A", "fields": ["value"], "composite_type": ["value", "side"], "index_type": "binary_tree"}), - ("in_memory_db", {}) - ]) + @pytest.mark.parametrize( + "database,params", + [ + ("redis_mongo_db", {"atom_type": "A", "fields": ["value"]}), + ("redis_mongo_db", {"atom_type": "A", "fields": ["value"], "named_type": "A"}), + ( + "redis_mongo_db", + {"atom_type": "A", "fields": ["value"], "composite_type": ["value", "side"]}, + ), + ( + "redis_mongo_db", + { + "atom_type": "A", + "fields": ["value"], + "named_type": "A", + "index_type": "binary_tree", + }, + ), + ( + "redis_mongo_db", + { + "atom_type": "A", + "fields": ["value", "side"], + "composite_type": ["value", "side"], + "index_type": "binary_tree", + }, + ), + ( + "redis_mongo_db", + { + "atom_type": "A", + "fields": ["value", "side"], + "composite_type": ["value", "side"], + "index_type": "token_inverted_list", + }, + ), + ( + "redis_mongo_db", + { + "atom_type": "A", + "fields": ["value"], + "named_type": "A", + "index_type": "token_inverted_list", + }, + ), + ( + "redis_mongo_db", + { + "atom_type": "A", + "fields": ["value"], + "named_type": "A", + "index_type": "binary_tree", + }, + ), + ( + "redis_mongo_db", + { + "atom_type": "A", + "fields": ["value"], + "composite_type": ["value", "side"], + "index_type": "token_inverted_list", + }, + ), + ( + "redis_mongo_db", + { + "atom_type": "A", + "fields": ["value"], + "composite_type": ["value", "side"], + "index_type": "binary_tree", + }, + ), + ("in_memory_db", {}), + ], + ) def test_create_field_index(self, database, params, request): if database == "in_memory_db": - pytest.skip("ERROR Not implemented on in_memory_db. See https://github.com/singnet/das-atom-db/issues/210") + pytest.skip( + "ERROR Not implemented on in_memory_db. See https://github.com/singnet/das-atom-db/issues/210" + ) db: AtomDB = request.getfixturevalue(database) index_id = db.create_field_index(**params) assert index_id @@ -825,15 +1029,27 @@ def test_create_field_index(self, database, params, request): assert isinstance(index_id2, str) assert index_id2 == index_id - @pytest.mark.parametrize("database,params", [ - ("redis_mongo_db", {"atom_type": "A", "fields": []}), - ("redis_mongo_db", - {"atom_type": "A", "fields": ["value"], "named_type": "A", "composite_type": ["value", "side"]}), - ("in_memory_db", {}) - ]) + @pytest.mark.parametrize( + "database,params", + [ + ("redis_mongo_db", {"atom_type": "A", "fields": []}), + ( + "redis_mongo_db", + { + "atom_type": "A", + "fields": ["value"], + "named_type": "A", + "composite_type": ["value", "side"], + }, + ), + ("in_memory_db", {}), + ], + ) def test_create_field_index_value_error(self, database, params, request): if database == "in_memory_db": - pytest.skip("ERROR Not implemented on in_memory_db. See https://github.com/singnet/das-atom-db/issues/210") + pytest.skip( + "ERROR Not implemented on in_memory_db. See https://github.com/singnet/das-atom-db/issues/210" + ) db: AtomDB = request.getfixturevalue(database) with pytest.raises(ValueError): db.create_field_index(**params) @@ -855,7 +1071,8 @@ def test_create_field_index_value_error(self, database, params, request): def test_bulk_insert(self, database, request): if database == "redis_mongo_db": pytest.skip( - "ERROR redis_mongo_db is not updating targets. See https://github.com/singnet/das-atom-db/issues/210") + "ERROR redis_mongo_db is not updating targets. See https://github.com/singnet/das-atom-db/issues/210" + ) db: AtomDB = request.getfixturevalue(database) node_a = self._add_node(db, "Aaa", "Test", database) link_a = self._add_link(db, "Test", [{"name": "A", "type": "A"}], database) @@ -863,13 +1080,13 @@ def test_bulk_insert(self, database, request): link_a["targets"] = [node_a["handle"]] db.bulk_insert([node_a, link_a]) count = db.count_atoms({"precise": True}) - n = db.get_atom(node_a["handle"]) - l = db.get_atom(link_a["handle"]) + node = db.get_atom(node_a["handle"]) + link = db.get_atom(link_a["handle"]) assert count["atom_count"] == 3 assert count["node_count"] == 2 assert count["link_count"] == 1 - assert n["name"] == "B" - assert l["targets"] == [node_a["handle"]] + assert node["name"] == "B" + assert link["targets"] == [node_a["handle"]] # Note no exception is raised if error @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) @@ -879,7 +1096,9 @@ def test_bulk_insert_exceptions(self, database, request): link_a = db._build_link({"targets": [], "type": "A"}) with pytest.raises(Exception): db.bulk_insert([node_a, link_a]) - pytest.skip("ERROR should raise an exception. See https://github.com/singnet/das-atom-db/issues/210") + pytest.skip( + "ERROR should raise an exception. See https://github.com/singnet/das-atom-db/issues/210" + ) @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_retrieve_all_atoms(self, database, request): @@ -895,7 +1114,9 @@ def test_retrieve_all_atoms(self, database, request): @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_commit(self, database, request): if database == "in_memory_db": - pytest.skip("ERROR Not implemented on in_memory_db. See https://github.com/singnet/das-atom-db/issues/210") + pytest.skip( + "ERROR Not implemented on in_memory_db. See https://github.com/singnet/das-atom-db/issues/210" + ) db: AtomDB = request.getfixturevalue(database) db.add_node({"name": "A", "type": "Test"}) db.add_link({"type": "Test", "targets": []}) @@ -909,7 +1130,8 @@ def test_commit(self, database, request): def test_commit_buffer(self, database, request): if database == "in_memory_db": pytest.skip( - "ERROR Not implemented on in_memory_db. See https://github.com/singnet/das-atom-db/issues/210") + "ERROR Not implemented on in_memory_db. See https://github.com/singnet/das-atom-db/issues/210" + ) db: AtomDB = request.getfixturevalue(database) node_a = self._add_node(db, "Aaa", "Test", database) link_a = self._add_link(db, "Test", [{"name": "A", "type": "A"}], database) @@ -923,7 +1145,6 @@ def test_commit_buffer(self, database, request): @pytest.mark.parametrize("database", ["redis_mongo_db"]) def test_commit_buffer_exception(self, database, request): - db: AtomDB = request.getfixturevalue(database) with pytest.raises(Exception): db.commit(buffer=[{"name": "A", "type": "A"}]) @@ -931,8 +1152,3 @@ def test_commit_buffer_exception(self, database, request): with pytest.raises(Exception, match="Failed to commit Atom Types"): db.mongo_bulk_insertion_buffer = {"atom_types": ("a", "a")} db.commit() - - - - - From 9f279308f127acc0a9c5833c4186a9d1227d2912 Mon Sep 17 00:00:00 2001 From: Edgar Brissow Date: Thu, 5 Sep 2024 18:47:27 -0300 Subject: [PATCH 5/6] Remove fakeredis --- pyproject.toml | 1 - tests/unit/fixtures.py | 21 ++++----------------- tests/unit/test_database.py | 3 --- 3 files changed, 4 insertions(+), 21 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index a2516edc..de36c43f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,7 +18,6 @@ pymongo = "^4.5.0" python-dotenv = "^1.0.0" mongomock = "^4.1.2" setuptools = "^70.2.0" -fakeredis = "^2.24.1" [tool.poetry.group.dev.dependencies] flake8 = "^6.1.0" diff --git a/tests/unit/fixtures.py b/tests/unit/fixtures.py index 1c488ed9..6de72dbe 100644 --- a/tests/unit/fixtures.py +++ b/tests/unit/fixtures.py @@ -14,25 +14,25 @@ def __init__(self, cache=dict()): def get(self, key): if key in self.cache: return self.cache[key] - return None # return nil + return None def set(self, key, value, *args, **kwargs): if self.cache: self.cache[key] = value return "OK" - return None # return nil in case of some issue + return None def hget(self, hash, key): if hash in self.cache: if key in self.cache[hash]: return self.cache[hash][key] - return None # return nil + return None def hset(self, hash, key, value, *args, **kwargs): if self.cache: self.cache[hash][key] = value return 1 - return None # return nil in case of some issue + return None def exists(self, key): if key in self.cache: @@ -43,7 +43,6 @@ def cache_overwrite(self, cache=dict()): self.cache = cache def sadd(self, key, *members): - # Simulate the sadd method of Redis if key not in self.cache: self.cache[key] = set() before_count = len(self.cache[key]) @@ -52,17 +51,14 @@ def sadd(self, key, *members): return after_count - before_count def smembers(self, key): - # Simulate the smembers method of Redis if key in self.cache: return self.cache[key] return set() def flushall(self): - # Simulate the flushall method of Redis self.cache.clear() def delete(self, *keys): - # Simulate the delete method of Redis deleted_count = 0 for key in keys: if key in self.cache: @@ -71,14 +67,12 @@ def delete(self, *keys): return deleted_count def getdel(self, key): - # Simulate the getdel method of Redis value = self.cache.get(key) if key in self.cache: del self.cache[key] return value def srem(self, key, *members): - # Simulate the srem method of Redis if key not in self.cache: return 0 initial_count = len(self.cache[key]) @@ -87,7 +81,6 @@ def srem(self, key, *members): return removed_count def sscan(self, name, cursor=0, match=None, count=None): - # Simulate the sscan method of Redis key = name if key not in self.cache: return (0, []) @@ -95,8 +88,6 @@ def sscan(self, name, cursor=0, match=None, count=None): elements = list(self.cache[key]) if match: elements = [e for e in elements if match in e] - - # Cursor-based iteration simulation start = cursor end = min(start + (count if count else len(elements)), len(elements)) new_cursor = end if end < len(elements) else 0 @@ -106,23 +97,19 @@ def sscan(self, name, cursor=0, match=None, count=None): @pytest.fixture def redis_mongo_db(): - # import fakeredis mongo_db = mongomock.MongoClient().db - # redis_db = fakeredis.FakeRedis() redis_db = MockRedis() with mock.patch( "hyperon_das_atomdb.adapters.redis_mongo_db.RedisMongoDB._connection_mongo_db", return_value=mongo_db, ), mock.patch( "hyperon_das_atomdb.adapters.redis_mongo_db.RedisMongoDB._connection_redis", - # return_value=fakeredis.FakeRedis(), return_value=redis_db, ): db = RedisMongoDB() db.mongo_atoms_collection = mongo_db.collection db.mongo_types_collection = mongo_db.collection - # db.mongo_atoms_collection.insert_many(loader("atom_collection_data.json")) db.all_mongo_collections = [ (MongoCollectionNames.ATOMS, db.mongo_atoms_collection), (MongoCollectionNames.ATOM_TYPES, db.mongo_types_collection), diff --git a/tests/unit/test_database.py b/tests/unit/test_database.py index d4780d50..861a4fea 100644 --- a/tests/unit/test_database.py +++ b/tests/unit/test_database.py @@ -1,9 +1,6 @@ from unittest import mock - import pytest - from hyperon_das_atomdb.database import AtomDB - from .fixtures import in_memory_db, redis_mongo_db # noqa: F401 From 403f11160c83a1619af00977e0ccfef44f5d97f5 Mon Sep 17 00:00:00 2001 From: Angelo Probst <33906777+angeloprobst@users.noreply.github.com> Date: Tue, 10 Sep 2024 14:42:07 -0300 Subject: [PATCH 6/6] added TODOs to highlight cases that must be revisited --- tests/unit/test_database.py | 41 +++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/tests/unit/test_database.py b/tests/unit/test_database.py index 861a4fea..1fa66c16 100644 --- a/tests/unit/test_database.py +++ b/tests/unit/test_database.py @@ -80,6 +80,7 @@ def test_link_handle_exceptions(self, database, expected, request): with pytest.raises(TypeError): db.link_handle("Similarity", None) # NOTE Unreachable + # TODO: unreachable code must be deleted or fixed to become reachable # with pytest.raises(ValueError): # db.link_handle("Similarity", set()) @@ -292,12 +293,14 @@ def test_get_node_name(self, database, request): @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_node_name_exceptions(self, database, request): if database == "redis_mongo_db": + # TODO: fix this pytest.skip( "ERROR in_memory returns a AtomDoesNotExist exception, redis_mongo returns ValueError. " "See https://github.com/singnet/das-atom-db/issues/210" ) db: AtomDB = request.getfixturevalue(database) # in memory returns a AtomDoesNotExist exception, redis_mongo returns ValueError + # TODO: should this be fixed/synced? I mean, make both raise the same exception? with pytest.raises(Exception, match="Nonexistent atom"): db.get_node_name("error") @@ -332,6 +335,7 @@ def test_get_node_by_name(self, database, request): @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_atoms_by_field(self, database, request): if database == "in_memory_db": + # TODO: fix this pytest.skip( "ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210" ) @@ -350,6 +354,7 @@ def test_get_atoms_by_field(self, database, request): @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_atoms_by_index(self, database, request): if database == "in_memory_db": + # TODO: fix this pytest.skip( "ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210" ) @@ -366,6 +371,7 @@ def test_get_atoms_by_index(self, database, request): @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_atoms_by_index_exceptions(self, database, request): if database == "in_memory_db": + # TODO: fix this pytest.skip( "ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210" ) @@ -376,6 +382,7 @@ def test_get_atoms_by_index_exceptions(self, database, request): @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_atoms_by_text_field_regex(self, database, request): if database == "in_memory_db": + # TODO: fix this pytest.skip( "ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210" ) @@ -394,6 +401,7 @@ def test_get_atoms_by_text_field_regex(self, database, request): @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_atoms_by_text_field_text(self, database, request): if database == "in_memory_db": + # TODO: fix this pytest.skip( "ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210" ) @@ -414,6 +422,7 @@ def test_get_atoms_by_text_field_text(self, database, request): @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_node_by_name_starting_with(self, database, request): if database == "in_memory_db": + # TODO: fix this pytest.skip( "ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210" ) @@ -442,7 +451,7 @@ def test_get_all_nodes(self, database, request): @pytest.mark.parametrize( "database,params,links_len,cursor_value", - [ + [ # TODO: differences here must be fixed if possible ("redis_mongo_db", {"link_type": "Ac"}, 3, 0), ("redis_mongo_db", {"link_type": "Ac", "names": True, "chunk_size": 1}, 3, 0), ("redis_mongo_db", {"link_type": "Ac", "names": True, "cursor": 0}, 3, 0), @@ -518,6 +527,7 @@ def test_is_ordered(self, database, request): @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_is_ordered_no_handle(self, database, request): if database == "redis_mongo_db": + # TODO: fix this pytest.skip( "ERROR redis_mongo_db is raising ValueError exception, should be AtomDoesNotExist. " "See https://github.com/singnet/das-atom-db/issues/210" @@ -528,7 +538,7 @@ def test_is_ordered_no_handle(self, database, request): @pytest.mark.parametrize( "database,params,links_len,cursor_value", - [ + [ # TODO: differences here must be fixed if possible ("redis_mongo_db", {}, 3, None), ("redis_mongo_db", {"handles_only": True}, 3, None), ("redis_mongo_db", {"no_target_format": True}, 3, None), @@ -558,7 +568,7 @@ def test_get_incoming_links(self, database, params, links_len, cursor_value, req @pytest.mark.parametrize( "database,params,links_len,cursor_value", - [ + [ # TODO: differences here must be fixed if possible ("redis_mongo_db", {}, 1, None), ("redis_mongo_db", {"toplevel_only": True}, 1, None), # ("redis_mongo_db", {"link_type": "NoTopLevel" , "toplevel_only": True}, 0, None), # doesn"t work @@ -604,7 +614,7 @@ def test_get_matched_links(self, database, params, links_len, cursor_value, requ @pytest.mark.parametrize( "database,params,links_len,cursor_value", - [ + [ # TODO: differences here must be fixed if possible ("redis_mongo_db", {"link_type": "Z", "target_handles": []}, 0, None), # ("redis_mongo_db", {"link_type": "*", "target_handles": ["*", "*"], "toplevel_only": True}, 0, None), # ("in_memory_db", {"link_type": "*", "target_handles": ["*", "*"]}, 0, None), @@ -620,7 +630,7 @@ def test_get_matched_no_links(self, database, params, links_len, cursor_value, r @pytest.mark.parametrize( "database,params,links_len,cursor_value,is_top_level", - [ + [ # TODO: differences here must be fixed if possible ("redis_mongo_db", {}, 1, None, True), ("redis_mongo_db", {}, 1, None, False), ("redis_mongo_db", {"toplevel_only": True}, 0, None, False), @@ -652,6 +662,7 @@ def test_get_matched_type_template( @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_matched_type(self, database, request): if database == "redis_mongo_db": + # TODO: fix this pytest.skip( "ERROR redis_mongo_db is returning more values. " "See https://github.com/singnet/das-atom-db/issues/210" @@ -671,7 +682,7 @@ def test_get_matched_type(self, database, request): @pytest.mark.parametrize( "database,params,top_level,n_links,n_nodes", - [ + [ # TODO: differences here must be fixed if possible ("redis_mongo_db", {}, True, 1, 1), ("redis_mongo_db", {"no_target_format": True}, False, 1, 1), # ("redis_mongo_db", {"targets_document": True}, False, 1, 1),# breaks when is a node @@ -760,6 +771,7 @@ def test_get_atom_as_dict(self, database, request): @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" @@ -773,6 +785,7 @@ def test_get_atom_as_dict_none(self, database, request): @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_get_atom_as_dict_exceptions(self, database, request): if database == "redis_mongo_db": + # TODO: fix this pytest.skip( "ERROR redis_mongo_db doesn't raises exception, they should return the same result/exception. " "See https://github.com/singnet/das-atom-db/issues/210" @@ -785,7 +798,7 @@ def test_get_atom_as_dict_exceptions(self, database, request): @pytest.mark.parametrize( "database,params", - [ + [ # TODO: differences here must be fixed if possible ("redis_mongo_db", {}), ("redis_mongo_db", {"precise": True}), ("redis_mongo_db", {"precise": False}), @@ -853,7 +866,7 @@ def test_add_node_discard(self, database, node, request): @pytest.mark.parametrize( "database,node", - [ + [ # TODO: differences here must be fixed if possible ("redis_mongo_db", {}), # NOTE it"s not breaking, should break? # ("redis_mongo_db", {"name": "A", "type": "A", "handle": ""}), @@ -895,6 +908,7 @@ def test_add_link(self, database, params, expected_count, top_level, request): @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_reindex(self, database, request): if database == "in_memory_db": + # TODO: fix this pytest.skip( "ERROR Not implemented. See https://github.com/singnet/das-atom-db/issues/210" ) @@ -906,6 +920,7 @@ def test_reindex(self, database, request): @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_delete_atom(self, database, request): if database == "in_memory_db": + # TODO: fix this pytest.skip( "ERROR Atom not in incoming_set. See https://github.com/singnet/das-atom-db/issues/210" ) @@ -930,6 +945,7 @@ def test_delete_atom(self, database, request): @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_delete_atom_exceptions(self, database, request): if database == "in_memory_db": + # TODO: fix this pytest.skip( "ERROR Atom not in incoming_set. See https://github.com/singnet/das-atom-db/issues/210" ) @@ -1014,6 +1030,7 @@ def test_delete_atom_exceptions(self, database, request): ) def test_create_field_index(self, database, params, request): if database == "in_memory_db": + # TODO: fix this pytest.skip( "ERROR Not implemented on in_memory_db. See https://github.com/singnet/das-atom-db/issues/210" ) @@ -1044,6 +1061,7 @@ def test_create_field_index(self, database, params, request): ) def test_create_field_index_value_error(self, database, params, request): if database == "in_memory_db": + # TODO: fix this pytest.skip( "ERROR Not implemented on in_memory_db. See https://github.com/singnet/das-atom-db/issues/210" ) @@ -1051,13 +1069,14 @@ def test_create_field_index_value_error(self, database, params, request): with pytest.raises(ValueError): db.create_field_index(**params) - # + # TODO: fix this or delete # @pytest.mark.parametrize("database,params", [ # ("redis_mongo_db", {"atom_type": "A", "fields": ["side"], "index_type": "wrong_type"}), # ("in_memory_db", {}) # ]) # def test_create_field_index_mongo_error(self, database, params, request): # if database == "in_memory_db": + # # TODO: fix this # pytest.skip("ERROR Not implemented on in_memory_db. See https://github.com/singnet/das-atom-db/issues/210") # # db: AtomDB = request.getfixturevalue(database) @@ -1067,6 +1086,7 @@ def test_create_field_index_value_error(self, database, params, request): @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_bulk_insert(self, database, request): if database == "redis_mongo_db": + # TODO: fix this pytest.skip( "ERROR redis_mongo_db is not updating targets. See https://github.com/singnet/das-atom-db/issues/210" ) @@ -1093,6 +1113,7 @@ def test_bulk_insert_exceptions(self, database, request): link_a = db._build_link({"targets": [], "type": "A"}) with pytest.raises(Exception): db.bulk_insert([node_a, link_a]) + # TODO: fix this pytest.skip( "ERROR should raise an exception. See https://github.com/singnet/das-atom-db/issues/210" ) @@ -1111,6 +1132,7 @@ def test_retrieve_all_atoms(self, database, request): @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_commit(self, database, request): if database == "in_memory_db": + # TODO: fix this pytest.skip( "ERROR Not implemented on in_memory_db. See https://github.com/singnet/das-atom-db/issues/210" ) @@ -1126,6 +1148,7 @@ def test_commit(self, database, request): @pytest.mark.parametrize("database", ["redis_mongo_db", "in_memory_db"]) def test_commit_buffer(self, database, request): if database == "in_memory_db": + # TODO: fix this pytest.skip( "ERROR Not implemented on in_memory_db. See https://github.com/singnet/das-atom-db/issues/210" )