From 042ae3c63a3354b504c9e22f6cfa19ac80c87f24 Mon Sep 17 00:00:00 2001
From: fspreck <f.spreckelsen@indiscale.com>
Date: Wed, 23 Mar 2022 09:57:27 +0100
Subject: [PATCH] WIP: Begin integration tests for json-schema datamodel parser

---
 integrationtests/test.sh                      |  3 +
 integrationtests/test_datamodel.schema.json   | 21 ++++++
 .../test_json_schema_datamodel_parser.py      | 71 +++++++++++++++++++
 3 files changed, 95 insertions(+)
 create mode 100644 integrationtests/test_datamodel.schema.json
 create mode 100644 integrationtests/test_json_schema_datamodel_parser.py

diff --git a/integrationtests/test.sh b/integrationtests/test.sh
index 1c0357e2..13d19775 100755
--- a/integrationtests/test.sh
+++ b/integrationtests/test.sh
@@ -82,5 +82,8 @@ python3 -m pytest test_crawl_with_datamodel_problems.py
 echo "Testing table export"
 python3 -m pytest test_base_table_exporter_integration.py
 
+echo "Testing json-schema datamodel parser"
+python3 -m pytest test_json_schema_datamodel_parser.py
+
 # Obsolete due to teardown in the above test.
 # echo "/n/n/n YOU NEED TO RESTART THE SERVER TO REDO TESTS!!!"
diff --git a/integrationtests/test_datamodel.schema.json b/integrationtests/test_datamodel.schema.json
new file mode 100644
index 00000000..d51eecfe
--- /dev/null
+++ b/integrationtests/test_datamodel.schema.json
@@ -0,0 +1,21 @@
+[
+    {
+        "title": "TestTypeWithAtomicProps",
+        "description": "RecordType with scalar atomic properties",
+        "type": "object",
+        "properties": {
+            "simple_text_prop": { "type": "string" }
+        },
+        "required": [ "simple_text_prop" ]
+    },
+    {
+        "title": "TestTypeWithReferencesAndEnum",
+        "type": "object",
+        "properties": {}
+    },
+    {
+        "title": "TestTypeWithLists",
+        "type": "object",
+        "properties": {}
+    }
+]
diff --git a/integrationtests/test_json_schema_datamodel_parser.py b/integrationtests/test_json_schema_datamodel_parser.py
new file mode 100644
index 00000000..46356519
--- /dev/null
+++ b/integrationtests/test_json_schema_datamodel_parser.py
@@ -0,0 +1,71 @@
+#
+# 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 os
+
+import caosdb as db
+from caosadvancedtools.models.parser import parse_model_from_json_schema
+
+
+def _clear_db():
+    ents = db.execute_query("FIND ENTITY WITH ID>99")
+    if ents:
+        ents.delete()
+
+
+def setup_module():
+    _clear_db()
+
+
+def teardown_module():
+    _clear_db()
+
+
+def _load_and_sync(fname):
+    """Load datamodel from json schema in fname and synchronize it without asking.
+
+    """
+    # @author Florian Spreckelsen
+    # @date 2022-03-23
+    fpath = os.path.join(os.path.dirname(os.path.abspath(__file__)), fname)
+    model = parse_model_from_json_schema(fpath)
+    model.sync_data_model(noquestion=True)
+
+
+def test_json_parsed_datamodel():
+    # @author Florian Spreckelsen
+    # @date 2022-03-23
+
+    _load_and_sync("test_datamodel.schema.json")
+
+    # RecordType with atomic properties
+    rt1 = db.execute_query(
+        "FIND RECORDTYPE TestTypeWithAtomicProps", unique=True)
+    assert rt1.description == "RecordType with scalar atomic properties"
+    assert rt1.get_property("simple_text_prop") is not None
+    assert rt1.get_property("simple_text_prop").datatype == db.TEXT
+    assert rt1.get_importance("simple_text_prop") == db.OBLIGATORY
+
+    # RecordType with references and enums
+    rt2 = db.execute_query(
+        "FIND RECORDTYPE TestTypeWithReferencesAndEnum", unique=True)
+
+    # Recordtype with lists
+    rt3 = db.execute_query("FIND RECORDTYPE TestTypeWithLists", unique=True)
-- 
GitLab