-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
214 lines (177 loc) · 7.18 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python3
# -------------------------------
# Copyright (C) 2016
# IBDB
# -------------------------------
# -------
# Imports
# -------
from unittest import main, TestCase
from models import *
from app import app
# ----------
# TestModels
# ----------
class TestModels (TestCase):
# ------
# Author
# ------
def test_model_author_1(self):
with app.app_context():
testauthor = Author.query.get(1)
self.assertEqual(testauthor.id, 1)
def test_model_author_2(self):
with app.app_context():
testauthor = Author.query.filter_by(
first_name='Anthony', last_name='Doerr')
self.assertEqual(testauthor[0].first_name, 'Anthony')
self.assertEqual(testauthor[0].last_name, 'Doerr')
def test_model_author_3(self):
with app.app_context():
testauthor = Author.query.filter_by(
first_name='Paula', last_name='Hawkins')
self.assertEqual(
testauthor[0].Books[0].title, 'THE GIRL ON THE TRAIN')
# ----------------
# Author.to_dict()
# ----------------
def test_author_to_dict_1(self):
with app.app_context():
testauthor = Author(id=1, first_name='Anthony', last_name='Doerr')
dictauthor = testauthor.to_dict()
self.assertEqual(testauthor.first_name, dictauthor['first_name'])
def test_author_to_dict_2(self):
with app.app_context():
testauthor = Author(id=1, first_name='Paula', last_name='Hawkins')
dictauthor = testauthor.to_dict()
self.assertEqual(testauthor.last_name, dictauthor['last_name'])
def test_author_to_dict_3(self):
with app.app_context():
testauthor = Author(
id=1, first_name='Harper', last_name='Lee', bio='hi')
dictauthor = testauthor.to_dict()
self.assertEqual(testauthor.bio, dictauthor['bio'])
# -----------------
# Author.get_html()
# -----------------
def test_author_get_html_1(self):
with app.app_context():
testauthor = Author(id=1, first_name='Anthony', last_name='Doerr')
htmlauthor = testauthor.get_html('anthony')
self.assertEqual(
"<td>Anthony Doerr</td><td><b><i>anthony</i></b> </td>", htmlauthor)
def test_author_get_html_2(self):
with app.app_context():
testauthor = Author(id=1, first_name='Anthony', last_name='Doerr')
htmlauthor = testauthor.get_html('doerr')
self.assertEqual(
"<td>Anthony Doerr</td><td><b><i>doerr</i></b> </td>", htmlauthor)
def test_author_get_html_3(self):
with app.app_context():
testauthor = Author(id=2, first_name='John', last_name='Green')
htmlauthor = testauthor.get_html('john')
self.assertEqual(
"<td>John Green</td><td><b><i>john</i></b> </td>", htmlauthor)
# -----------------
# Author.get_link()
# -----------------
def test_author_get_link_1(self):
with app.app_context():
testauthor = Author(id=1, first_name='Anthony', last_name='Doerr')
linkauthor = testauthor.get_link()
self.assertEqual("/author/1", linkauthor)
def test_author_get_link_2(self):
with app.app_context():
testauthor = Author(id=10000, first_name='John', last_name='Green')
linkauthor = testauthor.get_link()
self.assertEqual("/author/10000", linkauthor)
def test_author_get_link_3(self):
with app.app_context():
testauthor = Author(id=9999999999, first_name='Rick', last_name='Riordan')
linkauthor = testauthor.get_link()
self.assertEqual("/author/9999999999", linkauthor)
# ----
# Book
# ----
def test_model_book_1(self):
with app.app_context():
testbook = Book.query.get(1)
self.assertEqual(testbook.id, 1)
def test_model_book_2(self):
with app.app_context():
testbook = Book.query.filter_by(
title='ALL THE LIGHT WE CANNOT SEE').all()
self.assertEqual(testbook[0].title, 'ALL THE LIGHT WE CANNOT SEE')
def test_model_book_3(self):
with app.app_context():
testbook = Book.query.filter_by(
title='THE GIRL ON THE TRAIN').all()
self.assertEqual(testbook[0].author.first_name, 'Paula')
# --------------
# Book.to_dict()
# --------------
def test_book_to_dict_1(self):
with app.app_context():
testbook = Book(
id=1, title='ALL THE LIGHT WE CANNOT SEE', isbn='1', summary='hi')
dictbook = testbook.to_dict()
self.assertEqual(testbook.title, dictbook['title'])
def test_book_to_dict_2(self):
with app.app_context():
testbook = Book(
id=1, title='ALL THE LIGHT WE CANNOT SEE', isbn='1', summary='hi')
dictbook = testbook.to_dict()
self.assertEqual(testbook.isbn, dictbook['isbn'])
def test_book_to_dict_3(self):
with app.app_context():
testbook = Book(
id=1, title='ALL THE LIGHT WE CANNOT SEE', isbn='1', summary='hi')
dictbook = testbook.to_dict()
self.assertEqual(testbook.summary, dictbook['summary'])
# -----------------
# Book.get_html()
# -----------------
def test_book_get_html_1(self):
with app.app_context():
testbook = Book(title="moonshinning for dummies")
htmlbook = testbook.get_html('dummies')
self.assertEqual(
"<td>moonshinning for dummies</td><td>moonshinning for <b><i>dummies</i></b> </td>",
htmlbook)
def test_book_get_html_2(self):
with app.app_context():
testbook = Book(title="Going Bananas")
htmlbook = testbook.get_html('Bananas')
self.assertEqual(
"<td>Going Bananas</td><td>going <b><i>bananas</i></b> </td>",
htmlbook)
def test_book_get_html_3(self):
with app.app_context():
testbook = Book(title="Moonshine with Bananas")
htmlbook = testbook.get_html('Bananas')
self.assertEqual(
"<td>Moonshine with Bananas</td><td>moonshine with <b><i>bananas</i></b> </td>",
htmlbook)
# -----------------
# Book.get_link()
# -----------------
def test_book_get_link_1(self):
with app.app_context():
testbook = Book(id=5, title="The art of distilling good whiskey")
linkbook = testbook.get_link()
self.assertEqual("/book/5", linkbook)
def test_book_get_link_2(self):
with app.app_context():
testbook = Book(id=999999999999, title="Jim and Jack and Hank")
linkbook = testbook.get_link()
self.assertEqual("/book/999999999999", linkbook)
def test_book_get_link_3(self):
with app.app_context():
testbook = Book(id=1010101010, title="Daddy's Gone a Hunting")
linkbook = testbook.get_link()
self.assertEqual("/book/1010101010", linkbook)
# ----
# Main
# ----
if __name__ == '__main__':
main()