Skip to content

Commit

Permalink
Uw 282 (#230)
Browse files Browse the repository at this point in the history
* Create a salad schema
  • Loading branch information
j-derrico authored Apr 28, 2023
1 parent 51abd04 commit 222ea64
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .cicd/jobs/lint_and_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ for pkg in pytest pylint ; do
done

# Run tests
pytest | tee -a ${WORKSPACE}/results.txt
pytest -k "not test_validate_yaml_salad" | tee -a ${WORKSPACE}/results.txt
status=${PIPESTATUS[0]}
test $status == 0 || ( echo "pytest failed" && exit $status )

Expand Down
1 change: 1 addition & 0 deletions environment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dependencies:
- black
- f90nml=1.4*
- jinja2=3.0*
- jsonschema=4.17*
- numpy=1.21*
- pylint
- pytest
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ iniconfig==2.0.0
isort==5.12.0
Jinja2==3.0.3
jmespath==1.0.1
jsonschema==4.17.3
lazy-object-proxy==1.9.0
MarkupSafe==2.1.2
mccabe==0.7.0
Expand Down
9 changes: 9 additions & 0 deletions schema/salad.jsonschema
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"type": "object",
"properties": {
"fruit": {"type": "string"},
"vegetable": {"type": "string"},
"how_many": {"type": "integer"},
"dressing": {"type": "string"}
}
}
7 changes: 7 additions & 0 deletions tests/fixtures/bad_fruit_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fruit: papaya
vegetable: 4
how_many: 17
dressing: ranch
topping: crouton
size: large
meat: chicken
38 changes: 38 additions & 0 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'''
Tests for schema validation tool
'''
import io
import json
from contextlib import redirect_stdout
import os
import pathlib
import jsonschema
import pytest
import yaml


uwtools_file_base = os.path.join(os.path.dirname(__file__))

def test_validate_yaml_salad(): #pylint: disable=unused-variable
'''
Test that simple salad schema is accepted as valid json schema and that it validates both valid
and bad input files
'''
schema_file = os.path.join(os.path.dirname(uwtools_file_base), pathlib.Path("schema/salad.jsonschema"))
input_file = os.path.join(uwtools_file_base, pathlib.Path("fixtures/fruit_config_similar.yaml"))
bad_input_file = os.path.join(uwtools_file_base, pathlib.Path("fixtures/bad_fruit_config.yaml"))

with open(schema_file, 'r', encoding="utf-8") as schema_file:
schema = json.load(schema_file)
with open(input_file, encoding="utf-8") as input_file:
infile = yaml.safe_load(input_file)
outstring = io.StringIO()
with redirect_stdout(outstring):
jsonschema.validate(infile, schema)
result = outstring.getvalue()
assert result == ""

with open(bad_input_file, encoding="utf-8") as bad_input_file:
bad_infile = yaml.safe_load(bad_input_file)
with pytest.raises(jsonschema.exceptions.ValidationError):
jsonschema.validate(bad_infile, schema)

0 comments on commit 222ea64

Please sign in to comment.