diff --git a/src/linkahead/apiutils.py b/src/linkahead/apiutils.py index c30dc85f3a0353e66c81dea09851b6f6df6c310d..23643db77052cf3aca5396d92d2f0095cbdc5a81 100644 --- a/src/linkahead/apiutils.py +++ b/src/linkahead/apiutils.py @@ -576,9 +576,9 @@ def describe_diff(olddiff: dict[str, Any], newdiff: dict[str, Any], description += "property {} differs:\n".format(prop) if prop not in olddiff["properties"]: - description += "it does not exist in the " + label_old + ": \n" + description += "it does not exist in the " + label_old + ":\n" elif prop not in newdiff["properties"]: - description += "it does not exist in the " + label_new + ": \n" + description += "it does not exist in the " + label_new + ":\n" else: description += label_old + ": {}\n".format( olddiff["properties"][prop]) @@ -590,7 +590,7 @@ def describe_diff(olddiff: dict[str, Any], newdiff: dict[str, Any], label_old + " and the " + label_new + - " of {}\n\n".format(name))+description + " of {}\n\n".format(name)) + description return description diff --git a/tox.ini b/tox.ini index 592c660c5bbbf5805a3ecbb3e60c41f597182a55..c63555c27b73224106109c1f675e9525d9e89b74 100644 --- a/tox.ini +++ b/tox.ini @@ -17,6 +17,6 @@ max-line-length=100 [pytest] testpaths = unittests xfail_strict = True -addopts = -x -vv --cov=caosdb +addopts = -x -vv --cov=linkahead pythonpath = src diff --git a/unittests/test_apiutils.py b/unittests/test_apiutils.py index 4705f19a1bdfbc4358790f787f2dce9ea97fee48..c0e2f7ebb46251c05938e03a6ed5b150bc7f5294 100644 --- a/unittests/test_apiutils.py +++ b/unittests/test_apiutils.py @@ -1,6 +1,7 @@ # # This file is a part of the LinkAhead Project. # +# Copyright (C) 2024 Alexander Schlemmer <a.schlemmer@indiscale.com> # Copyright (C) 2020 Timm Fitschen <t.fitschen@indiscale.com> # Copyright (C) 2022 Florian Spreckelsen <f.spreckelsen@indiscale.com> # Copyright (C) 2022 Daniel Hornung <d.hornung@indiscale.com> @@ -31,7 +32,8 @@ import linkahead.apiutils import pytest from linkahead.apiutils import (EntityMergeConflictError, apply_to_ids, compare_entities, create_id_query, empty_diff, - merge_entities, resolve_reference) + merge_entities, resolve_reference, + describe_diff) from linkahead.common.models import SPECIAL_ATTRIBUTES @@ -627,3 +629,46 @@ def test_merge_id_with_resolved_entity(): 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 + + +def test_describe_diff(): + recA = db.Record() + recA.add_property(name="propA", value=2) + recA.add_property(name="propB", value=2) + recA.add_property(name="propD", value=-273, unit="K") + + recB = db.Record() + recB.add_property(name="propA", value=2) + recB.add_property(name="propB", value=12) + recB.add_property(name="propC", value="cool 17") + recB.add_property(name="propD", value=-273, unit="°C") + + diff = compare_entities(recA, recB) + diffout = describe_diff(*diff) + + assert diffout.startswith("## Difference between the old version and the new version of None") + + # The output of the describe_diff function is currently not ordered (e.g. by name of the property) + # so we cannot just compare a well-defined output string. + + assert "it does not exist in the old version:" in diffout + assert "old version: {'value': 2}" in diffout + assert "new version: {'value': 12}" in diffout + + assert "old version: {'unit': 'K'}" in diffout + assert "new version: {'unit': '°C'}" in diffout + + diffout = describe_diff(*diff, name="Entity") + assert diffout.startswith("## Difference between the old version and the new version of Entity") + + diffout = describe_diff(*diff, label_old="recA", label_new="recB") + assert "recA: {'value': 2}" in diffout + assert "recB: {'value': 12}" in diffout + + assert "recA: {'unit': 'K'}" in diffout + assert "recB: {'unit': '°C'}" in diffout + + assert "it does not exist in the recA:" in diffout + + assert "old" not in diffout + assert "new" not in diffout