forked from alexaorrico/AirBnB_clone_v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_user.py
executable file
·118 lines (100 loc) · 3.68 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
#!/usr/bin/python3
"""
Unit Test for User Class
"""
import unittest
from datetime import datetime
import models
import json
import os
User = models.user.User
BaseModel = models.base_model.BaseModel
storage_type = os.environ.get('HBNB_TYPE_STORAGE')
class TestUserDocs(unittest.TestCase):
"""Class for testing User Class docs"""
@classmethod
def setUpClass(cls):
print('\n\n.................................')
print('..... Testing Documentation .....')
print('........ User Class ........')
print('.................................\n\n')
def test_doc_file(self):
"""... documentation for the file"""
expected = '\nUser Class from Models Module\n'
actual = models.user.__doc__
self.assertEqual(expected, actual)
def test_doc_class(self):
"""... documentation for the class"""
expected = 'User class handles all application users'
actual = User.__doc__
self.assertEqual(expected, actual)
class TestUserInstances(unittest.TestCase):
"""testing for class instances"""
@classmethod
def setUpClass(cls):
print('\n\n.................................')
print('....... Testing Functions .......')
print('......... User Class .........')
print('.................................\n\n')
def setUp(self):
"""initializes new user for testing"""
self.user = User()
def test_instantiation(self):
"""... checks if User is properly instantiated"""
self.assertIsInstance(self.user, User)
@unittest.skipIf(storage_type == 'db', 'skip if environ is db')
def test_to_string(self):
"""... checks if BaseModel is properly casted to string"""
my_str = str(self.user)
my_list = ['User', 'id', 'created_at']
actual = 0
for sub_str in my_list:
if sub_str in my_str:
actual += 1
self.assertTrue(3 == actual)
@unittest.skipIf(storage_type == 'db', 'skip if environ is db')
def test_instantiation_no_updated(self):
"""... should not have updated attribute"""
self.user = User()
my_str = str(self.user)
actual = 0
if 'updated_at' in my_str:
actual += 1
self.assertTrue(0 == actual)
@unittest.skipIf(storage_type == 'db', 'skip if environ is db')
def test_updated_at(self):
"""... save function should add updated_at attribute"""
self.user.save()
actual = type(self.user.updated_at)
expected = type(datetime.now())
self.assertEqual(expected, actual)
@unittest.skipIf(storage_type == 'db', 'skip if environ is db')
def test_to_json(self):
"""... to_json should return serializable dict object"""
self.user_json = self.user.to_json()
actual = 1
try:
serialized = json.dumps(self.user_json)
except:
actual = 0
self.assertTrue(1 == actual)
@unittest.skipIf(storage_type == 'db', 'skip if environ is db')
def test_json_class(self):
"""... to_json should include class key with value User"""
self.user_json = self.user.to_json()
actual = None
if self.user_json['__class__']:
actual = self.user_json['__class__']
expected = 'User'
self.assertEqual(expected, actual)
def test_email_attribute(self):
"""... add email attribute"""
self.user.email = "[email protected]"
if hasattr(self.user, 'email'):
actual = self.user.email
else:
actual = ''
expected = "[email protected]"
self.assertEqual(expected, actual)
if __name__ == '__main__':
unittest.main