diff --git a/integrationtests/test_issues.py b/integrationtests/test_issues.py index 814e82ad75512ec8fe217294e1a9e86c6aa01ab3..76392f3a4ce20d7ed6b6ccc30c79f1ce400001f7 100644 --- a/integrationtests/test_issues.py +++ b/integrationtests/test_issues.py @@ -16,20 +16,18 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <https://www.gnu.org/licenses/>. # -from pytest import fixture, mark, raises - import linkahead as db -from linkahead.cached import cache_clear from caosadvancedtools.models.parser import parse_model_from_string - from caoscrawler.crawl import Crawler from caoscrawler.identifiable import Identifiable from caoscrawler.identifiable_adapters import CaosDBIdentifiableAdapter +from caoscrawler.scanner import (create_converter_registry, + scan_structure_elements) from caoscrawler.structure_elements import DictElement - -from caoscrawler.scanner import create_converter_registry, scan_structure_elements - +from linkahead.cached import cache_clear from linkahead.utils.register_tests import clear_database, set_test_key +from pytest import fixture, mark, raises + set_test_key("10b128cf8a1372f30aa3697466bb55e76974e0c16a599bb44ace88f19c8f61e2") @@ -171,8 +169,9 @@ def test_issue_83(clear_database): name=referencing_type.name).add_property(name=referenced_type.name, value=[ref_target1]) referencing2 = db.Record(name="Referencing2").add_parent( name=referencing_type.name).add_property(name=referenced_type.name, value=[ref_target2]) - referencing3 = db.Record(name="Referencing3").add_parent(name=referencing_type.name).add_property( - name=referenced_type.name, value=[ref_target1, ref_target2]) + referencing3 = db.Record(name="Referencing3").add_parent( + name=referencing_type.name).add_property(name=referenced_type.name, value=[ref_target1, + ref_target2]) records = db.Container().extend( [ref_target1, ref_target2, referencing1, referencing2, referencing3]) diff --git a/src/caoscrawler/sync_graph.py b/src/caoscrawler/sync_graph.py index ac484df26d0c43ad4e8fb831b4e1bb1b80a3819f..56206ac216b582ad8fa21f5de0fca884210bf373 100644 --- a/src/caoscrawler/sync_graph.py +++ b/src/caoscrawler/sync_graph.py @@ -378,7 +378,6 @@ class SyncGraph(): if self.get_equivalent(node) is not None: self._merge_into(node, self.get_equivalent(node)) else: - print(f'make {self.nodes.index(node)} exis') self._id_look_up[node.id] = node self._treat_existing(node) diff --git a/src/caoscrawler/sync_node.py b/src/caoscrawler/sync_node.py index 0c6c32b1922656c6c6d6cecb9ba1ca0c6b551c61..66113cb2a9d4aa26de6477b90c71c6be47b8d7c0 100644 --- a/src/caoscrawler/sync_node.py +++ b/src/caoscrawler/sync_node.py @@ -26,6 +26,7 @@ from typing import Any, Dict, List, Optional, Union from uuid import uuid4 as uuid import linkahead as db +from linkahead.common.models import _ParentList, _Properties class SyncNode(): @@ -52,11 +53,11 @@ class SyncNode(): None) -> None: self.id = entity.id self.role = entity.role - self.parents = entity.parents + self.parents = _ParentList().extend(entity.parents) self.path = entity.path self.name = entity.name self.description = entity.description - self.properties = list(entity.properties) + self.properties = _Properties().extend(entity.properties) self.uuid = uuid() self.identifiable = None self.registered_identifiable = registered_identifiable @@ -72,11 +73,10 @@ class SyncNode(): else: assert self.__getattribute__(attr) == other.__getattribute__(attr) for p in other.parents: - if p not in self.parents: + if not parent_in_list(p, self.parents): self.parents.append(p) for p in other.properties: - if p not in self.properties: - self.properties.append(p) + self.properties.append(p) def export_entity(self) -> db.Entity: ent = None @@ -96,5 +96,27 @@ class SyncNode(): raise db.apiutils.EntityMergeConflictError(f"Differing values were set for Property {p.name}:\n" f"{ent.get_property(p).value}\n{p.value}") else: - ent.add_property(p) + ent.add_property(id=p.id, name=p.name, value=p.value) return ent + + +def parent_in_list(parent, plist): + missing = False + if parent.name is not None: + if parent.name not in plist._element_by_name: + missing = True + if parent.id is not None: + if str(parent.id) not in plist._element_by_id: + missing = True + return not missing + + +def property_in_list(prop, plist): + missing = False + if prop.name is not None: + if prop.name not in plist._element_by_name: + missing = True + if prop.id is not None: + if str(prop.id) not in plist._element_by_id: + missing = True + return not missing diff --git a/unittests/test_sync_graph.py b/unittests/test_sync_graph.py index 0c39899e8c21bffeb249a20ed00d177c5360f5e5..64b70a59008607c225d38c4fdf82e452ff48bbc0 100644 --- a/unittests/test_sync_graph.py +++ b/unittests/test_sync_graph.py @@ -25,7 +25,8 @@ import linkahead as db import pytest from caoscrawler.identifiable import Identifiable from caoscrawler.identifiable_adapters import CaosDBIdentifiableAdapter -from caoscrawler.sync_graph import SyncGraph, SyncNode +from caoscrawler.sync_graph import SyncGraph +from caoscrawler.sync_node import SyncNode, parent_in_list, property_in_list from test_crawler import basic_retrieve_by_name_mock_up, mock_get_entity_by @@ -568,7 +569,7 @@ def test_sync_node(): assert export.id == rec_b.id assert export.name == rec_a.name for p in rec_a.parents + rec_b.parents: - assert p in export.parents + assert parent_in_list(p, export.parents) # if p.name is not None: # assert p.name in [el.name for el in export.parents] # if p.id is not None: @@ -601,9 +602,9 @@ def test_sync_node(): assert sn_a.id == rec_b.id assert sn_a.name == rec_a.name for p in rec_a.parents + rec_b.parents: - assert p in sn_a.parents + assert parent_in_list(p, sn_a.parents) for p in rec_a.properties + rec_b.properties: - assert p in sn_a.properties + assert property_in_list(p, sn_a.properties) assert sn_a.description == rec_b.description assert sn_a.role == rec_a.role @@ -634,3 +635,24 @@ def test_sync_node(): sn_b = SyncNode(db.File(path='101')) with pytest.raises(AssertionError): sn_a.update(sn_b) + + +def test_export_node(): + rec_a = (db.Record(id=101) + .add_parent("B") + .add_parent(id=103) + .add_property(name="a", value=[SyncNode(db.Record())]) + .add_property(name='b', id=103, value='b')) + + sn_a = SyncNode(rec_a) + exp = sn_a.export_entity() + assert exp.id == rec_a.id + assert exp.name == rec_a.name + for p in rec_a.parents: + assert len([el for el in exp.parents if p.name == el.name]) == 1 + for p in rec_a.properties: + assert p.value == exp.get_property(p.name).value + if isinstance(p.value, list): + assert len(p.value) == len(exp.get_property(p.name).value) + assert len(exp.properties) == len(rec_a.properties) + assert len(exp.parents) == len(rec_a.parents)