forked from alexaorrico/AirBnB_clone_v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_state.py
executable file
·116 lines (99 loc) · 3.64 KB
/
test_state.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
#!/usr/bin/python3
"""
Unit Test for State Class
"""
import unittest
from datetime import datetime
import models
import json
import os
State = models.state.State
BaseModel = models.base_model.BaseModel
storage_type = os.environ.get('HBNB_TYPE_STORAGE')
class TestStateDocs(unittest.TestCase):
"""Class for testing State docs"""
@classmethod
def setUpClass(cls):
print('\n\n.................................')
print('..... Testing Documentation .....')
print('........ State Class ........')
print('.................................\n\n')
def test_doc_file(self):
"""... documentation for the file"""
expected = '\nState Class from Models Module\n'
actual = models.state.__doc__
self.assertEqual(expected, actual)
def test_doc_class(self):
"""... documentation for the class"""
expected = 'State class handles all application states'
actual = State.__doc__
self.assertEqual(expected, actual)
class TestStateInstances(unittest.TestCase):
"""testing for class instances"""
@classmethod
def setUpClass(cls):
print('\n\n.................................')
print('....... Testing Functions .......')
print('......... State Class .........')
print('.................................\n\n')
def setUp(self):
"""initializes new state for testing"""
self.state = State()
def test_instantiation(self):
"""... checks if State is properly instantiated"""
self.assertIsInstance(self.state, State)
@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.state)
my_list = ['State', '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.state)
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.state.save()
actual = type(self.state.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.state_json = self.state.to_json()
actual = 1
try:
serialized = json.dumps(self.state_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 State"""
self.state_json = self.state.to_json()
actual = None
if self.state_json['__class__']:
actual = self.state_json['__class__']
expected = 'State'
self.assertEqual(expected, actual)
def test_name_attribute(self):
"""... add name attribute"""
self.state.name = "betty"
if hasattr(self.state, 'name'):
actual = self.state.name
else:
acual = ''
expected = "betty"
self.assertEqual(expected, actual)
if __name__ == '__main__':
unittest.main