-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_user_model.py
131 lines (101 loc) · 3.62 KB
/
test_user_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"""User model tests."""
# run these tests like:
#
# FLASK_ENV=production python -m unittest test_user_model.py
# (we set FLASK_ENV for this command, so it doesn’t use debug mode, and therefore won’t use the Debug Toolbar during our tests).
import os
from unittest import TestCase
from sqlalchemy import exc
from models import db, User
os.environ["DATABASE_URL"] = "postgresql:///capstone1test"
from app import app
app.config["SQLALCHEMY_DATABASE_URI"] = "postgres:///capstone1test"
db.create_all()
class UserModelTestCase(TestCase):
"""Test views for messages."""
def setUp(self):
"""Create test client, add sample data."""
db.drop_all()
db.create_all()
u1 = User(
username="testuser1",
password="HASHED_PASSWORD",
weight=150,
height=60,
diet_plan="Maintain current weight; 0 calories deficit",
gender="female",
age=50,
activity_level="Sedentary (little to no exercise)",
)
db.session.add(u1)
db.session.commit()
self.u1 = User.query.get(1)
self.u1_id = self.u1.id
self.client = app.test_client()
u3 = User.signup(
username="testuser3",
password="HASHED_PASSWORD3",
weight=200,
height=70,
diet_plan="Maintain current weight; 0 calories deficit",
gender="male",
age=60,
image_url="",
activity_level="Sedentary (little to no exercise)",
)
def tearDown(self):
db.session.rollback()
def test_user_model(self):
"""Does basic model work?"""
u2 = User(
username="testuser2",
password="HASHED_PASSWORD2",
weight=200,
height=70,
diet_plan="Maintain current weight; 0 calories deficit",
gender="male",
age=60,
activity_level="Sedentary (little to no exercise)",
)
db.session.add(u2)
db.session.commit()
# User should have no BMI record & no meals
self.assertEqual(len(u2.bmi), 0)
self.assertEqual(len(u2.user_food), 0)
# SignUp Tests
def test_valid_user_signup(self):
"""Test user method signup"""
u2 = User.signup(
username="testuser2",
password="HASHED_PASSWORD2",
weight=200,
height=70,
diet_plan="Maintain current weight; 0 calories deficit",
gender="male",
age=60,
image_url="",
activity_level="Sedentary (little to no exercise)",
)
self.assertNotEqual(u2.password, "HASHED_PASSWORD2")
self.assertEqual(len(User.query.all()), 3)
def test_invalid_username_signup(self):
with self.assertRaises(exc.IntegrityError) as context:
invalid_user = User.signup(
username="testuser1",
password="HASHED_PASSWORD2",
weight=200,
height=70,
diet_plan="Maintain current weight; 0 calories deficit",
gender="male",
age=60,
image_url="",
activity_level="Sedentary (little to no exercise)",
)
# Authentication tests
def test_valid_authentication(self):
u = User.authenticate("testuser3", "HASHED_PASSWORD3")
self.assertIsNotNone(u)
def test_invalid_username(self):
self.assertFalse(User.authenticate("testuser2", "HASHED_PASSWORD"))
def test_wrong_password(self):
self.assertFalse(User.authenticate("testuser3", "incorrect"))