-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_models.py
79 lines (68 loc) · 2.79 KB
/
test_models.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
# to run test
# SQLALCHEMY_WARN_20=1 python3 -m unittest test_models.py
from unittest import TestCase
from app import app
from models import db, User
class UserModelTestCase(TestCase):
""" Tests Users model """
def setUp(self):
"""Clean up any existing users."""
self.client = app.test_client()
# use test database
app.config.update({
"TESTING": True,
"SQLALCHEMY_ECHO": False,
"SQLALCHEMY_DATABASE_URI": "postgresql:///blogly_test",
"DEBUG_TB_HOSTS": ["dont-show-debug-toolbar"]
})
with app.app_context():
self.ids = [] # will hold users ids
db.drop_all()
db.create_all()
user = User(f_name="first_name_test",
l_name="last_name_test", img_url="testurl")
user2 = User(f_name="first_name_test",
l_name="last_name_test", img_url="testurl")
db.session.add(user)
db.session.add(user2)
# commit to test database
db.session.commit()
# append user ids
self.ids.append(user.id)
self.ids.append(user2.id)
# delete Users
User.query.delete()
def tearDown(self):
"""Clean up any fouled transaction."""
with app.app_context():
db.session.rollback()
def test_greet(self):
""" Test User greet method """
user = User(f_name="Ed", l_name="Aviles")
self.assertEqual(
user.greet(), f"Hi my name is {user.f_name} {user.l_name}!")
def test_get_all_users_by_first_name(self):
""" Test class method get_all_users_by_first_name """
with app.app_context():
# get all users form test database
users = User.query.all()
# get all users using User class method
users_list = User.get_all_users_by_first_name(users[0].f_name)
self.assertEqual(users, users_list)
def test_get_all_users_by_last_name(self):
""" Test class method get_all_users_by_last_name """
with app.app_context():
# get all users form test database
users = User.query.all()
# get all users using User class method
users_list = User.get_all_users_by_last_name(users[0].l_name)
self.assertEqual(users, users_list)
def test_get_all_users_by_default_image_url(self):
""" Test class method get_all_users_by_default_image_url """
with app.app_context():
# get all users form test database
users = User.query.all()
# get all users using User class method
users_list = User.get_all_users_by_default_image_url(
users[0].img_url)
self.assertEqual(users, users_list)