diff --git a/src/caosdb/apiutils.py b/src/caosdb/apiutils.py index 37a130b2dd9d43cb3d716d7ad9078af15f5c2434..6c2ba6f25269735d3b058dfe840d3e432b6f706c 100644 --- a/src/caosdb/apiutils.py +++ b/src/caosdb/apiutils.py @@ -219,7 +219,7 @@ def compare_entities(old_entity: Entity, new_entity: Entity, of circular references). NOTE: This function does not work for abstract properties! I.e. it is not possible - to directly compare two entities that are of class db.Property. + to directly compare two entities that are of class caosdb.Property. Parameters ---------- @@ -233,6 +233,12 @@ def compare_entities(old_entity: Entity, new_entity: Entity, identical records are stored in different objects. Default is False. """ + + for entity in (old_entity, new_entity): + if isinstance(entity, Property): + raise NotImplementedError("The function compare_entities does not work for " + "comparing abstract properties.") + olddiff: Dict[str, Any] = {"properties": {}, "parents": []} newdiff: Dict[str, Any] = {"properties": {}, "parents": []} diff --git a/unittests/test_apiutils.py b/unittests/test_apiutils.py index 5a5a5f9f4eef4c41f84be792890ec86813f2501c..1d6406a28dca19f1df86e6897813736d20dc6370 100644 --- a/unittests/test_apiutils.py +++ b/unittests/test_apiutils.py @@ -130,6 +130,10 @@ def test_compare_entities(): assert "tests_234234" in diff_r1["properties"] assert "tests_TT" in diff_r2["properties"] + # Check the value: + assert diff_r1["properties"]["tests"]["value"] == 3 + assert diff_r2["properties"]["tests"]["value"] == 45 + def test_compare_equality(): r1 = db.Record() r2 = db.Record() @@ -187,6 +191,10 @@ def test_compare_entities_units(): assert diff_r1["properties"]["test"]["unit"] == "cm" assert diff_r2["properties"]["test"]["unit"] == "m" + # Check the value: + assert diff_r1["properties"]["tests"]["value"] == 3 + assert diff_r2["properties"]["tests"]["value"] == 45 + def test_compare_special_properties(): # Test for all known special properties: @@ -247,46 +255,13 @@ def test_compare_importances(): assert len(diff["parents"]) == 0 -@pytest.mark.xfail def test_compare_properties(): p1 = db.Property() p2 = db.Property() - diff_r1, diff_r2 = compare_entities(p1, p2) - assert len(diff_r1["parents"]) == 0 - assert len(diff_r2["parents"]) == 0 - assert len(diff_r1["properties"]) == 0 - assert len(diff_r2["properties"]) == 0 - - p1.importance = "SUGGESTED" - diff_r1, diff_r2 = compare_entities(p1, p2) - assert len(diff_r1["parents"]) == 0 - assert len(diff_r2["parents"]) == 0 - assert len(diff_r1["properties"]) == 0 - assert len(diff_r2["properties"]) == 0 - assert "importance" in diff_r1 - assert diff_r1["importance"] == "SUGGESTED" - - # TODO: I'm not sure why it is not like this: - # assert diff_r2["importance"] is None - # ... but: - assert "importance" not in diff_r2 - - p2.importance = "SUGGESTED" - p1.value = 42 - p2.value = 4 - - diff_r1, diff_r2 = compare_entities(p1, p2) - assert len(diff_r1["parents"]) == 0 - assert len(diff_r2["parents"]) == 0 - assert len(diff_r1["properties"]) == 0 - assert len(diff_r2["properties"]) == 0 - - # Comparing values currently does not seem to be implemented: - assert "value" in diff_r1 - assert diff_r1["value"] == 42 - assert "value" in diff_r2 - assert diff_r2["value"] == 4 + with pytest.raises(NotImplementedError, match=".*abstract properties.*"): + compare_entities(p1, p2) + def test_copy_entities():