diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bb0cc59700172b87d2c824b06c553b00355c832..51ea93d60cf74636d38709b0b9b11763f2bc739f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed ### +- #90 compare_entities function in apiutils does not check units + ### Security ### ## [0.6.1] - 2021-12-03 ## diff --git a/src/caosdb/apiutils.py b/src/caosdb/apiutils.py index 21a8cc7ca700a8b119786dbef082db286ead5d57..4ee0dacd0ecf0a9e347866965786fb619dd7a725 100644 --- a/src/caosdb/apiutils.py +++ b/src/caosdb/apiutils.py @@ -637,6 +637,11 @@ def compare_entities(old_entity: Entity, new_entity: Entity): newdiff["properties"][prop.name]["datatype"] = \ matching[0].datatype + if (prop.unit != matching[0].unit): + olddiff["properties"][prop.name]["unit"] = prop.unit + newdiff["properties"][prop.name]["unit"] = \ + matching[0].unit + if (prop.value != matching[0].value): olddiff["properties"][prop.name]["value"] = prop.value newdiff["properties"][prop.name]["value"] = \ diff --git a/unittests/test_apiutils.py b/unittests/test_apiutils.py index 717595d049a5a7c27b644dc6079d02efa1cefb71..9cf9e1b7ac0043f3c256f1210272ff15b9df458b 100644 --- a/unittests/test_apiutils.py +++ b/unittests/test_apiutils.py @@ -144,3 +144,38 @@ def test_compare_entities(): assert "tests_234234" in diff_r1["properties"] assert "tests_TT" in diff_r2["properties"] + + +def test_compare_entities_units(): + r1 = db.Record() + r2 = db.Record() + r1.add_parent("bla") + r2.add_parent("bla") + r1.add_parent("lopp") + r1.add_property("test", value=2, unit="cm") + r2.add_property("test", value=2, unit="m") + r1.add_property("tests", value=3, unit="cm") + r2.add_property("tests", value=45, unit="cm") + r1.add_property("tester", value=3) + r2.add_property("tester", ) + r1.add_property("tests_234234", value=45, unit="cm") + r2.add_property("tests_TT", value=45, unit="cm") + + diff_r1, diff_r2 = compare_entities(r1, r2) + + assert len(diff_r1["parents"]) == 1 + assert len(diff_r2["parents"]) == 0 + assert len(diff_r1["properties"]) == 4 + assert len(diff_r2["properties"]) == 4 + + assert "tests" in diff_r1["properties"] + assert "tests" in diff_r2["properties"] + + assert "tester" in diff_r1["properties"] + assert "tester" in diff_r2["properties"] + + assert "tests_234234" in diff_r1["properties"] + assert "tests_TT" in diff_r2["properties"] + + assert diff_r1["properties"]["test"]["unit"] == "cm" + assert diff_r2["properties"]["test"]["unit"] == "m"