From 511aeaa7f36685b5c3ff4da38134d44744e5b0bf Mon Sep 17 00:00:00 2001
From: fspreck <f.spreckelsen@indiscale.com>
Date: Thu, 17 Feb 2022 14:44:57 +0100
Subject: [PATCH] TST: Add unittest for json-schema parser

---
 .../datamodel_string_properties.schema.json   | 13 +++++
 unittests/test_json_schema_model_parser.py    | 48 +++++++++++++++++++
 2 files changed, 61 insertions(+)
 create mode 100644 unittests/datamodel_string_properties.schema.json
 create mode 100644 unittests/test_json_schema_model_parser.py

diff --git a/unittests/datamodel_string_properties.schema.json b/unittests/datamodel_string_properties.schema.json
new file mode 100644
index 00000000..a6b3b6f9
--- /dev/null
+++ b/unittests/datamodel_string_properties.schema.json
@@ -0,0 +1,13 @@
+{
+    "title": "Dataset",
+    "description": "",
+    "type": "object",
+
+    "properties": {
+        "title": { "type": "string", "description": "full dataset title" },
+        "campaign": { "type": "string", "description": "FIXME" },
+        "method": { "type": "string", "description": "FIXME" }
+    },
+
+    "required": ["title"]
+}
diff --git a/unittests/test_json_schema_model_parser.py b/unittests/test_json_schema_model_parser.py
new file mode 100644
index 00000000..46bb8832
--- /dev/null
+++ b/unittests/test_json_schema_model_parser.py
@@ -0,0 +1,48 @@
+#
+# This file is a part of the CaosDB Project.
+#
+# Copyright (C) 2022 IndiScale GmbH <info@indiscale.com>
+# Copyright (C) 2022 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 caosdb as db
+from caosadvancedtools.models.parser import parse_model_from_json_schema
+
+
+def test_rt_with_string_properties():
+    """Test datamodel parsing of datamodel_string_properties.schema.json"""
+
+    model = parse_model_from_json_schema(
+        "datamodel_string_properties.schema.json")
+    assert "Dataset" in model
+    dataset_rt = model["Dataset"]
+    assert dataset.description == ""
+    assert len(dataset.get_properties()) == 3
+
+    assert dataset_rt.get_property("title") != None
+    assert dataset_rt.get_property("campaign") != None
+    assert dataset_rt.get_property("method") != None
+
+    title_prop = dataset_rt.get_property("title")
+    assert title_prop.datatype == db.TEXT
+    assert dataset_rt.get_importance(title_prop.name) == db.OBLIGATORY
+
+    campaign_prop = dataset_rt.get_property("campaign")
+    assert campaign_prop.datatype == db.TEXT
+    assert dataset_rt.get_importance(campaign_prop.name) == db.RECOMMENDED
+
+    method_prop = dataset_rt.get_property("method")
+    assert method_prop.datatype == db.TEXT
+    assert dataset_rt.get_importance(method_prop.name) == db.RECOMMENDED
-- 
GitLab