Skip to content

Commit

Permalink
Merge pull request #62 from Challe-P/master
Browse files Browse the repository at this point in the history
Fixed joke function
  • Loading branch information
mosbth authored Oct 1, 2024
2 parents 95b3dbe + 33d3c26 commit fc74f39
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
9 changes: 9 additions & 0 deletions jokeFiles/joke.json
Original file line number Diff line number Diff line change
@@ -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."
}
10 changes: 4 additions & 6 deletions marvin_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,15 +541,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")

Expand Down
2 changes: 1 addition & 1 deletion marvin_strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
18 changes: 18 additions & 0 deletions test_marvin_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -243,6 +251,16 @@ def testNameDayResponse(self):
self.assertNameDayOutput("double", "Idag har Alfred,Alfrida namnsdag")
self.assertNameDayOutput("nobody", "Ingen har namnsdag idag")

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")
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")
Expand Down

0 comments on commit fc74f39

Please sign in to comment.