From 587963284ca0b22053ace93802053ee7c0c2ccfa Mon Sep 17 00:00:00 2001 From: Vilhelm Malmberg Eskilsson Date: Tue, 1 Oct 2024 16:45:26 +0200 Subject: [PATCH 1/2] Fixed joke function by changing to new URL and changing call to requests. Added unit tests for joke function --- jokeFiles/joke.json | 9 +++++++++ marvin_actions.py | 10 ++++------ marvin_strings.json | 2 +- test_marvin_actions.py | 18 ++++++++++++++++++ 4 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 jokeFiles/joke.json diff --git a/jokeFiles/joke.json b/jokeFiles/joke.json new file mode 100644 index 0000000..2a3e3b9 --- /dev/null +++ b/jokeFiles/joke.json @@ -0,0 +1,9 @@ +{ + "categories":["dev"], + "created_at":"2020-01-05 13:42:19.324003", + "icon_url":"https://api.chucknorris.io/img/avatar/chuck-norris.png", + "id":"ae-78cogr-cb6x9hluwqtw", + "updated_at":"2020-01-05 13:42:19.324003", + "url":"https://api.chucknorris.io/jokes/ae-78cogr-cb6x9hluwqtw", + "value":"There is no Esc key on Chuck Norris' keyboard, because no one escapes Chuck Norris." +} diff --git a/marvin_actions.py b/marvin_actions.py index d4d528c..811b036 100644 --- a/marvin_actions.py +++ b/marvin_actions.py @@ -544,15 +544,13 @@ def marvinPrinciple(row): def getJoke(): """ - Retrieves joke from api.icndb.com/jokes/random?limitTo=[nerdy] + Retrieves joke from api.chucknorris.io/jokes/random?category=dev """ try: url = getString("joke", "url") - soup = urlopen(url) - rawData = soup.read() - encoding = soup.info().get_content_charset('utf8') - joke = json.loads(rawData.decode(encoding)) - return joke["value"]["joke"] + r = requests.get(url, timeout=5) + joke_data = r.json() + return joke_data["value"] except Exception: return getString("joke", "error") diff --git a/marvin_strings.json b/marvin_strings.json index 2ac8638..1fbcc57 100644 --- a/marvin_strings.json +++ b/marvin_strings.json @@ -264,7 +264,7 @@ "any": "Det finns många filosofier som en programmerare kan förhålla sig till. Hämta lite inspiration från en längre lista https://en.wikipedia.org/wiki/List_of_software_development_philosophies" }, "joke": { - "url": "http://api.icndb.com/jokes/random?limitTo=[nerdy]", + "url": "https://api.chucknorris.io/jokes/random?category=dev", "error": "Chuck Norris har inte tid underhålla er idag!" }, "commit": { diff --git a/test_marvin_actions.py b/test_marvin_actions.py index 54fc5d6..46d71eb 100644 --- a/test_marvin_actions.py +++ b/test_marvin_actions.py @@ -80,6 +80,14 @@ def assertNameDayOutput(self, exampleFile, expectedOutput): r.get.return_value = response self.assertActionOutput(marvin_actions.marvinNameday, "nameday", expectedOutput) + def assertJokeOutput(self, exampleFile, expectedOutput): + """Assert that a joke is returned, given an input file""" + with open(f"jokeFiles/{exampleFile}.json", "r", encoding="UTF-8") as f: + response = requests.models.Response() + response._content = str.encode(json.dumps(json.load(f))) + with mock.patch("marvin_actions.requests") as r: + r.get.return_value = response + self.assertActionOutput(marvin_actions.marvinJoke, "joke", expectedOutput) def testSmile(self): """Test that marvin can smile""" @@ -239,6 +247,16 @@ def testNameDayResponse(self): self.assertNameDayOutput("double", "Idag har Alfred,Alfrida namnsdag") self.assertNameDayOutput("nobody", "Ingen har namnsdag idag") + def testNameDayRequest(self): + """Test that marvin sends a proper request for a joke""" + with mock.patch("marvin_actions.requests") as r: + self.executeAction(marvin_actions.marvinJoke, "joke") + self.assertEqual(r.get.call_args.args[0], "https://api.chucknorris.io/jokes/random?category=dev") + + def testJoke(self): + """Test that marvin sends a joke when requested""" + self.assertJokeOutput("joke", "There is no Esc key on Chuck Norris' keyboard, because no one escapes Chuck Norris.") + def testUptime(self): """Test that marvin can provide the link to the uptime tournament""" self.assertStringsOutput(marvin_actions.marvinUptime, "visa lite uptime", "uptime", "info") From 33d3c26eb75cddec021d056bdbda78d507c71d26 Mon Sep 17 00:00:00 2001 From: Vilhelm Malmberg Eskilsson Date: Tue, 1 Oct 2024 16:48:39 +0200 Subject: [PATCH 2/2] Typo in function name --- test_marvin_actions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_marvin_actions.py b/test_marvin_actions.py index 46d71eb..2e46d0f 100644 --- a/test_marvin_actions.py +++ b/test_marvin_actions.py @@ -247,7 +247,7 @@ def testNameDayResponse(self): self.assertNameDayOutput("double", "Idag har Alfred,Alfrida namnsdag") self.assertNameDayOutput("nobody", "Ingen har namnsdag idag") - def testNameDayRequest(self): + def testJokeRequest(self): """Test that marvin sends a proper request for a joke""" with mock.patch("marvin_actions.requests") as r: self.executeAction(marvin_actions.marvinJoke, "joke")