diff --git a/unittests/table_json_conversion/create_jsonschema.py b/unittests/table_json_conversion/create_jsonschema.py index 07264649728f34d9ee6090ed171b5ec151672baf..9585f5458edf8f9d3f785099295a3e675230932c 100755 --- a/unittests/table_json_conversion/create_jsonschema.py +++ b/unittests/table_json_conversion/create_jsonschema.py @@ -67,6 +67,8 @@ def main(): do_not_create=["Organisation"]) prepare_datamodel("data/multiple_refs_model.yml", ["Training", "Person"], "data/multiple_refs_schema.json") + prepare_datamodel("data/indirect_model.yml", ["Wrapper"], + "data/indirect_schema.json") if __name__ == "__main__": diff --git a/unittests/table_json_conversion/data/indirect_data.json b/unittests/table_json_conversion/data/indirect_data.json new file mode 100644 index 0000000000000000000000000000000000000000..c77dd1ff2a703af6b6b2a0db19f450ac10616d9b --- /dev/null +++ b/unittests/table_json_conversion/data/indirect_data.json @@ -0,0 +1,18 @@ +{ + "Wrapper": { + "Results": [ + { + "year": 2022, + "avg_score": 2.4 + }, + { + "year": 2023, + "avg_score": 4.2 + } + ], + "Training": { + "name": "Basic Training", + "url": "www.example.com/training/basic" + } + } +} diff --git a/unittests/table_json_conversion/data/indirect_model.yml b/unittests/table_json_conversion/data/indirect_model.yml new file mode 100644 index 0000000000000000000000000000000000000000..2a7f4f98ff9a46478eb631e6990deceadc9a498c --- /dev/null +++ b/unittests/table_json_conversion/data/indirect_model.yml @@ -0,0 +1,18 @@ +Training: + recommended_properties: + url: + datatype: TEXT + description: 'The URL' +Results: + description: "Results for a training" + recommended_properties: + year: + datatype: INTEGER + avg_score: + description: The average score for the linked training. + datatype: DOUBLE +Wrapper: + recommended_properties: + Training: + Results: + datatype: LIST<Results> diff --git a/unittests/table_json_conversion/data/indirect_schema.json b/unittests/table_json_conversion/data/indirect_schema.json new file mode 100644 index 0000000000000000000000000000000000000000..64b6ff279c584456fe1f90454225d144c90e014d --- /dev/null +++ b/unittests/table_json_conversion/data/indirect_schema.json @@ -0,0 +1,63 @@ +{ + "type": "object", + "properties": { + "Wrapper": { + "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/indirect_template.xlsx b/unittests/table_json_conversion/data/indirect_template.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..cc614acb75b36e10143a29f28dff9fce7d5e006f Binary files /dev/null and b/unittests/table_json_conversion/data/indirect_template.xlsx differ diff --git a/unittests/table_json_conversion/test_fill_xlsx.py b/unittests/table_json_conversion/test_fill_xlsx.py index edc723d8db18e2f264811e8d4c2ac28d26d92b88..4af73e4d2125d05c4379bd88c618ae27545d5221 100644 --- a/unittests/table_json_conversion/test_fill_xlsx.py +++ b/unittests/table_json_conversion/test_fill_xlsx.py @@ -78,6 +78,10 @@ def test_fill_xlsx(): template_file=rfp("data/multiple_refs_template.xlsx"), known_good=rfp("data/multiple_refs_data.xlsx"), schema=rfp("data/multiple_refs_schema.json")) + fill_and_compare(json_file=rfp("data/indirect_data.json"), + template_file=rfp("data/indirect_template.xlsx"), + known_good=rfp("data/multiple_refs_data.xlsx"), + schema=rfp("data/indirect_schema.json")) def test_errors(): diff --git a/unittests/table_json_conversion/test_table_template_generator.py b/unittests/table_json_conversion/test_table_template_generator.py index b7a2dafcca6f8e467b3bdc05f46adf333619e97b..19cdff2a7fcbb091ca665ee1f27f9d9e27a8a51f 100644 --- a/unittests/table_json_conversion/test_table_template_generator.py +++ b/unittests/table_json_conversion/test_table_template_generator.py @@ -232,6 +232,25 @@ def test_model_with_multiple_refs(): _compare_generated_to_known_good( schema_file=rfp("data/multiple_refs_schema.json"), known_good=rfp("data/multiple_refs_template.xlsx"), - foreign_keys={'Training': {"__this__": ['date', 'url'], + foreign_keys={"Training": {"__this__": ["date", "url"], "Organisation": ["name"]}}, outfile=None) + + +def test_model_with_indirect_reference(): + _compare_generated_to_known_good( + schema_file=rfp("data/indirect_schema.json"), + known_good=rfp("data/indirect_template.xlsx"), + foreign_keys={"Wrapper": ["Training.name", "Training.url"]}, + outfile=None) + + +def test_exceptions(): + # Foreign keys must be lists + with pytest.raises(ValueError, match="Foreign keys must be a list of strings, but a single " + r"string was given:\n\['Wrapper'\] -> name"): + _compare_generated_to_known_good( + schema_file=rfp("data/indirect_schema.json"), + known_good=rfp("data/multiple_refs_template.xlsx"), + foreign_keys={"Wrapper": {"__this__": "name"}}, + outfile=None)