Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • caosdb/src/caosdb-advanced-user-tools
1 result
Show changes
Commits on Source (2)
......@@ -355,7 +355,7 @@ validation_schema: dict, optional
# Validation
if validation_schema is not None:
# convert to array_schema is given schema is a model_schema
# convert to array_schema if given schema is a model_schema
if 'properties' in validation_schema and validation_schema['properties'].values():
if list(validation_schema['properties'].values())[0]["type"] != "array":
validation_schema = array_schema_from_model_schema(read_or_dict(validation_schema))
......
......@@ -196,6 +196,32 @@ def test_errors():
known_good=rfp("data/simple_data.xlsx"),
schema=rfp("data/simple_schema.json"))
assert exc.value.message == "0.5 is not of type 'integer'"
# Check wrong data
with open(rfp("data/simple_data.json")) as json_file:
json_data = json.load(json_file)
json_data["Training"][0]["date"] = "2023-01"
with tempfile.NamedTemporaryFile(suffix='.json', mode='w+t') as temp_file:
json.dump(json_data, temp_file)
temp_file.seek(0)
with pytest.raises(AssertionError) as exc:
fill_and_compare(json_file=temp_file.name,
template_file=rfp("data/simple_template.xlsx"),
known_good=rfp("data/simple_data.xlsx"),
schema=rfp("data/simple_schema.json"))
assert "Training" in str(exc) and "2023-01" in str(exc)
# Check wrong schema
with open(rfp("data/simple_schema.json")) as json_file:
json_schema = json.load(json_file)
json_schema["properties"]["Person"]["properties"]["given_name"]["type"] = "integer"
with tempfile.NamedTemporaryFile(suffix='.json', mode='w+t') as temp_file:
json.dump(json_schema, temp_file)
temp_file.seek(0)
with pytest.raises(schema_exc.ValidationError) as exc:
fill_and_compare(json_file=rfp("data/simple_data.json"),
template_file=rfp("data/simple_template.xlsx"),
known_good=rfp("data/simple_data.xlsx"),
schema=temp_file.name)
assert "integer" in str(exc)
def test_data_schema_generation():
......
......@@ -24,6 +24,7 @@ import datetime
import json
import os
import re
import tempfile
from types import SimpleNamespace
from typing import Optional
......@@ -43,7 +44,7 @@ def rfp(*pathcomponents):
def convert_and_compare(xlsx_file: str, schema_file: str, known_good_file: str,
known_good_data: Optional[dict] = None, strict: bool = False,
validate: bool = False) -> dict:
validate: bool = True) -> dict:
"""Convert an XLSX file and compare to a known result.
Exactly one of ``known_good_file`` and ``known_good_data`` should be non-empty.
......@@ -57,7 +58,7 @@ json: dict
model_schema = json.load(sch_f)
data_schema = xlsx_utils.array_schema_from_model_schema(model_schema)
result = convert.to_dict(xlsx=xlsx_file, schema=data_schema, validate=True)
result = convert.to_dict(xlsx=xlsx_file, schema=data_schema, validate=validate)
if known_good_file:
with open(known_good_file, encoding="utf-8") as myfile:
expected = json.load(myfile)
......@@ -101,6 +102,33 @@ def test_conversions():
assert str(err.value).startswith("Values at path ['Training', 0, ")
def test_validation():
# Check wrong data
with open(rfp("data/simple_data.json")) as json_file:
known_good = json.load(json_file)
known_good["Training"][0]["date"] = "2023-01-02"
with tempfile.NamedTemporaryFile(suffix='.json', mode='w+t') as temp_file:
json.dump(known_good, temp_file)
temp_file.seek(0)
with pytest.raises(AssertionError) as exc:
convert_and_compare(xlsx_file=rfp("data/simple_data.xlsx"),
schema_file=rfp("data/simple_schema.json"),
known_good_file=temp_file.name)
assert "Training" in str(exc) and "2023-01-02" in str(exc)
# Check wrong schema
with open(rfp("data/simple_schema.json")) as json_file:
json_schema = json.load(json_file)
json_schema["properties"]["Person"]["properties"]["given_name"]["type"] = "integer"
with tempfile.NamedTemporaryFile(suffix='.json', mode='w+t') as temp_file:
json.dump(json_schema, temp_file)
temp_file.seek(0)
with pytest.raises(jsonschema.ValidationError) as exc:
convert_and_compare(xlsx_file=rfp("data/simple_data.xlsx"),
schema_file=temp_file.name,
known_good_file=rfp("data/simple_data.json"))
assert "integer" in str(exc)
def test_missing_columns():
with pytest.raises(ValueError) as caught:
convert.to_dict(xlsx=rfp("data/simple_data_missing.xlsx"),
......