diff --git a/unittests/table_json_conversion/create_jsonschema.py b/unittests/table_json_conversion/create_jsonschema.py index 6f556863bf12e1c9001be95cc7213a3e875a766e..61e4eb20964932d4e50b766868137b3538f7c26d 100755 --- a/unittests/table_json_conversion/create_jsonschema.py +++ b/unittests/table_json_conversion/create_jsonschema.py @@ -16,50 +16,57 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <https://www.gnu.org/licenses/>. -"""Create JSON-Schema according to configuration +"""Create JSON-Schema according to configuration. + """ import argparse import json -import os -import sys +from typing import List import caosadvancedtools.json_schema_exporter as jsex -import caosadvancedtools.models.parser as parser -import tomli - -# TODO why do I need a running LA instance? +from caosadvancedtools.models import parser +# import tomli -def prepare_datamodel(): - model = parser.parse_model_from_yaml("./model.yml") +def prepare_datamodel(modelfile, recordtypes: List[str], outfile: str, + do_not_create: List[str] = None): + if do_not_create is None: + do_not_create = [] + model = parser.parse_model_from_yaml(modelfile) exporter = jsex.JsonSchemaExporter(additional_properties=False, # additional_options_for_text_props=additional_text_options, # name_and_description_in_properties=True, name_property_for_new_records=True, - do_not_create=["Organisation"], + do_not_create=do_not_create, # do_not_retrieve=do_not_retrieve, + no_remote=True, + use_rt_pool=model, ) - schema_top = exporter.recordtype_to_json_schema(model.get_deep("Training")) - schema_pers = exporter.recordtype_to_json_schema(model.get_deep("Person")) - merged_schema = jsex.merge_schemas([schema_top, schema_pers]) + schemas = [] + for recordtype in recordtypes: + schemas.append(exporter.recordtype_to_json_schema(model.get_deep(recordtype))) + merged_schema = jsex.merge_schemas(schemas) - with open("model_schema.json", mode="w", encoding="utf8") as json_file: + with open(outfile, mode="w", encoding="utf8") as json_file: json.dump(merged_schema, json_file, ensure_ascii=False, indent=2) def _parse_arguments(): """Parse the arguments.""" - parser = argparse.ArgumentParser(description='') + arg_parser = argparse.ArgumentParser(description='') - return parser.parse_args() + return arg_parser.parse_args() def main(): """The main function of this script.""" - args = _parse_arguments() - prepare_datamodel() + _ = _parse_arguments() + prepare_datamodel("model_simple.yml", ["Training", "Person"], "model_schema.json", + do_not_create=["Organisation"]) + prepare_datamodel("model_multiple_refs.yml", ["Training", "Person"], + "schema_multiple_refs.json") if __name__ == "__main__": diff --git a/unittests/table_json_conversion/example.json b/unittests/table_json_conversion/example.json index 8d08fd09d168a85641d8a9c3cf9776b8d1c866b2..9997f17e76a46d5e97d842fdee40626047e7a347 100644 --- a/unittests/table_json_conversion/example.json +++ b/unittests/table_json_conversion/example.json @@ -1,30 +1,32 @@ { - "Training": { - "date": "2023-01-01", - "url": "www.indiscale.com", - "coach": [ - { - "family_name": "Sky", - "given_name": "Max", - "Organisation": "ECB" - },{ - "family_name": "Sky", - "given_name": "Min", - "Organisation": "ECB" - }], - "supervisor": { - "family_name": "Steve", - "given_name": "Stevie", + "Training": { + "date": "2023-01-01", + "url": "www.indiscale.com", + "coach": [ + { + "family_name": "Sky", + "given_name": "Max", + "Organisation": "ECB" + }, + { + "family_name": "Sky", + "given_name": "Min", + "Organisation": "ECB" + } + ], + "supervisor": { + "family_name": "Steve", + "given_name": "Stevie", "Organisation": "IMF" - }, - "duration": 1.0, - "participants": 1, - "subjects": ["Math", "Physics"], - "remote": false }, - "Person": { - "family_name": "Steve", - "given_name": "Stevie", - "Organisation": "IMF" - } + "duration": 1.0, + "participants": 1, + "subjects": ["Math", "Physics"], + "remote": false + }, + "Person": { + "family_name": "Steve", + "given_name": "Stevie", + "Organisation": "IMF" + } } diff --git a/unittests/table_json_conversion/model_multiple_refs.yml b/unittests/table_json_conversion/model_multiple_refs.yml new file mode 100644 index 0000000000000000000000000000000000000000..c95a97aca34137588aa3cd48458935d4cc2d7511 --- /dev/null +++ b/unittests/table_json_conversion/model_multiple_refs.yml @@ -0,0 +1,33 @@ +Person: + recommended_properties: + full_name: + datatype: TEXT + email: + datatype: TEXT + Organisation: +Training: + recommended_properties: + date: + datatype: DATETIME + description: 'The date of the training.' + url: + datatype: TEXT + description: 'The URL' + trainer: + datatype: LIST<Person> + participant: + datatype: LIST<Person> + supervisor: + datatype: Person + responsible: + datatype: Person + supervisor_inherit: + inherit_from_suggested: + - Person + responsible_inherit: + inherit_from_suggested: + - Person +Organisation: + recommended_properties: + Country: + datatype: TEXT diff --git a/unittests/table_json_conversion/model_schema.json b/unittests/table_json_conversion/model_schema.json index 6f2fffdea938da3e4e3a39397a1ddbfbaa47724c..f18fd6afd396d8f6c25e5db5f8570e89b40ab05a 100644 --- a/unittests/table_json_conversion/model_schema.json +++ b/unittests/table_json_conversion/model_schema.json @@ -91,6 +91,10 @@ }, "remote": { "type": "boolean" + }, + "slides": { + "type": "string", + "format": "data-url" } }, "$schema": "https://json-schema.org/draft/2020-12/schema" diff --git a/unittests/table_json_conversion/model.yml b/unittests/table_json_conversion/model_simple.yml similarity index 100% rename from unittests/table_json_conversion/model.yml rename to unittests/table_json_conversion/model_simple.yml diff --git a/unittests/table_json_conversion/schema_multiple_refs.json b/unittests/table_json_conversion/schema_multiple_refs.json new file mode 100644 index 0000000000000000000000000000000000000000..5678f72c242f3a4badd1db7a1ad7613acba5d83b --- /dev/null +++ b/unittests/table_json_conversion/schema_multiple_refs.json @@ -0,0 +1,279 @@ +{ + "type": "object", + "properties": { + "Training": { + "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" + }, + "Organisation": { + "type": "object", + "required": [], + "additionalProperties": false, + "title": "Organisation", + "properties": { + "name": { + "type": "string", + "description": "The name of the Record to be created" + }, + "Country": { + "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" + }, + "Organisation": { + "type": "object", + "required": [], + "additionalProperties": false, + "title": "Organisation", + "properties": { + "name": { + "type": "string", + "description": "The name of the Record to be created" + }, + "Country": { + "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" + }, + "Organisation": { + "type": "object", + "required": [], + "additionalProperties": false, + "title": "Organisation", + "properties": { + "name": { + "type": "string", + "description": "The name of the Record to be created" + }, + "Country": { + "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": "object", + "required": [], + "additionalProperties": false, + "title": "Organisation", + "properties": { + "name": { + "type": "string", + "description": "The name of the Record to be created" + }, + "Country": { + "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" + }, + "Organisation": { + "type": "object", + "required": [], + "additionalProperties": false, + "title": "Organisation", + "properties": { + "name": { + "type": "string", + "description": "The name of the Record to be created" + }, + "Country": { + "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" + }, + "Organisation": { + "type": "object", + "required": [], + "additionalProperties": false, + "title": "Organisation", + "properties": { + "name": { + "type": "string", + "description": "The name of the Record to be created" + }, + "Country": { + "type": "string" + } + } + } + } + } + }, + "$schema": "https://json-schema.org/draft/2020-12/schema" + }, + "Person": { + "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" + }, + "Organisation": { + "type": "object", + "required": [], + "additionalProperties": false, + "title": "Organisation", + "properties": { + "name": { + "type": "string", + "description": "The name of the Record to be created" + }, + "Country": { + "type": "string" + } + } + } + }, + "$schema": "https://json-schema.org/draft/2020-12/schema" + } + }, + "required": [ + "Training", + "Person" + ], + "additionalProperties": false, + "$schema": "https://json-schema.org/draft/2020-12/schema" +} \ No newline at end of file diff --git a/unittests/table_json_conversion/test_fill_xlsx.py b/unittests/table_json_conversion/test_fill_xlsx.py index a1458fc244f5360e384e4371401cf8a033928797..10ddcc32dad9a95d02fe5e6f1f593f9db26e0330 100644 --- a/unittests/table_json_conversion/test_fill_xlsx.py +++ b/unittests/table_json_conversion/test_fill_xlsx.py @@ -44,6 +44,6 @@ def test_detect(): def test_fill_xlsx(): path = os.path.join(tempfile.mkdtemp(), 'test.xlsx') assert not os.path.exists(path) - fill_template(rfp('example_template.xlsx'), rfp('example.json'), path) + fill_template(data=rfp('example.json'), template=rfp('example_template.xlsx'), result=path) assert os.path.exists(path) generated = load_workbook(path) # workbook can be read