forked from FrankieVexx/AirBnB_clone_v3
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_place.py
executable file
·200 lines (176 loc) · 7.67 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/python3
"""
Contains the TestPlaceDocs classes
"""
from datetime import datetime
import inspect
import models
from models import place
from models.base_model import BaseModel
import pep8
import unittest
Place = place.Place
class TestPlaceDocs(unittest.TestCase):
"""Tests to check the documentation and style of Place class"""
@classmethod
def setUpClass(cls):
"""Set up for the doc tests"""
cls.place_f = inspect.getmembers(Place, inspect.isfunction)
def test_pep8_conformance_place(self):
"""Test that models/place.py conforms to PEP8."""
pep8s = pep8.StyleGuide(quiet=True)
result = pep8s.check_files(['models/place.py'])
self.assertEqual(result.total_errors, 0,
"Found code style errors (and warnings).")
def test_pep8_conformance_test_place(self):
"""Test that tests/test_models/test_place.py conforms to PEP8."""
pep8s = pep8.StyleGuide(quiet=True)
result = pep8s.check_files(['tests/test_models/test_place.py'])
self.assertEqual(result.total_errors, 0,
"Found code style errors (and warnings).")
def test_place_module_docstring(self):
"""Test for the place.py module docstring"""
self.assertIsNot(place.__doc__, None,
"place.py needs a docstring")
self.assertTrue(len(place.__doc__) >= 1,
"place.py needs a docstring")
def test_place_class_docstring(self):
"""Test for the Place class docstring"""
self.assertIsNot(Place.__doc__, None,
"Place class needs a docstring")
self.assertTrue(len(Place.__doc__) >= 1,
"Place class needs a docstring")
def test_place_func_docstrings(self):
"""Test for the presence of docstrings in Place methods"""
for func in self.place_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 TestPlace(unittest.TestCase):
"""Test the Place class"""
def test_is_subclass(self):
"""Test that Place is a subclass of BaseModel"""
place = Place()
self.assertIsInstance(place, BaseModel)
self.assertTrue(hasattr(place, "id"))
self.assertTrue(hasattr(place, "created_at"))
self.assertTrue(hasattr(place, "updated_at"))
def test_city_id_attr(self):
"""Test Place has attr city_id, and it's an empty string"""
place = Place()
self.assertTrue(hasattr(place, "city_id"))
if models.storage_t == 'db':
self.assertEqual(place.city_id, None)
else:
self.assertEqual(place.city_id, "")
def test_user_id_attr(self):
"""Test Place has attr user_id, and it's an empty string"""
place = Place()
self.assertTrue(hasattr(place, "user_id"))
if models.storage_t == 'db':
self.assertEqual(place.user_id, None)
else:
self.assertEqual(place.user_id, "")
def test_name_attr(self):
"""Test Place has attr name, and it's an empty string"""
place = Place()
self.assertTrue(hasattr(place, "name"))
if models.storage_t == 'db':
self.assertEqual(place.name, None)
else:
self.assertEqual(place.name, "")
def test_description_attr(self):
"""Test Place has attr description, and it's an empty string"""
place = Place()
self.assertTrue(hasattr(place, "description"))
if models.storage_t == 'db':
self.assertEqual(place.description, None)
else:
self.assertEqual(place.description, "")
def test_number_rooms_attr(self):
"""Test Place has attr number_rooms, and it's an int == 0"""
place = Place()
self.assertTrue(hasattr(place, "number_rooms"))
if models.storage_t == 'db':
self.assertEqual(place.number_rooms, None)
else:
self.assertEqual(type(place.number_rooms), int)
self.assertEqual(place.number_rooms, 0)
def test_number_bathrooms_attr(self):
"""Test Place has attr number_bathrooms, and it's an int == 0"""
place = Place()
self.assertTrue(hasattr(place, "number_bathrooms"))
if models.storage_t == 'db':
self.assertEqual(place.number_bathrooms, None)
else:
self.assertEqual(type(place.number_bathrooms), int)
self.assertEqual(place.number_bathrooms, 0)
def test_max_guest_attr(self):
"""Test Place has attr max_guest, and it's an int == 0"""
place = Place()
self.assertTrue(hasattr(place, "max_guest"))
if models.storage_t == 'db':
self.assertEqual(place.max_guest, None)
else:
self.assertEqual(type(place.max_guest), int)
self.assertEqual(place.max_guest, 0)
def test_price_by_night_attr(self):
"""Test Place has attr price_by_night, and it's an int == 0"""
place = Place()
self.assertTrue(hasattr(place, "price_by_night"))
if models.storage_t == 'db':
self.assertEqual(place.price_by_night, None)
else:
self.assertEqual(type(place.price_by_night), int)
self.assertEqual(place.price_by_night, 0)
def test_latitude_attr(self):
"""Test Place has attr latitude, and it's a float == 0.0"""
place = Place()
self.assertTrue(hasattr(place, "latitude"))
if models.storage_t == 'db':
self.assertEqual(place.latitude, None)
else:
self.assertEqual(type(place.latitude), float)
self.assertEqual(place.latitude, 0.0)
def test_longitude_attr(self):
"""Test Place has attr longitude, and it's a float == 0.0"""
place = Place()
self.assertTrue(hasattr(place, "longitude"))
if models.storage_t == 'db':
self.assertEqual(place.longitude, None)
else:
self.assertEqual(type(place.longitude), float)
self.assertEqual(place.longitude, 0.0)
@unittest.skipIf(models.storage_t == 'db', "not testing File Storage")
def test_amenity_ids_attr(self):
"""Test Place has attr amenity_ids, and it's an empty list"""
place = Place()
self.assertTrue(hasattr(place, "amenity_ids"))
self.assertEqual(type(place.amenity_ids), list)
self.assertEqual(len(place.amenity_ids), 0)
def test_to_dict_creates_dict(self):
"""test to_dict method creates a dictionary with proper attrs"""
p = Place()
new_d = p.to_dict()
self.assertEqual(type(new_d), dict)
self.assertFalse("_sa_instance_state" in new_d)
for attr in p.__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"
p = Place()
new_d = p.to_dict()
self.assertEqual(new_d["__class__"], "Place")
self.assertEqual(type(new_d["created_at"]), str)
self.assertEqual(type(new_d["updated_at"]), str)
self.assertEqual(new_d["created_at"], p.created_at.strftime(t_format))
self.assertEqual(new_d["updated_at"], p.updated_at.strftime(t_format))
def test_str(self):
"""test that the str method has the correct output"""
place = Place()
string = "[Place] ({}) {}".format(place.id, place.__dict__)
self.assertEqual(string, str(place))