-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_console.py
executable file
·742 lines (650 loc) · 26.9 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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
#!/usr/bin/python3
"""Module for TestHBNBCommand class."""
from console import HBNBCommand
from models.engine.file_storage import FileStorage
import unittest
import datetime
from unittest.mock import patch
import sys
from io import StringIO
import re
import os
class TestHBNBCommand(unittest.TestCase):
"""Tests HBNBCommand console."""
attribute_values = {
str: "foobar108",
int: 1008,
float: 1.08
}
reset_values = {
str: "",
int: 0,
float: 0.0
}
test_random_attributes = {
"strfoo": "barfoo",
"intfoo": 248,
"floatfoo": 9.8
}
def setUp(self):
"""Sets up test cases."""
if os.path.isfile("file.json"):
os.remove("file.json")
self.resetStorage()
def resetStorage(self):
"""Resets FileStorage data."""
FileStorage._FileStorage__objects = {}
if os.path.isfile(FileStorage._FileStorage__file_path):
os.remove(FileStorage._FileStorage__file_path)
def test_help(self):
"""Tests the help command."""
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("help")
s = """
Documented commands (type help <topic>):
========================================
EOF all count create destroy help quit show update
"""
self.assertEqual(s, f.getvalue())
def test_help_EOF(self):
"""Tests the help command."""
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("help EOF")
s = 'Handles End Of File character.\n \n'
self.assertEqual(s, f.getvalue())
def test_help_quit(self):
"""Tests the help command."""
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("help quit")
s = 'Exits the program.\n \n'
self.assertEqual(s, f.getvalue())
def test_help_create(self):
"""Tests the help command."""
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("help create")
s = 'Creates an instance.\n \n'
self.assertEqual(s, f.getvalue())
def test_help_show(self):
"""Tests the help command."""
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("help show")
s = 'Prints the string representation of an instance.\n \n'
self.assertEqual(s, f.getvalue())
def test_help_destroy(self):
"""Tests the help command."""
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("help destroy")
s = 'Deletes an instance based on the class name and id.\n \n'
self.assertEqual(s, f.getvalue())
def test_help_all(self):
"""Tests the help command."""
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("help all")
s = 'Prints all string representation of all instances.\n \n'
self.assertEqual(s, f.getvalue())
def test_help_count(self):
"""Tests the help command."""
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("help count")
s = 'Counts the instances of a class.\n \n'
self.assertEqual(s, f.getvalue())
def test_help_update(self):
"""Tests the help command."""
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("help update")
s = 'Updates an instance by adding or updating attribute.\n \n'
self.assertEqual(s, f.getvalue())
def test_do_quit(self):
"""Tests quit commmand."""
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("quit")
msg = f.getvalue()
self.assertTrue(len(msg) == 0)
self.assertEqual("", msg)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("quit garbage")
msg = f.getvalue()
self.assertTrue(len(msg) == 0)
self.assertEqual("", msg)
def test_do_EOF(self):
"""Tests EOF commmand."""
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("EOF")
msg = f.getvalue()
self.assertTrue(len(msg) == 1)
self.assertEqual("\n", msg)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("EOF garbage")
msg = f.getvalue()
self.assertTrue(len(msg) == 1)
self.assertEqual("\n", msg)
def test_emptyline(self):
"""Tests emptyline functionality."""
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("\n")
s = ""
self.assertEqual(s, f.getvalue())
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd(" \n")
s = ""
self.assertEqual(s, f.getvalue())
def test_do_create(self):
"""Tests create for all classes."""
for classname in self.classes():
self.help_test_do_create(classname)
def help_test_do_create(self, classname):
"""Helper method to test the create commmand."""
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("create {}".format(classname))
uid = f.getvalue()[:-1]
self.assertTrue(len(uid) > 0)
key = "{}.{}".format(classname, uid)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("all {}".format(classname))
self.assertTrue(uid in f.getvalue())
def test_do_create_error(self):
"""Tests create command with errors."""
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("create")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** class name missing **")
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("create garbage")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** class doesn't exist **")
def test_do_show(self):
"""Tests show for all classes."""
for classname in self.classes():
self.help_test_do_show(classname)
self.help_test_show_advanced(classname)
def help_test_do_show(self, classname):
"""Helps test the show command."""
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("create {}".format(classname))
uid = f.getvalue()[:-1]
self.assertTrue(len(uid) > 0)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("show {} {}".format(classname, uid))
s = f.getvalue()[:-1]
self.assertTrue(uid in s)
def test_do_show_error(self):
"""Tests show command with errors."""
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("show")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** class name missing **")
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("show garbage")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** class doesn't exist **")
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("show BaseModel")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** instance id missing **")
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("show BaseModel 6524359")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** no instance found **")
def help_test_show_advanced(self, classname):
"""Helps test .show() command."""
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("create {}".format(classname))
uid = f.getvalue()[:-1]
self.assertTrue(len(uid) > 0)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd('{}.show("{}")'.format(classname, uid))
s = f.getvalue()
self.assertTrue(uid in s)
def test_do_show_error_advanced(self):
"""Tests show() command with errors."""
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd(".show()")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** class name missing **")
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("garbage.show()")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** class doesn't exist **")
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("BaseModel.show()")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** instance id missing **")
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd('BaseModel.show("6524359")')
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** no instance found **")
def test_do_destroy(self):
"""Tests destroy for all classes."""
for classname in self.classes():
self.help_test_do_destroy(classname)
self.help_test_destroy_advanced(classname)
def help_test_do_destroy(self, classname):
"""Helps test the destroy command."""
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("create {}".format(classname))
uid = f.getvalue()[:-1]
self.assertTrue(len(uid) > 0)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("destroy {} {}".format(classname, uid))
s = f.getvalue()[:-1]
self.assertTrue(len(s) == 0)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd(".all()")
self.assertFalse(uid in f.getvalue())
def test_do_destroy_error(self):
"""Tests destroy command with errors."""
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("destroy")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** class name missing **")
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("destroy garbage")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** class doesn't exist **")
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("destroy BaseModel")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** instance id missing **")
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("destroy BaseModel 6524359")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** no instance found **")
def help_test_destroy_advanced(self, classname):
"""Helps test the destroy command."""
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("create {}".format(classname))
uid = f.getvalue()[:-1]
self.assertTrue(len(uid) > 0)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd('{}.destroy("{}")'.format(classname, uid))
s = f.getvalue()[:-1]
self.assertTrue(len(s) == 0)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd(".all()")
self.assertFalse(uid in f.getvalue())
def test_do_destroy_error_advanced(self):
"""Tests destroy() command with errors."""
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd(".destroy()")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** class name missing **")
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("garbage.destroy()")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** class doesn't exist **")
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("BaseModel.destroy()")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** instance id missing **")
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd('BaseModel.destroy("6524359")')
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** no instance found **")
def test_do_all(self):
"""Tests all for all classes."""
for classname in self.classes():
self.help_test_do_all(classname)
self.help_test_all_advanced(classname)
def help_test_do_all(self, classname):
"""Helps test the all command."""
uid = self.create_class(classname)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("all")
s = f.getvalue()[:-1]
self.assertTrue(len(s) > 0)
self.assertIn(uid, s)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("all {}".format(classname))
s = f.getvalue()[:-1]
self.assertTrue(len(s) > 0)
self.assertIn(uid, s)
def test_do_all_error(self):
"""Tests all command with errors."""
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("all garbage")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** class doesn't exist **")
def help_test_all_advanced(self, classname):
"""Helps test the .all() command."""
uid = self.create_class(classname)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("{}.all()".format(classname))
s = f.getvalue()[:-1]
self.assertTrue(len(s) > 0)
self.assertIn(uid, s)
def test_do_all_error_advanced(self):
"""Tests all() command with errors."""
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("garbage.all()")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** class doesn't exist **")
def test_count_all(self):
"""Tests count for all classes."""
for classname in self.classes():
self.help_test_count_advanced(classname)
def help_test_count_advanced(self, classname):
"""Helps test .count() command."""
for i in range(20):
uid = self.create_class(classname)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("{}.count()".format(classname))
s = f.getvalue()[:-1]
self.assertTrue(len(s) > 0)
self.assertEqual(s, "20")
def test_do_count_error(self):
"""Tests .count() command with errors."""
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("garbage.count()")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** class doesn't exist **")
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd(".count()")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** class name missing **")
def test_update_1(self):
"""Tests update 1..."""
classname = "BaseModel"
attr = "foostr"
val = "fooval"
uid = self.create_class(classname)
cmd = '{}.update("{}", "{}", "{}")'
# cmd = 'update {} {} {} {}'
cmd = cmd.format(classname, uid, attr, val)
# print("CMD::", cmd)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd(cmd)
s = f.getvalue()
self.assertEqual(len(s), 0)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd('{}.show("{}")'.format(classname, uid))
s = f.getvalue()
self.assertIn(attr, s)
self.assertIn(val, s)
def test_update_2(self):
"""Tests update 1..."""
classname = "User"
attr = "foostr"
val = "fooval"
uid = self.create_class(classname)
cmd = '{}.update("{}", "{}", "{}")'
# cmd = 'update {} {} {} {}'
cmd = cmd.format(classname, uid, attr, val)
# print("CMD::", cmd)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd(cmd)
s = f.getvalue()
self.assertEqual(len(s), 0)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd('{}.show("{}")'.format(classname, uid))
s = f.getvalue()
self.assertIn(attr, s)
self.assertIn(val, s)
def test_update_3(self):
"""Tests update 1..."""
classname = "City"
attr = "foostr"
val = "fooval"
uid = self.create_class(classname)
cmd = '{}.update("{}", "{}", "{}")'
# cmd = 'update {} {} {} {}'
cmd = cmd.format(classname, uid, attr, val)
# print("CMD::", cmd)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd(cmd)
s = f.getvalue()
self.assertEqual(len(s), 0)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd('{}.show("{}")'.format(classname, uid))
s = f.getvalue()
self.assertIn(attr, s)
self.assertIn(val, s)
def test_update_4(self):
"""Tests update 1..."""
classname = "State"
attr = "foostr"
val = "fooval"
uid = self.create_class(classname)
cmd = '{}.update("{}", "{}", "{}")'
# cmd = 'update {} {} {} {}'
cmd = cmd.format(classname, uid, attr, val)
# print("CMD::", cmd)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd(cmd)
s = f.getvalue()
self.assertEqual(len(s), 0)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd('{}.show("{}")'.format(classname, uid))
s = f.getvalue()
self.assertIn(attr, s)
self.assertIn(val, s)
def test_update_5(self):
"""Tests update 1..."""
classname = "Amenity"
attr = "foostr"
val = "fooval"
uid = self.create_class(classname)
cmd = '{}.update("{}", "{}", "{}")'
# cmd = 'update {} {} {} {}'
cmd = cmd.format(classname, uid, attr, val)
# print("CMD::", cmd)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd(cmd)
s = f.getvalue()
self.assertEqual(len(s), 0)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd('{}.show("{}")'.format(classname, uid))
s = f.getvalue()
self.assertIn(attr, s)
self.assertIn(val, s)
def test_update_6(self):
"""Tests update 1..."""
classname = "Review"
attr = "foostr"
val = "fooval"
uid = self.create_class(classname)
cmd = '{}.update("{}", "{}", "{}")'
# cmd = 'update {} {} {} {}'
cmd = cmd.format(classname, uid, attr, val)
# print("CMD::", cmd)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd(cmd)
s = f.getvalue()
self.assertEqual(len(s), 0)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd('{}.show("{}")'.format(classname, uid))
s = f.getvalue()
self.assertIn(attr, s)
self.assertIn(val, s)
def test_update_7(self):
"""Tests update 1..."""
classname = "Place"
attr = "foostr"
val = "fooval"
uid = self.create_class(classname)
cmd = '{}.update("{}", "{}", "{}")'
# cmd = 'update {} {} {} {}'
cmd = cmd.format(classname, uid, attr, val)
# print("CMD::", cmd)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd(cmd)
s = f.getvalue()
self.assertEqual(len(s), 0)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd('{}.show("{}")'.format(classname, uid))
s = f.getvalue()
self.assertIn(attr, s)
self.assertIn(val, s)
def test_update_everything(self):
"""Tests update command with errthang, like a baws."""
for classname, cls in self.classes().items():
uid = self.create_class(classname)
for attr, value in self.test_random_attributes.items():
if type(value) is not str:
pass
quotes = (type(value) == str)
self.help_test_update(classname, uid, attr,
value, quotes, False)
self.help_test_update(classname, uid, attr,
value, quotes, True)
pass
if classname == "BaseModel":
continue
for attr, attr_type in self.attributes()[classname].items():
if attr_type not in (str, int, float):
continue
self.help_test_update(classname, uid, attr,
self.attribute_values[attr_type],
True, False)
self.help_test_update(classname, uid, attr,
self.attribute_values[attr_type],
False, True)
def help_test_update(self, classname, uid, attr, val, quotes, func):
"""Tests update commmand."""
# print("QUOTES", quotes)
FileStorage._FileStorage__objects = {}
if os.path.isfile("file.json"):
os.remove("file.json")
uid = self.create_class(classname)
value_str = ('"{}"' if quotes else '{}').format(val)
if func:
cmd = '{}.update("{}", "{}", {})'
else:
cmd = 'update {} {} {} {}'
cmd = cmd.format(classname, uid, attr, value_str)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd(cmd)
msg = f.getvalue()[:-1]
# print("MSG::", msg)
# print("CMD::", cmd)
self.assertEqual(len(msg), 0)
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd('{}.show("{}")'.format(classname, uid))
s = f.getvalue()
self.assertIn(str(val), s)
self.assertIn(attr, s)
def test_do_update_error(self):
"""Tests update command with errors."""
uid = self.create_class("BaseModel")
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("update")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** class name missing **")
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("update garbage")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** class doesn't exist **")
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("update BaseModel")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** instance id missing **")
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("update BaseModel 6534276893")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** no instance found **")
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd('update BaseModel {}'.format(uid))
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** attribute name missing **")
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd('update BaseModel {} name'.format(uid))
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** value missing **")
def test_do_update_error_advanced(self):
"""Tests update() command with errors."""
uid = self.create_class("BaseModel")
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd(".update()")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** class name missing **")
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("garbage.update()")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** class doesn't exist **")
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("BaseModel.update()")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** instance id missing **")
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("BaseModel.update(6534276893)")
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** no instance found **")
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd('BaseModel.update("{}")'.format(uid))
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** attribute name missing **")
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd('BaseModel.update("{}", "name")'.format(uid))
msg = f.getvalue()[:-1]
self.assertEqual(msg, "** value missing **")
def create_class(self, classname):
"""Creates a class for console tests."""
with patch('sys.stdout', new=StringIO()) as f:
HBNBCommand().onecmd("create {}".format(classname))
uid = f.getvalue()[:-1]
self.assertTrue(len(uid) > 0)
return uid
def help_load_dict(self, rep):
"""Helper method to test dictionary equality."""
rex = re.compile(r"^\[(.*)\] \((.*)\) (.*)$")
res = rex.match(rep)
self.assertIsNotNone(res)
s = res.group(3)
s = re.sub(r"(datetime\.datetime\([^)]*\))", "'\\1'", s)
d = json.loads(s.replace("'", '"'))
return d
def classes(self):
"""Returns a dictionary of valid classes and their references."""
from models.base_model import BaseModel
from models.user import User
from models.state import State
from models.city import City
from models.amenity import Amenity
from models.place import Place
from models.review import Review
classes = {"BaseModel": BaseModel,
"User": User,
"State": State,
"City": City,
"Amenity": Amenity,
"Place": Place,
"Review": Review}
return classes
def attributes(self):
"""Returns the valid attributes and their types for classname."""
attributes = {
"BaseModel":
{"id": str,
"created_at": datetime.datetime,
"updated_at": datetime.datetime},
"User":
{"email": str,
"password": str,
"first_name": str,
"last_name": str},
"State":
{"name": str},
"City":
{"state_id": str,
"name": str},
"Amenity":
{"name": str},
"Place":
{"city_id": str,
"user_id": str,
"name": str,
"description": str,
"number_rooms": int,
"number_bathrooms": int,
"max_guest": int,
"price_by_night": int,
"latitude": float,
"longitude": float,
"amenity_ids": list},
"Review":
{"place_id": str,
"user_id": str,
"text": str}
}
return attributes
if __name__ == "__main__":
unittest.main()