feat(cses): update cses with concept of a category
This commit is contained in:
parent
8df38d0ca8
commit
8e13b8c61d
5 changed files with 299 additions and 28 deletions
|
|
@ -1,5 +1,12 @@
|
|||
from unittest.mock import Mock
|
||||
from scrapers.cses import scrape, scrape_all_problems
|
||||
|
||||
from scrapers.cses import (
|
||||
denormalize_category_name,
|
||||
normalize_category_name,
|
||||
scrape,
|
||||
scrape_all_problems,
|
||||
scrape_category_problems,
|
||||
)
|
||||
from scrapers.models import ProblemSummary
|
||||
|
||||
|
||||
|
|
@ -19,12 +26,19 @@ def test_scrape_success(mocker, mock_cses_html):
|
|||
def test_scrape_all_problems(mocker):
|
||||
mock_response = Mock()
|
||||
mock_response.text = """
|
||||
<h1>Introductory Problems</h1>
|
||||
<a href="/problemset/task/1068">Weird Algorithm</a>
|
||||
<a href="/problemset/task/1083">Missing Number</a>
|
||||
<h1>Sorting and Searching</h1>
|
||||
<a href="/problemset/task/1084">Apartments</a>
|
||||
<div class="content">
|
||||
<h1>Introductory Problems</h1>
|
||||
<ul>
|
||||
<li><a href="/problemset/task/1068">Weird Algorithm</a></li>
|
||||
<li><a href="/problemset/task/1083">Missing Number</a></li>
|
||||
</ul>
|
||||
<h1>Sorting and Searching</h1>
|
||||
<ul>
|
||||
<li><a href="/problemset/task/1084">Apartments</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
"""
|
||||
mock_response.raise_for_status = Mock()
|
||||
|
||||
mocker.patch("scrapers.cses.requests.get", return_value=mock_response)
|
||||
|
||||
|
|
@ -45,3 +59,74 @@ def test_scrape_network_error(mocker):
|
|||
result = scrape("https://cses.fi/problemset/task/1068")
|
||||
|
||||
assert result == []
|
||||
|
||||
|
||||
def test_normalize_category_name():
|
||||
assert normalize_category_name("Sorting and Searching") == "sorting_and_searching"
|
||||
assert normalize_category_name("Dynamic Programming") == "dynamic_programming"
|
||||
assert normalize_category_name("Graph Algorithms") == "graph_algorithms"
|
||||
|
||||
|
||||
def test_denormalize_category_name():
|
||||
assert denormalize_category_name("sorting_and_searching") == "Sorting and Searching"
|
||||
assert denormalize_category_name("dynamic_programming") == "Dynamic Programming"
|
||||
assert denormalize_category_name("graph_algorithms") == "Graph Algorithms"
|
||||
|
||||
|
||||
def test_scrape_category_problems_success(mocker):
|
||||
mock_response = Mock()
|
||||
mock_response.text = """
|
||||
<div class="content">
|
||||
<h1>General</h1>
|
||||
<ul>
|
||||
<li><a href="/problemset/task/1000">Test Problem</a></li>
|
||||
</ul>
|
||||
<h1>Sorting and Searching</h1>
|
||||
<ul>
|
||||
<li><a href="/problemset/task/1640">Sum of Two Values</a></li>
|
||||
<li><a href="/problemset/task/1643">Maximum Subarray Sum</a></li>
|
||||
</ul>
|
||||
<h1>Dynamic Programming</h1>
|
||||
<ul>
|
||||
<li><a href="/problemset/task/1633">Dice Combinations</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
"""
|
||||
mock_response.raise_for_status = Mock()
|
||||
|
||||
mocker.patch("scrapers.cses.requests.get", return_value=mock_response)
|
||||
|
||||
result = scrape_category_problems("sorting_and_searching")
|
||||
|
||||
assert len(result) == 2
|
||||
assert result[0].id == "1640"
|
||||
assert result[0].name == "Sum of Two Values"
|
||||
assert result[1].id == "1643"
|
||||
assert result[1].name == "Maximum Subarray Sum"
|
||||
|
||||
|
||||
def test_scrape_category_problems_not_found(mocker):
|
||||
mock_response = Mock()
|
||||
mock_response.text = """
|
||||
<div class="content">
|
||||
<h1>Some Other Category</h1>
|
||||
<ul>
|
||||
<li><a href="/problemset/task/1000">Test Problem</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
"""
|
||||
mock_response.raise_for_status = Mock()
|
||||
|
||||
mocker.patch("scrapers.cses.requests.get", return_value=mock_response)
|
||||
|
||||
result = scrape_category_problems("nonexistent_category")
|
||||
|
||||
assert result == []
|
||||
|
||||
|
||||
def test_scrape_category_problems_network_error(mocker):
|
||||
mocker.patch("scrapers.cses.requests.get", side_effect=Exception("Network error"))
|
||||
|
||||
result = scrape_category_problems("sorting_and_searching")
|
||||
|
||||
assert result == []
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue