From ccda768bfbed804facfa3a4fe0e91de336cd61c4 Mon Sep 17 00:00:00 2001
From: Daniel <d.hornung@indiscale.com>
Date: Wed, 6 Mar 2024 11:57:08 +0100
Subject: [PATCH] WIP: More extensive testing data.

---
 .../create_jsonschema.py                      |  43 +--
 unittests/table_json_conversion/example.json  |  54 ++--
 .../model_multiple_refs.yml                   |  33 +++
 .../table_json_conversion/model_schema.json   |   4 +
 .../{model.yml => model_simple.yml}           |   0
 .../schema_multiple_refs.json                 | 279 ++++++++++++++++++
 .../table_json_conversion/test_fill_xlsx.py   |   2 +-
 7 files changed, 370 insertions(+), 45 deletions(-)
 create mode 100644 unittests/table_json_conversion/model_multiple_refs.yml
 rename unittests/table_json_conversion/{model.yml => model_simple.yml} (100%)
 create mode 100644 unittests/table_json_conversion/schema_multiple_refs.json

diff --git a/unittests/table_json_conversion/create_jsonschema.py b/unittests/table_json_conversion/create_jsonschema.py
index 6f556863..61e4eb20 100755
--- a/unittests/table_json_conversion/create_jsonschema.py
+++ b/unittests/table_json_conversion/create_jsonschema.py
@@ -16,50 +16,57 @@
 # 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/>.
 
-"""Create JSON-Schema according to configuration
+"""Create JSON-Schema according to configuration.
+
 """
 
 import argparse
 import json
-import os
-import sys
+from typing import List
 
 import caosadvancedtools.json_schema_exporter as jsex
-import caosadvancedtools.models.parser as parser
-import tomli
-
-# TODO why do I need a running LA instance?
+from caosadvancedtools.models import parser
+# import tomli
 
 
-def prepare_datamodel():
-    model = parser.parse_model_from_yaml("./model.yml")
+def prepare_datamodel(modelfile, recordtypes: List[str], outfile: str,
+                      do_not_create: List[str] = None):
+    if do_not_create is None:
+        do_not_create = []
+    model = parser.parse_model_from_yaml(modelfile)
 
     exporter = jsex.JsonSchemaExporter(additional_properties=False,
                                        # additional_options_for_text_props=additional_text_options,
                                        # name_and_description_in_properties=True,
                                        name_property_for_new_records=True,
-                                       do_not_create=["Organisation"],
+                                       do_not_create=do_not_create,
                                        # do_not_retrieve=do_not_retrieve,
+                                       no_remote=True,
+                                       use_rt_pool=model,
                                        )
-    schema_top = exporter.recordtype_to_json_schema(model.get_deep("Training"))
-    schema_pers = exporter.recordtype_to_json_schema(model.get_deep("Person"))
-    merged_schema = jsex.merge_schemas([schema_top, schema_pers])
+    schemas = []
+    for recordtype in recordtypes:
+        schemas.append(exporter.recordtype_to_json_schema(model.get_deep(recordtype)))
+    merged_schema = jsex.merge_schemas(schemas)
 
-    with open("model_schema.json", mode="w", encoding="utf8") as json_file:
+    with open(outfile, mode="w", encoding="utf8") as json_file:
         json.dump(merged_schema, json_file, ensure_ascii=False, indent=2)
 
 
 def _parse_arguments():
     """Parse the arguments."""
-    parser = argparse.ArgumentParser(description='')
+    arg_parser = argparse.ArgumentParser(description='')
 
-    return parser.parse_args()
+    return arg_parser.parse_args()
 
 
 def main():
     """The main function of this script."""
-    args = _parse_arguments()
-    prepare_datamodel()
+    _ = _parse_arguments()
+    prepare_datamodel("model_simple.yml", ["Training", "Person"], "model_schema.json",
+                      do_not_create=["Organisation"])
+    prepare_datamodel("model_multiple_refs.yml", ["Training", "Person"],
+                      "schema_multiple_refs.json")
 
 
 if __name__ == "__main__":
diff --git a/unittests/table_json_conversion/example.json b/unittests/table_json_conversion/example.json
index 8d08fd09..9997f17e 100644
--- a/unittests/table_json_conversion/example.json
+++ b/unittests/table_json_conversion/example.json
@@ -1,30 +1,32 @@
 {
-    "Training": {
-        "date": "2023-01-01",
-        "url": "www.indiscale.com",
-        "coach": [
-            {
-              "family_name": "Sky",
-              "given_name": "Max",
-              "Organisation": "ECB"
-              },{
-              "family_name": "Sky",
-              "given_name": "Min",
-              "Organisation": "ECB"
-              }],
-        "supervisor": {
-            "family_name": "Steve",
-            "given_name": "Stevie",
+  "Training": {
+    "date": "2023-01-01",
+    "url": "www.indiscale.com",
+    "coach": [
+      {
+        "family_name": "Sky",
+        "given_name": "Max",
+        "Organisation": "ECB"
+      },
+      {
+        "family_name": "Sky",
+        "given_name": "Min",
+        "Organisation": "ECB"
+      }
+    ],
+    "supervisor": {
+      "family_name": "Steve",
+      "given_name": "Stevie",
             "Organisation": "IMF"
-        },
-        "duration": 1.0,
-        "participants": 1,
-        "subjects": ["Math", "Physics"],
-        "remote": false
     },
-    "Person": {
-            "family_name": "Steve",
-            "given_name": "Stevie",
-            "Organisation": "IMF"
-        }
+    "duration": 1.0,
+    "participants": 1,
+    "subjects": ["Math", "Physics"],
+    "remote": false
+  },
+  "Person": {
+    "family_name": "Steve",
+    "given_name": "Stevie",
+    "Organisation": "IMF"
+  }
 }
diff --git a/unittests/table_json_conversion/model_multiple_refs.yml b/unittests/table_json_conversion/model_multiple_refs.yml
new file mode 100644
index 00000000..c95a97ac
--- /dev/null
+++ b/unittests/table_json_conversion/model_multiple_refs.yml
@@ -0,0 +1,33 @@
+Person:
+  recommended_properties:
+    full_name:
+      datatype: TEXT
+    email:
+      datatype: TEXT
+    Organisation:
+Training:
+  recommended_properties:
+    date:
+      datatype: DATETIME
+      description: 'The date of the training.'
+    url:
+      datatype: TEXT
+      description: 'The URL'
+    trainer:
+      datatype: LIST<Person>
+    participant:
+      datatype: LIST<Person>
+    supervisor:
+      datatype: Person
+    responsible:
+      datatype: Person
+    supervisor_inherit:
+      inherit_from_suggested:
+        - Person
+    responsible_inherit:
+      inherit_from_suggested:
+        - Person
+Organisation:
+  recommended_properties:
+    Country:
+      datatype: TEXT
diff --git a/unittests/table_json_conversion/model_schema.json b/unittests/table_json_conversion/model_schema.json
index 6f2fffde..f18fd6af 100644
--- a/unittests/table_json_conversion/model_schema.json
+++ b/unittests/table_json_conversion/model_schema.json
@@ -91,6 +91,10 @@
         },
         "remote": {
           "type": "boolean"
+        },
+        "slides": {
+          "type": "string",
+          "format": "data-url"
         }
       },
       "$schema": "https://json-schema.org/draft/2020-12/schema"
diff --git a/unittests/table_json_conversion/model.yml b/unittests/table_json_conversion/model_simple.yml
similarity index 100%
rename from unittests/table_json_conversion/model.yml
rename to unittests/table_json_conversion/model_simple.yml
diff --git a/unittests/table_json_conversion/schema_multiple_refs.json b/unittests/table_json_conversion/schema_multiple_refs.json
new file mode 100644
index 00000000..5678f72c
--- /dev/null
+++ b/unittests/table_json_conversion/schema_multiple_refs.json
@@ -0,0 +1,279 @@
+{
+  "type": "object",
+  "properties": {
+    "Training": {
+      "type": "object",
+      "required": [],
+      "additionalProperties": false,
+      "title": "Training",
+      "properties": {
+        "name": {
+          "type": "string",
+          "description": "The name of the Record to be created"
+        },
+        "date": {
+          "description": "The date of the training.",
+          "anyOf": [
+            {
+              "type": "string",
+              "format": "date"
+            },
+            {
+              "type": "string",
+              "format": "date-time"
+            }
+          ]
+        },
+        "url": {
+          "type": "string",
+          "description": "The URL"
+        },
+        "trainer": {
+          "type": "array",
+          "items": {
+            "type": "object",
+            "required": [],
+            "additionalProperties": false,
+            "title": "trainer",
+            "properties": {
+              "name": {
+                "type": "string",
+                "description": "The name of the Record to be created"
+              },
+              "full_name": {
+                "type": "string"
+              },
+              "email": {
+                "type": "string"
+              },
+              "Organisation": {
+                "type": "object",
+                "required": [],
+                "additionalProperties": false,
+                "title": "Organisation",
+                "properties": {
+                  "name": {
+                    "type": "string",
+                    "description": "The name of the Record to be created"
+                  },
+                  "Country": {
+                    "type": "string"
+                  }
+                }
+              }
+            }
+          }
+        },
+        "participant": {
+          "type": "array",
+          "items": {
+            "type": "object",
+            "required": [],
+            "additionalProperties": false,
+            "title": "participant",
+            "properties": {
+              "name": {
+                "type": "string",
+                "description": "The name of the Record to be created"
+              },
+              "full_name": {
+                "type": "string"
+              },
+              "email": {
+                "type": "string"
+              },
+              "Organisation": {
+                "type": "object",
+                "required": [],
+                "additionalProperties": false,
+                "title": "Organisation",
+                "properties": {
+                  "name": {
+                    "type": "string",
+                    "description": "The name of the Record to be created"
+                  },
+                  "Country": {
+                    "type": "string"
+                  }
+                }
+              }
+            }
+          }
+        },
+        "supervisor": {
+          "type": "object",
+          "required": [],
+          "additionalProperties": false,
+          "title": "supervisor",
+          "properties": {
+            "name": {
+              "type": "string",
+              "description": "The name of the Record to be created"
+            },
+            "full_name": {
+              "type": "string"
+            },
+            "email": {
+              "type": "string"
+            },
+            "Organisation": {
+              "type": "object",
+              "required": [],
+              "additionalProperties": false,
+              "title": "Organisation",
+              "properties": {
+                "name": {
+                  "type": "string",
+                  "description": "The name of the Record to be created"
+                },
+                "Country": {
+                  "type": "string"
+                }
+              }
+            }
+          }
+        },
+        "responsible": {
+          "type": "object",
+          "required": [],
+          "additionalProperties": false,
+          "title": "responsible",
+          "properties": {
+            "name": {
+              "type": "string",
+              "description": "The name of the Record to be created"
+            },
+            "full_name": {
+              "type": "string"
+            },
+            "email": {
+              "type": "string"
+            },
+            "Organisation": {
+              "type": "object",
+              "required": [],
+              "additionalProperties": false,
+              "title": "Organisation",
+              "properties": {
+                "name": {
+                  "type": "string",
+                  "description": "The name of the Record to be created"
+                },
+                "Country": {
+                  "type": "string"
+                }
+              }
+            }
+          }
+        },
+        "supervisor_inherit": {
+          "type": "object",
+          "required": [],
+          "additionalProperties": false,
+          "title": "supervisor_inherit",
+          "properties": {
+            "name": {
+              "type": "string",
+              "description": "The name of the Record to be created"
+            },
+            "full_name": {
+              "type": "string"
+            },
+            "email": {
+              "type": "string"
+            },
+            "Organisation": {
+              "type": "object",
+              "required": [],
+              "additionalProperties": false,
+              "title": "Organisation",
+              "properties": {
+                "name": {
+                  "type": "string",
+                  "description": "The name of the Record to be created"
+                },
+                "Country": {
+                  "type": "string"
+                }
+              }
+            }
+          }
+        },
+        "responsible_inherit": {
+          "type": "object",
+          "required": [],
+          "additionalProperties": false,
+          "title": "responsible_inherit",
+          "properties": {
+            "name": {
+              "type": "string",
+              "description": "The name of the Record to be created"
+            },
+            "full_name": {
+              "type": "string"
+            },
+            "email": {
+              "type": "string"
+            },
+            "Organisation": {
+              "type": "object",
+              "required": [],
+              "additionalProperties": false,
+              "title": "Organisation",
+              "properties": {
+                "name": {
+                  "type": "string",
+                  "description": "The name of the Record to be created"
+                },
+                "Country": {
+                  "type": "string"
+                }
+              }
+            }
+          }
+        }
+      },
+      "$schema": "https://json-schema.org/draft/2020-12/schema"
+    },
+    "Person": {
+      "type": "object",
+      "required": [],
+      "additionalProperties": false,
+      "title": "Person",
+      "properties": {
+        "name": {
+          "type": "string",
+          "description": "The name of the Record to be created"
+        },
+        "full_name": {
+          "type": "string"
+        },
+        "email": {
+          "type": "string"
+        },
+        "Organisation": {
+          "type": "object",
+          "required": [],
+          "additionalProperties": false,
+          "title": "Organisation",
+          "properties": {
+            "name": {
+              "type": "string",
+              "description": "The name of the Record to be created"
+            },
+            "Country": {
+              "type": "string"
+            }
+          }
+        }
+      },
+      "$schema": "https://json-schema.org/draft/2020-12/schema"
+    }
+  },
+  "required": [
+    "Training",
+    "Person"
+  ],
+  "additionalProperties": false,
+  "$schema": "https://json-schema.org/draft/2020-12/schema"
+}
\ No newline at end of file
diff --git a/unittests/table_json_conversion/test_fill_xlsx.py b/unittests/table_json_conversion/test_fill_xlsx.py
index a1458fc2..10ddcc32 100644
--- a/unittests/table_json_conversion/test_fill_xlsx.py
+++ b/unittests/table_json_conversion/test_fill_xlsx.py
@@ -44,6 +44,6 @@ def test_detect():
 def test_fill_xlsx():
     path = os.path.join(tempfile.mkdtemp(), 'test.xlsx')
     assert not os.path.exists(path)
-    fill_template(rfp('example_template.xlsx'), rfp('example.json'), path)
+    fill_template(data=rfp('example.json'), template=rfp('example_template.xlsx'), result=path)
     assert os.path.exists(path)
     generated = load_workbook(path)  # workbook can be read
-- 
GitLab