Skip to content

Commit

Permalink
Add tests for annotation helpers.
Browse files Browse the repository at this point in the history
  • Loading branch information
wRAR committed Sep 19, 2024
1 parent bcc9d30 commit 0c5c907
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
2 changes: 2 additions & 0 deletions scrapy_zyte_api/_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class _ActionResult(TypedDict, total=False):


def make_hashable(obj: Any) -> Any:
"""Converts input into hashable form, to use in ``Annotated``."""
if isinstance(obj, (tuple, list)):
return tuple((make_hashable(e) for e in obj))

Expand All @@ -67,6 +68,7 @@ def make_hashable(obj: Any) -> Any:


def _from_hashable(obj: Any) -> Any:
"""Converts a result of ``make_hashable`` back to original form."""
if isinstance(obj, tuple):
return [_from_hashable(o) for o in obj]

Expand Down
111 changes: 111 additions & 0 deletions tests/test_annotations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import pytest

from scrapy_zyte_api._annotations import (
_from_hashable,
actions,
custom_attrs,
make_hashable,
)


@pytest.mark.parametrize(
"input,expected",
[
([], ()),
(set(), ()),
({}, frozenset()),
("foo", "foo"),
(["foo"], ("foo",)),
(42, 42),
(
{"action": "foo", "id": "xx"},
frozenset({("action", "foo"), ("id", "xx")}),
),
(
[{"action": "foo", "id": "xx"}, {"action": "bar"}],
(
frozenset({("action", "foo"), ("id", "xx")}),
frozenset({("action", "bar")}),
),
),
(
{"action": "foo", "options": {"a": "b", "c": ["d", "e"]}},
frozenset(
{
("action", "foo"),
("options", frozenset({("a", "b"), ("c", ("d", "e"))})),
}
),
),
],
)
def test_make_hashable(input, expected):
assert make_hashable(input) == expected


@pytest.mark.parametrize(
"input,expected",
[
((), []),
(frozenset(), {}),
("foo", "foo"),
(("foo",), ["foo"]),
(42, 42),
(
frozenset({("action", "foo"), ("id", "xx")}),
{"action": "foo", "id": "xx"},
),
(
(
frozenset({("action", "foo"), ("id", "xx")}),
frozenset({("action", "bar")}),
),
[{"action": "foo", "id": "xx"}, {"action": "bar"}],
),
(
frozenset(
{
("action", "foo"),
("options", frozenset({("a", "b"), ("c", ("d", "e"))})),
}
),
{"action": "foo", "options": {"a": "b", "c": ["d", "e"]}},
),
],
)
def test_from_hashable(input, expected):
assert _from_hashable(input) == expected


@pytest.mark.parametrize(
"input,expected",
[
([], ()),
([{}], (frozenset(),)),
(
[{"action": "foo"}, {"action": "bar"}],
(
frozenset({("action", "foo")}),
frozenset({("action", "bar")}),
),
),
],
)
def test_actions(input, expected):
assert actions(input) == expected


@pytest.mark.parametrize(
"input,options,expected",
[
({}, None, (frozenset(), None)),
({"foo": "bar"}, None, (frozenset({("foo", "bar")}), None)),
(
{"foo": "bar"},
{"tokens": 42},
(frozenset({("foo", "bar")}), frozenset({("tokens", 42)})),
),
],
)
def test_custom_attrs(input, options, expected):
assert custom_attrs(input, options) == expected

0 comments on commit 0c5c907

Please sign in to comment.