Skip to content
Snippets Groups Projects
Commit 8edc1588 authored by Alexander Schlemmer's avatar Alexander Schlemmer
Browse files

TST(validator): unit tests for new validator behavior

parent 55e6f39b
Branches
Tags
2 merge requests!217TST: Make NamedTemporaryFiles Windows-compatible,!201Validator that checks created records using a json schema
Pipeline #58339 passed
...@@ -149,14 +149,15 @@ def validate(records: list[db.Record], schemas: dict[str, dict]) -> list[tuple[b ...@@ -149,14 +149,15 @@ def validate(records: list[db.Record], schemas: dict[str, dict]) -> list[tuple[b
retval = [] retval = []
for r in records: for r in records:
if len(r.parents) != 0: if len(r.parents) != 1:
raise RuntimeError( raise RuntimeError(
"Schema validation is only supported if records have exactly one parent.") "Schema validation is only supported if records have exactly one parent.")
if r.parents[0] not in schemas: parname = r.parents[0].name
if parname not in schemas:
raise RuntimeError( raise RuntimeError(
"No schema for record type {} in schema dictionary.".format(r.parents[0])) "No schema for record type {} in schema dictionary.".format(parname))
try: try:
jsonschema.validate(convert_record(r), schemas[r.parents[0]]) jsonschema.validate(convert_record(r), schemas[parname])
retval.append((True, None)) retval.append((True, None))
except ValidationError as ex: except ValidationError as ex:
retval.append((False, ex)) retval.append((False, ex))
......
...@@ -51,7 +51,8 @@ def test_create_json_schema(): ...@@ -51,7 +51,8 @@ def test_create_json_schema():
pobj = convert_record(r) pobj = convert_record(r)
# print(yaml.dump(pobj)) # print(yaml.dump(pobj))
# print(yaml.dump(json[0])) # print(yaml.dump(json[0]))
jsonschema.validate(pobj, json[0]) assert "Dataset" in json
jsonschema.validate(pobj, json["Dataset"])
# Failing test: # Failing test:
r = db.Record() r = db.Record()
...@@ -62,7 +63,7 @@ def test_create_json_schema(): ...@@ -62,7 +63,7 @@ def test_create_json_schema():
pobj = convert_record(r) pobj = convert_record(r)
with pytest.raises(ValidationError, match=".*'keywords' is a required property.*"): with pytest.raises(ValidationError, match=".*'keywords' is a required property.*"):
jsonschema.validate(pobj, json[0]) jsonschema.validate(pobj, json["Dataset"])
def test_validation(): def test_validation():
...@@ -83,6 +84,7 @@ def test_validation(): ...@@ -83,6 +84,7 @@ def test_validation():
valres = validate([r1, r2], json) valres = validate([r1, r2], json)
assert valres[0][0] is True assert valres[0][0] is True
assert len(valres[0][1]) == 1 assert valres[0][1] is None
assert valres[0][1][0] == json[0] assert not valres[1][0]
assert len(valres[1][1]) == 0 assert isinstance(valres[1][1], ValidationError)
assert valres[1][1].message == "'keywords' is a required property"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment