Repository: stigok/ruterstop

Issue:
Send queryparams til API-funksjonskall
Så man f.eks kan filtrere på retning med `GET /1234?direction=inbound`

Discussion:

Patch:
diff --git a/ruterstop/__init__.py b/ruterstop/__init__.py
index 0c1986c..d1d7f24 100644
--- a/ruterstop/__init__.py
+++ b/ruterstop/__init__.py
@@ -134,6 +134,23 @@ def parse_departures(raw_dict, *, date_fmt="%Y-%m-%dT%H:%M:%S%z"):
 
 
 @webapp.route("/<stop_id:int>")
+def get_departures_proxy(*args, **kwargs):
+    """
+    A proxy function for get_departures to make get_departures mock-/patchable
+    even after decorated by bottle.
+    Calls get_departures with whitelisted kwargs defined in the querystring of
+    the current web request.
+    """
+    q = bottle.request.query
+
+    if q.direction:
+        kwargs["directions"] = q.direction
+    if q.min_eta:
+        kwargs["min_eta"] = int(q.min_eta)
+
+    return get_departures(*args, **kwargs)
+
+
 def get_departures(*, stop_id=None, directions=None, min_eta=0, text=True):
     """
     Returns a filtered list of departures. If `text` is True, return stringified


Test patch:
diff --git a/ruterstop/tests/test_webapp.py b/ruterstop/tests/test_webapp.py
index c79d47a..7f05385 100644
--- a/ruterstop/tests/test_webapp.py
+++ b/ruterstop/tests/test_webapp.py
@@ -1,14 +1,5 @@
-"""
-Note for webtest and bottle:
-Even though I'd ideally want to mock ruterstop.get_departures, that function
-is decorated with @bottle.get() on import-time, consequently wrapping the
-underlying function before it is patched/mocked.
-A work-around for this is to mock get_realtime_stop instead, as it is the
-first function called in get_departures, and use that to e.g. raise Exceptions
-as a side-effect.
-"""
 from unittest import TestCase
-from unittest.mock import patch
+from unittest.mock import Mock, patch
 
 from webtest import TestApp
 
@@ -24,28 +15,30 @@ class WebAppTestCase(TestCase):
         self.app.reset()
         pass
 
-    # The patchability of this module isn't great for this kind of test
-    @patch('ruterstop.get_realtime_stop', return_value={"data": "foobar"})
-    @patch('ruterstop.parse_departures', returl_value=[])
-    def test_calls_api_on_proper_path(self, parse_departures, get_realtime_stop):
-        res = self.app.get('/1234')
-        get_realtime_stop.assert_called_once_with(stop_id=1234)
+    @patch("ruterstop.get_departures", return_value=None)
+    def test_calls_api_on_proper_path(self, mock):
+        res = self.app.get("/1234")
+        mock.assert_called_once_with(stop_id=1234)
 
-    def test_simple_404_error(self):
-        res = self.app.get('/', expect_errors=True)
-        self.assertEqual(res.content_type, 'text/plain')
+    @patch("ruterstop.get_departures", return_value=None)
+    def test_simple_404_error(self, mock):
+        res = self.app.get("/", expect_errors=True)
+        self.assertEqual(res.content_type, "text/plain")
         self.assertEqual(res.status_code, 404)
-        self.assertTrue(str(res.body).count('\n') <= 1) # a single line of text
         self.assertEqual(res.body, "Ugyldig stoppested".encode())
-
-    def test_simple_500_error(self):
-        with patch('ruterstop.get_realtime_stop') as mock:
-            mock.side_effect = Exception("Trigger a 500")
-
-            res = self.app.get('/1234', expect_errors=True)
-            mock.assert_called_once()
-
-            self.assertEqual(res.content_type, 'text/plain')
-            self.assertEqual(res.status_code, 500)
-            self.assertTrue(str(res.body).count('\n') <= 1) # a single line of text
-            self.assertEqual(res.body, "Feil på serveren".encode())
+        self.assertEqual(mock.call_count, 0)
+
+    @patch("ruterstop.get_departures", return_value=None)
+    def test_simple_500_error(self, mock):
+        mock.side_effect = Exception("barf")
+        res = self.app.get("/1234", expect_errors=True)
+        self.assertEqual(res.content_type, "text/plain")
+        self.assertEqual(res.status_code, 500)
+        self.assertEqual(res.body, "Feil på serveren".encode())
+        self.assertEqual(mock.call_count, 1)
+
+    @patch("ruterstop.get_departures", return_value=None)
+    def test_calls_api_with_querystring_params(self, mock):
+        self.app.get("/1234?direction=inbound&min_eta=5&bogusargs=1337")
+        mock.assert_called_once_with(stop_id=1234, directions="inbound",
+                                     min_eta=5)