Skip to content

Fix predictions method #8

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 3 additions & 5 deletions py_nextbus/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def get_route_config(self, route_tag=None, agency=None):

return self._perform_request(params=params)

def get_predictions(self, stop_tag, route_tag=None, agency=None):
def get_predictions(self, stop_tag, route_tag, agency=None):
"""Make a request to the NextBus API with the "predictions" command to get arrival time
predictions for a single stop. A route tag can optionally be provided to filter the
predictions down to only that particular route at the stop.
Expand Down Expand Up @@ -187,12 +187,10 @@ def get_predictions(self, stop_tag, route_tag=None, agency=None):
params = {
'command': 'predictions',
'a': agency,
's': stop_tag
's': stop_tag,
'r': route_tag,
}

if route_tag is not None:
params['routeTag'] = route_tag

return self._perform_request(params=params)

def get_predictions_for_multi_stops(self, stops, agency=None):
Expand Down
29 changes: 4 additions & 25 deletions py_nextbus/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,42 +237,21 @@ def test_parameters_passed_to_perform_request(self, perform_request, get_agency)
"""Test that the correct parameters are passed to the _perform_request method."""

stop_tag = 12345
route_tag = 'foo'
agency = 'foo'
nextbus_client = client.NextBusClient(output_format='json')
nextbus_client.get_predictions(stop_tag=stop_tag,
route_tag=route_tag,
agency=agency)

get_agency.assert_called_once_with(agency)
perform_request.assert_called_once_with(params={
'command': 'predictions',
'a': get_agency.return_value,
's': stop_tag
's': stop_tag,
'r': route_tag,
})

def test_route_tag_added_to_parameters_if_not_none(self, perform_request, get_agency):
"""Test that the "routeTag" key is added to the parameters passed to the the
_perform_request method if the value of the route_tag argument is not None."""

route_tag = 'foo'
nextbus_client = client.NextBusClient(output_format='json')
nextbus_client.get_predictions(stop_tag=12345,
route_tag=route_tag)

params = perform_request.call_args[1]['params']
self.assertIn('routeTag', params)
self.assertEqual(params['routeTag'], route_tag)

def test_route_tag_not_added_to_parameters_if_none(self, perform_request, get_agency):
"""Test that the "routeTag" key is not added to the parameters passed to the
_perform_request method if the value of the route_tag argument is None."""

nextbus_client = client.NextBusClient(output_format='json')
nextbus_client.get_predictions(stop_tag=12345,
route_tag=None)

params = perform_request.call_args[1]['params']
self.assertNotIn('routeTag', params)

@unittest.mock.patch('client.NextBusClient._get_agency')
@unittest.mock.patch('client.NextBusClient._perform_request')
class TestGetPredictionsForMultiStops(unittest.TestCase):
Expand Down