forked from alexaorrico/AirBnB_clone_v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_user.py
executable file
·155 lines (134 loc) · 5.85 KB
/
test_user.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/python3
"""
Contains the TestUserDocs classes and TestUser classes
"""
import hashlib
import inspect
import models
from models import user
from models.base_model import BaseModel
import pep8
import unittest
User = user.User
class TestUserDocs(unittest.TestCase):
"""Tests to check the documentation and style of User class"""
@classmethod
def setUpClass(cls):
"""Set up for the doc tests"""
cls.user_f = inspect.getmembers(User, inspect.isfunction)
def test_pep8_conformance_user(self):
"""Test that models/user.py conforms to PEP8."""
pep8s = pep8.StyleGuide(quiet=True)
result = pep8s.check_files(['models/user.py'])
self.assertEqual(result.total_errors, 0,
"Found code style errors (and warnings).")
def test_pep8_conformance_test_user(self):
"""Test that tests/test_models/test_user.py conforms to PEP8."""
pep8s = pep8.StyleGuide(quiet=True)
result = pep8s.check_files(['tests/test_models/test_user.py'])
self.assertEqual(result.total_errors, 0,
"Found code style errors (and warnings).")
def test_user_module_docstring(self):
"""Test for the user.py module docstring"""
self.assertIsNot(user.__doc__, None,
"user.py needs a docstring")
self.assertTrue(len(user.__doc__) >= 1,
"user.py needs a docstring")
def test_user_class_docstring(self):
"""Test for the User class docstring"""
self.assertIsNot(User.__doc__, None,
"User class needs a docstring")
self.assertTrue(len(User.__doc__) >= 1,
"User class needs a docstring")
def test_user_func_docstrings(self):
"""Test for the presence of docstrings in User methods"""
for func in self.user_f:
self.assertIsNot(func[1].__doc__, None,
"{:s} method needs a docstring". format(func[0]))
self.assertTrue(len(func[1].__doc__) >= 1,
"{:s} method needs a docstring".format(func[0]))
def test_is_subclass(self):
"""Test that User is a subclass of BaseModel"""
user = User()
self.assertIsInstance(user, BaseModel)
self.assertTrue(hasattr(user, "id"))
self.assertTrue(hasattr(user, "created_at"))
self.assertTrue(hasattr(user, "updated_at"))
def test_email_attr(self):
"""Test that User has attr email, and it's an empty string"""
user = User()
self.assertTrue(hasattr(user, "email"))
if models.storage_t == 'db':
self.assertEqual(user.email, None)
else:
self.assertEqual(user.email, "")
def test_password_attr(self):
"""Test that User has attr password, and it's an empty string"""
user = User()
self.assertTrue(hasattr(user, "password"))
if models.storage_t == 'db':
self.assertEqual(user.password, None)
else:
self.assertEqual(user.password, "")
def test_first_name_attr(self):
"""Test that User has attr first_name, and it's an empty string"""
user = User()
self.assertTrue(hasattr(user, "first_name"))
if models.storage_t == 'db':
self.assertEqual(user.first_name, None)
else:
self.assertEqual(user.first_name, "")
def test_last_name_attr(self):
"""Test that User has attr last_name, and it's an empty string"""
user = User()
self.assertTrue(hasattr(user, "last_name"))
if models.storage_t == 'db':
self.assertEqual(user.last_name, None)
else:
self.assertEqual(user.last_name, "")
def test_password_is_hashed_on_creation(self):
"""Test that User password is hashed on creation"""
user = User(password="mypassword")
expected_hash = hashlib.md5("mypassword".encode()).hexdigest()
self.assertEqual(user.password, expected_hash)
def test_password_is_hashed_on_update(self):
"""Test that User password is hashed when updated"""
user = User()
user.password = "newpassword"
expected_hash = hashlib.md5("newpassword".encode()).hexdigest()
self.assertEqual(user.password, expected_hash)
def test_to_dict_creates_dict(self):
"""test to_dict method creates a dictionary with proper attrs"""
u = User()
new_d = u.to_dict()
self.assertEqual(type(new_d), dict)
self.assertFalse("_sa_instance_state" in new_d)
for attr in u.__dict__:
if attr != "_sa_instance_state":
self.assertTrue(attr in new_d)
self.assertTrue("__class__" in new_d)
self.assertNotIn("password", new_d)
def test_to_dict_with_include_password(self):
"""Test to_dict with include_password parameter"""
u = User(password="mypassword")
new_d = u.to_dict(include_password=True)
self.assertIn("password", new_d)
self.assertEqual(new_d["password"],
hashlib.md5("mypassword".encode()).hexdigest())
def test_to_dict_values(self):
"""test that values in dict returned from to_dict are correct"""
t_format = "%Y-%m-%dT%H:%M:%S.%f"
u = User()
new_d = u.to_dict()
self.assertEqual(new_d["__class__"], "User")
self.assertEqual(type(new_d["created_at"]), str)
self.assertEqual(type(new_d["updated_at"]), str)
self.assertEqual(new_d["created_at"], u.created_at.strftime(t_format))
self.assertEqual(new_d["updated_at"], u.updated_at.strftime(t_format))
def test_str(self):
"""test that the str method has the correct output"""
user = User()
string = "[User] ({}) {}".format(user.id, user.__dict__)
self.assertEqual(string, str(user))
if __name__ == "__main__":
unittest.main()