-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.py
118 lines (87 loc) · 3.47 KB
/
main_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""
Test suite for the bookmarks app.
"""
import unittest
import mock
from testing.assertions import ResponseAssertions
from testing import helpers
from testing.cptestcase import BaseCherryPyTestCase
import apps.bookmarks.main
class TestBookmarks(BaseCherryPyTestCase, ResponseAssertions):
"""
Tests for the bookmarks application controller
"""
@classmethod
def setUpClass(cls):
"""Start a faux cherrypy server"""
helpers.start_server(apps.bookmarks.main.Controller)
@classmethod
def tearDownClass(cls):
"""Shut down the faux server"""
helpers.stop_server()
def test_allow(self):
"""Verify the controller's supported HTTP methods"""
response = self.request("/", method="HEAD")
self.assertAllowedMethods(response, ("GET", "POST", "DELETE"))
@mock.patch("cherrypy.tools.negotiable.render_html")
@mock.patch("cherrypy.engine.publish")
def test_empty(self, publish_mock, render_mock):
"""If the database is empty, a no-records message is returned"""
def side_effect(*args, **_):
"""Side effects local function"""
if args[0] == "bookmarks:recent":
return [[[], 0, _]]
return mock.DEFAULT
publish_mock.side_effect = side_effect
self.request("/")
self.assertEqual(
len(helpers.html_var(render_mock, "bookmarks")),
0
)
self.assertIsNone(helpers.html_var(render_mock, "query"))
@mock.patch("cherrypy.engine.publish")
def test_add_success(self, publish_mock):
"""A URL can be added to the database"""
def side_effect(*args, **_):
"""Side effects local function"""
if args[0] == "scheduler:add":
return [True]
return mock.DEFAULT
publish_mock.side_effect = side_effect
response = self.request("/", url="http://example.com", method="POST")
self.assertEqual(response.code, 204)
@mock.patch("cherrypy.engine.publish")
def test_add_fail(self, publish_mock):
"""URLs must be well-formed"""
def side_effect(*args, **_):
"""Side effects local function"""
if args[0] == "scheduler:add":
return [False]
return mock.DEFAULT
publish_mock.side_effect = side_effect
response = self.request("/", url="not-a-url", method="POST")
self.assertEqual(response.code, 400)
@mock.patch("cherrypy.engine.publish")
def test_delete_fail(self, publish_mock):
"""Deletion fails if the URL is not found"""
def side_effect(*args, **_):
"""Side effects local function"""
if args[0] == "bookmarks:remove":
return [0]
return mock.DEFAULT
publish_mock.side_effect = side_effect
response = self.request("/", url="http://example.com", method="DELETE")
self.assertEqual(response.code, 404)
@mock.patch("cherrypy.engine.publish")
def test_delete_success(self, publish_mock):
"""Successful deletion sends no response"""
def side_effect(*args, **_):
"""Side effects local function"""
if args[0] == "bookmarks:remove":
return [1]
return mock.DEFAULT
publish_mock.side_effect = side_effect
response = self.request("/", url="http://example.com", method="DELETE")
self.assertEqual(response.code, 204)
if __name__ == "__main__":
unittest.main()