diff --git a/src/caosadvancedtools/table_json_conversion/fill_xlsx.py b/src/caosadvancedtools/table_json_conversion/fill_xlsx.py index 92fae16c411639d0a0bd6c5566b34740b8fcd114..1f39f66d38c27c41389ce14a184671a4be23271b 100644 --- a/src/caosadvancedtools/table_json_conversion/fill_xlsx.py +++ b/src/caosadvancedtools/table_json_conversion/fill_xlsx.py @@ -355,7 +355,7 @@ validation_schema: dict, optional # Validation if validation_schema is not None: - # convert to array_schema is given schema is a model_schema + # convert to array_schema if given schema is a model_schema if 'properties' in validation_schema and validation_schema['properties'].values(): if list(validation_schema['properties'].values())[0]["type"] != "array": validation_schema = array_schema_from_model_schema(read_or_dict(validation_schema)) diff --git a/unittests/table_json_conversion/test_fill_xlsx.py b/unittests/table_json_conversion/test_fill_xlsx.py index f77131bc7e99e254b3b012586450734024720811..084f19baccedb6778ceb4ff67bf21dbe0b8e66ac 100644 --- a/unittests/table_json_conversion/test_fill_xlsx.py +++ b/unittests/table_json_conversion/test_fill_xlsx.py @@ -196,6 +196,32 @@ def test_errors(): known_good=rfp("data/simple_data.xlsx"), schema=rfp("data/simple_schema.json")) assert exc.value.message == "0.5 is not of type 'integer'" + # Check wrong data + with open(rfp("data/simple_data.json")) as json_file: + json_data = json.load(json_file) + json_data["Training"][0]["date"] = "2023-01" + with tempfile.NamedTemporaryFile(suffix='.json', mode='w+t') as temp_file: + json.dump(json_data, temp_file) + temp_file.seek(0) + with pytest.raises(AssertionError) as exc: + fill_and_compare(json_file=temp_file.name, + template_file=rfp("data/simple_template.xlsx"), + known_good=rfp("data/simple_data.xlsx"), + schema=rfp("data/simple_schema.json")) + assert "Training" in str(exc) and "2023-01" in str(exc) + # Check wrong schema + with open(rfp("data/simple_schema.json")) as json_file: + json_schema = json.load(json_file) + json_schema["properties"]["Person"]["properties"]["given_name"]["type"] = "integer" + with tempfile.NamedTemporaryFile(suffix='.json', mode='w+t') as temp_file: + json.dump(json_schema, temp_file) + temp_file.seek(0) + with pytest.raises(schema_exc.ValidationError) as exc: + fill_and_compare(json_file=rfp("data/simple_data.json"), + template_file=rfp("data/simple_template.xlsx"), + known_good=rfp("data/simple_data.xlsx"), + schema=temp_file.name) + assert "integer" in str(exc) def test_data_schema_generation(): diff --git a/unittests/table_json_conversion/test_read_xlsx.py b/unittests/table_json_conversion/test_read_xlsx.py index d453ab3593ec36aa1197727f5ed51d1fb6fea10f..10b462df83a68a6a51088170f8d7c0216bd495e5 100644 --- a/unittests/table_json_conversion/test_read_xlsx.py +++ b/unittests/table_json_conversion/test_read_xlsx.py @@ -24,6 +24,7 @@ import datetime import json import os import re +import tempfile from types import SimpleNamespace from typing import Optional @@ -43,7 +44,7 @@ def rfp(*pathcomponents): def convert_and_compare(xlsx_file: str, schema_file: str, known_good_file: str, known_good_data: Optional[dict] = None, strict: bool = False, - validate: bool = False) -> dict: + validate: bool = True) -> dict: """Convert an XLSX file and compare to a known result. Exactly one of ``known_good_file`` and ``known_good_data`` should be non-empty. @@ -57,7 +58,7 @@ json: dict 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) + result = convert.to_dict(xlsx=xlsx_file, schema=data_schema, validate=validate) if known_good_file: with open(known_good_file, encoding="utf-8") as myfile: expected = json.load(myfile) @@ -101,6 +102,33 @@ def test_conversions(): assert str(err.value).startswith("Values at path ['Training', 0, ") +def test_validation(): + # Check wrong data + with open(rfp("data/simple_data.json")) as json_file: + known_good = json.load(json_file) + known_good["Training"][0]["date"] = "2023-01-02" + with tempfile.NamedTemporaryFile(suffix='.json', mode='w+t') as temp_file: + json.dump(known_good, temp_file) + temp_file.seek(0) + with pytest.raises(AssertionError) as exc: + convert_and_compare(xlsx_file=rfp("data/simple_data.xlsx"), + schema_file=rfp("data/simple_schema.json"), + known_good_file=temp_file.name) + assert "Training" in str(exc) and "2023-01-02" in str(exc) + # Check wrong schema + with open(rfp("data/simple_schema.json")) as json_file: + json_schema = json.load(json_file) + json_schema["properties"]["Person"]["properties"]["given_name"]["type"] = "integer" + with tempfile.NamedTemporaryFile(suffix='.json', mode='w+t') as temp_file: + json.dump(json_schema, temp_file) + temp_file.seek(0) + with pytest.raises(jsonschema.ValidationError) as exc: + convert_and_compare(xlsx_file=rfp("data/simple_data.xlsx"), + schema_file=temp_file.name, + known_good_file=rfp("data/simple_data.json")) + assert "integer" in str(exc) + + def test_missing_columns(): with pytest.raises(ValueError) as caught: convert.to_dict(xlsx=rfp("data/simple_data_missing.xlsx"),