#!/usr/bin/python3 """ 17-main """ from models.rectangle import Rectangle from models.square import Square if __name__ == "__main__": r1 = Rectangle(3, 5, 1) r1_dictionary = r1.to_dictionary() r2 = Rectangle.create(**r1_dictionary) print(r1) print(r2) print(r1 is r2) print(r1 == r2) r1 = Square(3, 5, 1) r1_dictionary = r1.to_dictionary() r2 = Square.create(**r1_dictionary) print(r1) print(r2) print(r1 is r2) print(r1 == r2)
def test_B_json_string(self): r1 = Rectangle(10, 7, 2, 8, 1) dic = r1.to_dictionary() jsonDic = Base.to_json_string([dic]) self.assertEqual(type(jsonDic), str)
def test_C_json_string2(self): r1 = Rectangle(10, 7, 2, 8, 1) dic = r1.to_dictionary() jsonDic = Base.to_json_string([dic]) list = json.loads(jsonDic) self.assertEqual(list, [dic])
def test_K_json_file_created(self): t1 = Rectangle(10, 7, 2, 8, 1) Rectangle.save_to_file([t1]) with open("Rectangle.json", "r") as f: self.assertEqual([t1.to_dictionary()], json.load(f))
def test_42_create_invalid_key(self): """Test for create method with set.""" r1 = Rectangle(1, 2, 3, 4, 5) r1_dictionary = r1.to_dictionary() r2 = Rectangle.create(volume=1) self.assertEqual(r1.__dict__ == r2.__dict__, False)
def test21_to_json_string(self): """Test return the JSON string repr. of list_dictionaries""" r1 = Rectangle(10, 7, 2, 8) dictionary = r1.to_dictionary() json_dictionary = Rectangle.to_json_string([dictionary])
class TestRectangle(unittest.TestCase): """ Class containing functions to test: * Functionality of the Rectangle class. * Style of the Rectangle class. * Documentation of the Rectangle class. """ def setUp(self): """ Method to set the start point. """ # Rectangle("width", "height", "x", "y", "id") Base._Base__nb_objects = 0 self.r0 = Rectangle(8, 12, 2, 1, 12) self.r1 = Rectangle(10, 11) self.r2 = Rectangle(12, 13, 14, 15) self.r3 = Rectangle(2, 4, 0, 3, 13) self.r4 = Rectangle(2, 4, 0, 0) # Redirect output to verify output of print dependent functions sys.stdout = StringIO() def tearDown(self): """ Method to cleans everything up after running setup. """ sys.stdout = sys.__stdout__ def test_00_id(self): """ Tests for id. """ self.assertEqual(self.r0.id, 12) self.assertEqual(self.r1.id, 1) self.assertEqual(self.r2.id, 2) self.assertEqual(self.r3.width, 2) self.assertEqual(self.r4.x, 0) def test_TypeErrors(self): """ TypeError cases. """ self.assertRaises(TypeError, Rectangle, "a", 2, 7, 8) self.assertRaises(TypeError, Rectangle, [1, 2], 2, 7, 8) self.assertRaises(TypeError, Rectangle, 2.2, 2, 7, 8) self.assertRaises(TypeError, Rectangle, {2}, 2, 7, 8) self.assertRaises(TypeError, Rectangle, 2, 2, 7, (1, 2)) self.assertRaises(TypeError, Rectangle, 2, 2, 7, 5j) self.assertRaises(TypeError, Rectangle, 2, 2, 7, float("inf")) def test__ValueErrors(self): """ ValueError cases. """ self.assertRaises(ValueError, Rectangle, -8, 2, 7, 8) self.assertRaises(ValueError, Rectangle, 2, 2, 7, -8) self.assertRaises(ValueError, Rectangle, -8, 2, -7, 8) self.assertRaises(ValueError, Rectangle, -8, -2, 7, 8) def test_04_area(self): """ Test the area method. """ self.assertEqual(self.r0.area(), 96) def test_05_display_0(self): """ Test display method without x and y """ r1O = "##\n" \ "##\n" \ "##\n" \ "##\n" self.r4.display() self.assertEqual(sys.stdout.getvalue(), r1O) def test_07_display_1(self): """ Test display method with 'x' and 'y' position. """ r1O = "\n\n\n##\n" \ "##\n" \ "##\n" \ "##\n" self.r3.display() self.assertEqual(sys.stdout.getvalue(), r1O) def test_08_update(self): """ Tests that the update method uses setter with *args. """ self.r2.update(9, 3, 2, 0) self.assertEqual(self.r2.__str__(), "[Rectangle] (9) 0/15 - 3/2") def test_09_update(self): """ Tests that the update method uses setter with **kwargs. """ self.r0.update(x=1, height=2) self.r1.update(width=4, x=3, id=14, y=1) self.assertEqual(self.r0.__str__(), "[Rectangle] (12) 1/1 - 8/2") self.assertEqual(self.r1.__str__(), "[Rectangle] (14) 3/1 - 4/11") def test_13_to_dictionary(self): """ Test regular to_dictionary """ d1 = self.r0.to_dictionary() self.assertEqual({ "id": 12, "width": 8, "height": 12, "x": 2, "y": 1 }, d1) self.assertTrue(type(d1) is dict)
def test_to_dictionary_no_object_changes(self): r1 = Rectangle(10, 2, 1, 9, 5) r2 = Rectangle(5, 9, 1, 2, 10) r2.update(**r1.to_dictionary()) self.assertNotEqual(r1, r2)
def test_to_dictionary_arg(self): r = Rectangle(10, 2, 4, 1, 2) with self.assertRaises(TypeError): r.to_dictionary(1)
def test_to_dictionary_output(self): r = Rectangle(10, 2, 1, 9, 5) correct = {'x': 1, 'y': 9, 'id': 5, 'height': 2, 'width': 10} self.assertDictEqual(correct, r.to_dictionary())
def test_dictionary(self): """Comment""" re1 = Rectangle(10, 7, 2, 8, 70) dictionary = re1.to_dictionary() json_dictionary = Base.to_json_string([dictionary]) self.assertEqual(type(json_dictionary), str)
class TestRectangleclass(unittest.TestCase): """Test Cases Class""" def setUp(self): """Set up""" self.inst = Rectangle(1, 2, 3, 4, 5) def tearDown(self): """ Tear Dowm""" del self.inst Base._Base__nb_objects = 0 def test_to_dict(self): """ Tear Dowm""" r1 = self.inst.to_dictionary() exp = {'x': 3, 'y': 4, 'id': 5, 'height': 2, 'width': 1} self.assertEqual(r1, exp) def test_to_dict_1(self): """ Tear Dowm""" r1 = self.inst.to_dictionary() exp = {'x': 3, 'y': 4, 'id': 5, 'height': 2, 'width': 1} self.assertEqual(type(r1).__name__, type(exp).__name__) def test_area(self): """ Test Area""" self.assertEqual(self.inst.area(), 2) def out_c(self, x=None): """ Out std""" t = sys.stdout sys.stdout = StringIO() if x is None: self.inst.display() else: x.display() o = sys.stdout.getvalue() sys.stdout.close() sys.stdout = t return o def test_display(self): """ Test display""" display = "\n\n\n\n #\n #\n" o = self.out_c() self.assertEqual(o, display) def test_display_1(self): """ Test display""" r1 = Rectangle(4, 6) display = "####\n####\n####\n####\n####\n####\n" o = self.out_c(r1) self.assertEqual(o, display) def test_str(self): """ Test display""" self.assertEqual(str(self.inst), "[Rectangle] (5) 3/4 - 1/2") def test_update_att(self): """ Test display""" self.inst.update(89) self.assertEqual(str(self.inst), "[Rectangle] (89) 3/4 - 1/2") def test_update_dic(self): self.inst.update(x=6, height=8, y=3, width=7) self.assertEqual(str(self.inst), "[Rectangle] (5) 6/3 - 7/8") def test_id(self): """ Test id""" self.assertEqual(self.inst.id, 5) def test_id_1(self): """ Test id 1""" self.inst.id = 0 inst1 = Rectangle(1, 2) inst2 = Rectangle(2, 1) inst3 = Rectangle(1, 2, 3, 4) self.assertEqual(inst3.id, 3) def test_width(self): """ test width""" self.assertEqual(self.inst.width, 1) def test_height(self): """ test height""" self.assertEqual(self.inst.height, 2) def test_x(self): """ test x""" self.assertEqual(self.inst.x, 3) def test_y(self): """ test y""" self.assertEqual(self.inst.y, 4) def test_raise_width(self): """ test raise width""" with self.assertRaisesRegex(TypeError, "width must be an integer"): self.inst.width = "Fault" def test_raise_height(self): """ test raise heigt""" with self.assertRaisesRegex(TypeError, "height must be an integer"): self.inst.height = "Fault" def test_raise_x(self): """ test raise heigt""" with self.assertRaisesRegex(TypeError, "x must be an integer"): self.inst.x = "Fault" def test_raise_y(self): """ test raise heigt""" with self.assertRaisesRegex(TypeError, "y must be an integer"): self.inst.y = "Fault" def test_raise_width_1(self): """ test raise heigt""" with self.assertRaisesRegex(ValueError, "width must be > 0"): self.inst.width = 0 def test_raise_height_1(self): """ test raise heigt""" with self.assertRaisesRegex(ValueError, "height must be > 0"): self.inst.height = 0 def test_raise_x_1(self): """ test raise heigt""" with self.assertRaisesRegex(ValueError, "x must be >= 0"): self.inst.x = -1 def test_raise_y_1(self): """ test raise heigt""" with self.assertRaisesRegex(ValueError, "y must be >= 0"): self.inst.y = -1
def test_to_json_string_multiple_dicts(self): r = Rectangle(2, 4, 1, 2, 42) s = Square(2, 1, 2, 42) dicts = [r.to_dictionary(), s.to_dictionary()] self.assertTrue(len(Base.to_json_string(dicts)) == 92)
def test_to_json_string_basic_rectangle(self): r = Rectangle(2, 4, 1, 2, 42) dicts = [r.to_dictionary()] self.assertTrue(len(Base.to_json_string(dicts)) == 53)
def test_to_json_string_type(self): r = Rectangle(2, 4, 1, 2, 42) s = Square(2, 1, 2, 42) self.assertEqual(str, type(Base.to_json_string([r.to_dictionary()]))) self.assertEqual(str, type(Base.to_json_string([s.to_dictionary()])))
class TestBase(unittest.TestCase): """ Testing implementation of a class: Base """ def setUp(self): Base._reset() def test_normal(self): self.base = Base(5) self.assertEqual(self.base.id, 5) """ Check if nb_objects incremented """ self.base = Base() self.assertEqual(self.base._Base__nb_objects, 1) """ See if it id is set to nb_objects """ self.base = Base() self.assertEqual(self.base.id, 2) def test_bad_type(self): """ float """ with self.assertRaises(TypeError): self.base = Base(.5) """ character """ with self.assertRaises(TypeError): self.base = Base('f') with self.assertRaises(TypeError): self.base = Base(float("NaN")) with self.assertRaises(TypeError): self.base = Base(float("Inf")) with self.assertRaises(TypeError): self.base = Base(-float("Inf")) with self.assertRaises(TypeError): self.base = Base([2]) with self.assertRaises(TypeError): self.base = Base({2: 5}) with self.assertRaises(TypeError): self.base = Base((1, )) def test_bad_value(self): with self.assertRaises(ValueError): self.base = Base(-4) def test_arg_count(self): with self.assertRaises(TypeError): self.base = Base(2, 2) def test_json(self): self.base = Base() """ test one dictionary """ dic = {'test': 1, 'test2': 2, 'test3': 3} lidi = [dic] json_dict = self.base.to_json_string(lidi) self.assertTrue(isinstance(json_dict, str)) """ test multiple dictionary """ dic1 = {'test': 1, 'test2': 2, 'test3': 3} lidi = [dic, dic1] json_dict = self.base.to_json_string(lidi) self.assertTrue(isinstance(json_dict, str)) """ test empty or None """ json_dict = self.base.to_json_string([]) self.assertEqual(json_dict, "[]") json_dict = self.base.to_json_string(None) self.assertEqual(json_dict, "[]") def test_json_to_file(self): """ Test save_json: note* 52 characters per dict """ self.r1 = Rectangle(1, 2) Rectangle.save_to_file([self.r1]) with open("Rectangle.json", "r") as f: data = f.read() self.assertEqual(len(data), 52) self.r2 = Rectangle(3, 4) Rectangle.save_to_file([self.r1, self.r2]) with open("Rectangle.json", "r") as f: data = f.read() self.assertEqual(len(data), 104) """ Test empty list """ Rectangle.save_to_file([]) with open("Rectangle.json", "r") as f: data = f.read() self.assertEqual(len(data), 2) def test_from_json(self): self.rec = Rectangle(1, 2) li = [self.rec.to_dictionary()] json_list_input = Rectangle.to_json_string(li) from_json = Rectangle.from_json_string(json_list_input) self.assertTrue(isinstance(from_json, list)) self.assertTrue(isinstance(from_json[0], dict)) """ Test empty or None """ self.assertEqual(Rectangle.from_json_string(None), []) self.assertEqual(Rectangle.from_json_string("[]"), []) def test_create_cls(self): """ Test the function create: returns an instance with all attributes set """ self.r1 = Rectangle(10, 10, 10) self.r1_dict = self.r1.to_dictionary() self.r2 = Rectangle.create(**self.r1_dict) self.assertEqual(self.r2.__str__(), "[Rectangle] (1) 10/0 - 10/10") self.assertTrue(self.r2 is not self.r1) self.assertTrue(self.r2 != self.r1)