diff --git a/unittests/test_apiutils.py b/unittests/test_apiutils.py index b9a02926803c1e7b8134cde904ea2021d0281ff4..549312c367eea90eac79a9bbb4898cde76f8e8ac 100644 --- a/unittests/test_apiutils.py +++ b/unittests/test_apiutils.py @@ -588,3 +588,48 @@ def test_merge_missing_list_datatype_82(): with pytest.raises(TypeError) as te: merge_entities(recA, recB_without_DT, force=True) assert "Invalid datatype: List valued properties" in str(te.value) + + +def test_merge_id_with_resolved_entity(): + + rtname = "TestRT" + ref_id = 123 + ref_rec = db.Record(id=ref_id).add_parent(name=rtname) + + # recA has the resolved referenced record as value, recB its id. Otherwise, + # they are identical. + recA = db.Record().add_property(name=rtname, value=ref_rec) + recB = db.Record().add_property(name=rtname, value=ref_id) + + # default is strict: raise error since values are different + with pytest.raises(EntityMergeConflictError): + merge_entities(recA, recB) + + # Overwrite from right to left in both cases + merge_entities(recA, recB, merge_id_with_resolved_entity=True) + assert recA.get_property(rtname).value == ref_id + assert recA.get_property(rtname).value == recB.get_property(rtname).value + + recA = db.Record().add_property(name=rtname, value=ref_rec) + merge_entities(recB, recA, merge_id_with_resolved_entity=True) + assert recB.get_property(rtname).value == ref_rec + assert recA.get_property(rtname).value == recB.get_property(rtname).value + + # id mismatches + recB = db.Record().add_property(name=rtname, value=ref_id*2) + with pytest.raises(EntityMergeConflictError): + merge_entities(recA, recB, merge_id_with_resolved_entity=True) + + other_rec = db.Record(id=None).add_parent(name=rtname) + recA = db.Record().add_property(name=rtname, value=other_rec) + recB = db.Record().add_property(name=rtname, value=ref_id) + with pytest.raises(EntityMergeConflictError): + merge_entities(recA, recB, merge_id_with_resolved_entity=True) + + # also works in lists: + recA = db.Record().add_property( + name=rtname, datatype=db.LIST(rtname), value=[ref_rec, ref_id*2]) + recB = db.Record().add_property(name=rtname, datatype=db.LIST(rtname), value=[ref_id, ref_id*2]) + merge_entities(recA, recB, merge_id_with_resolved_entity=True) + assert recA.get_property(rtname).value == [ref_id, ref_id*2] + assert recA.get_property(rtname).value == recB.get_property(rtname).value