-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_message_model.py
92 lines (72 loc) · 2.65 KB
/
test_message_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
"""Message model tests."""
import os
from unittest import TestCase
from sqlalchemy.exc import IntegrityError
from models import db, User, Message, Follows, Like
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
db.create_all()
class UserModelTestCase(TestCase):
"""Test views for messages."""
def setUp(self):
"""Create test client, add sample data."""
Like.query.delete()
User.query.delete()
Message.query.delete()
Follows.query.delete()
u = User(
email="[email protected]",
username="testuser",
password="HASHED_PASSWORD"
)
db.session.add(u)
db.session.commit()
# store id
self.user = u
self.client = app.test_client()
def test_message_model(self):
"""Does basic model work?"""
m = Message(text='testText',
user_id=f'{self.user.id}')
db.session.add(m)
db.session.commit()
# test list type
self.assertEqual(len(self.user.messages), 1)
self.assertEqual(m.user, self.user)
self.assertEqual(f"{m}",
f"Message: {m.id}, testText, {self.user.id}")
def test_message_like(self):
""" Does the like relationship work? """
u2 = self._create_test_user()
m2 = Message(text='testText2')
u2.messages.append(m2)
db.session.commit()
self.user.liked_messages.append(m2)
db.session.commit()
like = Like.query.get((self.user.id, m2.id))
# test list type
# query count
self.assertEqual(len(self.user.liked_messages), 1)
self.assertEqual(like.user_id, self.user.id)
self.assertEqual(like.msg_id, m2.id)
# delete the like
Like.query.filter(Like.user_id == self.user.id,
Like.msg_id == m2.id).delete()
db.session.commit()
self.assertEqual(len(self.user.liked_messages), 0)
self.assertEqual(len(Like.query.all()), 0)
def _create_test_user(self):
""" Create user to login with. """
u = User.signup(
email="[email protected]",
username="testuser42",
password="HASHED_PASSWORD",
image_url="https://vignette.wikia.nocookie.net/questionablecontent/images/7/7a/Yelling_Bird.png/revision/latest?cb=20100107084653"
)
db.session.add(u)
db.session.commit()
return u