-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_models.py
More file actions
78 lines (59 loc) · 2.04 KB
/
test_models.py
File metadata and controls
78 lines (59 loc) · 2.04 KB
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
""" Test file for models.py.
When models.py is complete, this file should completely pass.
NOTE:
To make these tests accurate, this file DELETES all content in the
database first.
"""
from model import *
def check_test(func):
""" This is a decorator that simply prints out whether the function
it calls succeeded or not. You don't need to edit this.
"""
def func_wrapper(*args, **kwargs):
try:
func(*args, **kwargs)
print ":) {} passed".format(func.__name__)
except AssertionError:
print ":( {} failed".format(func.__name__)
return func_wrapper
@check_test
def test_connection():
# Test that we connect to the database OK
(conn, db) = connect_to_db()
query = "DELETE FROM users;"
db.execute(query)
conn.commit()
conn.close()
@check_test
def test_create():
# Test that the create user function finishes cleanly
result = create_user('test', 'test', 'test', 'test', 'test')
# assert is a special Python keyword that throws an error if the thing it
# is testing turns out not to be True. Our check_test decorator looks for
# errors and tells us the test failed if it found any errors like this.
assert result == 'success'
# Does this test pass the first time you run it?
# Why?
@check_test
def test_create_worked():
result = get_user_by_username('test')
assert result is not None
@check_test
def test_get_user_by_id():
result = get_user(1)
assert result is not None
@check_test
def test_list_users():
result = list_users()
assert isinstance(result, list) # isinstance says "is it of this type"
assert len(result) > 0
@check_test
def test_update_user():
result = update_user(1, 'password', 'newpass')
assert result == 'success'
for item in dir():
""" Loop through all the defined items we know about (functions, etc).
If the name starts with test_, assume it's a test function and run it!
"""
if item.startswith('test_'):
globals()[item]()