Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed joke function #62

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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")

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 @@ -239,6 +247,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