-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_square.py
660 lines (509 loc) · 21.3 KB
/
test_square.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
#!/usr/bin/python3
# test_square.py
# Brennan D Baraban <[email protected]>
"""Defines unittests for models/square.py.
Unittest classes:
TestSquare_instantiation - line 24
TestSquare_size - line 88
TestSquare_x - line 166
TestSquare_y - line 238
TestSquare_order_of_initialization - line 306
TestSquare_area - line 322
TestSquare_stdout - line 343
TestSquare_update_args - line 426
TestSquare_update_kwargs - line 538
TestSquare_to_dictionary - 640
"""
import io
import sys
import unittest
from models.base import Base
from models.square import Square
class TestSquare_instantiation(unittest.TestCase):
"""Unittests for testing instantiation of the Square class."""
def test_is_base(self):
self.assertIsInstance(Square(10), Base)
def test_is_rectangle(self):
self.assertIsInstance(Square(10), Square)
def test_no_args(self):
with self.assertRaises(TypeError):
Square()
def test_one_arg(self):
s1 = Square(10)
s2 = Square(11)
self.assertEqual(s1.id, s2.id - 1)
def test_two_args(self):
s1 = Square(10, 2)
s2 = Square(2, 10)
self.assertEqual(s1.id, s2.id - 1)
def test_three_args(self):
s1 = Square(10, 2, 2)
s2 = Square(2, 2, 10)
self.assertEqual(s1.id, s2.id - 1)
def test_four_args(self):
self.assertEqual(7, Square(10, 2, 2, 7).id)
def test_more_than_four_args(self):
with self.assertRaises(TypeError):
Square(1, 2, 3, 4, 5)
def test_size_private(self):
with self.assertRaises(AttributeError):
print(Square(10, 2, 3, 4).__size)
def test_size_getter(self):
self.assertEqual(5, Square(5, 2, 3, 9).size)
def test_size_setter(self):
s = Square(4, 1, 9, 2)
s.size = 8
self.assertEqual(8, s.size)
def test_width_getter(self):
s = Square(4, 1, 9, 2)
s.size = 8
self.assertEqual(8, s.width)
def test_height_getter(self):
s = Square(4, 1, 9, 2)
s.size = 8
self.assertEqual(8, s.height)
def test_x_getter(self):
self.assertEqual(0, Square(10).x)
def test_y_getter(self):
self.assertEqual(0, Square(10).y)
class TestSquare_size(unittest.TestCase):
"""Unittests for testing size initialization of the Square class."""
def test_None_size(self):
with self.assertRaisesRegex(TypeError, "width must be an integer"):
Square(None)
def test_str_size(self):
with self.assertRaisesRegex(TypeError, "width must be an integer"):
Square("invalid")
def test_float_size(self):
with self.assertRaisesRegex(TypeError, "width must be an integer"):
Square(5.5)
def test_complex_size(self):
with self.assertRaisesRegex(TypeError, "width must be an integer"):
Square(complex(5))
def test_dict_size(self):
with self.assertRaisesRegex(TypeError, "width must be an integer"):
Square({"a": 1, "b": 2}, 2)
def test_bool_size(self):
with self.assertRaisesRegex(TypeError, "width must be an integer"):
Square(True, 2, 3)
def test_list_size(self):
with self.assertRaisesRegex(TypeError, "width must be an integer"):
Square([1, 2, 3])
def test_set_size(self):
with self.assertRaisesRegex(TypeError, "width must be an integer"):
Square({1, 2, 3}, 2)
def test_tuple_size(self):
with self.assertRaisesRegex(TypeError, "width must be an integer"):
Square((1, 2, 3), 2, 3)
def test_frozenset_size(self):
with self.assertRaisesRegex(TypeError, "width must be an integer"):
Square(frozenset({1, 2, 3, 1}))
def test_range_size(self):
with self.assertRaisesRegex(TypeError, "width must be an integer"):
Square(range(5))
def test_bytes_size(self):
with self.assertRaisesRegex(TypeError, "width must be an integer"):
Square(b'Python')
def test_bytearray_size(self):
with self.assertRaisesRegex(TypeError, "width must be an integer"):
Square(bytearray(b'abcdefg'))
def test_memoryview_size(self):
with self.assertRaisesRegex(TypeError, "width must be an integer"):
Square(memoryview(b'abcdefg'))
def test_inf_size(self):
with self.assertRaisesRegex(TypeError, "width must be an integer"):
Square(float('inf'))
def test_nan_size(self):
with self.assertRaisesRegex(TypeError, "width must be an integer"):
Square(float('nan'))
# Test size values
def test_negative_size(self):
with self.assertRaisesRegex(ValueError, "width must be > 0"):
Square(-1, 2)
def test_zero_size(self):
with self.assertRaisesRegex(ValueError, "width must be > 0"):
Square(0, 2)
class TestSquare_x(unittest.TestCase):
"""Unittests for testing initialization of Square x attribute."""
def test_None_x(self):
with self.assertRaisesRegex(TypeError, "x must be an integer"):
Square(1, None)
def test_str_x(self):
with self.assertRaisesRegex(TypeError, "x must be an integer"):
Square(1, "invalid")
def test_float_x(self):
with self.assertRaisesRegex(TypeError, "x must be an integer"):
Square(1, 5.5)
def test_complex_x(self):
with self.assertRaisesRegex(TypeError, "x must be an integer"):
Square(1, complex(5))
def test_dict_x(self):
with self.assertRaisesRegex(TypeError, "x must be an integer"):
Square(1, {"a": 1, "b": 2}, 2)
def test_bool_x(self):
with self.assertRaisesRegex(TypeError, "x must be an integer"):
Square(1, True)
def test_list_x(self):
with self.assertRaisesRegex(TypeError, "x must be an integer"):
Square(1, [1, 2, 3])
def test_set_x(self):
with self.assertRaisesRegex(TypeError, "x must be an integer"):
Square(1, {1, 2, 3})
def test_tuple_x(self):
with self.assertRaisesRegex(TypeError, "x must be an integer"):
Square(1, (1, 2, 3))
def test_frozenset_x(self):
with self.assertRaisesRegex(TypeError, "x must be an integer"):
Square(1, frozenset({1, 2, 3, 1}))
def test_range_x(self):
with self.assertRaisesRegex(TypeError, "x must be an integer"):
Square(1, range(5))
def test_bytes_x(self):
with self.assertRaisesRegex(TypeError, "x must be an integer"):
Square(1, b'Python')
def test_bytearray_x(self):
with self.assertRaisesRegex(TypeError, "x must be an integer"):
Square(1, bytearray(b'abcdefg'))
def test_memoryview_x(self):
with self.assertRaisesRegex(TypeError, "x must be an integer"):
Square(1, memoryview(b'abcedfg'))
def test_inf_x(self):
with self.assertRaisesRegex(TypeError, "x must be an integer"):
Square(1, float('inf'), 2)
def test_nan_x(self):
with self.assertRaisesRegex(TypeError, "x must be an integer"):
Square(1, float('nan'), 2)
def test_negative_x(self):
with self.assertRaisesRegex(ValueError, "x must be >= 0"):
Square(5, -1, 0)
class TestSquare_y(unittest.TestCase):
"""Unittests for testing initialization of Square y attribute."""
def test_None_y(self):
with self.assertRaisesRegex(TypeError, "y must be an integer"):
Square(1, 3, None)
def test_str_y(self):
with self.assertRaisesRegex(TypeError, "y must be an integer"):
Square(1, 1, "invalid")
def test_float_y(self):
with self.assertRaisesRegex(TypeError, "y must be an integer"):
Square(1, 3, 5.5)
def test_complex_y(self):
with self.assertRaisesRegex(TypeError, "y must be an integer"):
Square(1, 3, complex(5))
def test_dict_y(self):
with self.assertRaisesRegex(TypeError, "y must be an integer"):
Square(1, 1, {"a": 1, "b": 2})
def test_list_y(self):
with self.assertRaisesRegex(TypeError, "y must be an integer"):
Square(1, 1, [1, 2, 3])
def test_set_y(self):
with self.assertRaisesRegex(TypeError, "y must be an integer"):
Square(1, 1, {1, 2, 3})
def test_tuple_y(self):
with self.assertRaisesRegex(TypeError, "y must be an integer"):
Square(1, 1, (1, 2, 3))
def test_frozenset_y(self):
with self.assertRaisesRegex(TypeError, "y must be an integer"):
Square(1, 3, frozenset({1, 2, 3, 1}))
def test_range_y(self):
with self.assertRaisesRegex(TypeError, "y must be an integer"):
Square(1, 3, range(5))
def test_bytes_y(self):
with self.assertRaisesRegex(TypeError, "y must be an integer"):
Square(1, 3, b'Python')
def test_bytearray_y(self):
with self.assertRaisesRegex(TypeError, "y must be an integer"):
Square(1, 3, bytearray(b'abcdefg'))
def test_memoryview_y(self):
with self.assertRaisesRegex(TypeError, "y must be an integer"):
Square(1, 3, memoryview(b'abcedfg'))
def test_inf_y(self):
with self.assertRaisesRegex(TypeError, "y must be an integer"):
Square(1, 1, float('inf'))
def test_nan_y(self):
with self.assertRaisesRegex(TypeError, "y must be an integer"):
Square(1, 1, float('nan'))
def test_negative_y(self):
with self.assertRaisesRegex(ValueError, "y must be >= 0"):
Square(3, 0, -1)
class TestSquare_order_of_initialization(unittest.TestCase):
"""Unittests for testing order of Square attribute initialization."""
def test_size_before_x(self):
with self.assertRaisesRegex(TypeError, "width must be an integer"):
Square("invalid size", "invalid x")
def test_size_before_y(self):
with self.assertRaisesRegex(TypeError, "width must be an integer"):
Square("invalid size", 1, "invalid y")
def test_x_before_y(self):
with self.assertRaisesRegex(TypeError, "x must be an integer"):
Square(1, "invalid x", "invalid y")
class TestSquare_area(unittest.TestCase):
"""Unittests for testing the area method of the Square class."""
def test_area_small(self):
self.assertEqual(100, Square(10, 0, 0, 1).area())
def test_area_large(self):
s = Square(999999999999999999, 0, 0, 1)
self.assertEqual(999999999999999998000000000000000001, s.area())
def test_area_changed_attributes(self):
s = Square(2, 0, 0, 1)
s.size = 7
self.assertEqual(49, s.area())
def test_area_one_arg(self):
s = Square(2, 10, 1, 1)
with self.assertRaises(TypeError):
s.area(1)
class TestSquare_stdout(unittest.TestCase):
"""Unittests for testing __str__ and display methods of Square class."""
@staticmethod
def capture_stdout(sq, method):
"""Captures and returns text printed to stdout.
Args:
sq (Square): The Square ot print to stdout.
method (str): The method to run on sq.
Returns:
The text printed to stdout by calling method on sq.
"""
capture = io.StringIO()
sys.stdout = capture
if method == "print":
print(sq)
else:
sq.display()
sys.stdout = sys.__stdout__
return capture
def test_str_method_print_size(self):
s = Square(4)
capture = TestSquare_stdout.capture_stdout(s, "print")
correct = "[Square] ({}) 0/0 - 4\n".format(s.id)
self.assertEqual(correct, capture.getvalue())
def test_str_method_size_x(self):
s = Square(5, 5)
correct = "[Square] ({}) 5/0 - 5".format(s.id)
self.assertEqual(correct, s.__str__())
def test_str_method_size_x_y(self):
s = Square(7, 4, 22)
correct = "[Square] ({}) 4/22 - 7".format(s.id)
self.assertEqual(correct, str(s))
def test_str_method_size_x_y_id(self):
s = Square(2, 88, 4, 19)
self.assertEqual("[Square] (19) 88/4 - 2", str(s))
def test_str_method_changed_attributes(self):
s = Square(7, 0, 0, [4])
s.size = 15
s.x = 8
s.y = 10
self.assertEqual("[Square] ([4]) 8/10 - 15", str(s))
def test_str_method_one_arg(self):
s = Square(1, 2, 3, 4)
with self.assertRaises(TypeError):
s.__str__(1)
# Test display method
def test_display_size(self):
s = Square(2, 0, 0, 9)
capture = TestSquare_stdout.capture_stdout(s, "display")
self.assertEqual("##\n##\n", capture.getvalue())
def test_display_size_x(self):
s = Square(3, 1, 0, 18)
capture = TestSquare_stdout.capture_stdout(s, "display")
self.assertEqual(" ###\n ###\n ###\n", capture.getvalue())
def test_display_size_y(self):
s = Square(4, 0, 1, 9)
capture = TestSquare_stdout.capture_stdout(s, "display")
display = "\n####\n####\n####\n####\n"
self.assertEqual(display, capture.getvalue())
def test_display_size_x_y(self):
s = Square(2, 3, 2, 1)
capture = TestSquare_stdout.capture_stdout(s, "display")
display = "\n\n ##\n ##\n"
self.assertEqual(display, capture.getvalue())
def test_display_one_arg(self):
s = Square(3, 4, 5, 2)
with self.assertRaises(TypeError):
s.display(1)
class TestSquare_update_args(unittest.TestCase):
"""Unittests for testing update args method of the Square class."""
def test_update_args_zero(self):
s = Square(10, 10, 10, 10)
s.update()
self.assertEqual("[Square] (10) 10/10 - 10", str(s))
def test_update_args_one(self):
s = Square(10, 10, 10, 10)
s.update(89)
self.assertEqual("[Square] (89) 10/10 - 10", str(s))
def test_update_args_two(self):
s = Square(10, 10, 10, 10)
s.update(89, 2)
self.assertEqual("[Square] (89) 10/10 - 2", str(s))
def test_update_args_three(self):
s = Square(10, 10, 10, 10)
s.update(89, 2, 3)
self.assertEqual("[Square] (89) 3/10 - 2", str(s))
def test_update_args_four(self):
s = Square(10, 10, 10, 10)
s.update(89, 2, 3, 4)
self.assertEqual("[Square] (89) 3/4 - 2", str(s))
def test_update_args_more_than_four(self):
s = Square(10, 10, 10, 10)
s.update(89, 2, 3, 4, 5)
self.assertEqual("[Square] (89) 3/4 - 2", str(s))
def test_update_args_width_setter(self):
s = Square(10, 10, 10, 10)
s.update(89, 2)
self.assertEqual(2, s.width)
def test_update_args_height_setter(self):
s = Square(10, 10, 10, 10)
s.update(89, 2)
self.assertEqual(2, s.height)
def test_update_args_None_id(self):
s = Square(10, 10, 10, 10)
s.update(None)
correct = "[Square] ({}) 10/10 - 10".format(s.id)
self.assertEqual(correct, str(s))
def test_update_args_None_id_and_more(self):
s = Square(10, 10, 10, 10)
s.update(None, 4, 5)
correct = "[Square] ({}) 5/10 - 4".format(s.id)
self.assertEqual(correct, str(s))
def test_update_args_twice(self):
s = Square(10, 10, 10, 10)
s.update(89, 2, 3, 4)
s.update(4, 3, 2, 89)
self.assertEqual("[Square] (4) 2/89 - 3", str(s))
def test_update_args_invalid_size_type(self):
s = Square(10, 10, 10, 10)
with self.assertRaisesRegex(TypeError, "width must be an integer"):
s.update(89, "invalid")
def test_update_args_size_zero(self):
s = Square(10, 10, 10, 10)
with self.assertRaisesRegex(ValueError, "width must be > 0"):
s.update(89, 0)
def test_update_args_size_negative(self):
s = Square(10, 10, 10, 10)
with self.assertRaisesRegex(ValueError, "width must be > 0"):
s.update(89, -4)
def test_update_args_invalid_x(self):
s = Square(10, 10, 10, 10)
with self.assertRaisesRegex(TypeError, "x must be an integer"):
s.update(89, 1, "invalid")
def test_update_args_x_negative(self):
s = Square(10, 10, 10, 10)
with self.assertRaisesRegex(ValueError, "x must be >= 0"):
s.update(98, 1, -4)
def test_update_args_invalid_y(self):
s = Square(10, 10, 10, 10)
with self.assertRaisesRegex(TypeError, "y must be an integer"):
s.update(89, 1, 2, "invalid")
def test_update_args_y_negative(self):
s = Square(10, 10, 10, 10)
with self.assertRaisesRegex(ValueError, "y must be >= 0"):
s.update(98, 1, 2, -4)
def test_update_args_size_before_x(self):
s = Square(10, 10, 10, 10)
with self.assertRaisesRegex(TypeError, "width must be an integer"):
s.update(89, "invalid", "invalid")
def test_update_args_size_before_y(self):
s = Square(10, 10, 10, 10)
with self.assertRaisesRegex(TypeError, "width must be an integer"):
s.update(89, "invalid", 2, "invalid")
def test_update_args_x_before_y(self):
s = Square(10, 10, 10, 10)
with self.assertRaisesRegex(TypeError, "x must be an integer"):
s.update(89, 1, "invalid", "invalid")
class TestSquare_update_kwargs(unittest.TestCase):
"""Unittests for testing update kwargs method of Square class."""
def test_update_kwargs_one(self):
s = Square(10, 10, 10, 10)
s.update(id=1)
self.assertEqual("[Square] (1) 10/10 - 10", str(s))
def test_update_kwargs_two(self):
s = Square(10, 10, 10, 10)
s.update(size=1, id=2)
self.assertEqual("[Square] (2) 10/10 - 1", str(s))
def test_update_kwargs_three(self):
s = Square(10, 10, 10, 10)
s.update(y=1, size=3, id=89)
self.assertEqual("[Square] (89) 10/1 - 3", str(s))
def test_update_kwargs_four(self):
s = Square(10, 10, 10, 10)
s.update(id=89, x=1, y=3, size=4)
self.assertEqual("[Square] (89) 1/3 - 4", str(s))
def test_update_kwargs_width_setter(self):
s = Square(10, 10, 10, 10)
s.update(id=89, size=8)
self.assertEqual(8, s.width)
def test_update_kwargs_height_setter(self):
s = Square(10, 10, 10, 10)
s.update(id=89, size=9)
self.assertEqual(9, s.height)
def test_update_kwargs_None_id(self):
s = Square(10, 10, 10, 10)
s.update(id=None)
correct = "[Square] ({}) 10/10 - 10".format(s.id)
self.assertEqual(correct, str(s))
def test_update_kwargs_None_id_and_more(self):
s = Square(10, 10, 10, 10)
s.update(id=None, size=7, x=18)
correct = "[Square] ({}) 18/10 - 7".format(s.id)
self.assertEqual(correct, str(s))
def test_update_kwargs_twice(self):
s = Square(10, 10, 10, 10)
s.update(id=89, x=1)
s.update(y=3, x=15, size=2)
self.assertEqual("[Square] (89) 15/3 - 2", str(s))
def test_update_kwargs_invalid_size(self):
s = Square(10, 10, 10, 10)
with self.assertRaisesRegex(TypeError, "width must be an integer"):
s.update(size="invalid")
def test_update_kwargs_size_zero(self):
s = Square(10, 10, 10, 10)
with self.assertRaisesRegex(ValueError, "width must be > 0"):
s.update(size=0)
def test_update_kwargs_size_negative(self):
s = Square(10, 10, 10, 10)
with self.assertRaisesRegex(ValueError, "width must be > 0"):
s.update(size=-3)
def test_update_kwargs_invalid_x(self):
s = Square(10, 10, 10, 10)
with self.assertRaisesRegex(TypeError, "x must be an integer"):
s.update(x="invalid")
def test_update_kwargs_x_negative(self):
s = Square(10, 10, 10, 10)
with self.assertRaisesRegex(ValueError, "x must be >= 0"):
s.update(x=-5)
def test_update_kwargs_invalid_y(self):
s = Square(10, 10, 10, 10)
with self.assertRaisesRegex(TypeError, "y must be an integer"):
s.update(y="invalid")
def test_update_kwargs_y_negative(self):
s = Square(10, 10, 10, 10)
with self.assertRaisesRegex(ValueError, "y must be >= 0"):
s.update(y=-5)
def test_update_args_and_kwargs(self):
s = Square(10, 10, 10, 10)
s.update(89, 2, y=6)
self.assertEqual("[Square] (89) 10/10 - 2", str(s))
def test_update_kwargs_wrong_keys(self):
s = Square(10, 10, 10, 10)
s.update(a=5, b=10)
self.assertEqual("[Square] (10) 10/10 - 10", str(s))
def test_update_kwargs_some_wrong_keys(self):
s = Square(10, 10, 10, 10)
s.update(size=5, id=89, a=1, b=54)
self.assertEqual("[Square] (89) 10/10 - 5", str(s))
class TestSquare_to_dictionary(unittest.TestCase):
"""Unittests for testing to_dictionary method of the Square class."""
def test_to_dictionary_output(self):
s = Square(10, 2, 1, 1)
correct = {'id': 1, 'x': 2, 'size': 10, 'y': 1}
self.assertDictEqual(correct, s.to_dictionary())
def test_to_dictionary_no_object_changes(self):
s1 = Square(10, 2, 1, 2)
s2 = Square(1, 2, 10)
s2.update(**s1.to_dictionary())
self.assertNotEqual(s1, s2)
def test_to_dictionary_arg(self):
s = Square(10, 10, 10, 10)
with self.assertRaises(TypeError):
s.to_dictionary(1)
if __name__ == "__main__":
unittest.main()