From a0332e0918ad3a7a1aac80a5694484c5d059b9a7 Mon Sep 17 00:00:00 2001 From: Daniel <d.hornung@indiscale.com> Date: Tue, 15 Apr 2025 17:23:51 +0200 Subject: [PATCH] TEST: Try casting before comparison. --- integrationtests/test_ex_import_xlsx.py | 11 ++++++----- unittests/table_json_conversion/__init__.py | 0 unittests/table_json_conversion/utils.py | 17 +++++++++++++++-- 3 files changed, 21 insertions(+), 7 deletions(-) delete mode 100644 unittests/table_json_conversion/__init__.py diff --git a/integrationtests/test_ex_import_xlsx.py b/integrationtests/test_ex_import_xlsx.py index 9a9a9a06..15fc437a 100755 --- a/integrationtests/test_ex_import_xlsx.py +++ b/integrationtests/test_ex_import_xlsx.py @@ -47,6 +47,7 @@ from utils import ( # noqa: E402, pylint: disable=wrong-import-position set_test_key("_CAOSDB_ADV_TEST_SUITE") + def rfp(*pathcomponents): """ Return full path. @@ -166,11 +167,11 @@ def setup(clear_database): .add_property(test_prop_2, value=0) .add_property(test_prop_3, value=test_person_2)) test_conference_0 = (db.Record(description="Only for Also").add_parent(test_rt_2) - .add_property(test_prop_4, value=[test_person_1])) + .add_property(test_prop_4, value=[test_person_1])) test_conference_1 = (db.Record(name="Official Conf", description="For everyone") - .add_parent(test_rt_2) - .add_property(test_prop_4, - value=[test_person_0, test_person_1, test_person_2])) + .add_parent(test_rt_2) + .add_property(test_prop_4, + value=[test_person_0, test_person_1, test_person_2])) testdata = [test_rt_0, test_rt_1, test_rt_2, test_prop_0, test_prop_1, test_prop_2, test_prop_3, test_prop_4, @@ -183,6 +184,7 @@ def setup(clear_database): c.extend(testdata) c.insert() + def test_successful_export(): records = next(db.execute_query("FIND Record", page_length=50)) tmp_path = Path('temp_test_successful_export.xlsx') @@ -252,7 +254,6 @@ def test_export_list_refs(tmpdir): # Check: Filled XLSX filled_generated = load_workbook(tmpdir / "result.xlsx") # For the moment: just check a few samples - assert filled_generated.get_sheet_names() == ['Training', 'Training.Organisation', 'Training.Organisation.Person', diff --git a/unittests/table_json_conversion/__init__.py b/unittests/table_json_conversion/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/unittests/table_json_conversion/utils.py b/unittests/table_json_conversion/utils.py index da3d17cf..846809d8 100644 --- a/unittests/table_json_conversion/utils.py +++ b/unittests/table_json_conversion/utils.py @@ -21,6 +21,7 @@ """Utilities for the tests. """ +from datetime import datetime from typing import Iterable, Union from openpyxl import Workbook @@ -48,7 +49,8 @@ Raise an assertion exception if they are not equal.""" assert_equal_jsons(el1, el2, allow_none=allow_none, allow_empty=allow_empty, path=this_path) continue - assert el1 == el2, f"Values at path {this_path} are not equal:\n{el1},\n{el2}" + assert equals_with_casting(el1, el2), ( + f"Values at path {this_path} are not equal:\n{el1},\n{el2}") continue # Case 2: exists only in one collection existing = json1.get(key, json2.get(key)) @@ -66,7 +68,18 @@ Raise an assertion exception if they are not equal.""" assert_equal_jsons(el1, el2, allow_none=allow_none, allow_empty=allow_empty, path=this_path) else: - assert el1 == el2, f"Values at path {this_path} are not equal:\n{el1},\n{el2}" + assert equals_with_casting(el1, el2), ( + f"Values at path {this_path} are not equal:\n{el1},\n{el2}") + + +def equals_with_casting(value1, value2) -> bool: + """Compare two values, return True if equal, False otherwise. Try to cast to clever datatypes. + """ + try: + return datetime.fromisoformat(value1) == datetime.fromisoformat(value2) + except (ValueError, TypeError): + pass + return value1 == value2 def purge_from_json(data: Union[dict, list], remove_keys: list[str]) -> Union[dict, list]: -- GitLab