-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_message_model.py
68 lines (45 loc) · 1.71 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
"""Message 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
from psycopg2.errors import UniqueViolation
from sqlalchemy.exc import IntegrityError as SQLIntegrityError
# 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 MessageModelTestCase(TestCase):
def setUp(self):
Message.query.delete()
User.query.delete()
u1 = User.signup("u1", "[email protected]", "password", None)
db.session.commit()
self.u1_id = u1.id
self.u1 = u1
self.client = app.test_client()
def tearDown(self):
db.session.rollback()
def test_adding_message(self):
"""test adding a message is successful"""
new_msg = Message(user_id=self.u1_id, text="test")
db.session.add(new_msg)
db.session.commit()
self.assertIn(new_msg, self.u1.messages)
def test_message_empty_text(self):
"""test integrity error when adding message if text input is empty"""
with self.assertRaises(SQLIntegrityError):
new_msg = Message(user_id=self.u1_id, text=None)
db.session.add(new_msg)
db.session.commit()