Files
sport-prog-club/tests/test_problem_parser.py
T
Кирилл ЯценкоandClaude Opus 5.5 dcb55d335b SCR-1: задача LeetCode находится по номеру и по названию
Ассистент сам перечислил «LeetCode 45 — Jump Game II», а на просьбу дать
код ответил, что такой задачи нет: разбор знал задачи LeetCode только по
слагу. Теперь понимает «45», «lc:45», «LC 45», «LeetCode 45 — …», а строку
без кода ищет по точному названию, если оно в каталоге одно. Это общий
разбор: работает и во вставке списка на странице, и в get_problem.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-02 19:33:47 +05:00

90 lines
3.6 KiB
Python

from __future__ import annotations
import pytest
from app.models import Platform, Problem
from app.services.problem_parser import parse_problem_list, search_problems
@pytest.fixture
async def catalog(session):
problems = [
Problem(platform=Platform.leetcode, external_id="1", slug="two-sum",
title="Two Sum", url="https://leetcode.com/problems/two-sum/",
difficulty="Easy", tags=["Array"]),
Problem(platform=Platform.leetcode, external_id="704", slug="binary-search",
title="Binary Search", url="https://leetcode.com/problems/binary-search/",
difficulty="Easy", tags=["Binary Search"]),
Problem(platform=Platform.codeforces, external_id="4A", slug="4a",
title="Watermelon", url="https://codeforces.com/problemset/problem/4/A",
rating=800, tags=["math"]),
Problem(platform=Platform.codeforces, external_id="1352A", slug="1352a",
title="Sum of Round Numbers",
url="https://codeforces.com/problemset/problem/1352/A", rating=800),
]
session.add_all(problems)
await session.commit()
return problems
async def test_parses_every_supported_form(session, catalog):
text = """
https://leetcode.com/problems/two-sum/
lc:binary-search
binary-search
https://codeforces.com/problemset/problem/4/A
cf:1352A
4A
"""
result = await parse_problem_list(session, text)
assert [p.external_id for p in result.problems] == ["1", "704", "4A", "1352A"]
assert result.unresolved == []
async def test_leetcode_problem_is_found_by_its_number(session, catalog):
text = """
704
lc:1
LC 704
LeetCode #1
LeetCode 704 — Binary Search (Easy)
"""
result = await parse_problem_list(session, text)
assert [p.external_id for p in result.problems] == ["704", "1"]
assert result.unresolved == []
async def test_a_number_does_not_swallow_a_slug_that_starts_with_digits(session):
session.add(Problem(platform=Platform.leetcode, external_id="717",
slug="1-bit-and-2-bit-characters", title="1-bit and 2-bit Characters",
url="https://leetcode.com/problems/1-bit-and-2-bit-characters/"))
await session.commit()
result = await parse_problem_list(session, "lc:1-bit-and-2-bit-characters")
assert [p.external_id for p in result.problems] == ["717"]
async def test_a_line_without_a_code_is_found_by_its_unique_title(session, catalog):
session.add(Problem(platform=Platform.codeforces, external_id="5A", slug="5a",
title="Two Sum", url="https://codeforces.com/problemset/problem/5/A"))
await session.commit()
result = await parse_problem_list(session, "binary search\nTwo Sum")
# «Two Sum» есть и на LeetCode, и на Codeforces — угадывать не берёмся.
assert [p.external_id for p in result.problems] == ["704"]
assert result.unresolved == ["Two Sum"]
async def test_reports_unknown_lines(session, catalog):
result = await parse_problem_list(session, "two-sum\nтакой-задачи-нет\n9999Z")
assert [p.external_id for p in result.problems] == ["1"]
assert result.unresolved == ["такой-задачи-нет", "9999Z"]
async def test_contest_url_form(session, catalog):
result = await parse_problem_list(session, "https://codeforces.com/contest/1352/problem/A")
assert [p.external_id for p in result.problems] == ["1352A"]
async def test_search_filters_by_platform(session, catalog):
found = await search_problems(session, "sum", Platform.leetcode)
assert [p.title for p in found] == ["Two Sum"]