diff --git a/src/caosadvancedtools/json_schema_exporter.py b/src/caosadvancedtools/json_schema_exporter.py index adaab3b80729ac7300cca9d5e00b547c6c1ee618..bb4d0f227a22ed664d670e2acae1b353738efef5 100644 --- a/src/caosadvancedtools/json_schema_exporter.py +++ b/src/caosadvancedtools/json_schema_exporter.py @@ -20,8 +20,6 @@ # with this program. If not, see <https://www.gnu.org/licenses/>. # -import json - import linkahead as db @@ -43,7 +41,7 @@ def _make_prop_from_prop(prop): return _make_text_property(prop.description, "date-time") json_prop = {} - if prop.description + # if prop.description def _make_text_property(description="", text_format=None, text_pattern=None): @@ -62,7 +60,7 @@ def _make_text_property(description="", text_format=None, text_pattern=None): def recordtype_to_json_schema(rt: db.RecordType, additional_properties: bool = True, - name_and_description_in_properties: bool = True): + name_and_description_in_properties: bool = False): """Create a jsonschema from a given RecordType that can be used, e.g., to validate a json specifying a record of the given type. @@ -75,7 +73,7 @@ def recordtype_to_json_schema(rt: db.RecordType, additional_properties: bool = T schema. Optional, default is True. name_and_description_in_properties : bool, optional Whether to include name and description in the `properties` section of - the schema to be exported. Optional, default is True. + the schema to be exported. Optional, default is False. Returns ------- diff --git a/unittests/test_json_schema_exporter.py b/unittests/test_json_schema_exporter.py new file mode 100644 index 0000000000000000000000000000000000000000..3a0729dded527367c8841f1bf697cb7925f77941 --- /dev/null +++ b/unittests/test_json_schema_exporter.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python +# encoding: utf-8 +# +# This file is a part of the CaosDB Project. +# +# Copyright (C) 2023 Indiscale GmbH <info@indiscale.com> +# Copyright (C) 2023 Florian Spreckelsen <f.spreckelsen@indiscale.com> +# +# This program is free software: you can redistribute it and/or modify it under +# the terms of the GNU Affero General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) any +# later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more +# details. +# +# 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/>. +# + +import linkahead as db + +from caosadvancedtools.json_schema_exporter import recordtype_to_json_schema as rtjs + + +def test_empty_rt(): + + rt = db.RecordType(name="Test", description="descr") + + schema = rtjs(rt) + + assert schema["title"] == rt.name + assert schema["description"] == rt.description + assert len(schema["properties"]) == 0 + assert len(schema["required"]) == 0 + + schema = rtjs(rt, additional_properties=False) + + assert schema["title"] == rt.name + assert schema["description"] == rt.description + assert len(schema["properties"]) == 0 + assert len(schema["required"]) == 0 + assert schema["additionalProperties"] is False + + schema = rtjs(rt, name_and_description_in_properties=True) + + assert len(schema["properties"]) == 2 + assert "name" in schema["properties"] + assert "description" in schema["properties"] + assert schema["properties"]["name"]["type"] == "string" + assert schema["properties"]["description"]["type"] == "string"