forked from alexaorrico/AirBnB_clone_v2
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
test_user.py
executable file
·132 lines (115 loc) · 4.9 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
#!/usr/bin/python3
"""
Contains the TestUserDocs classes
"""
from datetime import datetime
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 City 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]))
class TestUser(unittest.TestCase):
"""Test the User class"""
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_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 is not "_sa_instance_state":
self.assertTrue(attr in new_d)
self.assertTrue("__class__" in new_d)
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))