-
Notifications
You must be signed in to change notification settings - Fork 8
/
test_place.py
executable file
·249 lines (204 loc) · 7.68 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/python3
"""Defines unittests for models/place.py.
Unittest classes:
TestPlace_instantiation
TestPlace_save
TestPlace_to_dict
"""
import os
import models
import unittest
from datetime import datetime
from time import sleep
from models.place import Place
class TestPlace_instantiation(unittest.TestCase):
"""Unittests for testing instantiation of the Place class."""
def test_no_args_instantiates(self):
self.assertEqual(Place, type(Place()))
def test_new_instance_stored_in_objects(self):
self.assertIn(Place(), models.storage.all().values())
def test_id_is_public_str(self):
self.assertEqual(str, type(Place().id))
def test_created_at_is_public_datetime(self):
self.assertEqual(datetime, type(Place().created_at))
def test_updated_at_is_public_datetime(self):
self.assertEqual(datetime, type(Place().updated_at))
def test_city_id_is_public_class_attribute(self):
pl = Place()
self.assertEqual(str, type(Place.city_id))
self.assertIn("city_id", dir(pl))
self.assertNotIn("city_id", pl.__dict__)
def test_user_id_is_public_class_attribute(self):
pl = Place()
self.assertEqual(str, type(Place.user_id))
self.assertIn("user_id", dir(pl))
self.assertNotIn("user_id", pl.__dict__)
def test_name_is_public_class_attribute(self):
pl = Place()
self.assertEqual(str, type(Place.name))
self.assertIn("name", dir(pl))
self.assertNotIn("name", pl.__dict__)
def test_description_is_public_class_attribute(self):
pl = Place()
self.assertEqual(str, type(Place.description))
self.assertIn("description", dir(pl))
self.assertNotIn("desctiption", pl.__dict__)
def test_number_rooms_is_public_class_attribute(self):
pl = Place()
self.assertEqual(int, type(Place.number_rooms))
self.assertIn("number_rooms", dir(pl))
self.assertNotIn("number_rooms", pl.__dict__)
def test_number_bathrooms_is_public_class_attribute(self):
pl = Place()
self.assertEqual(int, type(Place.number_bathrooms))
self.assertIn("number_bathrooms", dir(pl))
self.assertNotIn("number_bathrooms", pl.__dict__)
def test_max_guest_is_public_class_attribute(self):
pl = Place()
self.assertEqual(int, type(Place.max_guest))
self.assertIn("max_guest", dir(pl))
self.assertNotIn("max_guest", pl.__dict__)
def test_price_by_night_is_public_class_attribute(self):
pl = Place()
self.assertEqual(int, type(Place.price_by_night))
self.assertIn("price_by_night", dir(pl))
self.assertNotIn("price_by_night", pl.__dict__)
def test_latitude_is_public_class_attribute(self):
pl = Place()
self.assertEqual(float, type(Place.latitude))
self.assertIn("latitude", dir(pl))
self.assertNotIn("latitude", pl.__dict__)
def test_longitude_is_public_class_attribute(self):
pl = Place()
self.assertEqual(float, type(Place.longitude))
self.assertIn("longitude", dir(pl))
self.assertNotIn("longitude", pl.__dict__)
def test_amenity_ids_is_public_class_attribute(self):
pl = Place()
self.assertEqual(list, type(Place.amenity_ids))
self.assertIn("amenity_ids", dir(pl))
self.assertNotIn("amenity_ids", pl.__dict__)
def test_two_places_unique_ids(self):
pl1 = Place()
pl2 = Place()
self.assertNotEqual(pl1.id, pl2.id)
def test_two_places_different_created_at(self):
pl1 = Place()
sleep(0.05)
pl2 = Place()
self.assertLess(pl1.created_at, pl2.created_at)
def test_two_places_different_updated_at(self):
pl1 = Place()
sleep(0.05)
pl2 = Place()
self.assertLess(pl1.updated_at, pl2.updated_at)
def test_str_representation(self):
dt = datetime.today()
dt_repr = repr(dt)
pl = Place()
pl.id = "123456"
pl.created_at = pl.updated_at = dt
plstr = pl.__str__()
self.assertIn("[Place] (123456)", plstr)
self.assertIn("'id': '123456'", plstr)
self.assertIn("'created_at': " + dt_repr, plstr)
self.assertIn("'updated_at': " + dt_repr, plstr)
def test_args_unused(self):
pl = Place(None)
self.assertNotIn(None, pl.__dict__.values())
def test_instantiation_with_kwargs(self):
dt = datetime.today()
dt_iso = dt.isoformat()
pl = Place(id="345", created_at=dt_iso, updated_at=dt_iso)
self.assertEqual(pl.id, "345")
self.assertEqual(pl.created_at, dt)
self.assertEqual(pl.updated_at, dt)
def test_instantiation_with_None_kwargs(self):
with self.assertRaises(TypeError):
Place(id=None, created_at=None, updated_at=None)
class TestPlace_save(unittest.TestCase):
"""Unittests for testing save method of the Place class."""
@classmethod
def setUp(self):
try:
os.rename("file.json", "tmp")
except IOError:
pass
def tearDown(self):
try:
os.remove("file.json")
except IOError:
pass
try:
os.rename("tmp", "file.json")
except IOError:
pass
def test_one_save(self):
pl = Place()
sleep(0.05)
first_updated_at = pl.updated_at
pl.save()
self.assertLess(first_updated_at, pl.updated_at)
def test_two_saves(self):
pl = Place()
sleep(0.05)
first_updated_at = pl.updated_at
pl.save()
second_updated_at = pl.updated_at
self.assertLess(first_updated_at, second_updated_at)
sleep(0.05)
pl.save()
self.assertLess(second_updated_at, pl.updated_at)
def test_save_with_arg(self):
pl = Place()
with self.assertRaises(TypeError):
pl.save(None)
def test_save_updates_file(self):
pl = Place()
pl.save()
plid = "Place." + pl.id
with open("file.json", "r") as f:
self.assertIn(plid, f.read())
class TestPlace_to_dict(unittest.TestCase):
"""Unittests for testing to_dict method of the Place class."""
def test_to_dict_type(self):
self.assertTrue(dict, type(Place().to_dict()))
def test_to_dict_contains_correct_keys(self):
pl = Place()
self.assertIn("id", pl.to_dict())
self.assertIn("created_at", pl.to_dict())
self.assertIn("updated_at", pl.to_dict())
self.assertIn("__class__", pl.to_dict())
def test_to_dict_contains_added_attributes(self):
pl = Place()
pl.middle_name = "Holberton"
pl.my_number = 98
self.assertEqual("Holberton", pl.middle_name)
self.assertIn("my_number", pl.to_dict())
def test_to_dict_datetime_attributes_are_strs(self):
pl = Place()
pl_dict = pl.to_dict()
self.assertEqual(str, type(pl_dict["id"]))
self.assertEqual(str, type(pl_dict["created_at"]))
self.assertEqual(str, type(pl_dict["updated_at"]))
def test_to_dict_output(self):
dt = datetime.today()
pl = Place()
pl.id = "123456"
pl.created_at = pl.updated_at = dt
tdict = {
'id': '123456',
'__class__': 'Place',
'created_at': dt.isoformat(),
'updated_at': dt.isoformat(),
}
self.assertDictEqual(pl.to_dict(), tdict)
def test_contrast_to_dict_dunder_dict(self):
pl = Place()
self.assertNotEqual(pl.to_dict(), pl.__dict__)
def test_to_dict_with_arg(self):
pl = Place()
with self.assertRaises(TypeError):
pl.to_dict(None)
if __name__ == "__main__":
unittest.main()