From 67c3f944e194a406b68c79a32770b36cefac702c Mon Sep 17 00:00:00 2001 From: Daniel <d.hornung@indiscale.com> Date: Wed, 26 Feb 2025 12:51:59 +0100 Subject: [PATCH] MAINT: Using array_schema_from_model_schema instead of hard coding. --- .../data/indirect_data_schema.json | 66 ------ .../data/multiple_choice_data_schema.json | 60 ----- .../data/multiple_refs_data_schema.json | 216 ------------------ .../table_json_conversion/test_read_xlsx.py | 22 +- 4 files changed, 13 insertions(+), 351 deletions(-) delete mode 100644 unittests/table_json_conversion/data/indirect_data_schema.json delete mode 100644 unittests/table_json_conversion/data/multiple_choice_data_schema.json delete mode 100644 unittests/table_json_conversion/data/multiple_refs_data_schema.json diff --git a/unittests/table_json_conversion/data/indirect_data_schema.json b/unittests/table_json_conversion/data/indirect_data_schema.json deleted file mode 100644 index f20eddaf..00000000 --- a/unittests/table_json_conversion/data/indirect_data_schema.json +++ /dev/null @@ -1,66 +0,0 @@ -{ - "type": "object", - "properties": { - "Wrapper": { - "type": "array", - "items": { - "type": "object", - "required": [], - "additionalProperties": false, - "title": "Wrapper", - "properties": { - "name": { - "type": "string", - "description": "The name of the Record to be created" - }, - "Training": { - "type": "object", - "required": [], - "additionalProperties": false, - "title": "Training", - "properties": { - "name": { - "type": "string", - "description": "The name of the Record to be created" - }, - "url": { - "type": "string", - "description": "The URL" - } - } - }, - "Results": { - "description": "Results for a training", - "type": "array", - "items": { - "type": "object", - "required": [], - "additionalProperties": false, - "description": "Results for a training", - "title": "Results", - "properties": { - "name": { - "type": "string", - "description": "The name of the Record to be created" - }, - "year": { - "type": "integer" - }, - "avg_score": { - "description": "The average score for the linked training.", - "type": "number" - } - } - } - } - }, - "$schema": "https://json-schema.org/draft/2020-12/schema" - } - } - }, - "required": [ - "Wrapper" - ], - "additionalProperties": false, - "$schema": "https://json-schema.org/draft/2020-12/schema" -} diff --git a/unittests/table_json_conversion/data/multiple_choice_data_schema.json b/unittests/table_json_conversion/data/multiple_choice_data_schema.json deleted file mode 100644 index 4a881260..00000000 --- a/unittests/table_json_conversion/data/multiple_choice_data_schema.json +++ /dev/null @@ -1,60 +0,0 @@ -{ - "type": "object", - "properties": { - "Training": { - "type": "array", - "items": { - "type": "object", - "required": [], - "additionalProperties": false, - "title": "Training", - "properties": { - "name": { - "type": "string", - "description": "The name of the Record to be created" - }, - "date": { - "description": "The date of the training.", - "anyOf": [ - { - "type": "string", - "format": "date" - }, - { - "type": "string", - "format": "date-time" - } - ] - }, - "skills": { - "description": "Skills that are trained.", - "type": "array", - "items": { - "enum": [ - "Planning", - "Communication", - "Evaluation" - ] - }, - "uniqueItems": true - }, - "exam_types": { - "type": "array", - "items": { - "enum": [ - "Oral", - "Written" - ] - }, - "uniqueItems": true - } - } - } - } - }, - "required": [ - "Training" - ], - "additionalProperties": false, - "$schema": "https://json-schema.org/draft/2020-12/schema" -} diff --git a/unittests/table_json_conversion/data/multiple_refs_data_schema.json b/unittests/table_json_conversion/data/multiple_refs_data_schema.json deleted file mode 100644 index 7971ad2a..00000000 --- a/unittests/table_json_conversion/data/multiple_refs_data_schema.json +++ /dev/null @@ -1,216 +0,0 @@ -{ - "type": "object", - "properties": { - "Training": { - "type": "array", - "items": { - "type": "object", - "required": [], - "additionalProperties": false, - "title": "Training", - "properties": { - "name": { - "type": "string", - "description": "The name of the Record to be created" - }, - "date": { - "description": "The date of the training.", - "anyOf": [ - { - "type": "string", - "format": "date" - }, - { - "type": "string", - "format": "date-time" - } - ] - }, - "url": { - "type": "string", - "description": "The URL" - }, - "trainer": { - "type": "array", - "items": { - "type": "object", - "required": [], - "additionalProperties": false, - "title": "trainer", - "properties": { - "name": { - "type": "string", - "description": "The name of the Record to be created" - }, - "full_name": { - "type": "string" - }, - "email": { - "type": "string" - } - } - } - }, - "participant": { - "type": "array", - "items": { - "type": "object", - "required": [], - "additionalProperties": false, - "title": "participant", - "properties": { - "name": { - "type": "string", - "description": "The name of the Record to be created" - }, - "full_name": { - "type": "string" - }, - "email": { - "type": "string" - } - } - } - }, - "supervisor": { - "type": "object", - "required": [], - "additionalProperties": false, - "title": "supervisor", - "properties": { - "name": { - "type": "string", - "description": "The name of the Record to be created" - }, - "full_name": { - "type": "string" - }, - "email": { - "type": "string" - } - } - }, - "responsible": { - "type": "object", - "required": [], - "additionalProperties": false, - "title": "responsible", - "properties": { - "name": { - "type": "string", - "description": "The name of the Record to be created" - }, - "full_name": { - "type": "string" - }, - "email": { - "type": "string" - } - } - }, - "Organisation": { - "type": "array", - "items": { - "type": "object", - "required": [], - "additionalProperties": false, - "title": "Organisation", - "properties": { - "name": { - "type": "string", - "description": "The name of the Record to be created" - }, - "Country": { - "type": "string" - }, - "Person": { - "type": "array", - "items": { - "type": "object", - "required": [], - "additionalProperties": false, - "title": "Person", - "properties": { - "name": { - "type": "string", - "description": "The name of the Record to be created" - }, - "full_name": { - "type": "string" - }, - "email": { - "type": "string" - } - } - } - } - } - } - }, - "supervisor_inherit": { - "type": "object", - "required": [], - "additionalProperties": false, - "title": "supervisor_inherit", - "properties": { - "name": { - "type": "string", - "description": "The name of the Record to be created" - }, - "full_name": { - "type": "string" - }, - "email": { - "type": "string" - } - } - }, - "responsible_inherit": { - "type": "object", - "required": [], - "additionalProperties": false, - "title": "responsible_inherit", - "properties": { - "name": { - "type": "string", - "description": "The name of the Record to be created" - }, - "full_name": { - "type": "string" - }, - "email": { - "type": "string" - } - } - } - }, - "$schema": "https://json-schema.org/draft/2020-12/schema" - } - }, - "Person": { - "type": "array", - "items": { - "type": "object", - "required": [], - "additionalProperties": false, - "title": "Person", - "properties": { - "name": { - "type": "string", - "description": "The name of the Record to be created" - }, - "full_name": { - "type": "string" - }, - "email": { - "type": "string" - } - }, - "$schema": "https://json-schema.org/draft/2020-12/schema" - } - } - }, - "required": [], - "additionalProperties": false, - "$schema": "https://json-schema.org/draft/2020-12/schema" -} diff --git a/unittests/table_json_conversion/test_read_xlsx.py b/unittests/table_json_conversion/test_read_xlsx.py index ae49f82a..d453ab35 100644 --- a/unittests/table_json_conversion/test_read_xlsx.py +++ b/unittests/table_json_conversion/test_read_xlsx.py @@ -30,7 +30,7 @@ from typing import Optional import jsonschema import pytest -from caosadvancedtools.table_json_conversion import convert +from caosadvancedtools.table_json_conversion import convert, xlsx_utils from .utils import assert_equal_jsons @@ -53,7 +53,11 @@ Returns json: dict The result of the conversion. """ - result = convert.to_dict(xlsx=xlsx_file, schema=schema_file, validate=True) + with open(schema_file, encoding="utf8", mode="r") as sch_f: + model_schema = json.load(sch_f) + data_schema = xlsx_utils.array_schema_from_model_schema(model_schema) + + result = convert.to_dict(xlsx=xlsx_file, schema=data_schema, validate=True) if known_good_file: with open(known_good_file, encoding="utf-8") as myfile: expected = json.load(myfile) @@ -66,33 +70,33 @@ json: dict def test_conversions(): """Test conversion from XLSX to JSON.""" convert_and_compare(xlsx_file=rfp("data/simple_data.xlsx"), - schema_file=rfp("data/simple_data_schema.json"), + schema_file=rfp("data/simple_schema.json"), known_good_file=rfp("data/simple_data.json")) convert_and_compare(xlsx_file=rfp("data/multiple_refs_data.xlsx"), - schema_file=rfp("data/multiple_refs_data_schema.json"), + schema_file=rfp("data/multiple_refs_schema.json"), known_good_file=rfp("data/multiple_refs_data.json")) convert_and_compare(xlsx_file=rfp("data/indirect_data.xlsx"), - schema_file=rfp("data/indirect_data_schema.json"), + schema_file=rfp("data/indirect_schema.json"), known_good_file=rfp("data/indirect_data.json")) convert_and_compare(xlsx_file=rfp("data/multiple_choice_data.xlsx"), - schema_file=rfp("data/multiple_choice_data_schema.json"), + schema_file=rfp("data/multiple_choice_schema.json"), known_good_file=rfp("data/multiple_choice_data.json"), strict=True) convert_and_compare(xlsx_file=rfp("data/simple_data_booleans.xlsx"), - schema_file=rfp("data/simple_data_schema.json"), + schema_file=rfp("data/simple_schema.json"), known_good_file=rfp("data/simple_data_booleans.json")) with open(rfp("data/simple_data.json"), encoding="utf-8") as myfile: expected_datetime = json.load(myfile) expected_datetime["Training"][0]["date"] = datetime.datetime(2023, 1, 1, 0, 0) convert_and_compare(xlsx_file=rfp("data/simple_data_datetime.xlsx"), - schema_file=rfp("data/simple_data_schema.json"), + schema_file=rfp("data/simple_schema.json"), known_good_file="", known_good_data=expected_datetime) # Data loss when saving as xlsx with pytest.raises(AssertionError) as err: convert_and_compare(xlsx_file=rfp("data/simple_data_ascii_chars.xlsx"), - schema_file=rfp("data/simple_data_schema.json"), + schema_file=rfp("data/simple_schema.json"), known_good_file=rfp("data/simple_data_ascii_chars.json")) assert str(err.value).startswith("Values at path ['Training', 0, ") -- GitLab