From d3c6f56e9c79ce624db9c46202065b7be6cf57d6 Mon Sep 17 00:00:00 2001 From: Alexander Schlemmer <a.schlemmer@indiscale.com> Date: Fri, 25 Oct 2024 10:37:21 +0200 Subject: [PATCH] test(apiutils): added a unittest for describe_diff function --- src/linkahead/apiutils.py | 6 ++--- tox.ini | 2 +- unittests/test_apiutils.py | 47 +++++++++++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/linkahead/apiutils.py b/src/linkahead/apiutils.py index c30dc85f..23643db7 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 592c660c..c63555c2 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 4705f19a..c0e2f7eb 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 -- GitLab