Skip to content

Commit

Permalink
add tests for info schema resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
braingram committed Dec 2, 2024
1 parent 0e8bd08 commit 873ad7d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
8 changes: 3 additions & 5 deletions asdf/_node_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _get_subschema_for_property(schema, key):

# next handle schema combiners
if "not" in schema:
# since we're only concerned here with if the schema applies
# Since we're only concerned here with if the schema applies
# it doesn't matter if the schema is nested in a not
subschema = _get_subschema_for_property(schema["not"], key)
if subschema is not None:
Expand Down Expand Up @@ -75,10 +75,8 @@ def _get_schema_key(schema, key):
applicable = []
if key in schema:
applicable.append(schema[key])
if "not" in schema:
possible = _get_schema_key(schema["not"], key)
if possible is not None:
applicable.append(possible)
# Here we don't consider any subschema under "not" to avoid
# false positives for keys like "type" etc.
for combiner in ("allOf", "oneOf", "anyOf"):
for combined_schema in schema.get(combiner, []):
possible = _get_schema_key(combined_schema, key)
Expand Down
32 changes: 32 additions & 0 deletions asdf/_tests/test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import tempfile

import numpy as np
import pytest

import asdf
from asdf.exceptions import AsdfInfoResolutionError
from asdf.extension import ExtensionManager, ExtensionProxy, ManifestExtension
from asdf.resource import DirectoryResourceMapping

Expand Down Expand Up @@ -715,3 +717,33 @@ def __str__(self):
assert "(NewlineStr)\n" in captured.out
assert "(CarriageReturnStr)\n" in captured.out
assert "(NiceStr): nice\n" in captured.out


@pytest.mark.parametrize(
"schema, expected",
[
({"title": "foo"}, "foo"),
({"allOf": [{"title": "foo"}]}, "foo"),
({"oneOf": [{"title": "foo"}]}, "foo"),
({"anyOf": [{"title": "foo"}]}, "foo"),
({"not": {"title": "foo"}}, None),
],
)
def test_node_info(schema, expected):
ni = asdf._node_info.NodeSchemaInfo.from_root_node("title", "root", {}, schema)
assert ni.info == expected


@pytest.mark.parametrize(
"schema",
[
{"allOf": [{"title": "foo"}, {"title": "bar"}]},
{"oneOf": [{"title": "foo"}, {"title": "bar"}]},
{"anyOf": [{"title": "foo"}, {"title": "bar"}]},
{"allOf": [{"title": "foo"}, {"title": "bar"}]},
],
)
def test_node_info_failure(schema):
ni = asdf._node_info.NodeSchemaInfo.from_root_node("title", "root", {}, schema)
with pytest.raises(AsdfInfoResolutionError):
ni.info

0 comments on commit 873ad7d

Please sign in to comment.