Skip to content

Commit

Permalink
Refactor anchor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nolan Woods committed Jul 1, 2022
1 parent 3c0a4fc commit 69a8eca
Showing 1 changed file with 62 additions and 4 deletions.
66 changes: 62 additions & 4 deletions tests/unit/test_loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pathlib

import pytest
import ruamel.yaml.composer

from check_jsonschema.loaders import BadFileTypeError, InstanceLoader, SchemaLoader
from check_jsonschema.loaders.instance.json5 import ENABLED as JSON5_ENABLED
Expand Down Expand Up @@ -55,11 +56,11 @@ def test_schemaloader_local_yaml_data(tmp_path, filename):
a:
type: object
properties:
b: &anchor
b:
type: array
items:
type: integer
c: &anchor
c:
type: string
"""
)
Expand All @@ -80,6 +81,38 @@ def test_schemaloader_local_yaml_data(tmp_path, filename):
}


@pytest.mark.parametrize(
"filename",
[
"schema.yaml",
],
)
@pytest.mark.filterwarnings("ignore:ReusedAnchorWarning")
def test_schemaloader_local_yaml_dup_anchor(tmp_path, filename):
f = tmp_path / filename
f.write_text(
"""
---
"$schema": https://json-schema.org/draft/2020-12/schema
type: object
properties:
a:
type: object
properties:
b: &anchor
type: array
items:
type: integer
c: &anchor
type: string
"""
)
try:
SchemaLoader(str(f))
except ruamel.yaml.composer.ComposerError as e:
raise AssertionError(f"YAML loader does not support duplicate anchors {e}")


@pytest.mark.parametrize(
"schemafile",
[
Expand Down Expand Up @@ -130,17 +163,42 @@ def test_instanceloader_yaml_data(tmp_path, filename, default_ft):
f.write_text(
"""\
a:
b: &anchor
b:
- 1
- 2
c: &anchor d
c: d
"""
)
loader = InstanceLoader([str(f)], default_filetype=default_ft)
data = list(loader.iter_files())
assert data == [(str(f), {"a": {"b": [1, 2], "c": "d"}})]


@pytest.mark.parametrize(
"filename",
[
"foo.yaml",
],
)
@pytest.mark.filterwarnings("ignore:ReusedAnchorWarning")
def test_instanceloader_yaml_dup_anchor(tmp_path, filename):
f = tmp_path / filename
f.write_text(
"""\
a:
b: &anchor
- 1
- 2
c: &anchor d
"""
)
loader = InstanceLoader([str(f)])
try:
list(loader.iter_files())
except ruamel.yaml.composer.ComposerError as e:
raise AssertionError(f"YAML loader does not support duplicate anchors {e}")


def test_instanceloader_unknown_type(tmp_path):
f = tmp_path / "foo" # no extension here
f.write_text("{}") # json data (could be detected as either)
Expand Down

0 comments on commit 69a8eca

Please sign in to comment.