-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #213 from scrapy-plugins/custom-attrs
Handle custom attributes received in the API response.
- Loading branch information
Showing
8 changed files
with
301 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import pytest | ||
|
||
from scrapy_zyte_api._annotations import ( | ||
_from_hashable, | ||
actions, | ||
custom_attrs, | ||
make_hashable, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"input,expected", | ||
[ | ||
([], ()), | ||
({}, 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 |
Oops, something went wrong.