diff --git a/src/linkahead/apiutils.py b/src/linkahead/apiutils.py index d61daeecb3d1ac2305090e7b7210d9c2c40bd33f..17bd5af4b223b9d0db84b2124b0393e07ba2f80c 100644 --- a/src/linkahead/apiutils.py +++ b/src/linkahead/apiutils.py @@ -251,12 +251,16 @@ def compare_entities(entity0: Optional[Entity] = None, # - Should there be a more detailed comparison of parents without id? # - Revisit filter - do we care about RecordType when matching? # How to treat None? + # - Should matching of parents also take the recordtype into account + # for parents that have a name but no id? # Suggestions for enhancements: # - For the comparison of entities in value and properties, consider # keeping a list of traversed entities, not only look at first layer # - Make the empty_diff functionality faster by adding a parameter to # this function so that it returns after the first found difference? # - Add parameter to restrict diff to some characteristics + # - Implement comparison of date where one is a string and the other is + # datetime if entity0 is None and old_entity is None: raise ValueError("Please provide the first entity as first argument (`entity0`)") if entity1 is None and new_entity is None: diff --git a/unittests/test_apiutils.py b/unittests/test_apiutils.py index 35f80e5e3dbe355b9bc32330587f6aa7bc2b0d63..2fb946c518d940bd505622284070a0f5fafdf12f 100644 --- a/unittests/test_apiutils.py +++ b/unittests/test_apiutils.py @@ -438,20 +438,43 @@ def test_compare_entities_battery(): t8 = db.Property(**alt_settings).add_parent(par3).add_property(prop3) diffs_0 = compare_entities(t7, t8), compare_entities(t7, t8, True) diffs_1 = compare_entities(t8, t7)[::-1], compare_entities(t8, t7, True)[::-1] - #assert diffs_0 == diffs_1 + assert diffs_0 == diffs_1 prop_settings = {"datatype": db.REFERENCE, "description": "desc of prop", "value": db.Record().add_parent(par3), "unit": '°'} t1.add_property(db.Property(name="description", **prop_settings)) t2.add_property(db.Property(name="description", **prop_settings)) -# try: -# diffs_0 = compare_entities(t1, t2), compare_entities(t1, t2, True) -# except Exception as e: -# diffs_0 = type(e) -# try: -# diffs_1 = compare_entities(t2, t1)[::-1], compare_entities(t2, t1, True)[::-1] -# except Exception as e: -# diffs_1 = type(e) -# assert diffs_0 == diffs_1 + # Order invariance for multi-property - either both fail or same result + try: + diffs_0 = compare_entities(t1, t2), compare_entities(t1, t2, True) + except Exception as e: + diffs_0 = type(e) + try: + diffs_1 = compare_entities(t2, t1)[::-1], compare_entities(t2, t1, True)[::-1] + except Exception as e: + diffs_1 = type(e) + assert diffs_0 == diffs_1 + # Property types + t09, t10 = db.RecordType(), db.RecordType() + for t, ex in [(db.INTEGER, [-12, 0]), (db.DATETIME, ["2030-01-01", "1012-02-29"]), + (db.DOUBLE, [13.23, 7.1]), (db.BOOLEAN, [True, False])]: + t09.add_property(db.Property(name=f"{t}:{ex[0]}", datatype=t, value=ex[0])) + t10.add_property(db.Property(name=f"{t}:{ex[0]}", datatype=t, value=ex[0])) + t09.add_property(name=f"{t}:{ex[1]}", datatype=t, value=ex[1]) + t10.add_property(name=f"{t}:{ex[1]}", datatype=t, value=ex[1]) + assert empty_diff(t09, t10) + t09.add_property(name=f"diff", value=1) + t10.add_property(name=f"diff", value=2) + assert not empty_diff(t09, t10) + # Default values + t09, t10 = db.Record(), db.Record() + t09.add_property(db.Property(name=f"A1"), value="A") + t10.add_property(name=f"A1", value="A") + t09.add_property(db.Property(id=12, name=f"A2"), value="A") + t10.add_property(id=12, name=f"A2", value="A") + t09.add_property(db.Property(id=15), value="A") + t10.add_property(id=15, value="A") + assert empty_diff(t09, t10) + # ToDo: extended tests for references def test_compare_special_properties():