fix(ci): include scrapers, though
This commit is contained in:
parent
560c8b2846
commit
2704fe6d72
4 changed files with 190 additions and 0 deletions
41
tests/scrapers/conftest.py
Normal file
41
tests/scrapers/conftest.py
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_codeforces_html():
|
||||
return """
|
||||
<div class="input">
|
||||
<pre>
|
||||
<div class="test-example-line-1">3</div>
|
||||
<div class="test-example-line-1">1 2 3</div>
|
||||
</pre>
|
||||
</div>
|
||||
<div class="output">
|
||||
<pre>
|
||||
<div class="test-example-line-1">6</div>
|
||||
</pre>
|
||||
</div>
|
||||
"""
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_atcoder_html():
|
||||
return """
|
||||
<h3>Sample Input 1</h3>
|
||||
<pre>3
|
||||
1 2 3</pre>
|
||||
<h3>Sample Output 1</h3>
|
||||
<pre>6</pre>
|
||||
"""
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_cses_html():
|
||||
return """
|
||||
<h1>Example</h1>
|
||||
<p>Input:</p>
|
||||
<pre>3
|
||||
1 2 3</pre>
|
||||
<p>Output:</p>
|
||||
<pre>6</pre>
|
||||
"""
|
||||
50
tests/scrapers/test_atcoder.py
Normal file
50
tests/scrapers/test_atcoder.py
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
from unittest.mock import Mock
|
||||
from scrapers.atcoder import scrape, scrape_contest_problems
|
||||
|
||||
|
||||
def test_scrape_success(mocker, mock_atcoder_html):
|
||||
mock_response = Mock()
|
||||
mock_response.text = mock_atcoder_html
|
||||
|
||||
mocker.patch("scrapers.atcoder.requests.get", return_value=mock_response)
|
||||
|
||||
result = scrape("https://atcoder.jp/contests/abc350/tasks/abc350_a")
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0][0] == "3\n1 2 3"
|
||||
assert result[0][1] == "6"
|
||||
|
||||
|
||||
def test_scrape_contest_problems(mocker):
|
||||
mock_response = Mock()
|
||||
mock_response.text = """
|
||||
<table class="table">
|
||||
<tr><th>Task</th><th>Name</th></tr>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td><a href="/contests/abc350/tasks/abc350_a">A - Water Tank</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td><a href="/contests/abc350/tasks/abc350_b">B - Dentist Aoki</a></td>
|
||||
</tr>
|
||||
</table>
|
||||
"""
|
||||
|
||||
mocker.patch("scrapers.atcoder.requests.get", return_value=mock_response)
|
||||
|
||||
result = scrape_contest_problems("abc350")
|
||||
|
||||
assert len(result) == 2
|
||||
assert result[0] == {"id": "a", "name": "A - Water Tank"}
|
||||
assert result[1] == {"id": "b", "name": "B - Dentist Aoki"}
|
||||
|
||||
|
||||
def test_scrape_network_error(mocker):
|
||||
mocker.patch(
|
||||
"scrapers.atcoder.requests.get", side_effect=Exception("Network error")
|
||||
)
|
||||
|
||||
result = scrape("https://atcoder.jp/contests/abc350/tasks/abc350_a")
|
||||
|
||||
assert result == []
|
||||
53
tests/scrapers/test_codeforces.py
Normal file
53
tests/scrapers/test_codeforces.py
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
from unittest.mock import Mock
|
||||
from scrapers.codeforces import scrape, scrape_contest_problems
|
||||
from scrapers.models import Problem
|
||||
|
||||
|
||||
def test_scrape_success(mocker, mock_codeforces_html):
|
||||
mock_scraper = Mock()
|
||||
mock_response = Mock()
|
||||
mock_response.text = mock_codeforces_html
|
||||
mock_scraper.get.return_value = mock_response
|
||||
|
||||
mocker.patch(
|
||||
"scrapers.codeforces.cloudscraper.create_scraper", return_value=mock_scraper
|
||||
)
|
||||
|
||||
result = scrape("https://codeforces.com/contest/1900/problem/A")
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0].input == "1\n3\n1 2 3"
|
||||
assert result[0].expected == "6"
|
||||
|
||||
|
||||
def test_scrape_contest_problems(mocker):
|
||||
mock_scraper = Mock()
|
||||
mock_response = Mock()
|
||||
mock_response.text = """
|
||||
<a href="/contest/1900/problem/A">A. Problem A</a>
|
||||
<a href="/contest/1900/problem/B">B. Problem B</a>
|
||||
"""
|
||||
mock_scraper.get.return_value = mock_response
|
||||
|
||||
mocker.patch(
|
||||
"scrapers.codeforces.cloudscraper.create_scraper", return_value=mock_scraper
|
||||
)
|
||||
|
||||
result = scrape_contest_problems("1900")
|
||||
|
||||
assert len(result) == 2
|
||||
assert result[0] == Problem(id="a", name="A. Problem A")
|
||||
assert result[1] == Problem(id="b", name="B. Problem B")
|
||||
|
||||
|
||||
def test_scrape_network_error(mocker):
|
||||
mock_scraper = Mock()
|
||||
mock_scraper.get.side_effect = Exception("Network error")
|
||||
|
||||
mocker.patch(
|
||||
"scrapers.codeforces.cloudscraper.create_scraper", return_value=mock_scraper
|
||||
)
|
||||
|
||||
result = scrape("https://codeforces.com/contest/1900/problem/A")
|
||||
|
||||
assert result == []
|
||||
46
tests/scrapers/test_cses.py
Normal file
46
tests/scrapers/test_cses.py
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
from unittest.mock import Mock
|
||||
from scrapers.cses import scrape, scrape_all_problems
|
||||
|
||||
|
||||
def test_scrape_success(mocker, mock_cses_html):
|
||||
mock_response = Mock()
|
||||
mock_response.text = mock_cses_html
|
||||
|
||||
mocker.patch("scrapers.cses.requests.get", return_value=mock_response)
|
||||
|
||||
result = scrape("https://cses.fi/problemset/task/1068")
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0][0] == "3\n1 2 3"
|
||||
assert result[0][1] == "6"
|
||||
|
||||
|
||||
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>
|
||||
"""
|
||||
|
||||
mocker.patch("scrapers.cses.requests.get", return_value=mock_response)
|
||||
|
||||
result = scrape_all_problems()
|
||||
|
||||
assert "Introductory Problems" in result
|
||||
assert "Sorting and Searching" in result
|
||||
assert len(result["Introductory Problems"]) == 2
|
||||
assert result["Introductory Problems"][0] == {
|
||||
"id": "1068",
|
||||
"name": "Weird Algorithm",
|
||||
}
|
||||
|
||||
|
||||
def test_scrape_network_error(mocker):
|
||||
mocker.patch("scrapers.cses.requests.get", side_effect=Exception("Network error"))
|
||||
|
||||
result = scrape("https://cses.fi/problemset/task/1068")
|
||||
|
||||
assert result == []
|
||||
Loading…
Add table
Add a link
Reference in a new issue