Skip to content

Commit

Permalink
add test of _check_args
Browse files Browse the repository at this point in the history
  • Loading branch information
dougiesquire committed Jun 27, 2023
1 parent e694fa4 commit 8997714
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 12 deletions.
15 changes: 7 additions & 8 deletions src/access_nri_intake/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from .catalog import EXP_JSONSCHEMA, translators
from .catalog.manager import CatalogManager
from .source import builders
from .utils import load_metadata_yaml, validate_against_schema
from .utils import load_metadata_yaml


class MetadataCheckError(Exception):
Expand Down Expand Up @@ -51,7 +51,12 @@ def _parse_inputs(config_yamls, build_path):

source_args["path"] = kwargs.pop("path")
metadata_yaml = kwargs.pop("metadata_yaml")
metadata = load_metadata_yaml(metadata_yaml)
try:
metadata = load_metadata_yaml(metadata_yaml, EXP_JSONSCHEMA)
except jsonschema.exceptions.ValidationError:
raise MetadataCheckError(

Check warning on line 57 in src/access_nri_intake/cli.py

View check run for this annotation

Codecov / codecov/patch

src/access_nri_intake/cli.py#L54-L57

Added lines #L54 - L57 were not covered by tests
f"Failed to validate metadata.yaml for {args['name']}. See traceback for details."
)
source_args["name"] = metadata["name"]
source_args["description"] = metadata["description"]
source_args["metadata"] = metadata
Expand All @@ -74,12 +79,6 @@ def _check_args(args_list):
for args in args_list:
names.append(args["name"])
uuids.append(args["metadata"]["experiment_uuid"])
try:
validate_against_schema(args["metadata"], EXP_JSONSCHEMA)
except jsonschema.exceptions.ValidationError:
raise MetadataCheckError(
f"Failed to validate metadata.yaml for {args['name']}. See traceback for details."
)

if len(names) != len(set(names)):
seen = set()
Expand Down
7 changes: 5 additions & 2 deletions src/access_nri_intake/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ def get_jsonschema(url, known_hash, required):
return schema, schema_required


def load_metadata_yaml(path):
def load_metadata_yaml(path, jsonschema):
"""
Load a metadata.yaml file, leaving dates as strings
Load a metadata.yaml file, leaving dates as strings, and validate against a jsonschema,
allowing for tuples as arrays
Parameters
----------
Expand Down Expand Up @@ -78,6 +79,8 @@ def remove_implicit_resolver(cls, tag_to_remove):
with open(path) as fpath:
metadata = yaml.load(fpath, Loader=NoDatesSafeLoader)

validate_against_schema(metadata, jsonschema)

return metadata


Expand Down
75 changes: 75 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,82 @@

import os

import pytest

from access_nri_intake.cli import MetadataCheckError, _check_args


def test_entrypoint():
"""
Check that entry point works
"""
exit_status = os.system("catalog-build --help")
assert exit_status == 0


@pytest.mark.parametrize(
"args, raises",
[
(
[
{
"name": "exp0",
"metadata": {
"experiment_uuid": "214e8e6d-3bc5-4353-98d3-b9e9a5507d4b"
},
},
{
"name": "exp1",
"metadata": {
"experiment_uuid": "7b0bc2c6-7cbb-4d97-8eb9-b0255c16d910"
},
},
],
False,
),
(
[
{
"name": "exp0",
"metadata": {
"experiment_uuid": "214e8e6d-3bc5-4353-98d3-b9e9a5507d4b"
},
},
{
"name": "exp0",
"metadata": {
"experiment_uuid": "7b0bc2c6-7cbb-4d97-8eb9-b0255c16d910"
},
},
],
True,
),
(
[
{
"name": "exp0",
"metadata": {
"experiment_uuid": "214e8e6d-3bc5-4353-98d3-b9e9a5507d4b"
},
},
{
"name": "exp1",
"metadata": {
"experiment_uuid": "214e8e6d-3bc5-4353-98d3-b9e9a5507d4b"
},
},
],
True,
),
],
)
def test_check_args(args, raises):
"""
Check that non-unique names and uuids return an error
"""
if raises:
with pytest.raises(MetadataCheckError) as excinfo:
_check_args(args)
assert "exp0" in str(excinfo.value)
else:
_check_args(args)
9 changes: 7 additions & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,15 @@ def test_load_metadata_yaml(tmp_path):
path = tmp_path / "metadata.yaml"
date = "2001-01-01"
contents = {"date": datetime.date.fromisoformat(date)}
schema = {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"date": {
"type": "string",
},
}
with open(path, mode="w") as fpath:
yaml.dump(contents, fpath)
metadata = load_metadata_yaml(path)
assert metadata == {"date": date}
load_metadata_yaml(path, schema)


@pytest.mark.parametrize("instance", [{"foo": [0, 1, 2]}, {"foo": (0, 1, 2)}])
Expand Down

0 comments on commit 8997714

Please sign in to comment.