Files

108 lines
5.6 KiB
Python

import unittest
import io
import json
from datetime import datetime
from unittest.mock import patch
import efa_bahn as efa
class EfaTests(unittest.TestCase):
def test_summer_and_winter(self):
self.assertEqual(efa.local('2026-10-08T15:00:00Z'), '2026-10-08T17:00:00+02:00')
self.assertEqual(efa.local('2026-12-08T15:00:00Z'), '2026-12-08T16:00:00+01:00')
def test_schedule_fallback_not_live(self):
point = {'departureTimePlanned': '2026-10-09T03:21:00Z',
'departureTimeEstimated': '2026-10-09T03:21:00Z'}
result = efa.status({}, point, 'departure')
self.assertIsNone(result['estimated'])
self.assertIsNone(result['delay_minutes'])
self.assertIsNotNone(result['estimated_unconfirmed'])
def test_delay_and_platform_change(self):
point = {'departureTimePlanned': '2026-10-08T15:22:00Z',
'departureTimeEstimated': '2026-10-08T15:39:00Z',
'properties': {'plannedPlatformName': '9', 'platformName': '10'}}
result = efa.status({'isRealtimeControlled': True}, point, 'departure')
self.assertEqual(result['delay_minutes'], 17)
self.assertTrue(result['platform_changed'])
def test_platform_label_not_change(self):
point = {'properties': {'platformName': 'Gleis 5', 'plannedPlatformName': '5'}}
self.assertFalse(efa.status({}, point, 'departure')['platform_changed'])
def test_stop_cancellation_and_unknown(self):
point = {'properties': {'realtimeStatus': ['DEPARTURE_CANCELLED']}}
self.assertTrue(efa.status({}, point, 'departure')['cancelled'])
self.assertIsNone(efa.status({}, {}, 'departure')['cancelled'])
def test_no_silent_ambiguous_station(self):
with patch.object(efa, 'stops', return_value=[{'name': 'Hbf Vorplatz', 'id': '1'}, {'name': 'Hbf tief', 'id': '2'}]):
with self.assertRaises(efa.EfaError):
efa.station('Hbf')
def test_departure_cutoff_and_overnight_duration(self):
def leg(dep, arr):
return {'origin': {'departureTimePlanned': dep}, 'destination': {'arrivalTimePlanned': arr},
'transportation': {'name': 'RE 2'}}
data = {'journeys': [
{'legs': [leg('2026-10-08T21:00:00Z', '2026-10-08T21:30:00Z')]},
{'legs': [leg('2026-10-08T21:55:00Z', '2026-10-08T22:15:00Z')]},
]}
with patch.object(efa, 'request', return_value=data):
result = efa.journeys({'id': '1'}, {'id': '2'}, datetime(2026, 10, 8, 23, 30, tzinfo=efa.BERLIN), 5, False)
self.assertEqual(len(result), 1)
self.assertEqual(result[0]['duration_minutes'], 20)
def test_alert_validity(self):
future = {'infoLinks': [{'title': 'Bauarbeiten Hannover'}],
'timestamps': {'validity': [{'from': '2026-10-10T03:00:00Z'}]}}
current = {'infoLinks': [{'title': 'Bauarbeiten Hannover'}],
'timestamps': {'validity': [{'from': '2026-10-01T03:00:00Z', 'to': '2026-10-12T03:00:00Z'}]}}
with patch.object(efa, 'request', return_value={'infos': {'current': [future, current]}}):
result = efa.alerts('Hannover', datetime(2026, 10, 8, 18, 0, tzinfo=efa.BERLIN), 5)
self.assertEqual(result['matching_count'], 1)
def test_partial_broker_error_with_stations(self):
data = {'systemMessages': [{'type': 'error', 'module': 'BROKER', 'code': -8011}],
'locations': [{'id': 'de:08222:2417', 'name': 'Mannheim, Hauptbahnhof'}]}
with patch.object(efa, 'urlopen', return_value=io.BytesIO(json.dumps(data).encode())):
self.assertEqual(efa.request('XML_STOPFINDER_REQUEST', {})['locations'][0]['id'], 'de:08222:2417')
data['locations'] = []
with patch.object(efa, 'urlopen', return_value=io.BytesIO(json.dumps(data).encode())):
with self.assertRaises(efa.EfaError):
efa.request('XML_STOPFINDER_REQUEST', {})
efa.REQUEST_WARNINGS.clear()
def test_city_main_station_without_forecourt(self):
options = [{'name': 'Mannheim, Hauptbahnhof', 'id': '1'}, {'name': 'Mannheim, Lindenhof, MA Hauptbahnhof Süd', 'id': '2'}]
with patch.object(efa, 'stops', return_value=options) as mock:
self.assertEqual(efa.station('Mannheim')['id'], '1')
mock.assert_called_once_with('Mannheim Hauptbahnhof')
def test_correct_weekday(self):
self.assertEqual(efa.date_label('2026-10-09T05:21:00+02:00'), 'Freitag, 09.10.2026')
def test_empty_alerts_not_all_clear(self):
text = efa.render_text({'ok': True, 'queried_at': 'now',
'alerts': {'matching_count': 0, 'matches': []}})
self.assertIn('NICHT aus', text)
self.assertIn('unvollständig', text)
def test_text_keeps_unknown_realtime_and_platform_change(self):
unknown = efa.status({}, {'departureTimePlanned': '2026-10-09T03:21:00Z',
'departureTimeEstimated': '2026-10-09T03:21:00Z'}, 'departure')
self.assertNotIn('Prognose', efa.status_text(unknown))
self.assertIn('keine bestätigten Echtzeitdaten', efa.status_text(unknown))
changed = efa.status({'isRealtimeControlled': True},
{'properties': {'platformName': '10', 'plannedPlatformName': '9'}}, 'departure')
self.assertIn('gemeldet 10, geplant 9', efa.status_text(changed))
def test_text_reports_error(self):
self.assertEqual(efa.render_text({'ok': False, 'error': 'test'}),
'EFA-Abfrage fehlgeschlagen: test')
if __name__ == '__main__':
unittest.main()