예제 #1
0
#!/usr/bin/python3
""" 16-main """
from models.rectangle import Rectangle

if __name__ == "__main__":

    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([])
    print("[{}] {}".format(type(list_input), list_input))
    print("[{}] {}".format(type(json_list_input), json_list_input))
    print("[{}] {}".format(type(list_output), list_output))
예제 #2
0
 def test_from_json_string_none(self):
     json_list_input = Rectangle.to_json_string(None)
     list_output = Rectangle.from_json_string(json_list_input)
     self.assertEqual(list_output, [])
 def test_from_json_string_diff_dataType(self):
     """test test_from_json_string method with different datatype"""
     with self.assertRaises(ValueError):
         Rectangle.from_json_string("a")
     with self.assertRaises(TypeError):
         Rectangle.from_json_string(True)
     with self.assertRaises(TypeError):
         Rectangle.from_json_string(False)
     with self.assertRaises(TypeError):
         Rectangle.from_json_string({1})
     with self.assertRaises(TypeError):
         Rectangle.from_json_string(2.3)
     with self.assertRaises(TypeError):
         Rectangle.from_json_string(2)
     with self.assertRaises(TypeError):
         Rectangle.from_json_string({'key': 1})
     with self.assertRaises(TypeError):
         Rectangle.from_json_string(float('nan'))
     with self.assertRaises(TypeError):
         Rectangle.from_json_string(float('inf'))
예제 #4
0
 def test_convert_empty(self):
     """Checks from_jon_string"""
     list_input = []
     json_list_input = Rectangle.to_json_string(list_input)
     list_output = Rectangle.from_json_string(json_list_input)
     self.assertEqual(list_output, list_input)
예제 #5
0
 def test_from_json_string_empty_list(self):
     json_list_input = Rectangle.to_json_string([])
     list_output = Rectangle.from_json_string(json_list_input)
     self.assertEqual(list_output, [])
 def test_from_json_string_none_one(self):
     """ Tests if json_string is None """
     json_object = Rectangle.from_json_string(None)
     self.assertEqual(json_object, '[]')
예제 #7
0
 def test3_jsonstr_to_dic(self):
     """ test json to dict """
     json_list_input3 = None
     list_output3 = Rectangle.from_json_string(json_list_input3)
     self.assertEqual(list_output3, [])
예제 #8
0
 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))
예제 #9
0
 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_two_rectangle(self):
     json_str = [{"id": 1, "width": 2, "height": 3, "x": 4, "y": 5},
                 {"id": 1, "width": 2, "height": 3, "x": 4, "y": 5}]
     json_list = Rectangle.to_json_string(json_str)
     json_str_2 = Rectangle.from_json_string(json_list)
     self.assertEqual(json_str, json_str_2)
    def test_base(self):
        """tests for base"""
        b1 = Base()
        self.assertEqual(b1.id, 1)
        self.assertIsInstance(b1, Base)

        b2 = Base()
        self.assertEqual(b2.id, 2)
        self.assertIsInstance(b2, Base)

        b3 = Base(9)
        self.assertEqual(b3.id, 9)
        self.assertIsInstance(b3, Base)

        b4 = Base()
        self.assertEqual(b4.id, 3)
        self.assertIsInstance(b4, Base)

        b5 = Rectangle(10, 7, 2, 8)
        dictionary = b5.to_dictionary()
        comdict = {'x': 2, 'width': 10, 'id': 4, 'height': 7, 'y': 8}
        self.assertDictEqual(dictionary, comdict)
        self.assertIn(Base.to_json_string([sorted(dictionary.items())]),
                      json.dumps([sorted(comdict.items())]))
        self.assertIsInstance(Base.to_json_string([dictionary]), str)

        self.assertEqual(Base.to_json_string([]), "[]")
        self.assertEqual(Base.to_json_string(None), "[]")

        with self.assertRaises(TypeError) as err:
            Base.to_json_string(5)
        self.assertEqual("argument must be a list of dictionaries",
                         str(err.exception))

        with self.assertRaises(TypeError) as err:
            Base.to_json_string("hello")
        self.assertEqual("argument must be a list of dictionaries",
                         str(err.exception))

        with self.assertRaises(TypeError) as err:
            Base.to_json_string((1, ))
        self.assertEqual("argument must be a list of dictionaries",
                         str(err.exception))

        with self.assertRaises(TypeError) as err:
            Base.to_json_string({'Diva': 'Lei'})
        self.assertEqual("argument must be a list of dictionaries",
                         str(err.exception))

        with self.assertRaises(TypeError) as err:
            Base.to_json_string(True)
        self.assertEqual("argument must be a list of dictionaries",
                         str(err.exception))

        Rectangle.save_to_file([])
        with open("Rectangle.json", "r") as f:
            self.assertEqual(f.read(), "[]")

        Rectangle.save_to_file(None)
        with open("Rectangle.json", "r") as f:
            self.assertEqual(f.read(), "[]")

        Square.save_to_file([])
        with open("Square.json", "r") as f:
            self.assertEqual(f.read(), "[]")

        Square.save_to_file(None)
        with open("Square.json", "r") as f:
            self.assertEqual(f.read(), "[]")

        with self.assertRaises(TypeError) as err:
            Rectangle.save_to_file({'Diva': 'Lei'})
        self.assertEqual("argument must be a list of instances",
                         str(err.exception))

        with self.assertRaises(TypeError) as err:
            Rectangle.save_to_file(5)
        self.assertEqual("argument must be a list of instances",
                         str(err.exception))

        with self.assertRaises(TypeError) as err:
            Rectangle.save_to_file(False)
        self.assertEqual("argument must be a list of instances",
                         str(err.exception))

        with self.assertRaises(TypeError) as err:
            Rectangle.save_to_file("Diva")
        self.assertEqual("argument must be a list of instances",
                         str(err.exception))

        with self.assertRaises(TypeError) as err:
            Rectangle.save_to_file([4, 5])
        self.assertEqual(
            "argument must be a list of instances" + " who inherits of Base",
            str(err.exception))

        with self.assertRaises(TypeError) as err:
            Rectangle.save_to_file([True, False])
        self.assertEqual(
            "argument must be a list of instances" + " who inherits of Base",
            str(err.exception))

        with self.assertRaises(TypeError) as err:
            Rectangle.save_to_file(["sun", "moon", "star"])
        self.assertEqual(
            "argument must be a list of instances" + " who inherits of Base",
            str(err.exception))

        b6 = Base()
        b7 = Base()

        with self.assertRaises(TypeError) as err:
            Rectangle.save_to_file([b6, b7])
        self.assertEqual(
            "argument must be a list of instances" + " who inherits of Base",
            str(err.exception))

        self.assertEqual(Rectangle.from_json_string(None), [])
        self.assertEqual(Rectangle.from_json_string(""), [])

        l_1 = {'id': 89, 'width': 10, 'height': 4}

        l_2 = Rectangle.to_json_string([l_1])
        self.assertIsInstance(l_2, str)

        l_3 = Rectangle.from_json_string(l_2)
        self.assertIsInstance(l_3, list)

        with self.assertRaises(TypeError) as err:
            Rectangle.from_json_string(6)
        self.assertEqual("argument must be a JSON string", str(err.exception))

        with self.assertRaises(TypeError) as err:
            Rectangle.from_json_string([1, 2])
        self.assertEqual("argument must be a JSON string", str(err.exception))

        with self.assertRaises(TypeError) as err:
            Rectangle.from_json_string(True)
        self.assertEqual("argument must be a JSON string", str(err.exception))

        with self.assertRaises(TypeError) as err:
            Rectangle.from_json_string((2, 6))
        self.assertEqual("argument must be a JSON string", str(err.exception))

        r1 = Rectangle(3, 5, 1)
        r1_dict = r1.to_dictionary()
        r2 = Rectangle.create(**r1_dict)

        self.assertEqual(r2.__str__(), "[Rectangle] (7) 1/0 - 3/5")
        self.assertIsNot(r1, r2)

        with self.assertRaises(TypeError) as err:
            Rectangle.create(6)
        self.assertEqual(
            "create() takes 1 positional argument" + " but 2 were given",
            str(err.exception))

        with self.assertRaises(TypeError) as err:
            Rectangle.create(True)
        self.assertEqual(
            "create() takes 1 positional argument" + " but 2 were given",
            str(err.exception))

        with self.assertRaises(TypeError) as err:
            Rectangle.create("Hello")
        self.assertEqual(
            "create() takes 1 positional argument" + " but 2 were given",
            str(err.exception))

        with self.assertRaises(TypeError) as err:
            Rectangle.create(r1_dict)
        self.assertEqual(
            "create() takes 1 positional argument" + " but 2 were given",
            str(err.exception))

        r3 = Rectangle(10, 7, 2, 8)
        r4 = Rectangle(2, 4)
        l_in = [r3, r4]
        Rectangle.save_to_file(l_in)
        l_out = Rectangle.load_from_file()

        self.assertEqual(l_out[0].__str__(), "[Rectangle] (9) 2/8 - 10/7")
        self.assertEqual(l_out[1].__str__(), "[Rectangle] (10) 0/0 - 2/4")

        s1 = Square(5)
        s2 = Square(7, 9, 1)
        ls_in = [s1, s2]

        Square.save_to_file(ls_in)
        ls_out = Square.load_from_file()

        self.assertEqual(ls_out[0].__str__(), "[Square] (13) 0/0 - 5")
        self.assertEqual(ls_out[1].__str__(), "[Square] (14) 9/1 - 7")

        with self.assertRaises(NameError) as err:
            Diva.load_from_file()
        self.assertEqual("name 'Diva' is not defined", str(err.exception))
 def test_from_json_string_None(self):
     """Testing for empty list as a parameter"""
     lista = []
     json_list = Rectangle.to_json_string(lista)
     list_out = Rectangle.from_json_string(json_list)
     self.assertEqual(list_out, [])
 def test_from_json_string_emptty(self):
     """Testing for none as a parameter"""
     lista = None
     json_list = Rectangle.to_json_string(lista)
     list_out = Rectangle.from_json_string(json_list)
     self.assertEqual(list_out, [])
예제 #14
0
 def test_convert_empty_string(self):
     """Checks from_jon_string"""
     list_output = Rectangle.from_json_string("[]")
     self.assertEqual(list_output, [])
    def test_13_test_from_json(self):
        '''to_json_string() signature'''
        with self.assertRaises(TypeError) as e:
            Base.from_json_string()
        s = "from_json_string() missing 1 required positional argument: \
'json_string'"

        self.assertEqual(str(e.exception), s)

        self.assertEqual(Base.from_json_string(None), [])
        self.assertEqual(Base.from_json_string(""), [])

        s = '[{"x": 1, "y": 2, "width": 3, "id": 4, "height": 5}, \
{"x": 101, "y": 20123, "width": 312321, "id": 522244, "height": 34340}]'

        d = [{
            'x': 1,
            'y': 2,
            'width': 3,
            'id': 4,
            'height': 5
        }, {
            'x': 101,
            'y': 20123,
            'width': 312321,
            'id': 522244,
            'height': 34340
        }]
        self.assertEqual(Base.from_json_string(s), d)

        d = [{}, {}]
        s = '[{}, {}]'
        self.assertEqual(Base.from_json_string(s), d)
        d = [{}]
        s = '[{}]'
        self.assertEqual(Base.from_json_string(s), d)

        d = [{"python": 666}, {"cat": 123}, {"YO": 0}]
        s = '[{"python": 666}, {"cat": 123}, {"YO": 0}]'
        self.assertEqual(Base.from_json_string(s), d)

        d = [{"python": 666}]
        s = '[{"python": 666}]'
        self.assertEqual(Base.from_json_string(s), d)

        d = [{'x': 1, 'y': 2, 'width': 3, 'id': 4, 'height': 5}]
        s = '[{"x": 1, "y": 2, "width": 3, "id": 4, "height": 5}]'
        self.assertEqual(Base.from_json_string(s), d)

        d = [{
            'x': 101,
            'y': 20123,
            'width': 312321,
            'id': 522244,
            'height': 34340
        }]
        s = '[{"x": 101, "y": 20123, "width": 312321, "id": 522244, \
"height": 34340}]'

        self.assertEqual(Base.from_json_string(s), d)

        list_in = [{
            'id': 89,
            'width': 10,
            'height': 4
        }, {
            'id': 7,
            'width': 1,
            'height': 7
        }]
        list_out = Rectangle.from_json_string(
            Rectangle.to_json_string(list_in))
        self.assertEqual(list_in, list_out)
예제 #16
0
    def test_base(self):
        # test for class Base, task 0

        a = Base()
        b = Base()
        c = Base()
        d = Base(12)
        e = Base()

        self.assertEqual(a.id, 1)
        self.assertEqual(b.id, 2)
        self.assertEqual(c.id, 3)
        self.assertEqual(d.id, 12)
        self.assertEqual(e.id, 4)

        self.assertTrue(type(a), Base)
        self.assertIsInstance(a, Base)

        # base has a method to_json_string(list_dictionaries)
        # to_json_string devuelve un string

        rect = Rectangle(10, 7, 2, 8)
        dictionary = rect.to_dictionary()
        json_dictionary = Base.to_json_string([dictionary])

        self.assertTrue(type(dictionary) == dict)
        self.assertTrue(type(json_dictionary) == str)

        # base has a class method save_to_file
        # writes the JSON string representation of a list of objs to a file

        r1 = Rectangle(10, 7, 2, 8)
        r2 = Rectangle(2, 4)
        Rectangle.save_to_file([r1, r2])

        with open("Rectangle.json", "r") as file:
            readed = file.read()

        self.assertTrue(len(readed) > 0)

        s1 = Square(1, 7, 2)
        s2 = Square(2, 4)
        Square.save_to_file([s1, s2])

        with open("Square.json", "r") as file:
            readed = file.read()

        self.assertTrue(len(readed) > 0)

        os.remove("Rectangle.json")

        Rectangle.save_to_file(None)

        with open("Rectangle.json", "r") as file:
            readed = file.read()

        self.assertTrue(readed == '[]')

        os.remove("Rectangle.json")
        os.remove("Square.json")

        # base has a static method from_json_string(json_string)
        # from_json_string devuelve una lista

        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)

        list_output = Rectangle.from_json_string(None)

        self.assertTrue(type(list_input) == list)
        self.assertTrue(type(json_list_input) == str)
        self.assertTrue(type(list_output) == list)
        self.assertTrue(list_output == [])

        # base has a class method create(cls, **dictionary)
        # returns an object with all the attributes setted

        r1 = Rectangle(3, 5, 1)
        r1_dictionary = r1.to_dictionary()
        r2 = Rectangle.create(**r1_dictionary)
        self.assertTrue(r1.x == r2.x)
        self.assertTrue(r1.y == r2.y)
        self.assertTrue(r1.id == r2.id)
        self.assertTrue(r1.width == r2.width)
        self.assertTrue(r1.height == r2.height)
        self.assertFalse(r1 is r2)
        self.assertFalse(r1 == r2)
 def test_from_json_string_none_two(self):
     """ Tests if json_string is empty """
     json_object = Rectangle.from_json_string('')
     self.assertEqual(json_object, '[]')
예제 #18
0
 def test_from_json_stringwithNonearg(self):
     """This function tests the from_json_string func"""
     Base.reset_objects()
     list_output = Rectangle.from_json_string(None)
     self.assertEqual(list_output, [])
     self.assertEqual(type(list_output), list)
예제 #19
0
 def test2_jsonstr_to_dic(self):
     """ test json to dict """
     json_list_input2 = "[]"
     list_output2 = Rectangle.from_json_string(json_list_input2)
     self.assertEqual(list_output2, [])
#!/usr/bin/python3
""" 16-main """
from models.rectangle import Rectangle

if __name__ == "__main__":

    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)
    print("[{}] {}".format(type(list_input), list_input))
    print("[{}] {}".format(type(json_list_input), json_list_input))
    print("[{}] {}".format(type(list_output), list_output))
예제 #21
0
 def test4_jsonstr_to_dic(self):
     """ test json to dict """
     with self.assertRaises(ValueError):
         json_list_input4 = "Hello Python"
         list_output4 = Rectangle.from_json_string(json_list_input4)
         self.assertEqual(list_output4, "")
 def test_from_json_string_basic_rectangle(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_input, list_output)