-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_library.py
199 lines (156 loc) · 6.24 KB
/
test_library.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
"""
Copyright 2017 Aleksy Barcz
This file is part of WebLibrary.
WebLibrary is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
WebLibrary is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with WebLibrary; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
from library import *
import pytest
from datetime import date, timedelta
@pytest.fixture()
def book():
return Book("Clean Code", "R.C. Martin")
@pytest.fixture()
def book2():
return Book("Clean Architecture", "R.C. Martin")
@pytest.fixture()
def user1():
return User("AB", "[email protected]")
@pytest.fixture()
def user2():
return User("CD", "[email protected]")
@pytest.fixture()
def library(book, book2, user1, user2):
library = Library([book, book2], [user1, user2])
library.check_out("Clean Architecture", "AB")
return library
@pytest.fixture()
def persistence():
#persistence = PersistenceManager("postgresql://postgres:postgres@localhost:5432/library")
persistence = PersistenceManager("sqlite:///:memory:")
persistence.create_tables()
return persistence
def test_user_email(user1):
assert(user1.email == "[email protected]")
def test_user_exists(library):
assert(library.has_user("AB"))
def test_no_user(library):
assert(not library.has_user("AG"))
def test_create_book(book):
assert(book.title == "Clean Code")
assert(book.author == "R.C. Martin")
def test_book_exists(library):
assert(library.has_book("Clean Code"))
def test_book_exists2(library):
assert(library.has_book("Clean Architecture"))
def test_book_not_exists(library):
assert(not library.has_book("Dirty Code"))
def test_book_available(library):
assert(library.available("Clean Code"))
def test_no_user_for_checkout(library):
with pytest.raises(Exception):
library.check_out("Clean Code", "AG")
def test_book_checked_out(library):
assert(not library.available("Clean Architecture"))
def test_no_check_out_if_checked_out(library):
with pytest.raises(Exception):
library.check_out("Clean Architecture", "AB")
def test_book_checked_by(library):
assert(library.checked_by("Clean Architecture") == "AB")
def test_book_checked_by2(library):
assert(library.checked_by("Clean Code") is None)
def test_no_hold(library):
assert(library.held_by("Clean Architecture") == None)
assert(not library.is_held("Clean Architecture"))
def test_hold(library):
library.hold("Clean Architecture", "CD")
assert(library.held_by("Clean Architecture") == "CD")
assert(library.is_held("Clean Architecture"))
def test_remove_hold(library):
library.hold("Clean Architecture", "CD")
assert(library.held_by("Clean Architecture") == "CD")
assert(library.is_held("Clean Architecture"))
library.remove_hold("Clean Architecture")
assert(not library.is_held("Clean Architecture"))
def test_checkout_removes_hold(library):
library.hold("Clean Architecture", "CD")
assert(library.held_by("Clean Architecture") == "CD")
assert(library.checked_by("Clean Architecture") == "AB")
library.bring_back("Clean Architecture")
assert(library.held_by("Clean Architecture") == "CD")
library.check_out("Clean Architecture", "CD")
assert(library.held_by("Clean Architecture") == None)
assert(library.checked_by("Clean Architecture") == "CD")
def test_cannot_hold_if_borrower(library):
with pytest.raises(Exception):
library.hold("Clean Architecture", "AB")
def test_double_hold(library):
library.hold("Clean Architecture", "CD")
assert(library.held_by("Clean Architecture") == "CD")
library.hold("Clean Architecture", "CD")
assert(library.held_by("Clean Architecture") == "CD")
def test_different_hold(library):
library.hold("Clean Architecture", "CD")
with pytest.raises(Exception):
library.hold("Clean Architecture", "AB")
assert(library.held_by("Clean Architecture") == "CD")
def test_no_hold_available(library):
assert(library.held_by("Clean Code") == None)
def test_cannot_hold_available(library):
with pytest.raises(Exception):
library.hold("Clean Code", "CD")
assert(library.held_by("Clean Code") == None)
def test_book_returned(library):
library.bring_back("Clean Architecture")
assert(library.available("Clean Architecture"))
assert(library.due_date("Clean Architecture") is None)
def test_cannot_return(library):
with pytest.raises(Exception):
library.bring_back("Dirty Code")
def test_not_due(library):
assert(library.due_date("Clean Code") is None)
def test_due(library):
assert(library.due_date("Clean Architecture") == date.today() + timedelta(days=library.checkout_days))
def test_not_overdue(library):
assert(not library.is_overdue("Clean Architecture"))
def test_overdue(library):
library.checkout_days = 0
library.check_out("Clean Code", "AB")
assert(library.is_overdue("Clean Code"))
def test_not_checked(library):
assert(library.checked_date("Clean Code") is None)
def test_checked(library):
assert(library.checked_date("Clean Architecture") == date.today())
def test_persistence(library, persistence):
persistence.connect()
library.hold("Clean Architecture", "CD")
persistence.store(library)
library2 = persistence.load()
assert(library2.checked_by("Clean Architecture") == "AB")
assert(library2.due_date("Clean Architecture") == date.today() + timedelta(days=library.checkout_days))
assert(not library2.available("Clean Architecture"))
assert(library2.held_by("Clean Architecture") == "CD")
assert(library2.available("Clean Code"))
assert(library2.has_book("Clean Code"))
persistence.close()
def test_double_store(library, persistence):
persistence.connect()
persistence.store(library)
persistence.store(library)
library2 = persistence.load()
assert(library2.checked_by("Clean Architecture") == "AB")
assert(library2.due_date("Clean Architecture") == date.today() + timedelta(days=library.checkout_days))
assert(not library2.available("Clean Architecture"))
assert(library2.available("Clean Code"))
assert(library2.has_book("Clean Code"))
assert(library2.has_user("AB"))
persistence.close()