4
4
from unittest .mock import MagicMock
5
5
6
6
from py_nextbus .client import NextBusClient
7
+ from tests .helpers .mock_responses import MOCK_AGENCY_LIST_RESPONSE
7
8
from tests .helpers .mock_responses import MOCK_PREDICTIONS_RESPONSE_NO_ROUTE
8
9
from tests .helpers .mock_responses import MOCK_PREDICTIONS_RESPONSE_WITH_ROUTE
10
+ from tests .helpers .mock_responses import MOCK_ROUTE_DETAILS_RESPONSE
11
+ from tests .helpers .mock_responses import MOCK_ROUTE_LIST_RESPONSE
9
12
from tests .helpers .mock_responses import TEST_AGENCY_ID
10
13
from tests .helpers .mock_responses import TEST_DIRECTION_ID
11
14
from tests .helpers .mock_responses import TEST_ROUTE_ID
@@ -16,6 +19,62 @@ class TestNextBusClient(unittest.TestCase):
16
19
def setUp (self ):
17
20
self .client = NextBusClient ()
18
21
22
+ @unittest .mock .patch ("py_nextbus.client.NextBusClient._get" )
23
+ def test_list_agencies (self , mock_get : MagicMock ):
24
+ mock_get .return_value = MOCK_AGENCY_LIST_RESPONSE
25
+
26
+ agencies = self .client .agencies ()
27
+
28
+ # Check critical agency keys
29
+ for agency in agencies :
30
+ self .assertIsNotNone (agency ["id" ])
31
+ self .assertIsNotNone (agency ["name" ])
32
+
33
+ # Check test agency name
34
+ self .assertIn (TEST_AGENCY_ID , [agency ["id" ] for agency in agencies ])
35
+
36
+ mock_get .assert_called_once_with ("agencies" )
37
+
38
+ @unittest .mock .patch ("py_nextbus.client.NextBusClient._get" )
39
+ def test_list_routes (self , mock_get : MagicMock ):
40
+ mock_get .return_value = MOCK_ROUTE_LIST_RESPONSE
41
+
42
+ routes = self .client .routes (TEST_AGENCY_ID )
43
+
44
+ # Check critical route keys
45
+ for route in routes :
46
+ self .assertIsNotNone (route ["id" ])
47
+ self .assertIsNotNone (route ["title" ])
48
+
49
+ # Check test route id
50
+ self .assertIn (TEST_ROUTE_ID , [route ["id" ] for route in routes ])
51
+
52
+ mock_get .assert_called_once_with (f"agencies/{ TEST_AGENCY_ID } /routes" )
53
+
54
+ @unittest .mock .patch ("py_nextbus.client.NextBusClient._get" )
55
+ def test_route_details (self , mock_get : MagicMock ):
56
+ mock_get .return_value = MOCK_ROUTE_DETAILS_RESPONSE
57
+
58
+ route_details = self .client .route_details (
59
+ TEST_ROUTE_ID , agency_id = TEST_AGENCY_ID
60
+ )
61
+
62
+ # Check critical route detail keys
63
+ for stop in route_details ["stops" ]:
64
+ self .assertIsNotNone (stop ["id" ])
65
+ self .assertIsNotNone (stop ["name" ])
66
+
67
+ for direction in route_details ["directions" ]:
68
+ self .assertIsNotNone (direction ["name" ])
69
+ self .assertIsNotNone (direction ["useForUi" ])
70
+ self .assertIsNotNone (direction ["stops" ])
71
+
72
+ self .assertIn (TEST_STOP_ID , [stop ["id" ] for stop in route_details ["stops" ]])
73
+
74
+ mock_get .assert_called_once_with (
75
+ f"agencies/{ TEST_AGENCY_ID } /routes/{ TEST_ROUTE_ID } "
76
+ )
77
+
19
78
@unittest .mock .patch ("py_nextbus.client.NextBusClient._get" )
20
79
def test_predictions_for_stop_no_route (self , mock_get : MagicMock ):
21
80
mock_get .return_value = MOCK_PREDICTIONS_RESPONSE_NO_ROUTE
@@ -27,8 +86,7 @@ def test_predictions_for_stop_no_route(self, mock_get: MagicMock):
27
86
self .assertEqual ({r ["stop" ]["id" ] for r in result }, {TEST_STOP_ID })
28
87
self .assertEqual (len (result ), 3 ) # Results include all routes
29
88
30
- mock_get .assert_called_once ()
31
- mock_get .assert_called_with (
89
+ mock_get .assert_called_once_with (
32
90
f"agencies/{ TEST_AGENCY_ID } /stops/{ TEST_STOP_ID } /predictions" ,
33
91
)
34
92
@@ -46,13 +104,15 @@ def test_predictions_for_stop_with_route(self, mock_get: MagicMock):
46
104
# Assert all predictions are for the correct stop
47
105
self .assertEqual ({r ["stop" ]["id" ] for r in result }, {TEST_STOP_ID })
48
106
self .assertEqual ({r ["route" ]["id" ] for r in result }, {TEST_ROUTE_ID })
107
+ # Assert all predictions are for the correct direction
49
108
self .assertEqual (
50
109
{p ["direction" ]["id" ] for r in result for p in r ["values" ]},
51
110
{TEST_DIRECTION_ID },
52
111
)
112
+ # Assert we only have the one prediction for the stop and route
113
+ self .assertEqual (len (result ), 1 )
53
114
54
- mock_get .assert_called_once ()
55
- mock_get .assert_called_with (
115
+ mock_get .assert_called_once_with (
56
116
f"agencies/{ TEST_AGENCY_ID } /nstops/{ TEST_ROUTE_ID } :{ TEST_STOP_ID } /predictions" ,
57
117
)
58
118
0 commit comments