diff --git a/tests/test_utils.py b/tests/test_utils.py index e5ca6cc..f5c678e 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,9 +1,16 @@ # Copyright 2023 ACCESS-NRI and contributors. See the top-level COPYRIGHT file for details. # SPDX-License-Identifier: Apache-2.0 +import datetime + import pytest +import yaml -from access_nri_intake.utils import get_jsonschema +from access_nri_intake.utils import ( + get_jsonschema, + load_metadata_yaml, + validate_against_schema, +) @pytest.mark.parametrize( @@ -11,6 +18,9 @@ ["2a09030653f495939c90a22e95dd1c4587c8695f7f07e17b9129a6491469f9fc", None], ) def test_get_jsonschema(known_hash): + """ + Test that jsonschema are correctly downloaded and required fields are overwritten + """ url = "https://raw.githubusercontent.com/ACCESS-NRI/schema/4e3d10e563d7c1c9f66e9ab92a2926cdec3d6893/file_asset.json" required = [ "path", @@ -24,3 +34,33 @@ def test_get_jsonschema(known_hash): required += ["foo"] with pytest.warns(UserWarning): _, _ = get_jsonschema(url=url, known_hash=known_hash, required=required) + + +def test_load_metadata_yaml(tmp_path): + """ + Test that dates are left as strings when reading metadata.yaml files + """ + path = tmp_path / "metadata.yaml" + date = "2001-01-01" + contents = {"date": datetime.date.fromisoformat(date)} + with open(path, mode="w") as fpath: + yaml.dump(contents, fpath) + metadata = load_metadata_yaml(path) + assert metadata == {"date": date} + + +@pytest.mark.parametrize("instance", [{"foo": [0, 1, 2]}, {"foo": (0, 1, 2)}]) +def test_validate_against_schema(instance): + """ + Test jsonschema validation, allowing for tuples as arrays + """ + schema = { + "$schema": "https://json-schema.org/draft/2020-12/schema", + "type": "object", + "properties": { + "foo": { + "type": "array", + }, + }, + } + validate_against_schema(instance, schema)