def test_two_update(self): r = Rectangle(10, 10, 3, 10, 10) r.update(13) r.height = 53 r.update(5) r.id = 32 self.assertEqual("[Rectangle] (32) 3/10 - 10/53", str(r))
def test_str(self): """ check if the printable representation is correct """ r9 = Rectangle(4, 6, 2, 1, 12) self.assertEqual(str(r9), '[Rectangle] (12) 2/1 - 4/6') r10 = Rectangle(5, 5) r10.id = 16 self.assertEqual(str(r10), '[Rectangle] (16) 0/0 - 5/5')
def test_to_json_string(self): """ check if return the correct json string from a list of dicts """ r10 = Rectangle(3, 4) r10.id = 18 expect_out = '[{"height": 4, "id": 18, "width": 3, "x": 0, "y": 0}, '\ '{"a": 1}]' self.assertEqual( Rectangle.to_json_string([r10.to_dictionary(), { "a": 1 }]), expect_out)
def test_ids(self): """tests ids""" Base._Base__nb_objects = 0 r1 = Rectangle(10, 2) r2 = Rectangle(2, 10) r3 = Rectangle(10, 2, 0, 0, 12) self.assertEqual(r1.id, 1) self.assertEqual(r2.id, 2) self.assertEqual(r3.id, 12) r3.id = "a" self.assertEqual(r3.id, "a")
def test_setters(self): r4 = Rectangle(3, 5, 6, 7, 14) r4.width = 5 self.assertEqual(r4.width, 5) r4.height = 8 self.assertEqual(r4.height, 8) r4.x = 3 self.assertEqual(r4.x, 3) r4.y = 1 self.assertEqual(r4.y, 1) r4.id = 2 self.assertEqual(r4.id, 2)
def test_save_to_file(self): """ check if save correctly the serialized objects in a file """ rect1 = Rectangle(1, 2, 3, 4, 5) rect2 = Rectangle(6, 7) rect2.id = 8 rect1.save_to_file([rect1, rect2]) with open("Rectangle.json", "r") as file: response = json.loads(file.read()) list_dicts = [{ 'y': 4, 'x': 3, 'id': 5, 'width': 1, 'height': 2 }, { 'y': 0, 'x': 0, 'id': 8, 'width': 6, 'height': 7 }] self.assertEqual(response, list_dicts) sqr1 = Square(1, 2, 3, 4) sqr2 = Square(5) sqr2.id = 6 sqr2.save_to_file([sqr1, sqr2]) with open("Square.json", "r") as file: response = json.loads(file.read()) list_dicts = [{ 'y': 3, 'x': 2, 'id': 4, 'size': 1 }, { 'y': 0, 'x': 0, 'id': 6, 'size': 5 }] self.assertEqual(response, list_dicts) rect1.save_to_file(None) with open("Rectangle.json", "r") as file: response = json.loads(file.read()) list_dicts = [] self.assertEqual(response, list_dicts) sqr2.save_to_file(None) with open("Square.json", "r") as file: response = json.loads(file.read()) list_dicts = [] self.assertEqual(response, list_dicts)
def testsetelements(self): rec = Rectangle(1, 2) rec.width = 5 rec.height = 3 rec.x = 4 rec.y = 8 rec.id = 98 self.assertEqual(rec.width, 5) self.assertEqual(rec.height, 3) self.assertEqual(rec.x, 4) self.assertEqual(rec.y, 8) self.assertEqual(rec.id, 98)
def test_set(self): r = Rectangle(1, 3) r.width = 5 r.height = 6 r.id = 4 r.x = 3 r.y = 2 self.assertEqual(r.width, 5) self.assertEqual(r.height, 6) self.assertEqual(r.x, 3) self.assertEqual(r.y, 2) self.assertEqual(r.id, 4)
def test_b1_setters(self): """testing setters""" r = Rectangle(3, 7, 9, 1, 2) r.width = 11 self.assertEqual(r.width, 11) r.height = 22 self.assertEqual(r.height, 22) r.x = 33 self.assertEqual(r.x, 33) r.y = 44 self.assertEqual(r.y, 44) r.id = 55 self.assertEqual(r.id, 55)
def check_change_att(self): """check if print changed""" r3 = Rectangle(7, 1, 1, 2, 3) r3_result = "[Rectangle] (3) 1/2 - 7/1" with patch('sys.stdout', new=StringIO()) as string: self.assertEqual(string.getvalue(), r3_result) r3.id = 5 r3.x = 0 r3.y = 0 r3.width = 7 r3.height = 7 r3_new_result = "[Rectangle] (5) 0/0 - 7/7" with patch('sys.stdout', new=StringIO()) as string: self.assertEqual(string.getvalue(), r3_new_result)
def test_init_attributes(self): """Checks when id is none """ r1 = Rectangle(10, 60) self.assertEqual(r1.id, 1) self.assertEqual(r1.width, 10) self.assertEqual(r1.height, 60) self.assertEqual(r1.x, 0) self.assertEqual(r1.y, 0) r2 = Rectangle(20, 40) self.assertEqual(r2.id, 2) self.assertEqual(r2.width, 20) self.assertEqual(r2.height, 40) self.assertEqual(r2.x, 0) self.assertEqual(r2.y, 0) r3 = Rectangle(10, 2, 4, 5) self.assertEqual(r3.id, 3) self.assertEqual(r3.width, 10) self.assertEqual(r3.height, 2) self.assertEqual(r3.x, 4) self.assertEqual(r3.y, 5) r4 = Rectangle(10, 2, 6) self.assertEqual(r4.id, 4) self.assertEqual(r4.width, 10) self.assertEqual(r4.height, 2) self.assertEqual(r4.x, 6) self.assertEqual(r4.y, 0) r5 = Rectangle(10, 2, 4, 5, 50) self.assertEqual(r5.id, 50) self.assertEqual(r5.width, 10) self.assertEqual(r5.height, 2) self.assertEqual(r5.x, 4) self.assertEqual(r5.y, 5) r6 = Rectangle(10, 2, 4, 5, 180) r6.id = 50 self.assertEqual(r6.id, 50) r6.width = 100 self.assertEqual(r6.width, 100) r6.height = 200 self.assertEqual(r6.height, 200) r6.x = 40 self.assertEqual(r6.x, 40) r6.y = 50 self.assertEqual(r6.y, 50)
def test_str_2(self): """ Test __str__ return value """ r1 = Rectangle(3, 2, 8, 8, 10) res = "[Rectangle] (10) 8/8 - 3/2\n" with patch('sys.stdout', new=StringIO()) as str_out: print(r1) self.assertEqual(str_out.getvalue(), res) r1.id = 1 r1.width = 7 r1.height = 15 res = "[Rectangle] (1) 8/8 - 7/15\n" with patch('sys.stdout', new=StringIO()) as str_out: print(r1) self.assertEqual(str_out.getvalue(), res)
def test_str5(self): """ Test for __str__ """ r1 = Rectangle(2, 3, 5, 6, 85) r1.id = 9 r1.x = 8 r1.y = 7 r1.width = 6 r1.height = 5 st = "[Rectangle] (9) 8/7 - 6/5" strP = str(r1) self.assertEqual(st, strP) with patch('sys.stdout', new=io.StringIO()) as p: print(r1, end='') pr = p.getvalue() self.assertEqual(st, pr)
def testgetandset(self): r = Rectangle(5, 2) self.assertEqual(r.width, 5) r.width = 9 self.assertEqual(r.width, 9) self.assertEqual(r.height, 2) r.height = 8 self.assertEqual(r.height, 8) self.assertEqual(r.x, 0) r.x = 8 self.assertEqual(r.x, 8) self.assertEqual(r.y, 0) r.y = 1 self.assertEqual(r.y, 1) r.id = 99 self.assertEqual(r.id, 99)
def test_display(self): """ test display """ obj1 = Rectangle(1, 1) self.assertEqual(obj1.display(), None) obj1 = Rectangle(3, 1) obj1.id = 8 self.assertEqual(obj1.display(), None) obj1 = Rectangle(1, 1) capture = TestClassRectangle.capture_stdout(obj1, "display") correct = "#\n" self.assertEqual(correct, capture.getvalue()) obj1 = Rectangle(1, 2) capture = TestClassRectangle.capture_stdout(obj1, "display") correct = "#\n#\n" self.assertEqual(correct, capture.getvalue())
def test_attr_rect(self): rect_inst_1 = Rectangle(10, 2) rect_inst_2 = Rectangle(10, 2, 0, 0, 12) rect_inst_3 = Rectangle(11, 3, 0, 0, None) self.assertEqual(rect_inst_1.width, 10) self.assertEqual(rect_inst_1.height, 2) self.assertEqual(rect_inst_2.width, 10) self.assertEqual(rect_inst_2.height, 2) self.assertEqual(rect_inst_2.x, 0) self.assertEqual(rect_inst_2.y, 0) self.assertEqual(rect_inst_2.id, 12) rect_inst_3.width = 20 self.assertEqual(rect_inst_3.width, 20) rect_inst_3.height = 20 self.assertEqual(rect_inst_3.height, 20) rect_inst_3.x = 20 self.assertEqual(rect_inst_3.x, 20) rect_inst_3.y = 20 self.assertEqual(rect_inst_3.y, 20) rect_inst_3.id = 20 self.assertEqual(rect_inst_3.id, 20) self.assertEqual(rect_inst_1.area(), 20)
def test_public_instance_atr(self): test_id = Rectangle(10, 12, 1, 2, 6) test_id.id = 200
def test_id(self): """[test ID] """ rec1 = Rectangle(1, 5) rec1.id = 5 self.assertEqual(rec1.id, 5)
def test_setters(self): """Public method to check types and values de getters methods""" r26 = Rectangle(2, 2) r26.width = 8 self.assertEqual(r26.width, 8) self.assertEqual(type(r26.width), int) with self.assertRaises(TypeError): r26.width = (3 + 5j) with self.assertRaises(TypeError): r26.width = [5, 7] with self.assertRaises(TypeError): r26.width = 5.2 with self.assertRaises(TypeError): r26.width = {'malo': 3, 'bueno': 6} with self.assertRaises(TypeError): r26.width = True with self.assertRaises(TypeError): r26.width = (5, 8) with self.assertRaises(TypeError): r26.width = "sY CiRbo" with self.assertRaises(TypeError): r26.width = ([4, 3]) with self.assertRaises(ValueError): r26.width = (0) with self.assertRaises(ValueError): r26.width = (-1) r26.height = 8 self.assertEqual(r26.height, 8) self.assertEqual(type(r26.height), int) with self.assertRaises(TypeError): r26.height = (3 + 5j) with self.assertRaises(TypeError): r26.height = [5, 7] with self.assertRaises(TypeError): r26.height = 5.2 with self.assertRaises(TypeError): r26.height = {'malo': 3, 'bueno': 6} with self.assertRaises(TypeError): r26.height = True with self.assertRaises(TypeError): r26.height = (5, 8) with self.assertRaises(TypeError): r26.height = "sY CiRbo" with self.assertRaises(TypeError): r26.height = ([4, 3]) with self.assertRaises(ValueError): r26.height = (0) with self.assertRaises(ValueError): r26.height = (-1) r26.x = 8 self.assertEqual(r26.x, 8) self.assertEqual(type(r26.x), int) with self.assertRaises(TypeError): r26.x = (3 + 5j) with self.assertRaises(TypeError): r26.x = [5, 7] with self.assertRaises(TypeError): r26.x = 5.2 with self.assertRaises(TypeError): r26.x = {'malo': 3, 'bueno': 6} with self.assertRaises(TypeError): r26.x = True with self.assertRaises(TypeError): r26.x = (5, 8) with self.assertRaises(TypeError): r26.x = "sY CiRbo" with self.assertRaises(TypeError): r26.x = ([4, 3]) with self.assertRaises(ValueError): r26.x = (-1) r26.y = 8 self.assertEqual(r26.y, 8) self.assertEqual(type(r26.y), int) with self.assertRaises(TypeError): r26.y = (3 + 5j) with self.assertRaises(TypeError): r26.y = [5, 7] with self.assertRaises(TypeError): r26.y = 5.2 with self.assertRaises(TypeError): r26.y = {'malo': 3, 'bueno': 6} with self.assertRaises(TypeError): r26.y = True with self.assertRaises(TypeError): r26.y = (5, 8) with self.assertRaises(TypeError): r26.y = "sY CiRbo" with self.assertRaises(TypeError): r26.y = ([4, 3]) with self.assertRaises(ValueError): r26.y = (-1) r26.id = 8 self.assertEqual(r26.id, 8) self.assertEqual(type(r26.id), int)