-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_user_model.py
164 lines (110 loc) · 4.91 KB
/
test_user_model.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
"""User model tests."""
# run these tests like:
#
# python -m unittest test_user_model.py
import os
from unittest import TestCase
from models import db, User, Message, Follows, connect_db
# BEFORE we import our app, let's set an environmental variable
# to use a different database for tests (we need to do this
# before we import our app, since that will have already
# connected to the database
os.environ['DATABASE_URL'] = "postgresql:///warbler_test"
# Now we can import app
from app import app
# Create our tables (we do this here, so we only create the tables
# once for all tests --- in each test, we'll delete the data
# and create fresh new clean test data
connect_db(app)
db.drop_all()
db.create_all()
class UserModelTestCase(TestCase):
def setUp(self):
User.query.delete()
u1 = User.signup("u1", "[email protected]", "password", None)
u2 = User.signup("u2", "[email protected]", "password", None)
db.session.commit()
self.u1_id = u1.id
self.u2_id = u2.id
self.client = app.test_client()
def tearDown(self):
db.session.rollback()
def test_user_model(self):
u1 = User.query.get(self.u1_id)
# User should have no messages & no followers
self.assertEqual(len(u1.messages), 0)
self.assertEqual(len(u1.followers), 0)
# Does the repr method work as expected?
def test_user_repr(self):
"""Test the repr method."""
u1 = User.query.get(self.u1_id)
self.assertEqual(
repr(u1),
f"<User #{self.u1_id}: u1, [email protected]>")
self.assertEqual(
repr(User.query.get(self.u1_id)),
f"<User #{self.u1_id}: u1, [email protected]>")
# Does is_following successfully detect when user1 is following user2?
def test_user_is_following(self):
"""Test the is_following method."""
u1 = User.query.get(self.u1_id)
u2 = User.query.get(self.u2_id)
u1.followers.append(u2)
self.assertTrue(u2.is_following(u1))
# Does is_following successfully detect when user1 is not following user2?
def test_user_is_not_following(self):
"""Test the is_following method."""
u1 = User.query.get(self.u1_id)
u2 = User.query.get(self.u2_id)
u2.followers.append(u1)
self.assertFalse(u2.is_following(u1))
# Does is_followed_by successfully detect when user1 is followed by user2?
def test_user_is_followed_by(self):
"""Test if is_followed detects when user1 is followed by user2"""
u1 = User.query.get(self.u1_id)
u2 = User.query.get(self.u2_id)
u1.followers.append(u2)
self.assertTrue(u1.is_followed_by(u2))
# Does is_followed_by successfully detect when user1 is not followed by user2?
def test_user_is_followed_by_false(self):
"""Test when user1 is not followed by user2"""
u1 = User.query.get(self.u1_id)
u2 = User.query.get(self.u2_id)
self.assertFalse(u1.is_followed_by(u2))
# Does User.signup successfully create a new user given valid credentials?
def test_user_signup(self):
"""Test the user signup successful if given valid credentials.
check that user is in database
"""
user = User.signup("u3", "[email protected]", "password", None)
user_in_db = User.query.filter(User.email == '[email protected]')
self.assertEqual(repr(user), f"<User #{user.id}: u3, [email protected]>")
self.assertTrue(bool(user_in_db))
# Does User.signup fail to create a new user if any of the validations (eg uniqueness, non-nullable fields) fail?
def test_user_signup_fail(self):
"""Test the user signup fail if any invalid data.
check new user not in database
"""
with self.assertRaises(ValueError):
User.signup("u3", "[email protected]", "", None)
# Does User.authenticate successfully return a user when given a valid username and password?
def test_authenticate_user(self):
"""Test if user is returned being given a valid username and password.
"""
User.signup("testname", "[email protected]", "password", None)
user = User.authenticate("testname", "password")
self.assertTrue(user)
# Does User.authenticate fail to return a user when the username is invalid?
def test_authenticate_username_invalid(self):
"""Test if False is returned if invalid username is given.
"""
User.signup("testname", "[email protected]", "password", None)
user = User.authenticate("", password="password")
self.assertFalse(user)
# Does User.authenticate fail to return a user when the password is invalid?
def test_authenticate_password_invalid(self):
"""Test if False is returned if invalid password is given.
"""
User.signup("testname", "[email protected]", "password", None)
user = User.authenticate("", password="paxxword")
self.assertFalse(user)