From 42b22d67fa4f7f526270d29befc15b4fd057ad9c Mon Sep 17 00:00:00 2001
From: florian <f.spreckelsen@inidscale.com>
Date: Tue, 3 May 2022 17:14:33 +0200
Subject: [PATCH] TST: Extend json cfood in unit tests

---
 unittests/test_converters.py                  |  7 ++-
 .../examples_json/jsontest_cfood.yml          | 55 +++++++++++++++++--
 .../examples_json/testjson.json               | 17 ++++--
 .../examples_json/testjson.schema.json        | 41 +++++++-------
 unittests/test_json.py                        | 19 ++++---
 5 files changed, 99 insertions(+), 40 deletions(-)

diff --git a/unittests/test_converters.py b/unittests/test_converters.py
index 100b1006..fcc89f2e 100644
--- a/unittests/test_converters.py
+++ b/unittests/test_converters.py
@@ -203,9 +203,10 @@ def test_json_converter(converter_registry):
     assert children[2].name == "archived"
     assert children[2].value.__class__ == bool
 
-    assert children[3].__class__ == DictDictElement
-    assert children[3].name == "coordinator"
-    assert children[3].value.__class__ == dict
+    assert children[3].__class__ == DictListElement
+    assert children[3].name == "Person"
+    assert children[3].value.__class__ == list
+    assert len(children[3].value) == 2
 
     assert children[4].__class__ == DictTextElement
     assert children[4].name == "start_date"
diff --git a/unittests/test_directories/examples_json/jsontest_cfood.yml b/unittests/test_directories/examples_json/jsontest_cfood.yml
index bcf79a2d..f1eb6a9f 100644
--- a/unittests/test_directories/examples_json/jsontest_cfood.yml
+++ b/unittests/test_directories/examples_json/jsontest_cfood.yml
@@ -3,13 +3,56 @@ JSONTest:  # name of the converter
   type: JSONFile
   match: '(.*)'
   validate: ./testjson.schema.json
-  subtree: 
-    element:  # name of the first subtree element which is a converter
+  records:
+    Project:  # this is an identifiable in this case
+      parents:
+        - Project  # not needed as the name is equivalent
+  subtree:
+    name_element:
+      type: DictTextElement
+      match_name: "name"
+      match_value: "(?P<name>.*)"
+      records:
+        Project:
+          name: $name
+    url_element:  # name of the first subtree element which is a converter
       type: DictTextElement
       match_value: "(?P<url>.*)"
       match_name: "url"
       records:
-        Project:  # this is an identifiable in this case
-          parents:
-            - Project  # not needed as the name is equivalent
-        url: $url
+        Project:
+          url: $url
+    persons_element:
+      type: DictListElement
+      match_name: "Person"
+      subtree:
+        person_element:
+          type: Dict
+          records:
+            Person:
+              parents:
+                - Person
+            Project:
+              Person: +$Person
+          subtree:
+            firstname_element:
+              type: DictTextElement
+              match_name: "firstname"
+              match_value: "(?P<firstname>.*)"
+              records:
+                Person:
+                  firstname: $firstname
+            lastname_element:
+              type: DictTextElement
+              match_name: "lastname"
+              match_value: "(?P<lastname>.*)"
+              records:
+                Person:
+                  lastname: $lastname
+            email_element:
+              type: DictTextElement
+              match_name: "email"
+              match_value: "(?P<email>.*)"
+              records:
+                Person:
+                  email: $email
diff --git a/unittests/test_directories/examples_json/testjson.json b/unittests/test_directories/examples_json/testjson.json
index cd26c9c3..b893b608 100644
--- a/unittests/test_directories/examples_json/testjson.json
+++ b/unittests/test_directories/examples_json/testjson.json
@@ -2,11 +2,18 @@
 	"name": "DEMO",
 	"projectId": 10002,
 	"archived": false,
-	"coordinator": {
-		"firstname": "Miri",
-		"lastname": "Mueller",
-		"email": "miri.mueller@science.de"
-	},
+	"Person": [
+        {
+		    "firstname": "Miri",
+		    "lastname": "Mueller",
+		    "email": "miri.mueller@science.de"
+	    },
+        {
+            "firstname": "Mara",
+            "lastname": "Mueller",
+		    "email": "mara.mueller@science.de"
+        }
+    ],
 	"start_date": "2022-03-01",
 	"candidates": ["Mouse", "Penguine"],
 	"rvalue": 0.4444,
diff --git a/unittests/test_directories/examples_json/testjson.schema.json b/unittests/test_directories/examples_json/testjson.schema.json
index a684e9b6..fc784a61 100644
--- a/unittests/test_directories/examples_json/testjson.schema.json
+++ b/unittests/test_directories/examples_json/testjson.schema.json
@@ -11,25 +11,28 @@
     "archived": {
       "type": "boolean"
     },
-    "coordinator": {
-      "type": "object",
-      "properties": {
-        "firstname": {
-          "type": "string"
-        },
-        "lastname": {
-          "type": "string"
-        },
-        "email": {
-          "type": "string"
+    "Person": {
+        "type": "array",
+        "items": {
+            "type": "object",
+            "properties": {
+                "firstname": {
+                    "type": "string"
+                },
+                "lastname": {
+                    "type": "string"
+                },
+                "email": {
+                    "type": "string"
+                }
+            },
+            "required": [
+                "firstname",
+                "lastname",
+                "email"
+            ],
+            "additionalProperties": true
         }
-      },
-      "required": [
-        "firstname",
-        "lastname",
-        "email"
-      ],
-      "additionalProperties": true
     },
     "start_date": {
       "type": "string",
@@ -51,7 +54,7 @@
   "required": [
     "name",
     "projectId",
-    "coordinator"
+    "Person"
   ],
   "additionalProperties": false
 }
diff --git a/unittests/test_json.py b/unittests/test_json.py
index d4da1fe7..9ceefa3d 100644
--- a/unittests/test_json.py
+++ b/unittests/test_json.py
@@ -47,19 +47,24 @@ def test_json():
     # Load and register converter packages:
     converter_registry = crawler.load_converters(crawler_definition)
 
-    crawler.start_crawling(
+    records = crawler.start_crawling(
         JSONFile(os.path.basename(json_file_path), json_file_path),
         crawler_definition,
         converter_registry
     )
-    subd = crawler.debug_tree
-    subc = crawler.debug_metadata
-    #print(json.dumps(subd, indent=3))
-    print(subd)
-    print(subc)
+
+    rec = [r for r in records if r.name == "DEMO"]
+    assert len(rec) == 1
+    rec = rec[0]
+    assert len(rec.parents) == 1
+    assert rec.parents[0].name == "Project"
+    assert rec.get_property("url") is not None
+    assert rec.get_property("url").value == "https://site.de/index.php/"
+
 
 def test_broken_validation():
-    crawler_definition_path = rfp("broken_cfoods", "broken_validation_path.yml")
+    crawler_definition_path = rfp(
+        "broken_cfoods", "broken_validation_path.yml")
     crawler = Crawler()
     with raises(FileNotFoundError) as err:
         crawler_definition = crawler.load_definition(crawler_definition_path)
-- 
GitLab