Skip to content
Snippets Groups Projects
Verified Commit 67c3f944 authored by Daniel Hornung's avatar Daniel Hornung
Browse files

MAINT: Using array_schema_from_model_schema instead of hard coding.

parent d9be5792
No related branches found
No related tags found
2 merge requests!138Release 0.14.0,!129Enable validation in table_json_conversion.convert.to_dict for use in XLSX-converter
{
"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"
}
{
"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"
}
{
"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"
}
...@@ -30,7 +30,7 @@ from typing import Optional ...@@ -30,7 +30,7 @@ from typing import Optional
import jsonschema import jsonschema
import pytest import pytest
from caosadvancedtools.table_json_conversion import convert from caosadvancedtools.table_json_conversion import convert, xlsx_utils
from .utils import assert_equal_jsons from .utils import assert_equal_jsons
...@@ -53,7 +53,11 @@ Returns ...@@ -53,7 +53,11 @@ Returns
json: dict json: dict
The result of the conversion. 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: if known_good_file:
with open(known_good_file, encoding="utf-8") as myfile: with open(known_good_file, encoding="utf-8") as myfile:
expected = json.load(myfile) expected = json.load(myfile)
...@@ -66,33 +70,33 @@ json: dict ...@@ -66,33 +70,33 @@ json: dict
def test_conversions(): def test_conversions():
"""Test conversion from XLSX to JSON.""" """Test conversion from XLSX to JSON."""
convert_and_compare(xlsx_file=rfp("data/simple_data.xlsx"), 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")) known_good_file=rfp("data/simple_data.json"))
convert_and_compare(xlsx_file=rfp("data/multiple_refs_data.xlsx"), 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")) known_good_file=rfp("data/multiple_refs_data.json"))
convert_and_compare(xlsx_file=rfp("data/indirect_data.xlsx"), 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")) known_good_file=rfp("data/indirect_data.json"))
convert_and_compare(xlsx_file=rfp("data/multiple_choice_data.xlsx"), 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"), known_good_file=rfp("data/multiple_choice_data.json"),
strict=True) strict=True)
convert_and_compare(xlsx_file=rfp("data/simple_data_booleans.xlsx"), 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")) known_good_file=rfp("data/simple_data_booleans.json"))
with open(rfp("data/simple_data.json"), encoding="utf-8") as myfile: with open(rfp("data/simple_data.json"), encoding="utf-8") as myfile:
expected_datetime = json.load(myfile) expected_datetime = json.load(myfile)
expected_datetime["Training"][0]["date"] = datetime.datetime(2023, 1, 1, 0, 0) expected_datetime["Training"][0]["date"] = datetime.datetime(2023, 1, 1, 0, 0)
convert_and_compare(xlsx_file=rfp("data/simple_data_datetime.xlsx"), 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) known_good_file="", known_good_data=expected_datetime)
# Data loss when saving as xlsx # Data loss when saving as xlsx
with pytest.raises(AssertionError) as err: with pytest.raises(AssertionError) as err:
convert_and_compare(xlsx_file=rfp("data/simple_data_ascii_chars.xlsx"), 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")) known_good_file=rfp("data/simple_data_ascii_chars.json"))
assert str(err.value).startswith("Values at path ['Training', 0, ") assert str(err.value).startswith("Values at path ['Training', 0, ")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment