Skip to content

Commit e71f38e

Browse files
vintaclaude
andcommitted
test: add coverage for detect_source_type, format_stars_short, extract_entries, and last_commit_at parsing
Co-Authored-By: Claude <noreply@anthropic.com>
1 parent aa2e742 commit e71f38e

2 files changed

Lines changed: 152 additions & 1 deletion

File tree

website/tests/test_build.py

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77

88
from build import (
99
build,
10+
detect_source_type,
11+
extract_entries,
1012
extract_github_repo,
13+
format_stars_short,
1114
load_stars,
1215
sort_entries,
1316
)
14-
from readme_parser import slugify
17+
from readme_parser import parse_readme, slugify
1518

1619
# ---------------------------------------------------------------------------
1720
# slugify
@@ -324,3 +327,133 @@ def test_no_stars_sorted_alphabetically(self):
324327
]
325328
result = sort_entries(entries)
326329
assert [e["name"] for e in result] == ["apple", "zebra"]
330+
331+
def test_builtin_between_starred_and_unstarred(self):
332+
entries = [
333+
{"name": "builtin", "stars": None, "source_type": "Built-in"},
334+
{"name": "starred", "stars": 100, "source_type": None},
335+
{"name": "unstarred", "stars": None, "source_type": None},
336+
]
337+
result = sort_entries(entries)
338+
assert [e["name"] for e in result] == ["starred", "builtin", "unstarred"]
339+
340+
341+
# ---------------------------------------------------------------------------
342+
# detect_source_type
343+
# ---------------------------------------------------------------------------
344+
345+
346+
class TestDetectSourceType:
347+
def test_github_repo_returns_none(self):
348+
assert detect_source_type("https://github.com/psf/requests") is None
349+
350+
def test_stdlib_url(self):
351+
assert detect_source_type("https://docs.python.org/3/library/asyncio.html") == "Built-in"
352+
353+
def test_gitlab_url(self):
354+
assert detect_source_type("https://gitlab.com/org/repo") == "GitLab"
355+
356+
def test_bitbucket_url(self):
357+
assert detect_source_type("https://bitbucket.org/org/repo") == "Bitbucket"
358+
359+
def test_non_github_external(self):
360+
assert detect_source_type("https://example.com/tool") == "External"
361+
362+
def test_github_non_repo_returns_none(self):
363+
assert detect_source_type("https://github.com/org/repo/wiki") is None
364+
365+
366+
# ---------------------------------------------------------------------------
367+
# format_stars_short
368+
# ---------------------------------------------------------------------------
369+
370+
371+
class TestFormatStarsShort:
372+
def test_under_1000(self):
373+
assert format_stars_short(500) == "500"
374+
375+
def test_exactly_1000(self):
376+
assert format_stars_short(1000) == "1k"
377+
378+
def test_large_number(self):
379+
assert format_stars_short(52000) == "52k"
380+
381+
def test_zero(self):
382+
assert format_stars_short(0) == "0"
383+
384+
385+
# ---------------------------------------------------------------------------
386+
# extract_entries
387+
# ---------------------------------------------------------------------------
388+
389+
390+
class TestExtractEntries:
391+
def test_basic_extraction(self):
392+
readme = textwrap.dedent("""\
393+
# T
394+
395+
---
396+
397+
**Tools**
398+
399+
## Widgets
400+
401+
- [widget](https://example.com) - A widget.
402+
403+
# Contributing
404+
405+
Done.
406+
""")
407+
groups = parse_readme(readme)
408+
categories = [c for g in groups for c in g["categories"]]
409+
entries = extract_entries(categories, groups)
410+
assert len(entries) == 1
411+
assert entries[0]["name"] == "widget"
412+
assert entries[0]["categories"] == ["Widgets"]
413+
assert entries[0]["groups"] == ["Tools"]
414+
415+
def test_duplicate_entry_merged(self):
416+
readme = textwrap.dedent("""\
417+
# T
418+
419+
---
420+
421+
**Tools**
422+
423+
## Alpha
424+
425+
- [shared](https://example.com/shared) - Shared lib.
426+
427+
## Beta
428+
429+
- [shared](https://example.com/shared) - Shared lib.
430+
431+
# Contributing
432+
433+
Done.
434+
""")
435+
groups = parse_readme(readme)
436+
categories = [c for g in groups for c in g["categories"]]
437+
entries = extract_entries(categories, groups)
438+
shared = [e for e in entries if e["name"] == "shared"]
439+
assert len(shared) == 1
440+
assert sorted(shared[0]["categories"]) == ["Alpha", "Beta"]
441+
442+
def test_source_type_detected(self):
443+
readme = textwrap.dedent("""\
444+
# T
445+
446+
---
447+
448+
## Stdlib
449+
450+
- [asyncio](https://docs.python.org/3/library/asyncio.html) - Async I/O.
451+
452+
# Contributing
453+
454+
Done.
455+
""")
456+
groups = parse_readme(readme)
457+
categories = [c for g in groups for c in g["categories"]]
458+
entries = extract_entries(categories, groups)
459+
assert entries[0]["source_type"] == "Built-in"

website/tests/test_fetch_github_stars.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,24 @@ def test_multiple_repos(self):
135135
assert result["a/x"]["stars"] == 100
136136
assert result["b/y"]["stars"] == 200
137137

138+
def test_extracts_last_commit_at(self):
139+
data = {
140+
"repo_0": {
141+
"stargazerCount": 100,
142+
"owner": {"login": "org"},
143+
"defaultBranchRef": {"target": {"committedDate": "2025-06-01T00:00:00Z"}},
144+
}
145+
}
146+
repos = ["org/repo"]
147+
result = parse_graphql_response(data, repos)
148+
assert result["org/repo"]["last_commit_at"] == "2025-06-01T00:00:00Z"
149+
150+
def test_missing_default_branch_ref(self):
151+
data = {"repo_0": {"stargazerCount": 50, "owner": {"login": "org"}}}
152+
repos = ["org/repo"]
153+
result = parse_graphql_response(data, repos)
154+
assert result["org/repo"]["last_commit_at"] == ""
155+
138156

139157
class TestMainSkipsFreshCache:
140158
"""Verify that main() skips fetching when all cache entries are fresh."""

0 commit comments

Comments
 (0)