Skip to content

Commit c1b50e1

Browse files
committed
Add a test for parsing links from JSON
1 parent c1b46c1 commit c1b50e1

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

tests/unit/test_collector.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import itertools
2+
import json
23
import logging
34
import os
45
import re
@@ -481,6 +482,57 @@ def test_parse_links__requires_python(
481482
_test_parse_links_data_attribute(anchor_html, "requires_python", expected)
482483

483484

485+
def test_parse_links_json() -> None:
486+
json_bytes = json.dumps(
487+
{
488+
"meta": {"api-version": "1.0"},
489+
"name": "holygrail",
490+
"files": [
491+
{
492+
"filename": "holygrail-1.0.tar.gz",
493+
"url": "https://example.com/files/holygrail-1.0.tar.gz",
494+
"hashes": {"sha256": "sha256 hash", "blake2b": "blake2b hash"},
495+
"requires-python": ">=3.7",
496+
"yanked": "Had a vulnerability",
497+
},
498+
{
499+
"filename": "holygrail-1.0-py3-none-any.whl",
500+
"url": "/files/holygrail-1.0-py3-none-any.whl",
501+
"hashes": {"sha256": "sha256 hash", "blake2b": "blake2b hash"},
502+
"requires-python": ">=3.7",
503+
"dist-info-metadata": False,
504+
},
505+
],
506+
}
507+
).encode("utf8")
508+
page = IndexContent(
509+
json_bytes,
510+
"application/vnd.pypi.simple.v1+json",
511+
encoding=None,
512+
# parse_links() is cached by url, so we inject a random uuid to ensure
513+
# the page content isn't cached.
514+
url=f"https://example.com/simple-{uuid.uuid4()}/",
515+
)
516+
links = list(parse_links(page, use_deprecated_html5lib=False))
517+
518+
assert links == [
519+
Link(
520+
"https://example.com/files/holygrail-1.0.tar.gz",
521+
comes_from=page.url,
522+
requires_python=">=3.7",
523+
yanked_reason="Had a vulnerability",
524+
hashes={"sha256": "sha256 hash", "blake2b": "blake2b hash"},
525+
),
526+
Link(
527+
"https://example.com/files/holygrail-1.0-py3-none-any.whl",
528+
comes_from=page.url,
529+
requires_python=">=3.7",
530+
yanked_reason=None,
531+
hashes={"sha256": "sha256 hash", "blake2b": "blake2b hash"},
532+
),
533+
]
534+
535+
484536
@pytest.mark.parametrize(
485537
"anchor_html, expected",
486538
[

0 commit comments

Comments
 (0)