-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_tests.py
297 lines (222 loc) · 9.66 KB
/
model_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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env python
import nose
from nose.tools import raises, assert_equals, assert_in, assert_not_in, assert_is_not_none, assert_is_none
from nose.plugins.skip import SkipTest
import pyDoubles.framework as mock
import json
from models import (Repository, Book, AlreadyOnLoanError, BorrowingWhileReservedError,
NotReservedError, NotCheckedOutError, NotTheBorrowerError)
# ----------------------------------------------------------------------
def test_book_class_empty():
book = Book('', '', '')
assert_equals(book.title, '')
assert_equals(book.description, '')
assert_equals(book.isbn, '')
assert_equals(book.status(), Book.AVAILABLE)
def test_book_class_populated_with_no_borrower():
book = Book('TITLE', 'DESCRIPTION', 'ISBN')
assert_equals(book.title, 'TITLE')
assert_equals(book.description, 'DESCRIPTION')
assert_equals(book.isbn, 'ISBN')
assert_equals(book.status(), Book.AVAILABLE)
def test_book_class_populated_with_borrower_is_borrowed():
book = Book('TITLE', 'DESCRIPTION', 'ISBN', 'BORROWER')
assert_equals(book.borrower, 'BORROWER')
assert_equals(book.status(), Book.BORROWED)
def test_book_class_with_reservers():
book = Book('TITLE', 'DESCRIPTION', 'ISBN', 'BORROWER', reservations=['RESERVER1', 'RESERVER2'])
assert_equals(len(book.reservations), 2)
# ----------------------------------------------------------------------
def test_book_can_reserve_new_book():
book = Book('TITLE', 'DESCRIPTION', 'ISBN')
book.reserve('RESERVER')
assert_equals(len(book.reservations), 1)
assert_in('RESERVER', book.reservations)
def test_book_can_reserve_new_book_once():
book = Book('TITLE', 'DESCRIPTION', 'ISBN')
book.reserve('RESERVER')
book.reserve('RESERVER')
assert_equals(len(book.reservations), 1)
assert_in('RESERVER', book.reservations)
def test_book_can_reserve_new_book_with_more_than_one_user():
book = Book('TITLE', 'DESCRIPTION', 'ISBN')
book.reserve('RESERVER')
book.reserve('ANOTHER RESERVER')
assert_equals(len(book.reservations), 2)
assert_equals('RESERVER', book.reservations[0])
assert_equals('ANOTHER RESERVER', book.reservations[1])
def test_book_can_un_reserve_book():
book = Book('TITLE', 'DESCRIPTION', 'ISBN')
book.reserve('RESERVER')
book.reserve('ANOTHER RESERVER')
book.reserve('YET ANOTHER RESERVER')
book.un_reserve('ANOTHER RESERVER')
book.un_reserve('RESERVER')
book.un_reserve('YET ANOTHER RESERVER')
assert_equals(0, len(book.reservations))
@raises(NotReservedError)
def test_book_cannot_un_reserve_book_when_not_reserved_by_you():
book = Book('TITLE', 'DESCRIPTION', 'ISBN')
book.reserve('RESERVER')
book.un_reserve('ANOTHER RESERVER')
def test_book_borrow_new_book():
book = Book('TITLE', 'DESCRIPTION', 'ISBN')
book.check_out('BORROWER')
assert_equals(book.status(), Book.BORROWED)
assert_equals(book.borrower, 'BORROWER')
# ----------------------------------------------------------------------
@raises(AlreadyOnLoanError)
def test_book_can_only_be_borrowed_once():
book = Book('TITLE', 'DESCRIPTION', 'ISBN')
book.check_out('BORROWER')
book.check_out('ANOTHER BORROWER')
@raises(BorrowingWhileReservedError)
def test_book_cannot_be_borrowed_when_reserved():
book = Book('TITLE', 'DESCRIPTION', 'ISBN')
book.reserve('RESERVER')
book.check_out('BORROWER')
def test_book_can_borrow_reserved_book_if_only_reserver():
book = Book('TITLE', 'DESCRIPTION', 'ISBN')
book.reserve('RESERVER')
book.check_out('RESERVER')
assert_equals(len(book.reservations), 0)
def test_book_can_borrow_reserved_book_if_first_reserver():
book = Book('TITLE', 'DESCRIPTION', 'ISBN')
book.reserve('RESERVER')
book.reserve('ANOTHER RESERVER')
book.check_out('RESERVER')
assert_equals(len(book.reservations), 1)
assert_equals('ANOTHER RESERVER', book.reservations[0])
@raises(BorrowingWhileReservedError)
def test_book_cannot_borrow_reserved_book_if_later_reserver():
book = Book('TITLE', 'DESCRIPTION', 'ISBN')
book.reserve('RESERVER')
book.reserve('ANOTHER RESERVER')
book.check_out('ANOTHER RESERVER')
# ----------------------------------------------------------------------
def test_book_can_return_book_if_borrower():
book = Book('TITLE', 'DESCRIPTION', 'ISBN')
book.check_out('BORROWER')
book.check_in('BORROWER')
@raises(NotTheBorrowerError)
def test_book_cannot_return_book_if_not_borrower():
book = Book('TITLE', 'DESCRIPTION', 'ISBN')
book.check_out('BORROWER')
book.check_in('ANOTHER BORROWER')
@raises(NotCheckedOutError)
def test_book_cannot_return_book_if_not_checked_out():
book = Book('TITLE', 'DESCRIPTION', 'ISBN')
book.check_in('BORROWER')
# ----------------------------------------------------------------------
def test_options_no_borrower():
book = Book('TITLE', 'DESCRIPTION', 'ISBN')
expected = { Book.CAN_RESERVE: True, Book.CAN_BORROW: True }
assert_equals(expected, book.get_options('SOMEONE'))
def test_options_no_borrower_with_one_reserver_by_reserver():
book = Book('TITLE', 'DESCRIPTION', 'ISBN')
book.reserve('RESERVER')
expected = { Book.CAN_BORROW: True, Book.CAN_CANCEL: True }
assert_equals(expected, book.get_options('RESERVER'))
def test_options_no_borrower_with_one_reserver_by_other():
book = Book('TITLE', 'DESCRIPTION', 'ISBN')
book.reserve('RESERVER')
expected = { Book.CAN_RESERVE: True }
assert_equals(expected, book.get_options('OTHER'))
def test_options_no_borrower_with_many_reservers_by_reserver():
book = Book('TITLE', 'DESCRIPTION', 'ISBN')
book.reserve('RESERVER')
book.reserve('RESERVER 2')
book.reserve('RESERVER 3')
expected = { Book.CAN_BORROW: True, Book.CAN_CANCEL: True }
assert_equals(expected, book.get_options('RESERVER'))
def test_options_no_borrower_with_many_reservers_by_other():
book = Book('TITLE', 'DESCRIPTION', 'ISBN')
book.reserve('RESERVER')
book.reserve('RESERVER 2')
book.reserve('RESERVER 3')
expected = { Book.CAN_RESERVE: True }
assert_equals(expected, book.get_options('OTHER'))
def test_options_with_borrower():
book = Book('TITLE', 'DESCRIPTION', 'ISBN')
book.check_out('BORROWER')
expected = { Book.CAN_RESERVE: True }
assert_equals(expected, book.get_options('OTHER'))
def test_options_for_borrower():
book = Book('TITLE', 'DESCRIPTION', 'ISBN')
book.check_out('BORROWER')
expected = { Book.CAN_RETURN: True }
assert_equals(expected, book.get_options('BORROWER'))
def test_options_with_borrower_with_many_reservers_by_reserver():
book = Book('TITLE', 'DESCRIPTION', 'ISBN')
book.check_out('BORROWER')
book.reserve('RESERVER')
book.reserve('RESERVER 2')
book.reserve('RESERVER 3')
expected = {}
expected[Book.CAN_CANCEL] = True
assert_equals(expected, book.get_options('RESERVER'))
# ----------------------------------------------------------------------
def test_links_reserve():
book = Book('TITLE', 'DESCRIPTION', 'ISBN')
book.get_options = mock.method_returning({ Book.CAN_RESERVE: True })
assert_equals(dict(rel='self', href='/books/ISBN'), book.links('RESERVER')[0])
assert_equals(dict(rel='/docs#reserve', href='/books/ISBN/reservations'), book.links('RESERVER')[1])
def test_links_borrow():
book = Book('TITLE', 'DESCRIPTION', 'ISBN')
book.get_options = mock.method_returning({ Book.CAN_BORROW: True })
assert_equals(dict(rel='self', href='/books/ISBN'), book.links('SOMEONE')[0])
assert_equals(dict(rel='/docs#borrow', href='/books/ISBN/borrower'), book.links('SOMEONE')[1])
def test_links_return():
book = Book('TITLE', 'DESCRIPTION', 'ISBN')
book.get_options = mock.method_returning({ Book.CAN_RETURN: True })
assert_equals(dict(rel='self', href='/books/ISBN'), book.links('BORROWER')[0])
assert_equals(dict(rel='/docs#return', href='/books/ISBN/return'), book.links('BORROWER')[1])
def test_links_cancel():
book = Book('TITLE', 'DESCRIPTION', 'ISBN')
book.get_options = mock.method_returning({ Book.CAN_CANCEL: True })
assert_equals(dict(rel='self', href='/books/ISBN'), book.links('RESERVER')[0])
assert_equals(dict(rel='/docs#cancel', href='/books/ISBN/reservations/RESERVER/cancel'), book.links('RESERVER')[1])
# ----------------------------------------------------------------------
def test_repository_find():
r = Repository()
def test_repository_find_one_fails():
r = Repository()
maybe_bk = r.find_one('NOTAREALISBN')
assert_is_none(maybe_bk)
def test_repository_find_empty():
r = Repository()
bk = Book('TITLE', 'DESCRIPTION', 'ISBN')
r.store(bk)
bk = Book('TITLE2', 'DESCRIPTION2', 'ISBN2')
r.store(bk)
bk = Book('TITLE3', 'DESCRIPTION3', 'ISBN3')
r.store(bk)
books = r.find()
assert_equals(len(books), 3)
def test_repository_find_nonempty():
r = Repository()
books = r.find()
assert_equals(len(books), 0)
def test_repository_save_new_one_works():
r = Repository()
bk = Book('NEWTITLE', 'NEWDESCRIPTION', 'NEWISBN')
r.store(bk)
maybe_bk = r.find_one('NEWISBN')
assert_is_not_none(maybe_bk)
assert_equals(maybe_bk.isbn, 'NEWISBN')
assert_equals(maybe_bk.title, 'NEWTITLE')
def test_repository_update_works():
r = Repository()
bk = Book('TITLE', 'DESCRIPTION', 'ISBN')
r.store(bk)
bk = r.find_one('ISBN')
bk.title = 'NEWTITLE'
bk.isbn = 'NEWISBN'
r.store(bk)
maybe_bk = r.find_one('ISBN')
assert_is_none(maybe_bk)
maybe_bk = r.find_one('NEWISBN')
assert_is_not_none(maybe_bk)
assert_equals(maybe_bk.isbn, 'NEWISBN')
assert_equals(maybe_bk.title, 'NEWTITLE')
# ----------------------------------------------------------------------