SHA256
forked from course-team/tjhlp-gitea
106 lines
3.4 KiB
Python
106 lines
3.4 KiB
Python
"""utils.py 单元测试: parse_repo, compute_issue_stats"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from tjhlp.utils import compute_issue_stats, parse_repo
|
|
|
|
# typer.Exit 在非 CLI 上下文中抛出 click.exceptions.Exit
|
|
_EXIT_EXCEPTIONS = (SystemExit, Exception)
|
|
|
|
|
|
class TestParseRepo:
|
|
def test_valid(self):
|
|
assert parse_repo("org/repo") == ("org", "repo")
|
|
|
|
def test_valid_with_hyphens(self):
|
|
assert parse_repo("tjhlp-2025f/hw01") == ("tjhlp-2025f", "hw01")
|
|
|
|
def test_no_slash(self):
|
|
with pytest.raises(_EXIT_EXCEPTIONS):
|
|
parse_repo("noslash")
|
|
|
|
def test_multiple_slashes(self):
|
|
with pytest.raises(_EXIT_EXCEPTIONS):
|
|
parse_repo("a/b/c")
|
|
|
|
def test_empty_org(self):
|
|
with pytest.raises(_EXIT_EXCEPTIONS):
|
|
parse_repo("/repo")
|
|
|
|
def test_empty_repo(self):
|
|
with pytest.raises(_EXIT_EXCEPTIONS):
|
|
parse_repo("org/")
|
|
|
|
def test_empty_string(self):
|
|
with pytest.raises(_EXIT_EXCEPTIONS):
|
|
parse_repo("")
|
|
|
|
|
|
class TestComputeIssueStats:
|
|
def test_empty_list(self):
|
|
stats = compute_issue_stats([])
|
|
assert stats["total"] == 0
|
|
assert stats["open"] == 0
|
|
assert stats["closed"] == 0
|
|
assert stats["no_reply"] == 0
|
|
|
|
def test_basic_counts(self):
|
|
issues = [
|
|
{"state": "open", "comments": 0},
|
|
{"state": "open", "comments": 2},
|
|
{"state": "closed", "comments": 1},
|
|
]
|
|
stats = compute_issue_stats(issues)
|
|
assert stats["total"] == 3
|
|
assert stats["open"] == 2
|
|
assert stats["closed"] == 1
|
|
assert stats["with_comments"] == 2
|
|
assert stats["no_reply"] == 1
|
|
|
|
def test_label_counts(self):
|
|
issues = [
|
|
{"state": "open", "comments": 0, "labels": [{"name": "bug"}, {"name": "紧急"}]},
|
|
{"state": "open", "comments": 0, "labels": [{"name": "bug"}]},
|
|
{"state": "closed", "comments": 1, "labels": []},
|
|
]
|
|
stats = compute_issue_stats(issues)
|
|
assert stats["label_counts"]["bug"] == 2
|
|
assert stats["label_counts"]["紧急"] == 1
|
|
|
|
def test_user_counts(self):
|
|
issues = [
|
|
{"state": "open", "comments": 0, "user": {"login": "student1"}},
|
|
{"state": "open", "comments": 0, "user": {"login": "student1"}},
|
|
{"state": "open", "comments": 0, "user": {"login": "student2"}},
|
|
]
|
|
stats = compute_issue_stats(issues)
|
|
assert stats["user_counts"]["student1"] == 2
|
|
assert stats["user_counts"]["student2"] == 1
|
|
|
|
def test_missing_user(self):
|
|
issues = [
|
|
{"state": "open", "comments": 0, "user": None},
|
|
{"state": "open", "comments": 0}, # no user key at all
|
|
]
|
|
stats = compute_issue_stats(issues)
|
|
assert stats["total"] == 2
|
|
assert len(stats["user_counts"]) == 0
|
|
|
|
def test_missing_labels(self):
|
|
issues = [
|
|
{"state": "open", "comments": 0}, # no labels key
|
|
]
|
|
stats = compute_issue_stats(issues)
|
|
assert len(stats["label_counts"]) == 0
|
|
|
|
def test_no_reply_only_counts_open(self):
|
|
"""closed issues with 0 comments should NOT count as no_reply"""
|
|
issues = [
|
|
{"state": "closed", "comments": 0},
|
|
{"state": "open", "comments": 0},
|
|
]
|
|
stats = compute_issue_stats(issues)
|
|
assert stats["no_reply"] == 1
|