diff --git a/unittests/test_json_schema_exporter.py b/unittests/test_json_schema_exporter.py index 57a45286b2590e857700e8ef41bbac84f9b529a7..5a3207d1681293dae983c805458d8c649ebf6ec0 100644 --- a/unittests/test_json_schema_exporter.py +++ b/unittests/test_json_schema_exporter.py @@ -42,10 +42,23 @@ def _mock_execute_query(query_string, unique=False, **kwargs): ]) all_records.extend(other_type_records) + referencing_type_rt = db.RecordType(name="ReferencingType") + referencing_type_records = db.Container().extend([ + db.Record(id=103).add_parent(referencing_type_rt), + db.Record(id=104, name="referencing").add_parent(referencing_type_rt) + ]) + all_records.extend(referencing_type_records) + + all_files.append(db.File(id=105, name="GenericFile.txt")) + if query_string == "SELECT name, id FROM RECORD 'OtherType'": return other_type_records elif query_string == "FIND RECORDTYPE WITH name='OtherType'" and unique is True: return other_type_rt + elif query_string == "SELECT name, id FROM RECORD 'ReferencingType'": + return referencing_type_records + elif query_string == "FIND RECORDTYPE WITH name='ReferencingType'" and unique is True: + return referencing_type_rt elif query_string == "SELECT name, id FROM RECORD": return all_records elif query_string == "SELECT name, id FROM FILE": @@ -289,6 +302,25 @@ def test_rt_with_references(): db.execute_query("SELECT name, id FROM FILE")) assert "oneOf" not in props["RefProp"] + example = { + "RefProp": "101, otherB" + } + validate(example, schema) + example = { + "RefProp": "23, I don't exist" + } + with raises(ValidationError): + # Wrong enum value + validate(example, schema) + example = { + "RefProp": { + "IntegerProp": 12 + } + } + with raises(ValidationError): + # Can't have objects in generic references + validate(example, schema) + rt = db.RecordType() rt.add_property(name="RefProp", datatype="OtherType") rt.add_property(name="OtherTextProp", datatype=db.TEXT) @@ -321,6 +353,19 @@ def test_rt_with_references(): assert "OtherTextProp" in props assert props["OtherTextProp"]["type"] == "string" + example = { + "RefProp": { + "IntegerProp": 12 + } + } + validate(example, schema) + + example = { + "RefProp": "101, otherB", + "OtherTextProp": "something" + } + validate(example, schema) + rt = db.RecordType(name="TestType", description="Some description") rt.add_property(name="RefProp", datatype=db.LIST(db.REFERENCE), description="I'm a list of references.") @@ -343,10 +388,75 @@ def test_rt_with_references(): assert "oneOf" not in items assert "description" not in items + example = { + "RefProp": "101, otherB" + } + with raises(ValidationError): + # Should be list but isn't + validate(example, schema) + example = { + "RefProp": ["101, otherB"] + } + validate(example, schema) + example = { + "RefProp": ["101, otherB", "102", "104, referencing"] + } + validate(example, schema) + rt = db.RecordType() rt.add_property(name="RefProp", datatype=db.LIST("OtherType")) - schema = rtjs(rt) + schema = rtjs(rt, additional_properties=False, + name_and_description_in_properties=True) + assert schema["additionalProperties"] is False + assert "name" in schema["properties"] + assert schema["properties"]["name"]["type"] == "string" + assert "description" in schema["properties"] + assert schema["properties"]["description"]["type"] == "string" + assert "RefProp" in schema["properties"] + assert schema["properties"]["RefProp"]["type"] == "array" + assert "additionalProperties" not in schema["properties"]["RefProp"] + assert "items" in schema["properties"]["RefProp"] + items = schema["properties"]["RefProp"]["items"] + assert "oneOf" in items + assert len(items["oneOf"]) == 2 + # same as above, we can't rely on the order + enum_index = 0 + if "enum" not in items["oneOf"][enum_index]: + enum_index = 1 - enum_index + assert "enum" in items["oneOf"][enum_index] + assert isinstance(items["oneOf"][enum_index]["enum"], list) + assert len(items["oneOf"][enum_index]["enum"]) == 3 + assert "100, otherA" in items["oneOf"][enum_index]["enum"] + assert "101, otherB" in items["oneOf"][enum_index]["enum"] + assert "102" in items["oneOf"][enum_index]["enum"] + other_type = items["oneOf"][1 - enum_index] + assert other_type["type"] == "object" + assert other_type["additionalProperties"] is False + assert "IntegerProp" in other_type["properties"] + assert len(other_type["required"]) == 1 + assert "IntegerProp" in other_type["required"] + + example = { + "RefProp": ["101, otherB", "102", "104, referencing"] + } + with raises(ValidationError): + # Wrong value in enum + validate(example, schema) + example = { + "RefProp": [{"IntegerProp": 12}] + } + validate(example, schema) + example = { + "RefProp": [{"IntegerProp": 12, "additionalProperty": "something"}] + } + with raises(ValidationError): + # we have additional_properties=False which propagates to subschemas + validate(example, schema) + example = { + "RefProp": [{"IntegerProp": 12}, "101, otherB"] + } + validate(example, schema) rt = db.RecordType() rt.add_property(name="FileProp", datatype=db.FILE)