From 18b0a3df13338d49052d018aeaae4cf98501630d Mon Sep 17 00:00:00 2001 From: Artem Makarov <132329+artemy@users.noreply.github.com> Date: Sat, 6 Jul 2024 17:17:24 +0200 Subject: [PATCH] Fix tests, improve alfred captions --- alfred/info.plist | 2 +- ns_schedule.py | 12 +++++++++++- ns_schedule_test.py | 14 +++++++------- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/alfred/info.plist b/alfred/info.plist index 50179cf..12efc42 100644 --- a/alfred/info.plist +++ b/alfred/info.plist @@ -147,7 +147,7 @@ subtext title - Destination station + Please provide destination station withspace diff --git a/ns_schedule.py b/ns_schedule.py index 95baa07..1e26b36 100644 --- a/ns_schedule.py +++ b/ns_schedule.py @@ -83,9 +83,19 @@ def new_item_from_trip(trip): 'arg': trip['shareUrl']['uri']} +def extract_arguments(): + try: + if len(sys.argv) == 3: + if sys.argv[1] and sys.argv[2]: + return sys.argv[1], sys.argv[2] + raise IndexError + except IndexError: + raise NoArgsError + + def retrieve_schedule(): try: - (origin, destination) = sys.argv[1], sys.argv[2] + (origin, destination) = extract_arguments() response = call_api(origin, destination) return [new_item_from_trip(trip) for trip in response['trips']] diff --git a/ns_schedule_test.py b/ns_schedule_test.py index be1a234..73d6cb4 100644 --- a/ns_schedule_test.py +++ b/ns_schedule_test.py @@ -15,7 +15,7 @@ def test_api_happy_flow(self): self.assertEqual(expected, create_headers()) @mock.patch("os.environ", {}) - @mock.patch("sys.argv", ["main", "Foo Bar"]) + @mock.patch("sys.argv", ["main", "Foo", "Bar"]) def test_no_api_key(self): expected = [{'title': 'Please specify API key in settings'}] self.assertEqual(expected, retrieve_schedule()) @@ -42,7 +42,7 @@ def test_too_many_args(self): self.assertEqual(expected, retrieve_schedule()) @mock.patch("os.environ", {'NS_APIKEY': 'Foo'}) - @mock.patch("sys.argv", ["main", "Foo Bar"]) + @mock.patch("sys.argv", ["main", "Foo", "Bar"]) def test_happyflow(self): with open('testdata/happyflow.json') as json_file: with mock.patch('urllib.request.urlopen') as urlopen_mock: @@ -64,7 +64,7 @@ def test_happyflow(self): self.assertEqual(expected, retrieve_schedule()) @mock.patch("os.environ", {'NS_APIKEY': 'Foo'}) - @mock.patch("sys.argv", ["main", "Foo Bar"]) + @mock.patch("sys.argv", ["main", "Foo", "Bar"]) def test_happyflow_cancelled(self): with open('testdata/cancelled.json') as json_file: with mock.patch('urllib.request.urlopen') as urlopen_mock: @@ -86,7 +86,7 @@ def test_happyflow_cancelled(self): self.assertEqual(expected, retrieve_schedule()) @mock.patch("os.environ", {'NS_APIKEY': 'Foo'}) - @mock.patch("sys.argv", ["main", "Foo Bar"]) + @mock.patch("sys.argv", ["main", "Foo", "Bar"]) def test_malformed_json(self): with open('testdata/malformed.json') as json_file: with mock.patch('urllib.request.urlopen') as urlopen_mock: @@ -97,15 +97,15 @@ def test_malformed_json(self): @mock.patch('urllib.request.urlopen') @mock.patch("os.environ", {'NS_APIKEY': 'Foo'}) - @mock.patch("sys.argv", ["main", "Foo Bar"]) + @mock.patch("sys.argv", ["main", "Foo", "Bar"]) def test_bad_request(self, urlopen_mock): urlopen_mock.side_effect = urllib.error.HTTPError(*[None] * 5) expected = [{"title": "Error contacting server"}] self.assertEqual(expected, retrieve_schedule()) - @mock.patch("sys.argv", ["main", "Den_Haag_Centraal Amsterdam_Centraal"]) + @mock.patch("sys.argv", ["main", "Den Haag Centraal", "Amsterdam Centraal"]) def test_argument_parsing(self): - expected = ["Den Haag Centraal", "Amsterdam Centraal"] + expected = ("Den Haag Centraal", "Amsterdam Centraal") self.assertEqual(extract_arguments(), expected)