def test_fromJsonString(self): """check from_json_string""" Base._Base__nb_objects = 0 list_input = [{ 'id': 89, 'width': 10, 'height': 4 }, { 'id': 7, 'width': 1, 'height': 7 }] # list dict json_list_input = Rectangle.to_json_string(list_input) # str list dict list_output = Rectangle.from_json_string(json_list_input) # list dict self.assertTrue(list_input == list_output)
def test_from_json_string(self): """ returns the list of the JSON string representation """ list_input = [{ 'id': 89, 'width': 10, 'height': 4 }, { 'id': 7, 'width': 1, 'height': 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(self): list_input = [{ 'id': 89, 'width': 10, 'height': 4 }, { 'id': 7, 'width': 1, 'height': 7 }] json_list_input = Rectangle.to_json_string(list_input) list_output = Rectangle.from_json_string(json_list_input) self.assertIsInstance(list_input, list) self.assertIsInstance(json_list_input, str) self.assertIsInstance(list_output, list)
def test_from_json_string_repre_failed(self): """ Failure in the input data type """ repre_json = [{ "id": 70, "width": 5, "height": 8 }, { "id": 3, "width": 2, "height": 5 }] data_in = Rectangle.to_json_string(repre_json) json_return = Rectangle.from_json_string(data_in) self.assertTrue(type(data_in), list) self.assertTrue(type(json_return), str)
def test_from_json_string(self): """Test from_json_string method""" with self.assertRaises(TypeError) as excep: Base.from_json_string() message = "from_json_string() missing 1 required positional argument: \ 'json_string'" self.assertEqual(str(excep.exception), message) self.assertEqual(Base.from_json_string(None), []) self.assertEqual(Base.from_json_string(""), []) string = '[{"x": 2, "y": 89, "width": 5, "id": 10, "height": 8}, \ {"x": 4, "y": 19, "width": 7, "id": 13, "height": 9}]' dic = [{'x': 2, 'y': 89, 'width': 5, 'id': 10, 'height': 8}, {'x': 4, 'y': 19, 'width': 7, 'id': 13, 'height': 9}] self.assertEqual(Base.from_json_string(string), dic) string = '[{}]' dic = [{}] self.assertEqual(Base.from_json_string(string), dic) string = '[{}, {}]' dic = [{}, {}] self.assertEqual(Base.from_json_string(string), dic) string = '[{"hol": 5, "ber": 9, "ton": 11, "is": 1, "awesome": 6}, \ {"a": 4, "b": 19, "c": 7, "d": 13, "e": 9}]' dic = [{'hol': 5, 'ber': 9, 'ton': 11, 'is': 1, 'awesome': 6}, {'a': 4, 'b': 19, 'c': 7, 'd': 13, 'e': 9}] self.assertEqual(Base.from_json_string(string), dic) list_rect = [{'width': 2, 'height': 5, 'id': 3}, {'width': 2, 'height': 5, 'id': 3}] list_rev = Rectangle.from_json_string( Rectangle.to_json_string(list_rect)) self.assertEqual(list_rect, list_rev) list_square = [{'size': 5, 'id': 3}, {'size': 2, 'id': 5}] list_rev = Square.from_json_string( Square.to_json_string(list_square)) self.assertEqual(list_square, list_rev)
def test_from_json_string_repre(self): """ Checks a list of JSON string representation """ repre_json = [{ 'id': 70, 'width': 5, 'height': 8 }, { 'id': 3, 'width': 2, 'height': 5 }] json_in = Rectangle.to_json_string(repre_json) json_return = Rectangle.from_json_string(json_in) self.assertEqual(type(json_in), str) self.assertEqual(type(repre_json), list) self.assertEqual(type(json_return), list)
def test_from_json_string_rect(self): list_input = [ {'id': 89, 'width': 10, 'height': 4}, {'id': 7, 'width': 1, 'height': 7} ] json_list_input = Rectangle.to_json_string(list_input) list_output = Rectangle.from_json_string(json_list_input) temp_stdout = StringIO() with contextlib.redirect_stdout(temp_stdout): print("[{}] {}".format(type(list_input), list_input)) output1 = temp_stdout.getvalue().strip() temp_stdout = StringIO() with contextlib.redirect_stdout(temp_stdout): print("[{}] {}".format(type(list_output), list_output)) output2 = temp_stdout.getvalue().strip() self.assertEqual(output1, output2)
def test_from_json_string_base(self): """Test that from_json_string method returns list of dictionaries from a JSON string. """ list_input = [{ 'id': 89, 'width': 10, 'height': 4 }, { 'id': 7, 'width': 1, 'height': 7 }] json_list_input = Rectangle.to_json_string(list_input) list_output = Rectangle.from_json_string(json_list_input) self.assertEqual(list_output, list_input)
def test_json_string_type(self): ''' Testing the returned type ''' list_input = [{ 'id': 2089, 'width': 10, 'height': 4 }, { 'id': 2712, 'width': 1, 'height': 7 }] json_list_input = Rectangle.to_json_string(list_input) list_output = Rectangle.from_json_string(json_list_input) self.assertEqual(type(list_input), list)
def test_da_save_to_file_contend(self): """test checks if the contend in Rectangle.json is correct """ r1 = Rectangle(10, 7, 2, 8) r2 = Rectangle(2, 4) Rectangle.save_to_file([r1, r2]) obj = [] for i in [r1, r2]: obj.append(i.to_dictionary()) json_str = Rectangle.to_json_string(obj) with open("Rectangle.json", "r") as file: contend_fl = file.read() self.assertEqual(json_str, contend_fl)
def test_from_json_string(self): """test if json string converts back""" list_input = [{ 'id': 89, 'width': 10, 'height': 4 }, { 'id': 7, 'width': 1, 'height': 7 }] json_list_input = Rectangle.to_json_string(list_input) list_output = Rectangle.from_json_string(json_list_input) self.assertEqual(type(json_list_input), str) self.assertEqual(type(list_output), list) self.assertEqual(list_output, list_input)
def test_to_JSON(self): """Tests the dictionary to JSON conversion""" s1 = Square(3) r1 = Rectangle(4, 5, 1, 2, 47) ld = [] ld.append(s1.to_dictionary()) ld.append(r1.to_dictionary()) ld_json = r1.to_json_string(ld) expected = [{"id":1, "size":3, "x":0, "y":0}, {"id":47, "width":4, "height":5, "x":1, "y":2}] expected_json = json.dumps(expected) self.assertEqual(ld_json, expected_json) self.assertEqual(s1.to_json_string(None), "[]") self.assertEqual(s1.to_json_string([]), "[]")
def test_json(self): """test json""" r1 = Rectangle(10, 7, 2, 8) dictionary = r1.to_dictionary() json_dictionary = Base.to_json_string([dictionary]) self.assertEqual(isinstance(json_dictionary, str)) self.assertEqual([dictionary], json.loads(json_dictionary)) r1 = Rectangle(10, 7, 2, 8) dictionary = r1.to_dictionary() json_dictionary = Rectangle.to_json_string([dictionary]) self.assertEqual([dictionary], json.loads(json_dictionary)) r1 = Rectangle(10, 7, 2, 8) dictionary = r1.to_dictionary() json_dictionary = Square.to_json_string([dictionary]) self.assertEqual([dictionary], json.loads(json_dictionary))
def test_from_json_string_with_valid_param(self): list_input = [ { "id": 89, "width": 10, "height": 4 }, { "id": 7, "width": 1, "height": 7 }, ] self.assertTrue(type(list_input), list) json_list_input = Rectangle.to_json_string(list_input) self.assertTrue(type(json_list_input), str) self.assertTrue(type(Base.from_json_string(json_list_input)), list)
def test_from_json_dictRx2(self): linput = [{ "id": 89, "width": 10, "height": 4, "x": 2, "y": 1 }, { "id": 7, "width": 1, "height": 7, "x": 8, "y": 2 }] json_linput = Rectangle.to_json_string(linput) loutput = Rectangle.from_json_string(json_linput) self.assertEqual(linput, loutput)
def test_17_0_fromJsonString(self): """Checks a list of JSON string format""" list_input = [{ 'id': 89, 'width': 10, 'height': 4 }, { 'id': 7, 'width': 1, 'height': 7 }] json_list_input = Rectangle.to_json_string(list_input) list_output = Rectangle.from_json_string(json_list_input) self.assertTrue(type(list_input), list) self.assertTrue(type(json_list_input), str) self.assertTrue(type(list_output), list)
def test_case3_2(self): """Test for from_json_string method in Base class""" self.assertEqual(Base.from_json_string(None), []) self.assertEqual(Base.from_json_string([]), []) l_in = [{ 'id': 89, 'width': 10, 'height': 4 }, { 'id': 7, 'width': 1, 'height': 7 }] j_in = Rectangle.to_json_string(l_in) l_out = Rectangle.from_json_string(j_in) self.assertEqual(type(l_in), list) self.assertEqual(type(j_in), str) self.assertEqual(type(l_out), list)
def test_fromJson(self): """tests the fromjson""" r_input = [{'id': 89, 'width': 10, 'height': 4}] s_input = [{'id': 89, 'size': 4}] json_list_input = Rectangle.to_json_string(r_input) list_output = Rectangle.from_json_string(json_list_input) self.assertTrue(type(list_output) is list) list_output = Rectangle.from_json_string([]) self.assertTrue(list_output == []) list_output = Rectangle.from_json_string(None) self.assertTrue(list_output == []) json_list_input = Square.to_json_string(s_input) list_output = Square.from_json_string(json_list_input) self.assertTrue(type(list_output) is list) list_output = Square.from_json_string([]) self.assertTrue(list_output == []) list_output = Square.from_json_string(None) self.assertTrue(list_output == [])
def test_from_json_str_to_dic(self): list_input = [{ 'id': 89, 'width': 10, 'height': 4 }, { 'id': 7, 'width': 1, 'height': 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) list_output = Rectangle.from_json_string(None) self.assertEqual([], list_output) list_output = Rectangle.from_json_string([]) self.assertEqual([], list_output)
def test_json_string(self): ''' Testing that the json string gets converted into a list ''' list_input = [{ 'id': 2089, 'width': 10, 'height': 4 }, { 'id': 2712, 'width': 1, 'height': 7 }] json_list_input = Rectangle.to_json_string(list_input) list_output = Rectangle.from_json_string(json_list_input) s1 = {'id': 2089, 'width': 10, 'height': 4} s2 = {'height': 7, 'id': 2712, 'width': 1} self.assertEqual(list_input[0], s1) self.assertEqual(list_input[1], s2)
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_save_to_file(self): """Test the save_to_file method """ rect = Rectangle(1, 1) types = (int, float, str, tuple, list, dict, bool) insts = [rect] + [Rectangle(1, 1, id=t()) for t in types] fname = 'Rectangle.json' try: remove(fname) except FileNotFoundError: pass self.assertIsNone(Rectangle.save_to_file(None)) with open(fname) as ifile: self.assertEqual(ifile.read(), '[]') for index in range(len(insts)): self.assertIsNone(Rectangle.save_to_file(insts[index:])) with open(fname) as ifile: self.assertEqual(ifile.read(), Rectangle.to_json_string( [obj.to_dictionary() for obj in insts[index:]] ))
def test_from_json_string_success(self): """test from_json_string method""" Base._Base__nb_objects = 0 list_input = [{ 'id': 89, 'width': 10, 'height': 4 }, { 'id': 7, 'width': 1, 'height': 7 }] json_list_input = Rectangle.to_json_string(list_input) list_output = Rectangle.from_json_string(json_list_input) self.assertEqual(type(list_output), list) self.assertDictEqual(list_output[0], list_input[0]) self.assertDictEqual(list_output[1], list_input[1]) list_output = Rectangle.from_json_string(None) self.assertEqual(type(list_output), list) self.assertEqual(list_output, [])
def test_from_json_string(self): list_input = [{ 'id': 89, 'width': 10, 'height': 4 }, { 'id': 7, 'width': 1, 'height': 7 }] json_list_input = Rectangle.to_json_string(list_input) list_output = Rectangle.from_json_string(json_list_input) l = [{ 'height': 4, 'width': 10, 'id': 89 }, { 'height': 7, 'width': 1, 'id': 7 }] self.assertEqual(list_output, l)
def test1_jsonstr_to_dic(self): """ test json to dict """ list_input1 = [{ 'id': 97, 'width': 5, 'height': 4 }, { 'id': 79, 'width': 4, 'height': 5 }] json_list_input1 = Rectangle.to_json_string(list_input1) list_output1 = Rectangle.from_json_string(json_list_input1) self.assertEqual(list_output1, [{ 'height': 4, 'width': 5, 'id': 97 }, { 'height': 5, 'width': 4, 'id': 79 }])
def test_jason_string(self): lists = [{ 'id': 89, 'width': 8, 'height': 3 }, { 'id': 10, 'width': 3, 'height': 10 }] json_list_input = Rectangle.to_json_string(lists) list_output = Rectangle.from_json_string(json_list_input) self.assertEqual(list_output, [{ 'id': 89, 'width': 8, 'height': 3 }, { 'id': 10, 'width': 3, 'height': 10 }]) self.assertTrue(type(list_output), list)
def test_json_str_to_dic_1(self): """test json, str to dictionary""" Base._Base__nb_objects = 0 list_input = [{ 'id': 89, 'width': 10, 'height': 4 }, { 'id': 7, 'width': 1, 'height': 7 }] json_list_input = Rectangle.to_json_string(list_input) list_output = Rectangle.from_json_string(json_list_input) self.assertEqual(list_output, [{ 'height': 4, 'width': 10, 'id': 89 }, { 'height': 7, 'width': 1, 'id': 7 }])
def test_22_from_json_string_Rout(self): """Test from_json_string output Rectangle""" d = [{ 'x': 1, 'y': 2, 'width': 3, 'id': 4, 'height': 5 }, { 'x': 2, 'y': 4, 'width': 6, 'id': 8, 'height': 10 }] out = Rectangle.from_json_string(Rectangle.to_json_string(d)) self.assertEqual(d, out) self.assertTrue(type(out) is list) self.assertEqual(len(out), 2) self.assertTrue(type(out[0]) is dict) self.assertTrue(type(out[1]) is dict)
def test_ab_from_json_string(self): """This function tests the from_json_string func""" list_input = [{ 'id': 89, 'width': 10, 'height': 4 }, { 'id': 7, 'width': 1, 'height': 7 }] json_list_input = Rectangle.to_json_string(list_input) list_output = Rectangle.from_json_string(json_list_input) self.assertEqual(list_output, [{ "height": 4, "width": 10, "id": 89 }, { "height": 7, "width": 1, "id": 7 }]) self.assertEqual(type(list_output), list)
def test_from_json_string_three(self): """Test from_json_string three inputs.""" list_input = [{ 'id': 89, 'width': 10, 'height': 4 }, { 'id': 7, 'width': 1, 'height': 7 }] json_list_input = Rectangle.to_json_string(list_input) list_output = Rectangle.from_json_string(json_list_input) self.assertEqual(list_output, [{ 'id': 89, 'width': 10, 'height': 4 }, { 'id': 7, 'width': 1, 'height': 7 }]) self.assertTrue(type(list_output), list)