forked from alexaorrico/AirBnB_clone_v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_base_model.py
executable file
·148 lines (125 loc) · 4.85 KB
/
test_base_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/python3
"""
Unit Test for BaseModel Class
"""
import unittest
from datetime import datetime
import models
import json
import os
BaseModel = models.base_model.BaseModel
storage_type = os.environ.get('HBNB_TYPE_STORAGE')
class TestBaseModelDocs(unittest.TestCase):
"""Class for testing BaseModel docs"""
@classmethod
def setUpClass(cls):
print('\n\n.................................')
print('..... Testing Documentation .....')
print('..... For BaseModel Class .....')
print('.................................\n\n')
def test_doc_file(self):
"""... documentation for the file"""
expected = '\nBaseModel Class of Models Module\n'
actual = models.base_model.__doc__
self.assertEqual(expected, actual)
def test_doc_init(self):
"""... documentation for init function"""
expected = 'instantiation of new BaseModel Class'
actual = BaseModel.__init__.__doc__
self.assertEqual(expected, actual)
def test_doc_save(self):
"""... documentation for save function"""
expected = 'updates attribute updated_at to current time'
actual = BaseModel.save.__doc__
self.assertEqual(expected, actual)
def test_doc_to_json(self):
"""... documentation for to_json function"""
expected = 'returns json representation of self'
actual = BaseModel.to_json.__doc__
self.assertEqual(expected, actual)
def test_doc_str(self):
"""... documentation for to str function"""
expected = 'returns string type representation of object instance'
actual = BaseModel.__str__.__doc__
self.assertEqual(expected, actual)
class TestBaseModelInstances(unittest.TestCase):
"""testing for class instances"""
@classmethod
def setUpClass(cls):
print('\n\n.................................')
print('....... Testing Functions .......')
print('..... For BaseModel Class .....')
print('.................................\n\n')
def setUp(self):
"""initializes new BaseModel instance for testing"""
self.model = BaseModel()
def test_instantiation(self):
"""... checks if BaseModel is properly instantiated"""
self.assertIsInstance(self.model, BaseModel)
@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.model)
my_list = ['BaseModel', '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_to_string(self):
"""... checks if BaseModel is properly casted to string"""
my_str = str(self.model)
my_list = ['BaseModel', '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"""
my_str = str(self.model)
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_save(self):
"""... save function should add updated_at attribute"""
self.model.save()
actual = type(self.model.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"""
my_model_json = self.model.to_json()
actual = 1
try:
serialized = json.dumps(my_model_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 BaseModel"""
my_model_json = self.model.to_json()
actual = None
if my_model_json['__class__']:
actual = my_model_json['__class__']
expected = 'BaseModel'
self.assertEqual(expected, actual)
def test_name_attribute(self):
"""... add name attribute"""
self.model.name = "Holberton"
actual = self.model.name
expected = "Holberton"
self.assertEqual(expected, actual)
def test_number_attribute(self):
"""... add number attribute"""
self.model.number = 98
actual = self.model.number
self.assertTrue(98 == actual)
if __name__ == '__main__':
unittest.main