Skip to content

Commit

Permalink
Use multiple context handles with a single with statement
Browse files Browse the repository at this point in the history
To reduce indentation.
  • Loading branch information
kh31d4r authored and mosbth committed Nov 24, 2024
1 parent 513e42f commit d857dad
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions test_marvin_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,11 @@ def assertBBQResponse(self, todaysDate, bbqDate, expectedMessageKey):
if expectedMessageKey in ["base", "week", "eternity"]:
message = message % bbqDate

with mock.patch("marvin_actions.datetime") as d:
with mock.patch("marvin_actions.datetime") as d, mock.patch("marvin_actions.random") as r:
d.date.today.return_value = todaysDate
with mock.patch("marvin_actions.random") as r:
r.randint.return_value = 1
expected = f"{url}. {message}"
self.assertActionOutput(marvin_actions.marvinTimeToBBQ, "dags att grilla", expected)
r.randint.return_value = 1
expected = f"{url}. {message}"
self.assertActionOutput(marvin_actions.marvinTimeToBBQ, "dags att grilla", expected)


def assertNameDayOutput(self, exampleFile, expectedOutput):
Expand Down Expand Up @@ -243,11 +242,10 @@ def testNameDayReaction(self):

def testNameDayRequest(self):
"""Test that marvin sends a proper request for nameday info"""
with mock.patch("marvin_actions.requests") as r:
with mock.patch("marvin_actions.datetime") as d:
d.datetime.now.return_value = date(2024, 1, 2)
self.executeAction(marvin_actions.marvinNameday, "namnsdag")
self.assertEqual(r.get.call_args.args[0], "https://api.dryg.net/dagar/v2.1/2024/1/2")
with mock.patch("marvin_actions.requests") as r, mock.patch("marvin_actions.datetime") as d:
d.datetime.now.return_value = date(2024, 1, 2)
self.executeAction(marvin_actions.marvinNameday, "namnsdag")
self.assertEqual(r.get.call_args.args[0], "https://api.dryg.net/dagar/v2.1/2024/1/2")

def testNameDayResponse(self):
"""Test that marvin properly parses nameday responses"""
Expand Down

0 comments on commit d857dad

Please sign in to comment.