From 4de4c1d556f8968acf0075b5eeebda8832f3675e Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Wed, 1 Apr 2026 14:33:10 +0100 Subject: [PATCH] Add app route tests and fix stale assertions --- tests/test_app.py | 74 ++++++++++++++++++++++++++++++++++ tests/test_eurostar_scraper.py | 1 + tests/test_trip_planner.py | 10 ++--- 3 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 tests/test_app.py diff --git a/tests/test_app.py b/tests/test_app.py new file mode 100644 index 0000000..c8d9dce --- /dev/null +++ b/tests/test_app.py @@ -0,0 +1,74 @@ +import app as app_module + + +def _client(): + app_module.app.config['TESTING'] = True + return app_module.app.test_client() + + +def _stub_data(monkeypatch): + monkeypatch.setattr(app_module, 'get_cached', lambda key: None) + monkeypatch.setattr(app_module, 'set_cached', lambda key, data: None) + monkeypatch.setattr( + app_module.rtt_scraper, + 'fetch', + lambda travel_date, user_agent: [ + {'depart_bristol': '07:00', 'arrive_paddington': '08:45', 'headcode': '1A23'}, + ], + ) + monkeypatch.setattr( + app_module.eurostar_scraper, + 'fetch', + lambda destination, travel_date, user_agent: [ + { + 'depart_st_pancras': '10:01', + 'arrive_destination': '13:34', + 'destination': destination, + 'train_number': 'ES 9014', + }, + ], + ) + monkeypatch.setattr( + app_module.eurostar_scraper, + 'timetable_url', + lambda destination: f'https://example.test/{destination.lower().replace(" ", "-")}', + ) + + +def test_index_shows_fixed_departure_and_destination_radios(): + client = _client() + + resp = client.get('/') + html = resp.get_data(as_text=True) + + assert resp.status_code == 200 + assert 'Departure point' in html + assert 'Bristol Temple Meads' in html + assert html.count('type="radio"') == len(app_module.DESTINATIONS) + assert 'destination-rotterdam' in html + + +def test_search_redirects_to_results_with_selected_params(): + client = _client() + + resp = client.get('/search?destination=rotterdam&travel_date=2026-04-10&min_connection=60&max_connection=120') + + assert resp.status_code == 302 + assert resp.headers['Location'].endswith( + '/results/rotterdam/2026-04-10?min_connection=60&max_connection=120' + ) + + +def test_results_shows_same_day_destination_switcher(monkeypatch): + _stub_data(monkeypatch) + client = _client() + + resp = client.get('/results/paris/2026-04-10?min_connection=60&max_connection=120') + html = resp.get_data(as_text=True) + + assert resp.status_code == 200 + assert 'Switch destination for Friday 10 April 2026' in html + assert 'Paris Gare du Nord' in html + assert '/results/brussels/2026-04-10?min_connection=60&max_connection=120' in html + assert '/results/rotterdam/2026-04-10?min_connection=60&max_connection=120' in html + assert 'ES 9014' in html diff --git a/tests/test_eurostar_scraper.py b/tests/test_eurostar_scraper.py index 137cf71..1015b8a 100644 --- a/tests/test_eurostar_scraper.py +++ b/tests/test_eurostar_scraper.py @@ -49,6 +49,7 @@ def test_parse_single_departure(): 'depart_st_pancras': '06:01', 'arrive_destination': '09:34', 'destination': 'Paris Gare du Nord', + 'train_number': '', } diff --git a/tests/test_trip_planner.py b/tests/test_trip_planner.py index 2ea1e25..391d422 100644 --- a/tests/test_trip_planner.py +++ b/tests/test_trip_planner.py @@ -71,8 +71,8 @@ def test_min_connection_enforced(): # ES at 09:59 should be excluded, 10:00 should be included es_too_close = {'depart_st_pancras': '09:59', 'arrive_destination': '13:00', 'destination': 'Paris Gare du Nord'} es_ok = {'depart_st_pancras': '10:00', 'arrive_destination': '13:00', 'destination': 'Paris Gare du Nord'} - assert combine_trips([GWR_FAST], [es_too_close], DATE) == [] - trips = combine_trips([GWR_FAST], [es_ok], DATE) + assert combine_trips([GWR_FAST], [es_too_close], DATE, min_connection_minutes=75) == [] + trips = combine_trips([GWR_FAST], [es_ok], DATE, min_connection_minutes=75) assert len(trips) == 1 @@ -80,9 +80,9 @@ def test_max_connection_enforced(): # Arrive Paddington 08:45, max 140 min → latest St Pancras 11:05 es_ok = {'depart_st_pancras': '11:05', 'arrive_destination': '14:00', 'destination': 'Paris Gare du Nord'} es_too_late = {'depart_st_pancras': '11:06', 'arrive_destination': '14:00', 'destination': 'Paris Gare du Nord'} - trips = combine_trips([GWR_FAST], [es_ok], DATE) + trips = combine_trips([GWR_FAST], [es_ok], DATE, max_connection_minutes=140) assert len(trips) == 1 - assert combine_trips([GWR_FAST], [es_too_late], DATE) == [] + assert combine_trips([GWR_FAST], [es_too_late], DATE, max_connection_minutes=140) == [] # --------------------------------------------------------------------------- @@ -104,7 +104,7 @@ def test_only_earliest_eurostar_per_gwr(): def test_multiple_gwr_trains(): gwr2 = {'depart_bristol': '08:00', 'arrive_paddington': '09:45'} es = {'depart_st_pancras': '11:01', 'arrive_destination': '14:34', 'destination': 'Paris Gare du Nord'} - trips = combine_trips([GWR_FAST, gwr2], [es], DATE) + trips = combine_trips([GWR_FAST, gwr2], [es], DATE, max_connection_minutes=140) assert len(trips) == 2 assert trips[0]['depart_bristol'] == '07:00' assert trips[1]['depart_bristol'] == '08:00'