From 8edc1588b5360f375197e0159b3db1b0a0c7764e Mon Sep 17 00:00:00 2001 From: Alexander Schlemmer <a.schlemmer@indiscale.com> Date: Fri, 29 Nov 2024 09:29:42 +0100 Subject: [PATCH] TST(validator): unit tests for new validator behavior --- src/caoscrawler/validator.py | 9 +++++---- unittests/test_validation.py | 12 +++++++----- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/caoscrawler/validator.py b/src/caoscrawler/validator.py index 67775163..3cd57cd5 100644 --- a/src/caoscrawler/validator.py +++ b/src/caoscrawler/validator.py @@ -149,14 +149,15 @@ def validate(records: list[db.Record], schemas: dict[str, dict]) -> list[tuple[b retval = [] for r in records: - if len(r.parents) != 0: + if len(r.parents) != 1: raise RuntimeError( "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( - "No schema for record type {} in schema dictionary.".format(r.parents[0])) + "No schema for record type {} in schema dictionary.".format(parname)) try: - jsonschema.validate(convert_record(r), schemas[r.parents[0]]) + jsonschema.validate(convert_record(r), schemas[parname]) retval.append((True, None)) except ValidationError as ex: retval.append((False, ex)) diff --git a/unittests/test_validation.py b/unittests/test_validation.py index 6db0674e..216b51fa 100644 --- a/unittests/test_validation.py +++ b/unittests/test_validation.py @@ -51,7 +51,8 @@ def test_create_json_schema(): pobj = convert_record(r) # print(yaml.dump(pobj)) # print(yaml.dump(json[0])) - jsonschema.validate(pobj, json[0]) + assert "Dataset" in json + jsonschema.validate(pobj, json["Dataset"]) # Failing test: r = db.Record() @@ -62,7 +63,7 @@ def test_create_json_schema(): pobj = convert_record(r) with pytest.raises(ValidationError, match=".*'keywords' is a required property.*"): - jsonschema.validate(pobj, json[0]) + jsonschema.validate(pobj, json["Dataset"]) def test_validation(): @@ -83,6 +84,7 @@ def test_validation(): valres = validate([r1, r2], json) assert valres[0][0] is True - assert len(valres[0][1]) == 1 - assert valres[0][1][0] == json[0] - assert len(valres[1][1]) == 0 + assert valres[0][1] is None + assert not valres[1][0] + assert isinstance(valres[1][1], ValidationError) + assert valres[1][1].message == "'keywords' is a required property" -- GitLab