-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_journal.py
210 lines (158 loc) · 5.45 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
# -*- coding: utf-8 -*-
from contextlib import closing
import pytest
from flask import session
from journal import app
from journal import connect_db
from journal import get_database_connection
from journal import init_db
TEST_DSN = 'dbname=test_learning_journal'
def clear_db():
with closing(connect_db()) as db:
db.cursor().execute("DROP TABLE entries")
db.commit()
@pytest.fixture(scope='session')
def test_app():
"""configure out app for use in testing"""
app.config['DATABASE'] = TEST_DSN
app.config['TESTING'] = True
@pytest.fixture(scope='session')
def db(test_app, request):
"""initialize the entries table and drop it when finished"""
init_db()
def cleanup():
clear_db()
request.addfinalizer(cleanup)
@pytest.yield_fixture(scope='function')
def req_context(db):
"""run test within a rest request context so that 'g' is present"""
with app.test_request_context('/'):
yield
con = get_database_connection()
con.rollback()
@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)
# manually commit transaction here to avoid rollback due to
# handled exceptions
get_database_connection().commit()
def cleanup():
with app.test_request_context('/'):
con = get_database_connection()
cur = con.cursor()
cur.execute("DELETE FROM entries")
# and here as well
con.commit()
request.addfinalizer(cleanup)
return expected
def run_independent_query(query, params=[]):
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")
write_entry(*expected)
rows = run_independent_query("SELECT * FROM entries")
assert len(rows) == 1
for val in expected:
assert val in rows[0]
'''###############
test edit functions
################'''
def test_get_one_entry(req_context):
from journal import get_one_entry
from journal import write_entry
expected = ("My Title", "My Text")
write_entry(*expected) # find actual row numbers from test db
target_entry = run_independent_query('SELECT id FROM entries')[0]
entry_2_update = get_one_entry(target_entry)
print entry_2_update
assert expected[0] == entry_2_update[1]
assert expected[1] == entry_2_update[2]
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", "<p>My Text</p>")
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_empty_listing(db):
actual = app.test_client().get('/').data
expected = 'No entries here so far'
assert expected in actual
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',
}
with app.test_client() as c:
with c.session_transaction() as sess:
sess['logged_in'] = True
actual = c.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 expected in actual
def test_do_login_success(req_context):
username, password = ('admin', 'admin')
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):
password = 'admin'
bad_username = 'wronguser'
from journal import do_login
with pytest.raises(ValueError):
do_login(bad_username, password)
SUBMIT_BTN = '<input type="submit" value="Share" name="Share"/>'
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):
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