-
Notifications
You must be signed in to change notification settings - Fork 8
/
test_file_storage.py
executable file
·173 lines (150 loc) · 5.41 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
#!/usr/bin/python3
"""Defines unittests for models/engine/file_storage.py.
Unittest classes:
TestFileStorage_instantiation
TestFileStorage_methods
"""
import os
import json
import models
import unittest
from datetime import datetime
from models.base_model import BaseModel
from models.engine.file_storage import FileStorage
from models.user import User
from models.state import State
from models.place import Place
from models.city import City
from models.amenity import Amenity
from models.review import Review
class TestFileStorage_instantiation(unittest.TestCase):
"""Unittests for testing instantiation of the FileStorage class."""
def test_FileStorage_instantiation_no_args(self):
self.assertEqual(type(FileStorage()), FileStorage)
def test_FileStorage_instantiation_with_arg(self):
with self.assertRaises(TypeError):
FileStorage(None)
def test_FileStorage_file_path_is_private_str(self):
self.assertEqual(str, type(FileStorage._FileStorage__file_path))
def testFileStorage_objects_is_private_dict(self):
self.assertEqual(dict, type(FileStorage._FileStorage__objects))
def test_storage_initializes(self):
self.assertEqual(type(models.storage), FileStorage)
class TestFileStorage_methods(unittest.TestCase):
"""Unittests for testing methods of the FileStorage class."""
@classmethod
def setUp(self):
try:
os.rename("file.json", "tmp")
except IOError:
pass
@classmethod
def tearDown(self):
try:
os.remove("file.json")
except IOError:
pass
try:
os.rename("tmp", "file.json")
except IOError:
pass
FileStorage._FileStorage__objects = {}
def test_all(self):
self.assertEqual(dict, type(models.storage.all()))
def test_all_with_arg(self):
with self.assertRaises(TypeError):
models.storage.all(None)
def test_new(self):
bm = BaseModel()
us = User()
st = State()
pl = Place()
cy = City()
am = Amenity()
rv = Review()
models.storage.new(bm)
models.storage.new(us)
models.storage.new(st)
models.storage.new(pl)
models.storage.new(cy)
models.storage.new(am)
models.storage.new(rv)
self.assertIn("BaseModel." + bm.id, models.storage.all().keys())
self.assertIn(bm, models.storage.all().values())
self.assertIn("User." + us.id, models.storage.all().keys())
self.assertIn(us, models.storage.all().values())
self.assertIn("State." + st.id, models.storage.all().keys())
self.assertIn(st, models.storage.all().values())
self.assertIn("Place." + pl.id, models.storage.all().keys())
self.assertIn(pl, models.storage.all().values())
self.assertIn("City." + cy.id, models.storage.all().keys())
self.assertIn(cy, models.storage.all().values())
self.assertIn("Amenity." + am.id, models.storage.all().keys())
self.assertIn(am, models.storage.all().values())
self.assertIn("Review." + rv.id, models.storage.all().keys())
self.assertIn(rv, models.storage.all().values())
def test_new_with_args(self):
with self.assertRaises(TypeError):
models.storage.new(BaseModel(), 1)
def test_new_with_None(self):
with self.assertRaises(AttributeError):
models.storage.new(None)
def test_save(self):
bm = BaseModel()
us = User()
st = State()
pl = Place()
cy = City()
am = Amenity()
rv = Review()
models.storage.new(bm)
models.storage.new(us)
models.storage.new(st)
models.storage.new(pl)
models.storage.new(cy)
models.storage.new(am)
models.storage.new(rv)
models.storage.save()
save_text = ""
with open("file.json", "r") as f:
save_text = f.read()
self.assertIn("BaseModel." + bm.id, save_text)
self.assertIn("User." + us.id, save_text)
self.assertIn("State." + st.id, save_text)
self.assertIn("Place." + pl.id, save_text)
self.assertIn("City." + cy.id, save_text)
self.assertIn("Amenity." + am.id, save_text)
self.assertIn("Review." + rv.id, save_text)
def test_save_with_arg(self):
with self.assertRaises(TypeError):
models.storage.save(None)
def test_reload(self):
bm = BaseModel()
us = User()
st = State()
pl = Place()
cy = City()
am = Amenity()
rv = Review()
models.storage.new(bm)
models.storage.new(us)
models.storage.new(st)
models.storage.new(pl)
models.storage.new(cy)
models.storage.new(am)
models.storage.new(rv)
models.storage.save()
models.storage.reload()
objs = FileStorage._FileStorage__objects
self.assertIn("BaseModel." + bm.id, objs)
self.assertIn("User." + us.id, objs)
self.assertIn("State." + st.id, objs)
self.assertIn("Place." + pl.id, objs)
self.assertIn("City." + cy.id, objs)
self.assertIn("Amenity." + am.id, objs)
self.assertIn("Review." + rv.id, objs)
def test_reload_with_arg(self):
with self.assertRaises(TypeError):
models.storage.reload(None)
if __name__ == "__main__":
unittest.main()