forked from alexaorrico/AirBnB_clone_v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_place.py
executable file
·117 lines (99 loc) · 3.66 KB
/
test_place.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
#!/usr/bin/python3
"""
Unit Test for Place Class
"""
import unittest
from datetime import datetime
import models
import json
import os
Place = models.place.Place
BaseModel = models.base_model.BaseModel
storage_type = os.environ.get('HBNB_TYPE_STORAGE')
class TestPlaceDocs(unittest.TestCase):
"""Class for testing BaseModel docs"""
@classmethod
def setUpClass(cls):
print('\n\n.................................')
print('..... Testing Documentation .....')
print('........ Place Class ........')
print('.................................\n\n')
def test_doc_file(self):
"""... documentation for the file"""
expected = '\nPlace Class from Models Module\n'
actual = models.place.__doc__
self.assertEqual(expected, actual)
def test_doc_class(self):
"""... documentation for the class"""
expected = 'Place class handles all application places'
actual = Place.__doc__
self.assertEqual(expected, actual)
class TestPlaceInstances(unittest.TestCase):
"""testing for class instances"""
@classmethod
def setUpClass(cls):
print('\n\n.................................')
print('....... Testing Functions .......')
print('......... Place Class .........')
print('.................................\n\n')
def setUp(self):
"""initializes new place for testing"""
self.place = Place()
def test_instantiation(self):
"""... checks if Place is properly instantiated"""
self.assertIsInstance(self.place, Place)
@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.place)
my_list = ['Place', '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.place)
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.place.save()
actual = type(self.place.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.place_json = self.place.to_json()
actual = 1
try:
serialized = json.dumps(self.place_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 Place"""
self.place_json = self.place.to_json()
actual = None
if self.place_json['__class__']:
actual = self.place_json['__class__']
expected = 'Place'
self.assertEqual(expected, actual)
def test_guest_attribute(self):
"""... add guest attribute"""
self.place.max_guest = 3
if hasattr(self.place, 'max_guest'):
actual = self.place.max_guest
else:
actual = ''
expected = 3
self.assertEqual(expected, actual)
if __name__ == '__main__':
unittest.main