-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_base.py
549 lines (440 loc) · 18.1 KB
/
test_base.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
#!/usr/bin/python3
"""Defines unittests for base.py.
Unittest classes:
TestBase_instantiation - line 21
TestBase_to_json_string - line 108
TestBase_save_to_file - line 154
TestBase_from_json_string - line 232
TestBase_create - line 286
TestBase_load_from_file - line 338
TestBase_save_to_file_csv - line 404
TestBase_load_from_file_csv - line 482
"""
import os
import unittest
from models.base import Base
from models.rectangle import Rectangle
from models.square import Square
class TestBase_instantiation(unittest.TestCase):
"""Unittests for testing instantiation of the Base class."""
def test_no_arg(self):
b1 = Base()
b2 = Base()
self.assertEqual(b1.id, b2.id - 1)
def test_three_bases(self):
b1 = Base()
b2 = Base()
b3 = Base()
self.assertEqual(b1.id, b3.id - 2)
def test_None_id(self):
b1 = Base(None)
b2 = Base(None)
self.assertEqual(b1.id, b2.id - 1)
def test_unique_id(self):
self.assertEqual(12, Base(12).id)
def test_nb_instances_after_unique_id(self):
b1 = Base()
b2 = Base(12)
b3 = Base()
self.assertEqual(b1.id, b3.id - 1)
def test_id_public(self):
b = Base(12)
b.id = 15
self.assertEqual(15, b.id)
def test_nb_instances_private(self):
with self.assertRaises(AttributeError):
print(Base(12).__nb_instances)
def test_str_id(self):
self.assertEqual("hello", Base("hello").id)
def test_float_id(self):
self.assertEqual(5.5, Base(5.5).id)
def test_complex_id(self):
self.assertEqual(complex(5), Base(complex(5)).id)
def test_dict_id(self):
self.assertEqual({"a": 1, "b": 2}, Base({"a": 1, "b": 2}).id)
def test_bool_id(self):
self.assertEqual(True, Base(True).id)
def test_list_id(self):
self.assertEqual([1, 2, 3], Base([1, 2, 3]).id)
def test_tuple_id(self):
self.assertEqual((1, 2), Base((1, 2)).id)
def test_set_id(self):
self.assertEqual({1, 2, 3}, Base({1, 2, 3}).id)
def test_frozenset_id(self):
self.assertEqual(frozenset({1, 2, 3}), Base(frozenset({1, 2, 3})).id)
def test_range_id(self):
self.assertEqual(range(5), Base(range(5)).id)
def test_bytes_id(self):
self.assertEqual(b'Python', Base(b'Python').id)
def test_bytearray_id(self):
self.assertEqual(bytearray(b'abcefg'), Base(bytearray(b'abcefg')).id)
def test_memoryview_id(self):
self.assertEqual(memoryview(b'abcefg'), Base(memoryview(b'abcefg')).id)
def test_inf_id(self):
self.assertEqual(float('inf'), Base(float('inf')).id)
def test_NaN_id(self):
self.assertNotEqual(float('nan'), Base(float('nan')).id)
def test_two_args(self):
with self.assertRaises(TypeError):
Base(1, 2)
class TestBase_to_json_string(unittest.TestCase):
"""Unittests for testing to_json_string method of Base class."""
def test_to_json_string_rectangle_type(self):
r = Rectangle(10, 7, 2, 8, 6)
self.assertEqual(str, type(Base.to_json_string([r.to_dictionary()])))
def test_to_json_string_rectangle_one_dict(self):
r = Rectangle(10, 7, 2, 8, 6)
self.assertTrue(len(Base.to_json_string([r.to_dictionary()])) == 53)
def test_to_json_string_rectangle_two_dicts(self):
r1 = Rectangle(2, 3, 5, 19, 2)
r2 = Rectangle(4, 2, 4, 1, 12)
list_dicts = [r1.to_dictionary(), r2.to_dictionary()]
self.assertTrue(len(Base.to_json_string(list_dicts)) == 106)
def test_to_json_string_square_type(self):
s = Square(10, 2, 3, 4)
self.assertEqual(str, type(Base.to_json_string([s.to_dictionary()])))
def test_to_json_string_square_one_dict(self):
s = Square(10, 2, 3, 4)
self.assertTrue(len(Base.to_json_string([s.to_dictionary()])) == 39)
def test_to_json_string_square_two_dicts(self):
s1 = Square(10, 2, 3, 4)
s2 = Square(4, 5, 21, 2)
list_dicts = [s1.to_dictionary(), s2.to_dictionary()]
self.assertTrue(len(Base.to_json_string(list_dicts)) == 78)
def test_to_json_string_empty_list(self):
self.assertEqual("[]", Base.to_json_string([]))
def test_to_json_string_none(self):
self.assertEqual("[]", Base.to_json_string(None))
def test_to_json_string_no_args(self):
with self.assertRaises(TypeError):
Base.to_json_string()
def test_to_json_string_more_than_one_arg(self):
with self.assertRaises(TypeError):
Base.to_json_string([], 1)
class TestBase_save_to_file(unittest.TestCase):
"""Unittests for testing save_to_file method of Base class."""
@classmethod
def tearDown(self):
"""Delete any created files."""
try:
os.remove("Rectangle.json")
except IOError:
pass
try:
os.remove("Square.json")
except IOError:
pass
try:
os.remove("Base.json")
except IOError:
pass
def test_save_to_file_one_rectangle(self):
r = Rectangle(10, 7, 2, 8, 5)
Rectangle.save_to_file([r])
with open("Rectangle.json", "r") as f:
self.assertTrue(len(f.read()) == 53)
def test_save_to_file_two_rectangles(self):
r1 = Rectangle(10, 7, 2, 8, 5)
r2 = Rectangle(2, 4, 1, 2, 3)
Rectangle.save_to_file([r1, r2])
with open("Rectangle.json", "r") as f:
self.assertTrue(len(f.read()) == 105)
def test_save_to_file_one_square(self):
s = Square(10, 7, 2, 8)
Square.save_to_file([s])
with open("Square.json", "r") as f:
self.assertTrue(len(f.read()) == 39)
def test_save_to_file_two_squares(self):
s1 = Square(10, 7, 2, 8)
s2 = Square(8, 1, 2, 3)
Square.save_to_file([s1, s2])
with open("Square.json", "r") as f:
self.assertTrue(len(f.read()) == 77)
def test_save_to_file_cls_name_for_filename(self):
s = Square(10, 7, 2, 8)
Base.save_to_file([s])
with open("Base.json", "r") as f:
self.assertTrue(len(f.read()) == 39)
def test_save_to_file_overwrite(self):
s = Square(9, 2, 39, 2)
Square.save_to_file([s])
s = Square(10, 7, 2, 8)
Square.save_to_file([s])
with open("Square.json", "r") as f:
self.assertTrue(len(f.read()) == 39)
def test_save_to_file_None(self):
Square.save_to_file(None)
with open("Square.json", "r") as f:
self.assertEqual("[]", f.read())
def test_save_to_file_empty_list(self):
Square.save_to_file([])
with open("Square.json", "r") as f:
self.assertEqual("[]", f.read())
def test_save_to_file_no_args(self):
with self.assertRaises(TypeError):
Rectangle.save_to_file()
def test_save_to_file_more_than_one_arg(self):
with self.assertRaises(TypeError):
Square.save_to_file([], 1)
class TestBase_from_json_string(unittest.TestCase):
"""Unittests for testing from_json_string method of Base class."""
def test_from_json_string_type(self):
list_input = [{"id": 89, "width": 10, "height": 4}]
json_list_input = Rectangle.to_json_string(list_input)
list_output = Rectangle.from_json_string(json_list_input)
self.assertEqual(list, type(list_output))
def test_from_json_string_one_rectangle(self):
list_input = [{"id": 89, "width": 10, "height": 4, "x": 7}]
json_list_input = Rectangle.to_json_string(list_input)
list_output = Rectangle.from_json_string(json_list_input)
self.assertEqual(list_input, list_output)
def test_from_json_string_two_rectangles(self):
list_input = [
{"id": 89, "width": 10, "height": 4, "x": 7, "y": 8},
{"id": 98, "width": 5, "height": 2, "x": 1, "y": 3},
]
json_list_input = Rectangle.to_json_string(list_input)
list_output = Rectangle.from_json_string(json_list_input)
self.assertEqual(list_input, list_output)
def test_from_json_string_one_square(self):
list_input = [{"id": 89, "size": 10, "height": 4}]
json_list_input = Square.to_json_string(list_input)
list_output = Square.from_json_string(json_list_input)
self.assertEqual(list_input, list_output)
def test_from_json_string_two_squares(self):
list_input = [
{"id": 89, "size": 10, "height": 4},
{"id": 7, "size": 1, "height": 7}
]
json_list_input = Square.to_json_string(list_input)
list_output = Square.from_json_string(json_list_input)
self.assertEqual(list_input, list_output)
def test_from_json_string_None(self):
self.assertEqual([], Base.from_json_string(None))
def test_from_json_string_empty_list(self):
self.assertEqual([], Base.from_json_string("[]"))
def test_from_json_string_no_args(self):
with self.assertRaises(TypeError):
Base.from_json_string()
def test_from_json_string_more_than_one_arg(self):
with self.assertRaises(TypeError):
Base.from_json_string([], 1)
class TestBase_create(unittest.TestCase):
"""Unittests for testing create method of Base class."""
def test_create_rectangle_original(self):
r1 = Rectangle(3, 5, 1, 2, 7)
r1_dictionary = r1.to_dictionary()
r2 = Rectangle.create(**r1_dictionary)
self.assertEqual("[Rectangle] (7) 1/2 - 3/5", str(r1))
def test_create_rectangle_new(self):
r1 = Rectangle(3, 5, 1, 2, 7)
r1_dictionary = r1.to_dictionary()
r2 = Rectangle.create(**r1_dictionary)
self.assertEqual("[Rectangle] (7) 1/2 - 3/5", str(r2))
def test_create_rectangle_is(self):
r1 = Rectangle(3, 5, 1, 2, 7)
r1_dictionary = r1.to_dictionary()
r2 = Rectangle.create(**r1_dictionary)
self.assertIsNot(r1, r2)
def test_create_rectangle_equals(self):
r1 = Rectangle(3, 5, 1, 2, 7)
r1_dictionary = r1.to_dictionary()
r2 = Rectangle.create(**r1_dictionary)
self.assertNotEqual(r1, r2)
def test_create_square_original(self):
s1 = Square(3, 5, 1, 7)
s1_dictionary = s1.to_dictionary()
s2 = Square.create(**s1_dictionary)
self.assertEqual("[Square] (7) 5/1 - 3", str(s1))
def test_create_square_new(self):
s1 = Square(3, 5, 1, 7)
s1_dictionary = s1.to_dictionary()
s2 = Square.create(**s1_dictionary)
self.assertEqual("[Square] (7) 5/1 - 3", str(s2))
def test_create_square_is(self):
s1 = Square(3, 5, 1, 7)
s1_dictionary = s1.to_dictionary()
s2 = Square.create(**s1_dictionary)
self.assertIsNot(s1, s2)
def test_create_square_equals(self):
s1 = Square(3, 5, 1, 7)
s1_dictionary = s1.to_dictionary()
s2 = Square.create(**s1_dictionary)
self.assertNotEqual(s1, s2)
class TestBase_load_from_file(unittest.TestCase):
"""Unittests for testing load_from_file_method of Base class."""
@classmethod
def tearDown(self):
"""Delete any created files."""
try:
os.remove("Rectangle.json")
except IOError:
pass
try:
os.remove("Square.json")
except IOError:
pass
def test_load_from_file_first_rectangle(self):
r1 = Rectangle(10, 7, 2, 8, 1)
r2 = Rectangle(2, 4, 5, 6, 2)
Rectangle.save_to_file([r1, r2])
list_rectangles_output = Rectangle.load_from_file()
self.assertEqual(str(r1), str(list_rectangles_output[0]))
def test_load_from_file_second_rectangle(self):
r1 = Rectangle(10, 7, 2, 8, 1)
r2 = Rectangle(2, 4, 5, 6, 2)
Rectangle.save_to_file([r1, r2])
list_rectangles_output = Rectangle.load_from_file()
self.assertEqual(str(r2), str(list_rectangles_output[1]))
def test_load_from_file_rectangle_types(self):
r1 = Rectangle(10, 7, 2, 8, 1)
r2 = Rectangle(2, 4, 5, 6, 2)
Rectangle.save_to_file([r1, r2])
output = Rectangle.load_from_file()
self.assertTrue(all(type(obj) == Rectangle for obj in output))
def test_load_from_file_first_square(self):
s1 = Square(5, 1, 3, 3)
s2 = Square(9, 5, 2, 3)
Square.save_to_file([s1, s2])
list_squares_output = Square.load_from_file()
self.assertEqual(str(s1), str(list_squares_output[0]))
def test_load_from_file_second_square(self):
s1 = Square(5, 1, 3, 3)
s2 = Square(9, 5, 2, 3)
Square.save_to_file([s1, s2])
list_squares_output = Square.load_from_file()
self.assertEqual(str(s2), str(list_squares_output[1]))
def test_load_from_file_square_types(self):
s1 = Square(5, 1, 3, 3)
s2 = Square(9, 5, 2, 3)
Square.save_to_file([s1, s2])
output = Square.load_from_file()
self.assertTrue(all(type(obj) == Square for obj in output))
def test_load_from_file_no_file(self):
output = Square.load_from_file()
self.assertEqual([], output)
def test_load_from_file_more_than_one_arg(self):
with self.assertRaises(TypeError):
Base.load_from_file([], 1)
class TestBase_save_to_file_csv(unittest.TestCase):
"""Unittests for testing save_to_file_csv method of Base class."""
@classmethod
def tearDown(self):
"""Delete any created files."""
try:
os.remove("Rectangle.csv")
except IOError:
pass
try:
os.remove("Square.csv")
except IOError:
pass
try:
os.remove("Base.csv")
except IOError:
pass
def test_save_to_file_csv_one_rectangle(self):
r = Rectangle(10, 7, 2, 8, 5)
Rectangle.save_to_file_csv([r])
with open("Rectangle.csv", "r") as f:
self.assertTrue("5,10,7,2,8", f.read())
def test_save_to_file_csv_two_rectangles(self):
r1 = Rectangle(10, 7, 2, 8, 5)
r2 = Rectangle(2, 4, 1, 2, 3)
Rectangle.save_to_file_csv([r1, r2])
with open("Rectangle.csv", "r") as f:
self.assertTrue("5,10,7,2,8\n2,4,1,2,3", f.read())
def test_save_to_file_csv_one_square(self):
s = Square(10, 7, 2, 8)
Square.save_to_file_csv([s])
with open("Square.csv", "r") as f:
self.assertTrue("8,10,7,2", f.read())
def test_save_to_file_csv_two_squares(self):
s1 = Square(10, 7, 2, 8)
s2 = Square(8, 1, 2, 3)
Square.save_to_file_csv([s1, s2])
with open("Square.csv", "r") as f:
self.assertTrue("8,10,7,2\n3,8,1,2", f.read())
def test_save_to_file__csv_cls_name(self):
s = Square(10, 7, 2, 8)
Base.save_to_file_csv([s])
with open("Base.csv", "r") as f:
self.assertTrue("8,10,7,2", f.read())
def test_save_to_file_csv_overwrite(self):
s = Square(9, 2, 39, 2)
Square.save_to_file_csv([s])
s = Square(10, 7, 2, 8)
Square.save_to_file_csv([s])
with open("Square.csv", "r") as f:
self.assertTrue("8,10,7,2", f.read())
def test_save_to_file__csv_None(self):
Square.save_to_file_csv(None)
with open("Square.csv", "r") as f:
self.assertEqual("[]", f.read())
def test_save_to_file_csv_empty_list(self):
Square.save_to_file_csv([])
with open("Square.csv", "r") as f:
self.assertEqual("[]", f.read())
def test_save_to_file_csv_no_args(self):
with self.assertRaises(TypeError):
Rectangle.save_to_file_csv()
def test_save_to_file_csv_more_than_one_arg(self):
with self.assertRaises(TypeError):
Square.save_to_file_csv([], 1)
class TestBase_load_from_file_csv(unittest.TestCase):
"""Unittests for testing load_from_file_csv method of Base class."""
@classmethod
def tearDown(self):
"""Delete any created files."""
try:
os.remove("Rectangle.csv")
except IOError:
pass
try:
os.remove("Square.csv")
except IOError:
pass
def test_load_from_file_csv_first_rectangle(self):
r1 = Rectangle(10, 7, 2, 8, 1)
r2 = Rectangle(2, 4, 5, 6, 2)
Rectangle.save_to_file_csv([r1, r2])
list_rectangles_output = Rectangle.load_from_file_csv()
self.assertEqual(str(r1), str(list_rectangles_output[0]))
def test_load_from_file_csv_second_rectangle(self):
r1 = Rectangle(10, 7, 2, 8, 1)
r2 = Rectangle(2, 4, 5, 6, 2)
Rectangle.save_to_file_csv([r1, r2])
list_rectangles_output = Rectangle.load_from_file_csv()
self.assertEqual(str(r2), str(list_rectangles_output[1]))
def test_load_from_file_csv_rectangle_types(self):
r1 = Rectangle(10, 7, 2, 8, 1)
r2 = Rectangle(2, 4, 5, 6, 2)
Rectangle.save_to_file_csv([r1, r2])
output = Rectangle.load_from_file_csv()
self.assertTrue(all(type(obj) == Rectangle for obj in output))
def test_load_from_file_csv_first_square(self):
s1 = Square(5, 1, 3, 3)
s2 = Square(9, 5, 2, 3)
Square.save_to_file_csv([s1, s2])
list_squares_output = Square.load_from_file_csv()
self.assertEqual(str(s1), str(list_squares_output[0]))
def test_load_from_file_csv_second_square(self):
s1 = Square(5, 1, 3, 3)
s2 = Square(9, 5, 2, 3)
Square.save_to_file_csv([s1, s2])
list_squares_output = Square.load_from_file_csv()
self.assertEqual(str(s2), str(list_squares_output[1]))
def test_load_from_file_csv_square_types(self):
s1 = Square(5, 1, 3, 3)
s2 = Square(9, 5, 2, 3)
Square.save_to_file_csv([s1, s2])
output = Square.load_from_file_csv()
self.assertTrue(all(type(obj) == Square for obj in output))
def test_load_from_file_csv_no_file(self):
output = Square.load_from_file_csv()
self.assertEqual([], output)
def test_load_from_file_csv_more_than_one_arg(self):
with self.assertRaises(TypeError):
Base.load_from_file_csv([], 1)
if __name__ == "__main__":
unittest.main()