/
test_journal.py
410 lines (240 loc) · 10.1 KB
/
test_journal.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import contextlib # closing
import pytest
from journal import app
from journal import connect_db
from journal import get_database_connection
from journal import init_db
# The walkthrough implied this manages browser cookies when used...
from flask import session
TEST_DSN = 'dbname=test_learning_journal user=fried'
SUBMIT_BTN = '<input type="submit" value="Share" name="Share"/>'
# Used for testing isolation. The wipe half of reinitting the database.
def clear_db():
# This ensures the connection is closed later.
# Context library is all for this kind of context stuff.
with contextlib.closing(connect_db()) as db:
# Testing is not supposed to be used with a deployed database,
# apparently. That's where TEST_DSN's specification comes in:
# This will all be done in the test_learning_journal db.
# ...
# NOTE: This database must be created manually on the CLI.
# Done with:
# createdb test_learning_journal
db.cursor().execute("DROP TABLE entries")
db.commit()
@pytest.fixture(scope='session')
def test_app():
''' Configure the app for use in testing. '''
# What test_app() will do here is access the testing database
# (which is created outside of my python, on the CLI (for now))
# Flask apps have config dictionaries in them by design.
app.config['DATABASE'] = TEST_DSN
app.config['TESTING'] = True
# "The fixture function is defined with parameters.
# The names of the parameters must match registered fixtures.
# The fixtures named as parameters will be run surrounding the new fixture."
@pytest.fixture(scope='session')
def db(test_app, request):
''' Initialize the entries table and drop it when finished. '''
# This is the "fixture function" with its "registered fixture" parameters.
# The request parameter is a fixture that pytest gives you; you use it
# to connect the cleanup() function to the db fixture.
init_db()
# Unexplained methods: cleardb addfinalizer cleanup
# "The request parameter is a fixture that pytest registers.
# You use it to connect the cleanup function to the db fixture.
# This means that cleanup will be run after tests are complete
# as a tear-down action."
def cleanup():
clear_db()
# I THINK @app.teardown_request is a finalizer? Maaaaybe... ???
request.addfinalizer(cleanup)
# This one makes helps tests run in isolation from each other.
# Specifically it makes a generator function fixture.
# This is critical because generators preserve internal state.
# As a result, "the entire test happens inside context manager scope"
@pytest.yield_fixture(scope='function')
def req_context(db):
''' Run tests within a test request context so that 'g' is present. '''
# Wait... flask.g would not be available if we didn't make this
# "request context" function?
with app.test_request_context('/'):
# First, yield nothing.
# Wat.
yield
con = get_database_connection()
con.rollback()
# "Flask creates g when a cycle begines, but tests
# have no request/response cycle.
# Flasks's app.test_request_context is a "context provider".
# Used in a with statement, it creates a mock request/response cycle."
# ...
# What this means is, there's no web server running to test this,
# BUT we can simulate what would happen if there was.. by calling
# appname.app.test_request_context()
# "The request only exists inside the with block, so the
# callback pattern used in the db fixture would not work."
# I think that is referring to the request.addfinalizer(cleanup)
# line?
# "Because yield preserves internal state, the entire test
# happens inside the context manager scope"
# "When control returns to the fixture, code after the yield
# statement is executed as the tear-down action."
# Now begins the testing of the database schema.
def run_independent_query(query, params=[]):
# This function simply formalizes what I've been doing all along
# to make DB queries inside Python.
con = get_database_connection()
cur = con.cursor()
cur.execute(query, params)
return cur.fetchall()
def test_write_entry(req_context):
from journal import write_entry
expected = ("My Title", "My Text")
# Remember, star args are just how you unpack things.
# ((double star args unpack things into a dict.))
write_entry(*expected)
# "run_independent_query() is a 'helper function' you can re-use."
# Where's it come from, pytest? By way of the decorator??
rows = run_independent_query("SELECT * FROM entries")
# Huh, so this is just assertEquals... from pytest?
# Maybe not, since it's its own freestanding operation?
assert len(rows) == 1
for val in expected:
assert val in rows[0]
def test_edit_entry(req_context):
from journal import edit_entry
from journal import write_entry
expected = ("My Title", "My Text")
write_entry(*expected)
the_row_we_added = run_independent_query("SELECT * FROM entries")
def test_get_all_entries_empty(req_context):
from journal import get_all_entries
entries = get_all_entries()
assert len(entries) == 0
def test_get_all_entries(req_context):
from journal import get_all_entries, write_entry
expected = ("My Title", "My Test")
write_entry(*expected)
entries = get_all_entries()
assert len(entries) == 1
for entry in entries:
assert expected[0] == entry['title']
assert expected[1] == entry['text']
assert 'created' in entry
def test_get_entry(req_context):
from journal import get_entry, write_entry, get_all_entries
# Verify it's empty
entries = get_all_entries()
assert len(entries) == 0
expected = ("Get Entry Title", "Get Entry Test")
write_entry(*expected)
# Verify it isn't empty
entries = get_all_entries()
assert len(entries) == 1
# Derive the id and use it to get_entry():
the_only_entry = get_entry(entries[0]['id'])
# I wish I had more time to do that properly, but it works.
assert expected[0] == the_only_entry['title']
assert expected[1] == the_only_entry['text']
assert 'created' in the_only_entry
def test_empty_listing(db):
# "app.test_client() returns a mock HTTP client,
# like a web browser for development."
# "Because this test actually creates a request, we don't need to use
# the req_context fixture. Having an initialized database is enough"
# "The data attribute of the response returned by client.get()
# holds the full rendered HTML of our page."
actual = app.test_client().get('/').data
expected = 'No entries here so far'
assert expected in actual
@pytest.fixture(scope='function')
def with_entry(db, request):
from journal import write_entry
expected = (u'Test Title)', u'Test Text')
with app.test_request_context('/'):
write_entry(*expected)
get_database_connection().commit()
def cleanup():
# NOTE: "You use a test_request_context in both setup and
# teardown to ensure that flask.g exists."
with app.test_request_context('/'):
con = get_database_connection()
cur = con.cursor()
cur.execute("DELETE FROM entries")
con.commit()
# Also note that allowing the two "with" blocks to close commits the
# transactions for each test context.
request.addfinalizer(cleanup)
return expected
def test_listing(with_entry):
expected = with_entry
actual = app.test_client().get('/').data
for value in expected:
assert value in actual
def test_add_entries(db):
entry_data = {
u'title': u'Hello',
u'text': u'This is a post',
}
# "The post method of the Flask test_client sends an HTTP POST
# request to the provided URL."
actual = app.test_client().post(
'/add', data=entry_data, follow_redirects=True
).data
assert 'No entries here so far' not in actual
for expected in entry_data.values():
# "assert that the line in entry data is also in the actual data"
assert expected in actual
def test_do_login_success(req_context):
username, password = ('admin', 'admin')
# In-function imports look weird and wrong.
# Shouldn't they be for things that might be optional
# and thus could be skipped? Such as not unit tests?
from journal import do_login
assert 'logged_in' not in session
do_login(username, password)
assert 'logged_in' in session
def test_do_login_bad_password(req_context):
username = 'admin'
bad_password = 'wrongpassword'
from journal import do_login
with pytest.raises(ValueError):
do_login(username, bad_password)
def test_do_login_bad_username(req_context):
bad_username = 'wronguser'
password = 'admin'
from journal import do_login
with pytest.raises(ValueError):
do_login(bad_username, password)
def login_helper(username, password):
login_data = {
'username': username,
'password': password
}
client = app.test_client()
return client.post(
'/login', data=login_data, follow_redirects=True
)
def test_start_as_anonymous(db):
client = app.test_client()
anon_home = client.get('/').data
assert SUBMIT_BTN not in anon_home
def test_login_success(db):
# Is this unencrypted password okay because it's not deployed?
# The walkthrough DID say "never" store passwords unencrypted...
# "Anywhere".
username, password = ('admin', 'admin')
response = login_helper(username, password)
assert SUBMIT_BTN in response.data
def test_login_fails(db):
username, password = ('admin', 'wrong')
response = login_helper(username, password)
assert 'Login Failed' in response.data
def test_logout(db):
home = login_helper('admin', 'admin').data
assert SUBMIT_BTN in home
client = app.test_client()
response = client.get('/logout')
assert SUBMIT_BTN not in response.data
assert response.status_code == 302