-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_console.py
executable file
·297 lines (260 loc) · 11.6 KB
/
test_console.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
#!/usr/bin/python3
"""
Defines unittests for console.py
"""
import os
import models
import console
import unittest
from io import StringIO
from unittest.mock import patch
from models.engine.db_storage import DBStorage
from models.engine.file_storage import FileStorage
HBNBCommand = console.HBNBCommand
class TestHBNBCommandWithFileStorage(unittest.TestCase):
"""Unittests for testing the HBNB console with FileStorage"""
@classmethod
def setUpClass(cls):
"""Testing setup.
Rename any existing file.json temporarily.
Create an instance of the command interpreter.
"""
try:
os.rename("file.json", "original")
except IOError:
pass
cls.HBNB = HBNBCommand()
@classmethod
def tearDownClass(cls):
"""Testing teardown.
Restore original file.json.
Delete the test HBNBCommand instance.
"""
try:
os.rename("original", "file.json")
except IOError:
pass
del cls.HBNB
if type(models.storage) == DBStorage:
models.storage._DBStorage__session.close()
def setUp(self):
"""Reset FileStorage objects dictionary."""
FileStorage._FileStorage__objects = {}
def tearDown(self):
"""Delete any created file.json."""
try:
os.remove("file.json")
except IOError:
pass
def test_docstrings(self):
"""Check all methods have docstrings."""
self.assertIsNotNone(HBNBCommand.__doc__)
self.assertIsNotNone(HBNBCommand.preloop.__doc__)
self.assertIsNotNone(HBNBCommand.postloop.__doc__)
self.assertIsNotNone(HBNBCommand.do_EOF.__doc__)
self.assertIsNotNone(HBNBCommand.emptyline.__doc__)
self.assertIsNotNone(HBNBCommand.do_quit.__doc__)
self.assertIsNotNone(HBNBCommand.default.__doc__)
self.assertIsNotNone(HBNBCommand.do_create.__doc__)
self.assertIsNotNone(HBNBCommand.do_show.__doc__)
self.assertIsNotNone(HBNBCommand.do_destroy.__doc__)
self.assertIsNotNone(HBNBCommand.do_all.__doc__)
self.assertIsNotNone(HBNBCommand.do_count.__doc__)
self.assertIsNotNone(HBNBCommand.do_update.__doc__)
self.assertIsNotNone(HBNBCommand.help_Amenity.__doc__)
self.assertIsNotNone(HBNBCommand.help_BaseModel.__doc__)
self.assertIsNotNone(HBNBCommand.help_City.__doc__)
self.assertIsNotNone(HBNBCommand.help_Place.__doc__)
self.assertIsNotNone(HBNBCommand.help_Review.__doc__)
self.assertIsNotNone(HBNBCommand.help_State.__doc__)
self.assertIsNotNone(HBNBCommand.complete_all.__doc__)
self.assertIsNotNone(HBNBCommand.complete_count.__doc__)
self.assertIsNotNone(HBNBCommand.complete_create.__doc__)
self.assertIsNotNone(HBNBCommand.complete_destroy.__doc__)
self.assertIsNotNone(HBNBCommand.complete_show.__doc__)
self.assertIsNotNone(HBNBCommand.complete_update.__doc__)
@patch("sys.stdout", new_callable=StringIO)
def test_emptyline(self, mock_stdout):
"""Test empty line input."""
self.HBNB.onecmd("\n")
self.assertEqual("", mock_stdout.getvalue())
@patch("sys.stdout", new_callable=StringIO)
def test_quit(self, mock_stdout):
"""Test the quit command."""
self.HBNB.onecmd("quit")
self.assertEqual("", mock_stdout.getvalue())
@patch("sys.stdout", new_callable=StringIO)
def test_quit(self, mock_stdout):
"""Test the quit command."""
self.HBNB.onecmd("quit")
self.assertEqual("", mock_stdout.getvalue())
@unittest.skipIf(type(models.storage) == DBStorage, "Testing DBStorage")
@patch("sys.stdout", new_callable=StringIO)
def test_create(self, mock_stdout):
"""Test the create command on all models."""
models = [
"BaseModel", "Amenity", "City", "State", "Place", "User", "Review"]
models_uuids = []
for model in models:
self.HBNB.onecmd(f"create {model}")
models_uuids.append(mock_stdout.getvalue())
for uuid in models_uuids:
self.HBNB.onecmd(f"all {model}")
self.assertIn(uuid, mock_stdout.getvalue())
@unittest.skipIf(type(models.storage) == DBStorage, "Testing DBStorage")
def test_create_kwargs(self):
"""Test the create command with kwargs."""
with patch("sys.stdout", new=StringIO()) as f:
kwargs = (
'create Place city_id="1234" name="Johnsons" '
"number_rooms=3 latitude=24.89 longitude=f"
)
self.HBNB.onecmd(kwargs)
placeId = f.getvalue().strip()
with patch("sys.stdout", new=StringIO()) as f:
self.HBNB.onecmd("all Place")
output = f.getvalue()
self.assertIn(placeId, output)
self.assertIn("'city_id': '1234'", output)
self.assertIn("'name': 'Johnsons'", output)
self.assertIn("'number_rooms': 3", output)
self.assertIn("'latitude': 24.89", output)
self.assertNotIn("'longitude'", output)
def test_create_error_messages(self):
"""Test create command error messages."""
with patch("sys.stdout", new=StringIO()) as f:
self.HBNB.onecmd("create")
self.assertEqual("** class name missing **\n", f.getvalue())
with patch("sys.stdout", new=StringIO()) as f:
self.HBNB.onecmd("create asdfsfsd")
self.assertEqual("** class doesn't exist **\n", f.getvalue())
def test_count(self):
"""Test the count command."""
with patch("sys.stdout", new=StringIO()) as f:
self.HBNB.onecmd("count abcd")
self.assertEqual("** class doesn't exist **\n", f.getvalue())
with patch("sys.stdout", new=StringIO()) as f:
self.HBNB.onecmd("count Place")
self.assertEqual("0\n", f.getvalue())
def test_show(self):
"""Test the show command."""
call = [
["show", "** class name missing **\n"],
["show abc", "** class doesn't exist **\n"],
["show BaseModel", "** instance id missing **\n"],
["show BaseModel abc-123", "** no instance found **\n"],
]
for input, expected_output in call:
with patch("sys.stdout", new=StringIO()) as f:
self.HBNB.onecmd(input)
self.assertEqual(expected_output, f.getvalue())
def test_destroy(self):
"""Test the destroy command."""
call = [
["destroy", "** class name missing **\n"],
["destroy abc", "** class doesn't exist **\n"],
["destroy BaseModel", "** instance id missing **\n"],
["destroy BaseModel abc-123", "** no instance found **\n"],
]
for input, expected_output in call:
with patch("sys.stdout", new=StringIO()) as f:
self.HBNB.onecmd(input)
self.assertEqual(expected_output, f.getvalue())
@unittest.skipIf(type(models.storage) == DBStorage, "Testing DBStorage")
def test_all(self):
"""Test the all command."""
with patch("sys.stdout", new=StringIO()) as f:
self.HBNB.onecmd("all abc")
self.assertEqual("** class doesn't exist **\n", f.getvalue())
with patch("sys.stdout", new=StringIO()) as f:
self.HBNB.onecmd("all Place")
self.assertEqual("[]\n", f.getvalue())
@unittest.skipIf(type(models.storage) == DBStorage, "Testing DBStorage")
def test_update(self):
"""Test the update command."""
with patch("sys.stdout", new=StringIO()) as f:
self.HBNB.onecmd("create User")
usr_id = f.getvalue()
call = [
["update", "** class name missing **\n"],
["update abc", "** class doesn't exist **\n"],
["update User", "** instance id missing **\n"],
["update User abc-123", "** no instance found **\n"],
[f"update User {usr_id}", "** attribute name missing **\n"],
[f"update User {usr_id} Name", "** value missing **\n"],
]
for input, expected_output in call:
with patch("sys.stdout", new=StringIO()) as f:
self.HBNB.onecmd(input)
self.assertEqual(expected_output, f.getvalue())
@unittest.skipIf(type(models.storage) == DBStorage, "Testing DBStorage")
@patch("sys.stdout", new_callable=StringIO)
def test_create(self, mock_stdout):
"""Test the alternate create command on all models."""
models = [
"BaseModel", "Amenity", "City", "State", "Place", "User", "Review"]
models_uuids = []
for model in models:
self.HBNB.onecmd(f"{model}.create()")
models_uuids.append(mock_stdout.getvalue())
for uuid in models_uuids:
self.HBNB.onecmd(f"all {model}")
self.assertIn(uuid, mock_stdout.getvalue())
@unittest.skipIf(type(models.storage) == DBStorage, "Testing DBStorage")
def test_alt_count(self):
"""Test the alternate count command."""
with patch("sys.stdout", new=StringIO()) as f:
self.HBNB.onecmd("asdfsdfsd.count()")
self.assertEqual("** class doesn't exist **\n", f.getvalue())
with patch("sys.stdout", new=StringIO()) as f:
self.HBNB.onecmd("State.count()")
self.assertEqual("0\n", f.getvalue())
@unittest.skipIf(type(models.storage) == DBStorage, "Testing DBStorage")
def test_alt_all(self):
"""Test alternate all command."""
with patch("sys.stdout", new=StringIO()) as f:
self.HBNB.onecmd("abc.all()")
self.assertEqual("** class doesn't exist **\n", f.getvalue())
with patch("sys.stdout", new=StringIO()) as f:
self.HBNB.onecmd("Place.all()")
self.assertEqual("[]\n", f.getvalue())
def test_alt_show(self):
"""Test the alternate show command."""
call = [
["abc.show()", "** class doesn't exist **\n"],
["BaseModel.show()", "** instance id missing **\n"],
["BaseModel.show(abc-123)", "** no instance found **\n"],
]
for input, expected_output in call:
with patch("sys.stdout", new=StringIO()) as f:
self.HBNB.onecmd(input)
self.assertEqual(expected_output, f.getvalue())
def test_alt_destroy(self):
"""Test the alternate destroy command."""
call = [
["abc.destroy()", "** class doesn't exist **\n"],
["BaseModel.destroy()", "** instance id missing **\n"],
["BaseModel.destroy(abc-123)", "** no instance found **\n"],
]
for input, expected_output in call:
with patch("sys.stdout", new=StringIO()) as f:
self.HBNB.onecmd(input)
self.assertEqual(expected_output, f.getvalue())
def test_alt_update(self):
"""Test the alternate update command."""
with patch("sys.stdout", new=StringIO()) as f:
self.HBNB.onecmd("create User")
usr_id = f.getvalue()
call = [
["abc.update()", "** class doesn't exist **\n"],
["User.update()", "** instance id missing **\n"],
["User.update(abc-123)", "** no instance found **\n"],
[f"User.update({usr_id})", "** attribute name missing **\n"],
[f"User.update({usr_id}, name)", "** value missing **\n"],
]
for input, expected_output in call:
with patch("sys.stdout", new=StringIO()) as f:
self.HBNB.onecmd(input)
self.assertEqual(expected_output, f.getvalue())
if __name__ == "__main__":
unittest.main()