Files
ismc-hamchin/apps/scraper/tests/test_scraper.py
Meghdad d14b2b98c4
CI / python (push) Canceled after 0s
CI / web (push) Canceled after 0s
init
2026-09-12 22:59:58 +03:30

80 lines
3.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import unittest
from bs4 import BeautifulSoup
from hamchin_scraper.scraper import (
extract_rows,
lesson_groups,
parse_schedule,
successful_form_fields,
to_english_digits,
)
class ScraperTests(unittest.TestCase):
def test_normalizes_persian_and_arabic_digits(self):
self.assertEqual(to_english_digits("۱۲۳٤٥6"), "123456")
def test_extracts_successful_aspnet_fields(self):
soup = BeautifulSoup(
"""
<form>
<input type="hidden" name="__VIEWSTATE" value="state">
<input type="checkbox" name="remember" value="yes" checked>
<input type="checkbox" name="ignored" value="no">
<select name="period"><option value="1">old</option><option value="2" selected>new</option></select>
<input type="submit" name="submit" value="go">
</form>
""",
"html.parser",
)
self.assertEqual(
successful_form_fields(soup),
{"__VIEWSTATE": "state", "remember": "yes", "period": "2"},
)
def test_finds_lesson_postbacks_but_not_grid_sort_links(self):
soup = BeautifulSoup(
"""
<form>
<a href='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("lesson$1", "", true, "", "", false, true))'> رجال:پایه 9</a>
<table id="ContentPlaceHolder1_CtlSelectionUnits1_SelectableUnitsGridView">
<tr><th><a href='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("sort$1", "", true, "", "", false, true))'>درس</a></th></tr>
</table>
</form>
""",
"html.parser",
)
self.assertEqual(lesson_groups(soup), [("رجال:پایه 9", "lesson$1", "")])
def test_extracts_class_row(self):
soup = BeautifulSoup(
"""
<table id="ContentPlaceHolder1_CtlSelectionUnits1_SelectableUnitsGridView">
<tr><th>head</th></tr>
<tr>
<td>۱</td><td><select><option>انتخاب عادی</option></select></td>
<td>رجال:پایه 9</td><td>رجال:نیمسال اول:طبقه ۳</td><td>20</td>
<td>استاد نمونه</td><td>محدوده</td><td>مدرسه نمونه</td><td>18</td>
<td><input value="مدرسه نمونه:شنبه,یکشنبه:[08:00-09:00]"></td>
</tr>
</table>
""",
"html.parser",
)
row = extract_rows(soup, "1406-1405", "برنامه جاری", "رجال:پایه 9")[0]
self.assertEqual(row.teacher, "استاد نمونه")
self.assertEqual(row.days, ["شنبه", "یکشنبه"])
self.assertEqual(row.time, "08:00-09:00")
self.assertEqual(row.remaining_capacity, "18")
def test_parses_named_time(self):
self.assertEqual(
parse_schedule("مدرسه فیضیه:شنبه:[بعد از نماز عصر]"),
(["شنبه"], "بعد از نماز عصر", "مدرسه فیضیه"),
)
if __name__ == "__main__":
unittest.main()