-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_user_views.py
112 lines (86 loc) · 3.48 KB
/
test_user_views.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
'''Test user_views'''
# run these tests like:
#
# python -m unittest test_user_model.py
import os
from unittest import TestCase
from models import db, User, Message, Follows, bcrypt, Likes
from bs4 import BeautifulSoup
# 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, CURR_USER_KEY
# 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
db.create_all()
class UserViewsTests(TestCase):
'''Test case for user view functions'''
def setUp(self):
User.query.delete()
Message.query.delete()
Follows.query.delete()
self.client = app.test_client()
self.testuser = User.signup(
username="testuserlen",
password="password",
image_url=None,
email="[email protected]"
)
self.testuser_id = 10000
self.testuser.id = self.testuser_id
self.u1 = User.signup("abc", "[email protected]", "password", None)
self.u1_id = 778
self.u1.id = self.u1_id
self.u2 = User.signup("efg", "[email protected]", "password", None)
self.u2_id = 884
self.u2.id = self.u2_id
self.u3 = User.signup("hij", "[email protected]", "password", None)
self.u4 = User.signup("testing", "[email protected]", "password", None)
db.session.commit()
def tearDown(self):
resp = super().tearDown()
db.session.rollback()
return resp
def test_unloggedin_home(self):
'''homepage if user is not logged in'''
resp = self.client.get('/')
html = resp.get_data(as_text=True)
self.assertIn('Sign up now to get your own personalized timeline!', html)
def test_loggedin_view(self):
'''homepage if user is logged in'''
with self.client as client:
with client.session_transaction() as sess:
sess[CURR_USER_KEY] = self.testuser.id
resp = client.get('/')
html = resp.get_data(as_text=True)
self.assertEqual(resp.status_code, 200)
self.assertIn('Log out', html)
def setup_likes(self):
m1 = Message(text="trending warble", user_id=self.testuser_id)
m2 = Message(text="Eating some lunch", user_id=self.testuser_id)
m3 = Message(id=9876, text="likable warble", user_id=self.u1_id)
db.session.add_all([m1, m2, m3])
db.session.commit()
l1 = Likes(user_id=self.testuser_id, message_id=9876)
db.session.add(l1)
db.session.commit()
def test_user_likes_number(self):
self.setup_likes()
with self.client as c:
resp = c.get(f'/users/{self.testuser_id}')
self.assertEqual(resp.status_code, 200)
soup = BeautifulSoup(str(resp.data), 'html.parser')
found = soup.find_all('li', {'class': 'stat'})
self.assertEqual(len(found), 4)
# 2 count of messages
self.assertIn('2', found[0].text)
# 0 count of followers
self.assertIn('0', found[1].text)
# 0 count of folliwing
self.assertIn('0', found[2].text)
# 1 count of likes
self.assertIn('1', found[3].text)