diff --git a/akm_tools/validation/custom_exceptions.py b/akm_tools/validation/custom_exceptions.py index 9545c66..a237dab 100644 --- a/akm_tools/validation/custom_exceptions.py +++ b/akm_tools/validation/custom_exceptions.py @@ -1,24 +1,26 @@ from typing import Dict, List - +from deepdiff import DeepDiff +import json class IDConflictException(Exception): def __init__(self, instances: List[Dict]): - err_msg = f"More than 2 instances with same ID ! \n{instances}\n" + err_msg = f"More than 2 instances with same ID ! \n{json.dumps(instances,indent=2)}\n" super().__init__(err_msg) self.message = err_msg class BaseInstanceOverwiteException(Exception): - def __init__(self, base_instance, extended_instance): + def __init__(self, base_instance: dict, extended_instance: dict): + diff = DeepDiff(base_instance, extended_instance, ignore_order=True) err_msg = ( - f"The extended instance :\n{extended_instance}\nis overwriting properties of base instance\n{base_instance}\n" + f"Issue with {base_instance['id']}\nThe extended instance is overwriting properties of base instance:\n{diff.pretty()}\n" ) super().__init__(err_msg) self.message = err_msg class InvalidReferentIDException(Exception): - def __init__(self, instance, referentID): - err_msg = f"The instance :\n{instance}\nis referencing an invalid id : '{referentID}'\n" + def __init__(self, instance : dict, referentID): + err_msg = f"The instance :\n{json.dumps(instance,indent=2)}\nis referencing an invalid id : '{referentID}'\n" super().__init__(err_msg) self.message = err_msg diff --git a/poetry.lock b/poetry.lock index 4fd53bd..8bade46 100644 --- a/poetry.lock +++ b/poetry.lock @@ -90,6 +90,24 @@ files = [ {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, ] +[[package]] +name = "deepdiff" +version = "7.0.1" +description = "Deep Difference and Search of any Python object/data. Recreate objects by adding adding deltas to each other." +optional = false +python-versions = ">=3.8" +files = [ + {file = "deepdiff-7.0.1-py3-none-any.whl", hash = "sha256:447760081918216aa4fd4ca78a4b6a848b81307b2ea94c810255334b759e1dc3"}, + {file = "deepdiff-7.0.1.tar.gz", hash = "sha256:260c16f052d4badbf60351b4f77e8390bee03a0b516246f6839bc813fb429ddf"}, +] + +[package.dependencies] +ordered-set = ">=4.1.0,<4.2.0" + +[package.extras] +cli = ["click (==8.1.7)", "pyyaml (==6.0.1)"] +optimize = ["orjson"] + [[package]] name = "exceptiongroup" version = "1.2.0" @@ -161,6 +179,20 @@ files = [ {file = "mypy_extensions-1.0.0.tar.gz", hash = "sha256:75dbf8955dc00442a438fc4d0666508a9a97b6bd41aa2f0ffe9d2f2725af0782"}, ] +[[package]] +name = "ordered-set" +version = "4.1.0" +description = "An OrderedSet is a custom MutableSet that remembers its order, so that every" +optional = false +python-versions = ">=3.7" +files = [ + {file = "ordered-set-4.1.0.tar.gz", hash = "sha256:694a8e44c87657c59292ede72891eb91d34131f6531463aab3009191c77364a8"}, + {file = "ordered_set-4.1.0-py3-none-any.whl", hash = "sha256:046e1132c71fcf3330438a539928932caf51ddbc582496833e23de611de14562"}, +] + +[package.extras] +dev = ["black", "mypy", "pytest"] + [[package]] name = "packaging" version = "23.2" @@ -443,4 +475,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "8bcacca50ab544267b3cf0ffca0e66771127c0ea1afa971fdbe8c9b20d01417c" +content-hash = "3216fe50fced89fe99a1bcc6ef4271e082ca2bb4bbafdb34a60c10983e56d157" diff --git a/pyproject.toml b/pyproject.toml index 4756c2e..e8a6fac 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,6 +9,7 @@ python = "^3.10" PyYAML = "*" jsonschema = "*" black = "^24.2.0" +deepdiff = "^7.0.1" [tool.poetry.group.dev.dependencies] diff --git a/tests/test_custom_exceptions.py b/tests/test_custom_exceptions.py index 81111e0..e451cc3 100644 --- a/tests/test_custom_exceptions.py +++ b/tests/test_custom_exceptions.py @@ -13,7 +13,6 @@ def test_IDConflictException(): instances = [{"id": 1}, {"id": 1}, {"id": 1}] with pytest.raises(IDConflictException) as excinfo: raise IDConflictException(instances) - assert str(excinfo.value) == f"More than 2 instances with same ID ! \n{instances}\n" def test_BaseInstanceOverwiteException(): @@ -24,10 +23,6 @@ def test_BaseInstanceOverwiteException(): extended_instance = {"id": "data_instance2", "name": "test"} with pytest.raises(BaseInstanceOverwiteException) as excinfo: raise BaseInstanceOverwiteException(base_instance, extended_instance) - assert ( - str(excinfo.value) - == f"The extended instance :\n{extended_instance}\nis overwriting properties of base instance\n{base_instance}\n" - ) def test_InvalidReferentIDException(): @@ -38,4 +33,3 @@ def test_InvalidReferentIDException(): referentID_value = instance["isA"]["referentID"] with pytest.raises(InvalidReferentIDException) as excinfo: raise InvalidReferentIDException(instance, referentID_value) - assert str(excinfo.value) == f"The instance :\n{instance}\nis referencing an invalid id : '{referentID_value}'\n"