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

Some unitttest for when APIs fail. #70

Merged
merged 3 commits into from
Oct 6, 2024
Merged

Conversation

Challe-P
Copy link
Contributor

@Challe-P Challe-P commented Oct 5, 2024

No description provided.

Copy link
Contributor

@kh31d4r kh31d4r left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your commit message is all on one line, making it hard to read, try making a header and a body, there are some great tips here: https://cbea.ms/git-commit/ (have a look at the "seven rules")

@Challe-P
Copy link
Contributor Author

Challe-P commented Oct 5, 2024

I promise to do better in the future!

@@ -579,5 +579,7 @@ def marvinCommit(row):
msg = None
if any(r in row for r in ["commit", "-m"]):
commitMsg = getCommit()
if commitMsg == "Du får komma på ett själv. Jag är trasig för tillfället!":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we could fix getCommit() so that it returns the right value to begin with?

Copy link
Contributor Author

@Challe-P Challe-P Oct 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this?

def getCommit():
    """
    Retrieves random commit message from whatthecommit.com/index.html
    """
    try:
        url = getString("commit", "url")
        r = requests.get(url, timeout=5)
        res = r.text.strip()
        msg = "Använd detta meddelandet: '{}'".format(res)
        return msg
    except Exception:
        return getString("commit", "error")

def marvinCommit(row):
    """
    Display a random commit message
    """
    msg = None
    if any(r in row for r in ["commit", "-m"]):
        commitMsg = getCommit()
    return commitMsg

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this is good. if we were going to reuse getCommit() somewhere, we might want to have it only return the commit message, or throw an exception, and do the string formatting in marvinCommit(). That way getCommit() acts more like an API, and marvinCommit() defines the interaction with marvin. but i don't think that will ever happen, so we don't need to go that far.

def testVideoOfTodayFail(self):
"""Tests that marvin says the right thing when he doesn't have a video for the day"""
with mock.patch("marvin_actions.getString") as gs:
gs.return_value = ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will getString return an empty string if the key doesn't exist?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, it throws a KeyError. Maybe the code should be wrapped in a try/catch block?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i mostly wanted the tests to test the actual behavior, rather than force execution of all code branches

"""Tests that marvin says the right thing when he doesn't have a video for the day"""
with mock.patch("marvin_actions.getString") as gs:
gs.return_value = ""
response = " Jag har ännu ingen passande video för denna dagen."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the leading space really be there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The returned string has a leading space, for some reason.

def videoOfToday():
    """
    Check what day it is and provide a url to a suitable video together with a greeting.
    """
    dayNum = datetime.date.weekday(datetime.date.today()) + 1
    msg = getString("weekdays", str(dayNum))
    video = getString("video-of-today", str(dayNum))

    if video:
        msg += " En passande video är " + video
    else:
        msg += " Jag har ännu ingen passande video för denna dagen."

    return msg

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should remove that in a separate PR then, leave it for now.

"""Tests that marvin returns the proper error message when nameday API is down"""
with mock.patch("marvin_actions.requests.get", side_effect=Exception('API Down!')) as r:
res = self.executeAction(marvin_actions.marvinNameday, 'namnsdag')
self.assertEqual(res, "Något gick snett, jag har inte en susning om någon har namnsdag idag eller inte.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't it possible to use assertStringsOutput here?

"""Tests that marvin returns the proper error message when joke API is down"""
with mock.patch("marvin_actions.requests.get", side_effect=Exception('API Down!')) as r:
res = self.executeAction(marvin_actions.marvinJoke, 'joke')
self.assertEqual(res, "Chuck Norris har inte tid underhålla er idag!")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, assertStringsOutput?

"""Tests that marvin sends the proper message when get commit fails"""
with mock.patch("marvin_actions.requests.get", side_effect=Exception('API Down!')) as r:
res = self.executeAction(marvin_actions.marvinCommit, "vad skriver man efter commit -m?")
self.assertEqual(res, "Du får komma på ett själv. Jag är trasig för tillfället!")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and again here

@kh31d4r
Copy link
Contributor

kh31d4r commented Oct 5, 2024

I promise to do better in the future!

you can change the message and use force push to update the message

@mosbth
Copy link
Owner

mosbth commented Oct 6, 2024

Let me know when its ready to merge.

@kh31d4r can let us know if its best practise to resolve each conversation here, or just say "its fine, we talked about it and agreed what to do".

@kh31d4r
Copy link
Contributor

kh31d4r commented Oct 6, 2024

im not sure what is best practice for github workflows. personally, i like to keep the history clean by amending commits, but maybe it's the norm on github to fix comments in separate commits?

@mosbth
Copy link
Owner

mosbth commented Oct 6, 2024

I guess that when it comes to a PR, and one are asked to fix something in it. before merge, then it sounds like the content of the PR should be updated. It feels like "all in one place, one prupose".

@kh31d4r
Copy link
Contributor

kh31d4r commented Oct 6, 2024

Yes, within the PR for sure. But by adding new commits to the PR or by updating the existing ones?

@Challe-P Challe-P changed the title Some unitttest for when APIs fail. Fixed commit message function on fail Some unitttest for when APIs fail. Oct 6, 2024
Some format to f-strings, some lines that were too long
Fixes sun functionality by changing to new API. Also adds unit tests for sun function.
Adds unit tests to check if the correct messages are returned if request function fails
@mosbth mosbth merged commit 790f62b into mosbth:master Oct 6, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants