diff --git a/src/linkahead/apiutils.py b/src/linkahead/apiutils.py index c571a849411322fc6b5fda890b89ed13cb5c8c1f..53c9957e045055d4bc69fb240ef651fe9a5e71b0 100644 --- a/src/linkahead/apiutils.py +++ b/src/linkahead/apiutils.py @@ -321,7 +321,8 @@ def compare_entities(entity0: Entity, entity1: Entity, # Compare Entities if (compare_referenced_records and isinstance(val0, Entity) and isinstance(val1, Entity)): - if empty_diff(val0, val1, False): + if empty_diff(val0, val1, False, + entity_name_id_equivalency): continue # Compare Entity name and id if entity_name_id_equivalency: @@ -366,7 +367,8 @@ def compare_entities(entity0: Entity, entity1: Entity, # Recursive call to determine the differences between properties # Note: Can lead to infinite recursion if two properties have # themselves or each other as subproperties - od, nd = compare_entities(comp1, comp2, compare_referenced_records) + od, nd = compare_entities(comp1, comp2, compare_referenced_records, + entity_name_id_equivalency) # We do not care about parents and properties here, discard od.pop("parents") od.pop("properties") @@ -419,7 +421,8 @@ def compare_entities(entity0: Entity, entity1: Entity, def empty_diff(old_entity: Entity, new_entity: Entity, - compare_referenced_records: bool = False) -> bool: + compare_referenced_records: bool = False, + entity_name_id_equivalency: bool = False) -> bool: """Check whether the `compare_entities` found any differences between old_entity and new_entity. @@ -431,10 +434,13 @@ def empty_diff(old_entity: Entity, new_entity: Entity, Whether to compare referenced records in case of both, `old_entity` and `new_entity`, have the same reference properties and both have a Record object as value. - + entity_name_id_equivalency : bool, optional + If set to True, the comparison between an entity and an int or str also + checks whether the int/str matches the name or id of the entity, so + Entity(id=100) == 100 == "100". """ - olddiff, newdiff = compare_entities( - old_entity, new_entity, compare_referenced_records) + olddiff, newdiff = compare_entities(old_entity, new_entity, + compare_referenced_records, entity_name_id_equivalency) for diff in [olddiff, newdiff]: for key in ["parents", "properties"]: if len(diff[key]) > 0: @@ -499,8 +505,9 @@ def merge_entities(entity_a: Entity, """ # Compare both entities: - diff_r1, diff_r2 = compare_entities( - entity_a, entity_b, compare_referenced_records=merge_references_with_empty_diffs) + diff_r1, diff_r2 = compare_entities(entity_a, entity_b, + entity_name_id_equivalency=merge_id_with_resolved_entity, + compare_referenced_records=merge_references_with_empty_diffs) # Go through the comparison and try to apply changes to entity_a: for key in diff_r2["parents"]: