Skip to content

Commit c7b4464

Browse files
authored
Merge pull request #6 from ViViDboarder/more-unit-tests
Add more unit tests
2 parents 2760111 + c5b4b6d commit c7b4464

File tree

2 files changed

+66
-6
lines changed

2 files changed

+66
-6
lines changed

acceptance/client_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class ClientTest(unittest.TestCase):
1313
def setUp(self):
1414
self.client = NextBusClient()
1515

16-
def test_list_gencies(self):
17-
agencies: list[dict[str, str]] = self.client.agencies()
16+
def test_list_agencies(self):
17+
agencies = self.client.agencies()
1818

1919
# Check critical agency keys
2020
for agency in agencies:

tests/client_test.py

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
from unittest.mock import MagicMock
55

66
from py_nextbus.client import NextBusClient
7+
from tests.helpers.mock_responses import MOCK_AGENCY_LIST_RESPONSE
78
from tests.helpers.mock_responses import MOCK_PREDICTIONS_RESPONSE_NO_ROUTE
89
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
912
from tests.helpers.mock_responses import TEST_AGENCY_ID
1013
from tests.helpers.mock_responses import TEST_DIRECTION_ID
1114
from tests.helpers.mock_responses import TEST_ROUTE_ID
@@ -16,6 +19,62 @@ class TestNextBusClient(unittest.TestCase):
1619
def setUp(self):
1720
self.client = NextBusClient()
1821

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+
1978
@unittest.mock.patch("py_nextbus.client.NextBusClient._get")
2079
def test_predictions_for_stop_no_route(self, mock_get: MagicMock):
2180
mock_get.return_value = MOCK_PREDICTIONS_RESPONSE_NO_ROUTE
@@ -27,8 +86,7 @@ def test_predictions_for_stop_no_route(self, mock_get: MagicMock):
2786
self.assertEqual({r["stop"]["id"] for r in result}, {TEST_STOP_ID})
2887
self.assertEqual(len(result), 3) # Results include all routes
2988

30-
mock_get.assert_called_once()
31-
mock_get.assert_called_with(
89+
mock_get.assert_called_once_with(
3290
f"agencies/{TEST_AGENCY_ID}/stops/{TEST_STOP_ID}/predictions",
3391
)
3492

@@ -46,13 +104,15 @@ def test_predictions_for_stop_with_route(self, mock_get: MagicMock):
46104
# Assert all predictions are for the correct stop
47105
self.assertEqual({r["stop"]["id"] for r in result}, {TEST_STOP_ID})
48106
self.assertEqual({r["route"]["id"] for r in result}, {TEST_ROUTE_ID})
107+
# Assert all predictions are for the correct direction
49108
self.assertEqual(
50109
{p["direction"]["id"] for r in result for p in r["values"]},
51110
{TEST_DIRECTION_ID},
52111
)
112+
# Assert we only have the one prediction for the stop and route
113+
self.assertEqual(len(result), 1)
53114

54-
mock_get.assert_called_once()
55-
mock_get.assert_called_with(
115+
mock_get.assert_called_once_with(
56116
f"agencies/{TEST_AGENCY_ID}/nstops/{TEST_ROUTE_ID}:{TEST_STOP_ID}/predictions",
57117
)
58118

0 commit comments

Comments
 (0)