-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_message_model.py
More file actions
79 lines (49 loc) · 1.85 KB
/
test_message_model.py
File metadata and controls
79 lines (49 loc) · 1.85 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
79
"""Message model tests."""
# run these tests like:
#
# python -m unittest test_message_model.py
import os
from unittest import TestCase
from models import db, User, Message, Likes
# 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
db.create_all()
class MessageModelTestCase(TestCase):
"""Test message model."""
def setUp(self):
db.drop_all()
db.create_all()
user = User.signup("testuser", "test@email.com", "testpw", None)
user.id = 111111
db.session.commit()
self.user = User.query.get(111111)
self.client = app.test_client()
def tearDown(self):
res = super().tearDown()
db.session.rollback()
return res
def test_message_model(self):
"""Does basic model work?"""
message = Message(text="test", user_id=111111)
db.session.add(message)
db.session.commit()
self.assertEqual(self.user.messages[0].text, "test")
def test_message_like(self):
message = Message(text="test", user_id=111111)
user = User.signup("testuser2", "test2@email.com", "testpw2", None)
user.id = 222222
db.session.add_all([message, user])
db.session.commit()
user.likes.append(message)
db.session.commit()
likes = Likes.query.filter(Likes.user_id == 222222).all()
self.assertEqual(len(likes), 1)
self.assertEqual(likes[0].message_id, message.id)