Initial commit.
This commit is contained in:
commit
a8e0bd39e5
16 changed files with 981 additions and 0 deletions
71
tests/test_rtt_scraper.py
Normal file
71
tests/test_rtt_scraper.py
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
import pytest
|
||||
from scraper.realtime_trains import _fmt, _parse_services
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# _fmt
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def test_fmt_four_digits():
|
||||
assert _fmt('0830') == '08:30'
|
||||
|
||||
def test_fmt_already_colon():
|
||||
assert _fmt('08:30') == '08:30'
|
||||
|
||||
def test_fmt_strips_non_digits():
|
||||
assert _fmt('08h30') == '08:30'
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# _parse_services
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _make_html(services: list[tuple[str, str]], time_class: str) -> str:
|
||||
"""Build a minimal servicelist HTML with (train_id, time) pairs."""
|
||||
items = ''
|
||||
for tid, time in services:
|
||||
items += f'''
|
||||
<a class="service">
|
||||
<div class="tid">{tid}</div>
|
||||
<div class="time plan {time_class}">{time}</div>
|
||||
</a>'''
|
||||
return f'<div class="servicelist">{items}</div>'
|
||||
|
||||
|
||||
def test_parse_services_departures():
|
||||
html = _make_html([('1A23', '0700'), ('2B45', '0830')], 'd')
|
||||
result = _parse_services(html, 'div.time.plan.d')
|
||||
assert result == {'1A23': '07:00', '2B45': '08:30'}
|
||||
|
||||
|
||||
def test_parse_services_arrivals():
|
||||
html = _make_html([('1A23', '0845')], 'a')
|
||||
result = _parse_services(html, 'div.time.plan.a')
|
||||
assert result == {'1A23': '08:45'}
|
||||
|
||||
|
||||
def test_parse_services_no_servicelist():
|
||||
assert _parse_services('<html></html>', 'div.time.plan.d') == {}
|
||||
|
||||
|
||||
def test_parse_services_skips_missing_time():
|
||||
html = '''
|
||||
<div class="servicelist">
|
||||
<a class="service"><div class="tid">1A23</div></a>
|
||||
<a class="service"><div class="tid">2B45</div><div class="time plan d">0900</div></a>
|
||||
</div>'''
|
||||
result = _parse_services(html, 'div.time.plan.d')
|
||||
assert '1A23' not in result
|
||||
assert result == {'2B45': '09:00'}
|
||||
|
||||
|
||||
def test_parse_services_skips_empty_time():
|
||||
html = '''
|
||||
<div class="servicelist">
|
||||
<a class="service">
|
||||
<div class="tid">1A23</div>
|
||||
<div class="time plan d"> </div>
|
||||
</a>
|
||||
</div>'''
|
||||
result = _parse_services(html, 'div.time.plan.d')
|
||||
assert result == {}
|
||||
Loading…
Add table
Add a link
Reference in a new issue