Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ Changelog = "https://referencing.readthedocs.io/en/stable/changes/"
Source = "https://github.com/python-jsonschema/referencing"

[dependency-groups]
dev = ["pytest>9"]
dev = [
"hypothesis>=6.100",
"pytest>9",
]
docs = [
"furo",
"pygments-github-lexers",
Expand Down
45 changes: 45 additions & 0 deletions referencing/tests/strategies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Hypothesis strategies for exercising registries and resources."""

from __future__ import annotations

from typing import Any

from hypothesis import strategies as st

from referencing import Registry, Resource

json_values = st.recursive(
st.none() | st.booleans() | st.integers() | st.text(),
lambda children: (
st.lists(children, max_size=3)
| st.dictionaries(st.text(), children, max_size=3)
),
max_leaves=10,
)
"""A strategy producing JSON-compatible values."""

uris = st.text(
alphabet=st.characters(blacklist_categories=("Cs",)),
min_size=1,
max_size=20,
).map(lambda path: f"urn:example:{path}")
"""A strategy producing non-empty opaque resource identifiers."""


@st.composite
def resources(draw: st.DrawFn) -> Resource[Any]:
"""Generate an opaque resource containing a JSON-compatible value."""
return Resource.opaque(draw(json_values))


@st.composite
def registries(draw: st.DrawFn) -> Registry[Any]:
"""Generate a registry containing zero or more opaque resources."""
pairs = draw(
st.lists(
st.tuples(uris, resources()),
max_size=5,
unique_by=lambda pair: pair[0],
),
)
return Registry().with_resources(pairs)
18 changes: 18 additions & 0 deletions referencing/tests/test_strategies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from hypothesis import given, settings

from referencing.tests.strategies import registries, resources


@settings(max_examples=25)
@given(resources())
def test_resources_are_opaque(resource):
assert resource.id() is None
assert list(resource.subresources()) == []
assert list(resource.anchors()) == []


@settings(max_examples=25)
@given(registries())
def test_registries_contain_unique_resource_uris(registry):
assert len(registry) == len(set(registry))
assert all(registry[uri].id() is None for uri in registry)
61 changes: 60 additions & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.