Skip to content

Commit 790f62b

Browse files
Challe-Pmosbth
authored andcommitted
Unit test for when APIs are down
Adds unit tests to check if the correct messages are returned if request function fails
1 parent f942a2b commit 790f62b

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

test_marvin_actions.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def testSource(self):
146146

147147
def testBudord(self):
148148
"""Test that marvin knows all the commandments"""
149-
for n in range(1, 5):
149+
for n in range(1, 6):
150150
self.assertStringsOutput(marvin_actions.marvinBudord, f"budord #{n}", "budord", f"#{n}")
151151

152152
self.assertStringsOutput(marvin_actions.marvinBudord,"visa stentavla 1", "budord", "#1")
@@ -261,6 +261,15 @@ def testNameDayResponse(self):
261261
self.assertNameDayOutput("double", "Idag har Alfred,Alfrida namnsdag")
262262
self.assertNameDayOutput("nobody", "Ingen har namnsdag idag")
263263

264+
def testNameDayError(self):
265+
"""Tests that marvin returns the proper error message when nameday API is down"""
266+
with mock.patch("marvin_actions.requests.get", side_effect=Exception("API Down!")):
267+
self.assertStringsOutput(
268+
marvin_actions.marvinNameday,
269+
"har någon namnsdag idag?",
270+
"nameday",
271+
"error")
272+
264273
def testJokeRequest(self):
265274
"""Test that marvin sends a proper request for a joke"""
266275
with mock.patch("marvin_actions.requests") as r:
@@ -324,6 +333,15 @@ def testCommitResponse(self):
324333
expected = f"Använd detta meddelandet: '{message}'"
325334
self.assertActionOutput(marvin_actions.marvinCommit, "commit", expected)
326335

336+
def testCommitError(self):
337+
"""Tests that marvin sends the proper message when get commit fails"""
338+
with mock.patch("marvin_actions.requests.get", side_effect=Exception('API Down!')):
339+
self.assertStringsOutput(
340+
marvin_actions.marvinCommit,
341+
"vad skriver man efter commit -m?",
342+
"commit",
343+
"error")
344+
327345
def testMorning(self):
328346
"""Test that marvin wishes good morning, at most once per day"""
329347
marvin_general_actions.lastDateGreeted = None

0 commit comments

Comments
 (0)