krz/daily-poem
Poetry emailed, at your will.
clone: git clone https://gitbay.org/krz/daily-poem.git
main: tests/test_main.py · raw
1import unittest
2from unittest.mock import patch
3
4from main import build_message, fetch_poem
5
6
7class FakeResponse:
8 def __init__(self, payload):
9 self._payload = payload
10
11 def json(self):
12 return self._payload
13
14
15class TestBuildMessage(unittest.TestCase):
16 def test_headers_and_body(self):
17 msg = build_message("A Title", "An Author", "4", "one\ntwo\n", "from@x", "to@y")
18
19 self.assertEqual(msg["Subject"], "Your Daily Poem (4 lines)")
20 self.assertEqual(msg["From"], "from@x")
21 self.assertEqual(msg["To"], "to@y")
22 self.assertIn("A Title", msg.get_payload())
23 self.assertIn("An Author", msg.get_payload())
24 self.assertIn("one\ntwo", msg.get_payload())
25
26
27PAYLOAD = [
28 {
29 "title": "Sonnet",
30 "author": "Anon",
31 "linecount": "2",
32 "lines": ["first line", "second line"],
33 }
34]
35
36
37class TestFetchPoem(unittest.TestCase):
38 """PoetryDB is stubbed: the test covers the shape this code expects from it,
39 not the service being up."""
40
41 def test_fields_are_extracted_and_lines_joined(self):
42 with patch("main.requests.get", return_value=FakeResponse(PAYLOAD)):
43 title, author, line_count, lines = fetch_poem()
44
45 self.assertEqual(title, "Sonnet")
46 self.assertEqual(author, "Anon")
47 self.assertEqual(line_count, "2")
48 self.assertEqual(lines, "first line\nsecond line\n")