-
Notifications
You must be signed in to change notification settings - Fork 8
/
test_file_storage.py
101 lines (80 loc) · 3.34 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
#!/usr/bin/python3
""" Module of Unittests """
import unittest
from models.base_model import BaseModel
from models.engine.file_storage import FileStorage
from models import storage
import os
import json
class FileStorageTests(unittest.TestCase):
""" Suite of File Storage Tests """
my_model = BaseModel()
def testClassInstance(self):
""" Check instance """
self.assertIsInstance(storage, FileStorage)
def testStoreBaseModel(self):
""" Test save and reload functions """
self.my_model.full_name = "BaseModel Instance"
self.my_model.save()
bm_dict = self.my_model.to_dict()
all_objs = storage.all()
key = bm_dict['__class__'] + "." + bm_dict['id']
self.assertEqual(key in all_objs, True)
def testStoreBaseModel2(self):
""" Test save, reload and update functions """
self.my_model.my_name = "First name"
self.my_model.save()
bm_dict = self.my_model.to_dict()
all_objs = storage.all()
key = bm_dict['__class__'] + "." + bm_dict['id']
self.assertEqual(key in all_objs, True)
self.assertEqual(bm_dict['my_name'], "First name")
create1 = bm_dict['created_at']
update1 = bm_dict['updated_at']
self.my_model.my_name = "Second name"
self.my_model.save()
bm_dict = self.my_model.to_dict()
all_objs = storage.all()
self.assertEqual(key in all_objs, True)
create2 = bm_dict['created_at']
update2 = bm_dict['updated_at']
self.assertEqual(create1, create2)
self.assertNotEqual(update1, update2)
self.assertEqual(bm_dict['my_name'], "Second name")
def testHasAttributes(self):
"""verify if attributes exist"""
self.assertEqual(hasattr(FileStorage, '_FileStorage__file_path'), True)
self.assertEqual(hasattr(FileStorage, '_FileStorage__objects'), True)
def testsave(self):
"""verify if JSON exists"""
self.my_model.save()
self.assertEqual(os.path.exists(storage._FileStorage__file_path), True)
self.assertEqual(storage.all(), storage._FileStorage__objects)
def testreload(self):
"""test if reload """
self.my_model.save()
self.assertEqual(os.path.exists(storage._FileStorage__file_path), True)
dobj = storage.all()
FileStorage._FileStorage__objects = {}
self.assertNotEqual(dobj, FileStorage._FileStorage__objects)
storage.reload()
for key, value in storage.all().items():
self.assertEqual(dobj[key].to_dict(), value.to_dict())
def testSaveSelf(self):
""" Check save self """
msg = "save() takes 1 positional argument but 2 were given"
with self.assertRaises(TypeError) as e:
FileStorage.save(self, 100)
self.assertEqual(str(e.exception), msg)
def test_save_FileStorage(self):
""" Test if 'new' method is working good """
var1 = self.my_model.to_dict()
new_key = var1['__class__'] + "." + var1['id']
storage.save()
with open("file.json", 'r') as fd:
var2 = json.load(fd)
new = var2[new_key]
for key in new:
self.assertEqual(var1[key], new[key])
if __name__ == '__main__':
unittest.main()