Files
PromptCR-Lab/backend/tests/test_git_parser.py
T
2026-09-19 12:54:45 +08:00

41 lines
1.3 KiB
Python

"""Tests for Git parser and candidate selection."""
import os
from pathlib import Path
import pytest
from app.dataset.git_parser import score_commit, select_candidates
def _make_repo(tmp_path: Path) -> str:
repo = tmp_path / "repo"
repo.mkdir()
os.system(
f'cd {repo} && git init -q && git config user.email "test@example.com" && git config user.name "Test"'
)
(repo / "main.py").write_text("print('hello')\n")
os.system(f'cd {repo} && git add . && git commit -q -m "init"')
(repo / "main.py").write_text("print('world')\n")
os.system(f'cd {repo} && git add . && git commit -q -m "update main"')
(repo / "app.java").write_text("class App {}\n")
os.system(f'cd {repo} && git add . && git commit -q -m "add java app"')
return str(repo)
def test_score_commit_prefers_message_and_size(tmp_path):
repo = _make_repo(tmp_path)
from app.dataset.git_parser import list_commits
commits = list_commits(repo)
assert len(commits) >= 2
scores = [score_commit(c) for c in commits]
assert all(0 <= s <= 1 for s in scores)
def test_select_candidates(tmp_path):
repo = _make_repo(tmp_path)
candidates = select_candidates(repo, count=2, languages=["python", "java"])
assert len(candidates) <= 2
assert all(c.repo == "repo" for c in candidates)