-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_file_storage.py
executable file
·374 lines (303 loc) · 12.5 KB
/
test_file_storage.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/usr/bin/python3
"""Unittest module for the FileStorage class."""
import unittest
from datetime import datetime
import time
from models.base_model import BaseModel
from models.engine.file_storage import FileStorage
from models import storage
import re
import json
import os
class TestFileStorage(unittest.TestCase):
"""Test Cases for the FileStorage class."""
def setUp(self):
"""Sets up test methods."""
pass
def resetStorage(self):
"""Resets FileStorage data."""
FileStorage._FileStorage__objects = {}
if os.path.isfile(FileStorage._FileStorage__file_path):
os.remove(FileStorage._FileStorage__file_path)
def tearDown(self):
"""Tears down test methods."""
self.resetStorage()
pass
def test_5_instantiation(self):
"""Tests instantiation of storage class."""
self.assertEqual(type(storage).__name__, "FileStorage")
def test_3_init_no_args(self):
"""Tests __init__ with no arguments."""
self.resetStorage()
with self.assertRaises(TypeError) as e:
FileStorage.__init__()
msg = "descriptor '__init__' of 'object' object needs an argument"
self.assertEqual(str(e.exception), msg)
def test_3_init_many_args(self):
"""Tests __init__ with many arguments."""
self.resetStorage()
with self.assertRaises(TypeError) as e:
b = FileStorage(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
msg = "object() takes no parameters"
self.assertEqual(str(e.exception), msg)
def test_5_attributes(self):
"""Tests class attributes."""
self.resetStorage()
self.assertTrue(hasattr(FileStorage, "_FileStorage__file_path"))
self.assertTrue(hasattr(FileStorage, "_FileStorage__objects"))
self.assertEqual(getattr(FileStorage, "_FileStorage__objects"), {})
def help_test_all(self, classname):
"""Helper tests all() method for classname."""
self.resetStorage()
self.assertEqual(storage.all(), {})
o = storage.classes()[classname]()
storage.new(o)
key = "{}.{}".format(type(o).__name__, o.id)
self.assertTrue(key in storage.all())
self.assertEqual(storage.all()[key], o)
def test_5_all_base_model(self):
"""Tests all() method for BaseModel."""
self.help_test_all("BaseModel")
def test_5_all_user(self):
"""Tests all() method for User."""
self.help_test_all("User")
def test_5_all_state(self):
"""Tests all() method for State."""
self.help_test_all("State")
def test_5_all_city(self):
"""Tests all() method for City."""
self.help_test_all("City")
def test_5_all_amenity(self):
"""Tests all() method for Amenity."""
self.help_test_all("Amenity")
def test_5_all_place(self):
"""Tests all() method for Place."""
self.help_test_all("Place")
def test_5_all_review(self):
"""Tests all() method for Review."""
self.help_test_all("Review")
def help_test_all_multiple(self, classname):
"""Helper tests all() method with many objects for classname."""
self.resetStorage()
self.assertEqual(storage.all(), {})
cls = storage.classes()[classname]
objs = [cls() for i in range(1000)]
[storage.new(o) for o in objs]
self.assertEqual(len(objs), len(storage.all()))
for o in objs:
key = "{}.{}".format(type(o).__name__, o.id)
self.assertTrue(key in storage.all())
self.assertEqual(storage.all()[key], o)
def test_5_all_multiple_base_model(self):
"""Tests all() method with many objects."""
self.help_test_all_multiple("BaseModel")
def test_5_all_multiple_user(self):
"""Tests all_multiple() method for User."""
self.help_test_all_multiple("User")
def test_5_all_multiple_state(self):
"""Tests all_multiple() method for State."""
self.help_test_all_multiple("State")
def test_5_all_multiple_city(self):
"""Tests all_multiple() method for City."""
self.help_test_all_multiple("City")
def test_5_all_multiple_amenity(self):
"""Tests all_multiple() method for Amenity."""
self.help_test_all_multiple("Amenity")
def test_5_all_multiple_place(self):
"""Tests all_multiple() method for Place."""
self.help_test_all_multiple("Place")
def test_5_all_multiple_review(self):
"""Tests all_multiple() method for Review."""
self.help_test_all_multiple("Review")
def test_5_all_no_args(self):
"""Tests all() with no arguments."""
self.resetStorage()
with self.assertRaises(TypeError) as e:
FileStorage.all()
msg = "all() missing 1 required positional argument: 'self'"
self.assertEqual(str(e.exception), msg)
def test_5_all_excess_args(self):
"""Tests all() with too many arguments."""
self.resetStorage()
with self.assertRaises(TypeError) as e:
FileStorage.all(self, 98)
msg = "all() takes 1 positional argument but 2 were given"
self.assertEqual(str(e.exception), msg)
def help_test_new(self, classname):
"""Helps tests new() method for classname."""
self.resetStorage()
cls = storage.classes()[classname]
o = cls()
storage.new(o)
key = "{}.{}".format(type(o).__name__, o.id)
self.assertTrue(key in FileStorage._FileStorage__objects)
self.assertEqual(FileStorage._FileStorage__objects[key], o)
def test_5_new_base_model(self):
"""Tests new() method for BaseModel."""
self.help_test_new("BaseModel")
def test_5_new_user(self):
"""Tests new() method for User."""
self.help_test_new("User")
def test_5_new_state(self):
"""Tests new() method for State."""
self.help_test_new("State")
def test_5_new_city(self):
"""Tests new() method for City."""
self.help_test_new("City")
def test_5_new_amenity(self):
"""Tests new() method for Amenity."""
self.help_test_new("Amenity")
def test_5_new_place(self):
"""Tests new() method for Place."""
self.help_test_new("Place")
def test_5_new_review(self):
"""Tests new() method for Review."""
self.help_test_new("Review")
def test_5_new_no_args(self):
"""Tests new() with no arguments."""
self.resetStorage()
with self.assertRaises(TypeError) as e:
storage.new()
msg = "new() missing 1 required positional argument: 'obj'"
self.assertEqual(str(e.exception), msg)
def test_5_new_excess_args(self):
"""Tests new() with too many arguments."""
self.resetStorage()
b = BaseModel()
with self.assertRaises(TypeError) as e:
storage.new(b, 98)
msg = "new() takes 2 positional arguments but 3 were given"
self.assertEqual(str(e.exception), msg)
def help_test_save(self, classname):
"""Helps tests save() method for classname."""
self.resetStorage()
cls = storage.classes()[classname]
o = cls()
storage.new(o)
key = "{}.{}".format(type(o).__name__, o.id)
storage.save()
self.assertTrue(os.path.isfile(FileStorage._FileStorage__file_path))
d = {key: o.to_dict()}
with open(FileStorage._FileStorage__file_path,
"r", encoding="utf-8") as f:
self.assertEqual(len(f.read()), len(json.dumps(d)))
f.seek(0)
self.assertEqual(json.load(f), d)
def test_5_save_base_model(self):
"""Tests save() method for BaseModel."""
self.help_test_save("BaseModel")
def test_5_save_user(self):
"""Tests save() method for User."""
self.help_test_save("User")
def test_5_save_state(self):
"""Tests save() method for State."""
self.help_test_save("State")
def test_5_save_city(self):
"""Tests save() method for City."""
self.help_test_save("City")
def test_5_save_amenity(self):
"""Tests save() method for Amenity."""
self.help_test_save("Amenity")
def test_5_save_place(self):
"""Tests save() method for Place."""
self.help_test_save("Place")
def test_5_save_review(self):
"""Tests save() method for Review."""
self.help_test_save("Review")
def test_5_save_no_args(self):
"""Tests save() with no arguments."""
self.resetStorage()
with self.assertRaises(TypeError) as e:
FileStorage.save()
msg = "save() missing 1 required positional argument: 'self'"
self.assertEqual(str(e.exception), msg)
def test_5_save_excess_args(self):
"""Tests save() with too many arguments."""
self.resetStorage()
with self.assertRaises(TypeError) as e:
FileStorage.save(self, 98)
msg = "save() takes 1 positional argument but 2 were given"
self.assertEqual(str(e.exception), msg)
def help_test_reload(self, classname):
"""Helps test reload() method for classname."""
self.resetStorage()
storage.reload()
self.assertEqual(FileStorage._FileStorage__objects, {})
cls = storage.classes()[classname]
o = cls()
storage.new(o)
key = "{}.{}".format(type(o).__name__, o.id)
storage.save()
storage.reload()
self.assertEqual(o.to_dict(), storage.all()[key].to_dict())
def test_5_reload_base_model(self):
"""Tests reload() method for BaseModel."""
self.help_test_reload("BaseModel")
def test_5_reload_user(self):
"""Tests reload() method for User."""
self.help_test_reload("User")
def test_5_reload_state(self):
"""Tests reload() method for State."""
self.help_test_reload("State")
def test_5_reload_city(self):
"""Tests reload() method for City."""
self.help_test_reload("City")
def test_5_reload_amenity(self):
"""Tests reload() method for Amenity."""
self.help_test_reload("Amenity")
def test_5_reload_place(self):
"""Tests reload() method for Place."""
self.help_test_reload("Place")
def test_5_reload_review(self):
"""Tests reload() method for Review."""
self.help_test_reload("Review")
def help_test_reload_mismatch(self, classname):
"""Helps test reload() method for classname."""
self.resetStorage()
storage.reload()
self.assertEqual(FileStorage._FileStorage__objects, {})
cls = storage.classes()[classname]
o = cls()
storage.new(o)
key = "{}.{}".format(type(o).__name__, o.id)
storage.save()
o.name = "Laura"
storage.reload()
self.assertNotEqual(o.to_dict(), storage.all()[key].to_dict())
def test_5_reload_mismatch_base_model(self):
"""Tests reload() method mismatch for BaseModel."""
self.help_test_reload_mismatch("BaseModel")
def test_5_reload_mismatch_user(self):
"""Tests reload_mismatch() method for User."""
self.help_test_reload_mismatch("User")
def test_5_reload_mismatch_state(self):
"""Tests reload_mismatch() method for State."""
self.help_test_reload_mismatch("State")
def test_5_reload_mismatch_city(self):
"""Tests reload_mismatch() method for City."""
self.help_test_reload_mismatch("City")
def test_5_reload_mismatch_amenity(self):
"""Tests reload_mismatch() method for Amenity."""
self.help_test_reload_mismatch("Amenity")
def test_5_reload_mismatch_place(self):
"""Tests reload_mismatch() method for Place."""
self.help_test_reload_mismatch("Place")
def test_5_reload_mismatch_review(self):
"""Tests reload_mismatch() method for Review."""
self.help_test_reload_mismatch("Review")
def test_5_reload_no_args(self):
"""Tests reload() with no arguments."""
self.resetStorage()
with self.assertRaises(TypeError) as e:
FileStorage.reload()
msg = "reload() missing 1 required positional argument: 'self'"
self.assertEqual(str(e.exception), msg)
def test_5_reload_excess_args(self):
"""Tests reload() with too many arguments."""
self.resetStorage()
with self.assertRaises(TypeError) as e:
FileStorage.reload(self, 98)
msg = "reload() takes 1 positional argument but 2 were given"
self.assertEqual(str(e.exception), msg)
if __name__ == '__main__':
unittest.main()