diff --git a/CHANGELOG.md b/CHANGELOG.md index 3021cbc6c0ba1287ac7b8d71dd9abd1a18fd73b3..11212ffbf578160298c3abc72fe9aa366b4bb164 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ 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 +- #94 some special properties were not checked in compare_entities ### Security ### diff --git a/src/caosdb/apiutils.py b/src/caosdb/apiutils.py index 4ee0dacd0ecf0a9e347866965786fb619dd7a725..a8256976b79d71f514fcc1dc9c868f1eeebc76e9 100644 --- a/src/caosdb/apiutils.py +++ b/src/caosdb/apiutils.py @@ -1,12 +1,11 @@ # -*- coding: utf-8 -*- # -# ** header v3.0 # This file is a part of the CaosDB Project. # # Copyright (C) 2018 Research Group Biomedical Physics, # Max-Planck-Institute for Dynamics and Self-Organization Göttingen # Copyright (C) 2020 Timm Fitschen <t.fitschen@indiscale.com> -# Copyright (C) 2020 IndiScale GmbH <info@indiscale.com> +# Copyright (C) 2020-2022 IndiScale GmbH <info@indiscale.com> # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as @@ -109,7 +108,7 @@ def retrieve_entities_with_ids(entities): for i in range(len(entities)//step+1): collection.extend( execute_query( - create_id_query(entities[i*step:(i+1)*step]))) + create_id_query(entities[i*step:(i+1)*step]))) return collection @@ -558,7 +557,8 @@ def getCommitIn(folder): return t.readline().strip() -COMPARED = ["name", "role", "datatype", "description", "importance"] +COMPARED = ["name", "role", "datatype", "description", "importance", + "id", "path", "checksum", "size"] def compare_entities(old_entity: Entity, new_entity: Entity): @@ -653,7 +653,8 @@ def compare_entities(old_entity: Entity, new_entity: Entity): olddiff["properties"].pop(prop.name) else: - raise NotImplementedError() + raise NotImplementedError( + "Comparison not implemented for multi-properties.") for prop in new_entity.properties: if len([0 for p in old_entity.properties if p.name == prop.name]) == 0: diff --git a/tox.ini b/tox.ini index 22c89f765c612ff78572ee2cab20dfab2e740e84..b1061a57c6a136cb29f77a1d0c03383ab82ecf8b 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist=py36, py37, py38, py39 +envlist=py36, py37, py38, py39, py310 skip_missing_interpreters = true [testenv] diff --git a/unittests/test_apiutils.py b/unittests/test_apiutils.py index b22a0ba36d4c19c4229bc3df73f29fb8c651dda8..0294646f6c526230a8e9fb722d56aa23a8f9285c 100644 --- a/unittests/test_apiutils.py +++ b/unittests/test_apiutils.py @@ -1,12 +1,11 @@ # -*- encoding: utf-8 -*- # -# ** header v3.0 # This file is a part of the CaosDB Project. # # Copyright (C) 2018 Research Group Biomedical Physics, # Max-Planck-Institute for Dynamics and Self-Organization Göttingen # Copyright (C) 2020 Timm Fitschen <t.fitschen@indiscale.com> -# Copyright (C) 2020 IndiScale GmbH <info@indiscale.com> +# Copyright (C) 2020-2022 IndiScale GmbH <info@indiscale.com> # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as @@ -179,3 +178,55 @@ def test_compare_entities_units(): assert diff_r1["properties"]["test"]["unit"] == "cm" assert diff_r2["properties"]["test"]["unit"] == "m" + + +def test_compare_special_properties(): + # Test for all known special properties: + SPECIAL_PROPERTIES = ("description", "name", + "checksum", "size", "path", "id") + INTS = ("size", "id") + HIDDEN = ("checksum", "size") + + for key in SPECIAL_PROPERTIES: + set_key = key + if key in HIDDEN: + set_key = "_" + key + r1 = db.Record() + r2 = db.Record() + if key not in INTS: + setattr(r1, set_key, "bla 1") + setattr(r2, set_key, "bla 1") + else: + setattr(r1, set_key, 1) + setattr(r2, set_key, 1) + + diff_r1, diff_r2 = compare_entities(r1, r2) + print(diff_r1) + print(diff_r2) + assert key not in diff_r1 + assert key not in diff_r2 + assert len(diff_r1["parents"]) == 0 + assert len(diff_r2["parents"]) == 0 + assert len(diff_r1["properties"]) == 0 + assert len(diff_r2["properties"]) == 0 + + if key not in INTS: + setattr(r2, set_key, "bla test") + else: + setattr(r2, set_key, 2) + + diff_r1, diff_r2 = compare_entities(r1, r2) + print(r1) + print(r2) + print(diff_r1) + print(diff_r2) + assert key in diff_r1 + assert key in diff_r2 + if key not in INTS: + assert diff_r1[key] == "bla 1" + assert diff_r2[key] == "bla test" + else: + assert diff_r1[key] == 1 + assert diff_r2[key] == 2 + assert len(diff_r1["properties"]) == 0 + assert len(diff_r2["properties"]) == 0