Skip to content

Add more unit tests #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions acceptance/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class ClientTest(unittest.TestCase):
def setUp(self):
self.client = NextBusClient()

def test_list_gencies(self):
agencies: list[dict[str, str]] = self.client.agencies()
def test_list_agencies(self):
agencies = self.client.agencies()

# Check critical agency keys
for agency in agencies:
Expand Down
68 changes: 64 additions & 4 deletions tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
from unittest.mock import MagicMock

from py_nextbus.client import NextBusClient
from tests.helpers.mock_responses import MOCK_AGENCY_LIST_RESPONSE
from tests.helpers.mock_responses import MOCK_PREDICTIONS_RESPONSE_NO_ROUTE
from tests.helpers.mock_responses import MOCK_PREDICTIONS_RESPONSE_WITH_ROUTE
from tests.helpers.mock_responses import MOCK_ROUTE_DETAILS_RESPONSE
from tests.helpers.mock_responses import MOCK_ROUTE_LIST_RESPONSE
from tests.helpers.mock_responses import TEST_AGENCY_ID
from tests.helpers.mock_responses import TEST_DIRECTION_ID
from tests.helpers.mock_responses import TEST_ROUTE_ID
Expand All @@ -16,6 +19,62 @@ class TestNextBusClient(unittest.TestCase):
def setUp(self):
self.client = NextBusClient()

@unittest.mock.patch("py_nextbus.client.NextBusClient._get")
def test_list_agencies(self, mock_get: MagicMock):
mock_get.return_value = MOCK_AGENCY_LIST_RESPONSE

agencies = self.client.agencies()

# Check critical agency keys
for agency in agencies:
self.assertIsNotNone(agency["id"])
self.assertIsNotNone(agency["name"])

# Check test agency name
self.assertIn(TEST_AGENCY_ID, [agency["id"] for agency in agencies])

mock_get.assert_called_once_with("agencies")

@unittest.mock.patch("py_nextbus.client.NextBusClient._get")
def test_list_routes(self, mock_get: MagicMock):
mock_get.return_value = MOCK_ROUTE_LIST_RESPONSE

routes = self.client.routes(TEST_AGENCY_ID)

# Check critical route keys
for route in routes:
self.assertIsNotNone(route["id"])
self.assertIsNotNone(route["title"])

# Check test route id
self.assertIn(TEST_ROUTE_ID, [route["id"] for route in routes])

mock_get.assert_called_once_with(f"agencies/{TEST_AGENCY_ID}/routes")

@unittest.mock.patch("py_nextbus.client.NextBusClient._get")
def test_route_details(self, mock_get: MagicMock):
mock_get.return_value = MOCK_ROUTE_DETAILS_RESPONSE

route_details = self.client.route_details(
TEST_ROUTE_ID, agency_id=TEST_AGENCY_ID
)

# Check critical route detail keys
for stop in route_details["stops"]:
self.assertIsNotNone(stop["id"])
self.assertIsNotNone(stop["name"])

for direction in route_details["directions"]:
self.assertIsNotNone(direction["name"])
self.assertIsNotNone(direction["useForUi"])
self.assertIsNotNone(direction["stops"])

self.assertIn(TEST_STOP_ID, [stop["id"] for stop in route_details["stops"]])

mock_get.assert_called_once_with(
f"agencies/{TEST_AGENCY_ID}/routes/{TEST_ROUTE_ID}"
)

@unittest.mock.patch("py_nextbus.client.NextBusClient._get")
def test_predictions_for_stop_no_route(self, mock_get: MagicMock):
mock_get.return_value = MOCK_PREDICTIONS_RESPONSE_NO_ROUTE
Expand All @@ -27,8 +86,7 @@ def test_predictions_for_stop_no_route(self, mock_get: MagicMock):
self.assertEqual({r["stop"]["id"] for r in result}, {TEST_STOP_ID})
self.assertEqual(len(result), 3) # Results include all routes

mock_get.assert_called_once()
mock_get.assert_called_with(
mock_get.assert_called_once_with(
f"agencies/{TEST_AGENCY_ID}/stops/{TEST_STOP_ID}/predictions",
)

Expand All @@ -46,13 +104,15 @@ def test_predictions_for_stop_with_route(self, mock_get: MagicMock):
# Assert all predictions are for the correct stop
self.assertEqual({r["stop"]["id"] for r in result}, {TEST_STOP_ID})
self.assertEqual({r["route"]["id"] for r in result}, {TEST_ROUTE_ID})
# Assert all predictions are for the correct direction
self.assertEqual(
{p["direction"]["id"] for r in result for p in r["values"]},
{TEST_DIRECTION_ID},
)
# Assert we only have the one prediction for the stop and route
self.assertEqual(len(result), 1)

mock_get.assert_called_once()
mock_get.assert_called_with(
mock_get.assert_called_once_with(
f"agencies/{TEST_AGENCY_ID}/nstops/{TEST_ROUTE_ID}:{TEST_STOP_ID}/predictions",
)

Expand Down