Skip to content

Commit

Permalink
Fix tests, improve alfred captions
Browse files Browse the repository at this point in the history
  • Loading branch information
artemy committed Jul 6, 2024
1 parent f0b55fa commit 18b0a3d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion alfred/info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
<key>subtext</key>
<string></string>
<key>title</key>
<string>Destination station</string>
<string>Please provide destination station</string>
<key>withspace</key>
<true/>
</dict>
Expand Down
12 changes: 11 additions & 1 deletion ns_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']]
Expand Down
14 changes: 7 additions & 7 deletions ns_schedule_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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)


Expand Down

0 comments on commit 18b0a3d

Please sign in to comment.