From a323f4c020b69697f25d3ea4a0dc8cacbf395c3c Mon Sep 17 00:00:00 2001 From: Timm Fitschen <t.fitschen@indiscale.com> Date: Wed, 30 Mar 2022 12:40:08 +0200 Subject: [PATCH] TST: add another test for json-schema parser --- src/caosadvancedtools/models/parser.py | 9 ++++++++- .../datamodel_missing_property_type.schema.json | 7 +++++++ unittests/test_json_schema_model_parser.py | 8 ++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 unittests/json-schema-models/datamodel_missing_property_type.schema.json diff --git a/src/caosadvancedtools/models/parser.py b/src/caosadvancedtools/models/parser.py index e9c459ca..a71badf3 100644 --- a/src/caosadvancedtools/models/parser.py +++ b/src/caosadvancedtools/models/parser.py @@ -566,7 +566,7 @@ class JsonSchemaParser(Parser): """Extends the yaml parser to read in datamodels defined in a json schema.""" # @author Florian Spreckelsen # @date 2022-02-17 - # @review Daniel Hornung 2022-02-18 + # @review Timm Fitschen 2022-02-30 def parse_model_from_json_schema(self, filename: str): """Return a datamodel created from the definition in the json schema in @@ -584,6 +584,7 @@ class JsonSchemaParser(Parser): """ # @author Florian Spreckelsen # @date 2022-02-17 + # @review Timm Fitschen 2022-02-30 with open(filename, 'r') as schema_file: model_dict = json.load(schema_file) @@ -603,6 +604,7 @@ class JsonSchemaParser(Parser): our : DataModel The datamodel defined in `model_dict` """ + # @review Timm Fitschen 2022-02-30 if isinstance(model_dict, dict): model_dict = [model_dict] @@ -625,6 +627,7 @@ class JsonSchemaParser(Parser): return DataModel(self.model.values()) def _get_atomic_datatype(self, elt): + # @review Timm Fitschen 2022-02-30 if elt["type"] == "string": if "format" in elt and elt["format"] in ["date", "date-time"]: return db.DATETIME @@ -640,6 +643,7 @@ class JsonSchemaParser(Parser): raise JsonSchemaDefinitionError(f"Unkown atomic type in {elt}.") def _treat_element(self, elt: dict, name: str): + # @review Timm Fitschen 2022-02-30 force_list = False if name in self.model: return self.model[name], force_list @@ -664,6 +668,7 @@ class JsonSchemaParser(Parser): return ent, force_list def _treat_record_type(self, elt: dict, name: str): + # @review Timm Fitschen 2022-02-30 rt = db.RecordType(name=name) if "required" in elt: required = elt["required"] @@ -689,6 +694,7 @@ class JsonSchemaParser(Parser): return rt def _treat_enum(self, elt: dict, name: str): + # @review Timm Fitschen 2022-02-30 if "type" in elt and elt["type"] == "integer": raise NotImplementedError( "Integer-enums are not allowd until " @@ -704,6 +710,7 @@ class JsonSchemaParser(Parser): return rt def _treat_list(self, elt: dict, name: str): + # @review Timm Fitschen 2022-02-30 if not "items" in elt: raise JsonSchemaDefinitionError( diff --git a/unittests/json-schema-models/datamodel_missing_property_type.schema.json b/unittests/json-schema-models/datamodel_missing_property_type.schema.json new file mode 100644 index 00000000..eac3cc56 --- /dev/null +++ b/unittests/json-schema-models/datamodel_missing_property_type.schema.json @@ -0,0 +1,7 @@ +{ + "title": "Dataset", + "type": "object", + "properties": { + "method": { "description": "Missing property type" } + } +} diff --git a/unittests/test_json_schema_model_parser.py b/unittests/test_json_schema_model_parser.py index ed0b48ce..359f3c6a 100644 --- a/unittests/test_json_schema_model_parser.py +++ b/unittests/test_json_schema_model_parser.py @@ -134,6 +134,14 @@ def test_required_no_list(): assert "'Dataset' is not of type 'array'" in str(err.value) +def test_missing_property_type(): + """Exception must be raised when "type" is missing.""" + with pytest.raises(JsonSchemaDefinitionError) as err: + parse_model_from_json_schema( + os.path.join(FILEPATH, + "datamodel_missing_property_type.schema.json")) + assert "FIXME" in str(err.value) + def test_enum(): """Enums are represented in references to records of a specific type.""" # @author Florian Spreckelsen -- GitLab