Skip to content
Snippets Groups Projects
Commit 9fdfa254 authored by Florian Spreckelsen's avatar Florian Spreckelsen
Browse files

TST: Begin unit tests for json schema exporter

parent 4c27d928
No related branches found
No related tags found
2 merge requests!89ENH: JsonSchemaExporter accepts do_not_create parameter.,!80F simple schema export
Pipeline #42096 failed
......@@ -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
-------
......
#!/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"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment