-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
187 lines (136 loc) · 5.05 KB
/
tests.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import unittest
from application import app, is_isbn_code, login_only
from flask import session
class TestViews(unittest.TestCase):
def setUp(self):
self.app = app.test_client()
self.app.testing = True
def tearDown(self):
pass
def test_index_get_response(self):
r = self.app.get('/')
self.assertEqual(200, r.status_code)
def test_index_post_response(self):
r = self.app.post('/')
self.assertEqual(405, r.status_code)
def test_login_get_response(self):
r = self.app.get('/login')
self.assertEqual(200, r.status_code)
def test_login_post_response(self):
r = self.app.post('/login')
self.assertEqual(200, r.status_code)
def test_register_get_response(self):
r = self.app.get('/register')
self.assertEqual(200, r.status_code)
def test_register_post_response(self):
r = self.app.post('/register')
self.assertEqual(200, r.status_code)
def test_logout_get_response(self):
r = self.app.get('/logout')
self.assertEqual(302, r.status_code)
def test_logout_post_response(self):
r = self.app.post('/logout')
self.assertEqual(405, r.status_code)
def test_search_get_response(self):
r = self.app.get('/search')
self.assertEqual(302, r.status_code)
def test_search_post_response(self):
r = self.app.post('/search')
self.assertEqual(302, r.status_code)
def test_book_get_response_empty(self):
r = self.app.get('/book')
self.assertEqual(404, r.status_code)
def test_book_get_response(self):
r = self.app.get('/book/0380795272')
self.assertEqual(302, r.status_code)
def test_book_post_response(self):
r = self.app.post('/book/0380795272')
self.assertEqual(302, r.status_code)
def test_author_get_response_empty(self):
r = self.app.get('/author')
self.assertEqual(404, r.status_code)
def test_author_get_response(self):
r = self.app.get('/author/1')
self.assertEqual(302, r.status_code)
def test_author_post_response(self):
r = self.app.post('/author/0380795272')
self.assertEqual(405, r.status_code)
class TestHelpers(unittest.TestCase):
def setUp(self):
self.app = app.test_client()
self.app.testing = True
def tearDown(self):
pass
def test_empty_not_isbn(self):
check = is_isbn_code('')
self.assertEqual(False, check)
def test_author_not_isbn(self):
check = is_isbn_code('Raymond E. Feist')
self.assertEqual(False, check)
def test_title_not_isbn(self):
check = is_isbn_code('Krondor: The Betrayal')
self.assertEqual(False, check)
def test_isbn10_is_isbn(self):
check = is_isbn_code('0380795272')
self.assertEqual('0380795272', check)
def test_isbn13_is_isbn(self):
check = is_isbn_code('9780380795277')
self.assertEqual('0380795272', check)
def test_isbn_cleanup(self):
check = is_isbn_code('#0-380-79527-2/')
self.assertEqual('0380795272', check)
class TestFuncDeco(unittest.TestCase):
def setUp(self):
self.app = app.test_client()
self.app.testing = True
def tearDown(self):
with app.test_request_context():
session['user'] = None
def test_func_wo_dec_returns_1(self):
def func():
return 1
check = func()
self.assertEqual(1, check)
def test_func_w_dec_returns_302(self):
with app.test_request_context():
@login_only
def func():
return 1
check = func()
self.assertEqual(302, check.status_code)
def test_func_w_dec_returns_1_when_session(self):
with app.test_request_context():
session['user'] = 'test'
@login_only
def func():
return 1
check = func()
self.assertEqual(1, check)
class TestAPI(unittest.TestCase):
def setUp(self):
self.app = app.test_client()
self.app.testing = True
def tearDown(self):
pass
def test_api_404_response(self):
r = self.app.get('/api/wrongisbn')
self.assertEqual(404, r.status_code)
def test_api_get_response(self):
r = self.app.get('/api/0380795272')
self.assertEqual(200, r.status_code)
def test_api_return_json(self):
r = self.app.get('/api/0380795272')
self.assertEqual(True, r.is_json)
def test_api_json_keys(self):
r = self.app.get('/api/0380795272')
expected_json_keys = [
'author', 'average_score', 'isbn', 'review_count', 'title', 'year']
actual_json_keys = r.get_json().keys()
# symmetric difference should be an empty list (no difference)
diff = set(expected_json_keys) ^ set(actual_json_keys)
self.assertEqual(set(), diff)
def test_api_post_response(self):
r = self.app.post('/api/0380795272')
self.assertEqual(405, r.status_code)
if __name__ == '__main__':
unittest.main()