Example #1
0
 def test_json_string_good(self):
     """Test the function with good cases
     """
     base = Base(100)
     list_input = [{
         'id': 89,
         'width': 10,
         'height': 4
     }, {
         'id': 7,
         'width': 1,
         'height': 7
     }]
     my_json = base.to_json_string(list_input)
     self.assertEqual(base.from_json_string(my_json), [{
         'height': 4,
         'width': 10,
         'id': 89
     }, {
         'height': 7,
         'width': 1,
         'id': 7
     }])
    def test_from_json_string(self):
        """Tests regular from_json_string"""
        json_str = '[{"id": 5, "width": 12, "height": 14, "x": 4, "y": 10}, \
{"id": 7, "width": 1, "height": 18, "x": 3, "y": 7}]'

        json_l = Base.from_json_string(json_str)
        self.assertTrue(type(json_l) is list)
        self.assertEqual(len(json_l), 2)
        self.assertTrue(type(json_l[0]) is dict)
        self.assertTrue(type(json_l[1]) is dict)
        self.assertEqual(json_l[0], {
            "id": 5,
            "width": 12,
            "height": 14,
            "x": 4,
            "y": 10
        })
        self.assertEqual(json_l[1], {
            "id": 7,
            "width": 1,
            "height": 18,
            "x": 3,
            "y": 7
        })
    def test_from_json_string(self):
        """testing from Json string"""
        list_input = '[{"id": 89, "width": 10, "height": 4, "x": 7, "y": 9}, \
{"id": 2, "width": 2, "height": 3, "x": 4, "y": 0}]'

        json_l = Base.from_json_string(list_input)
        self.assertTrue(type(json_l) is list)
        self.assertEqual(len(json_l), 2)
        self.assertTrue(type(json_l[0]) is dict)
        self.assertTrue(type(json_l[1]) is dict)
        self.assertEqual(json_l[0], {
            "id": 89,
            "width": 10,
            "height": 4,
            "x": 7,
            "y": 9
        })
        self.assertEqual(json_l[1], {
            "id": 2,
            "width": 2,
            "height": 3,
            "x": 4,
            "y": 0
        })
    def test_from_json_string(self):
        """Test from_json_string method"""

        l1 = None
        l2 = []
        l3 = [[1, 2]]
        l4 = [{'a': 1}]
        l5 = [{'a': 'c'}]
        l6 = "string"
        s1 = Base.to_json_string(l1)
        s2 = Base.to_json_string(l2)
        s3 = Base.to_json_string(l3)
        s4 = Base.to_json_string(l4)
        s5 = Base.to_json_string(l5)
        s6 = Base.to_json_string(l6)
        self.assertEqual(Base.from_json_string(s1), [])
        self.assertEqual(Base.from_json_string(s2), l2)
        self.assertEqual(Base.from_json_string(s3), l3)
        self.assertEqual(Base.from_json_string(s4), l4)
        self.assertEqual(Base.from_json_string(s5), l5)
        self.assertEqual(Base.from_json_string(s6), l6)
Example #5
0
 def test_from_json_TE2(self):
     with self.assertRaises(TypeError):
         Base.from_json_string([], 10)
Example #6
0
 def test_from_json_string_none(self):
     """test from json string none"""
     lo = None
     jsonstr = Base.from_json_string(lo)
     self.assertEqual(jsonstr, None or [])
Example #7
0
 def test_from_json_empty(self):
     self.assertEqual([], Base.from_json_string("[]"))
Example #8
0
 def from_json_test(self):
     """ from_json method test """
     self.assertEqual(Base.from_json_string(None), '[]')
     self.assertEqual(Base.from_json_string([]), '[]')
 def test_json_empty_dict(self):
     """Test json with empty dict"""
     list_input = [{}]
     json_list_input = Base.to_json_string(list_input)
     listob = Base.from_json_string(json_list_input)
     self.assertEqual(listob, [{}])
 def test_from_json_empty(self):
     """ Checking from_json_string is empty """
     test = Base.from_json_string("[]")
     self.assertEqual(test, [])
 def test_from_json_returns(self):
     """ Checking from_json_string returns """
     test = Base.from_json_string('[{"id": 89}]')
     self.assertIs(type(test), list)
Example #12
0
    def test_08_from_json_str(self):
        """Checks the JSON string representation in a file
        """
        with self.assertRaises(TypeError):
            Base.from_json_string([])
        with self.assertRaises(TypeError):
            Base.from_json_string(())
        with self.assertRaises(TypeError):
            Base.from_json_string({})
        with self.assertRaises(TypeError):
            Base.from_json_string(True)
        with self.assertRaises(TypeError):
            Base.from_json_string(2.5)
        with self.assertRaises(TypeError):
            Rectangle.from_json_string([])
        with self.assertRaises(TypeError):
            Rectangle.from_json_string(())
        with self.assertRaises(TypeError):
            Rectangle.from_json_string({})
        with self.assertRaises(TypeError):
            Rectangle.from_json_string(True)
        with self.assertRaises(TypeError):
            Rectangle.from_json_string(2.5)
        with self.assertRaises(TypeError):
            Square.from_json_string([])
        with self.assertRaises(TypeError):
            Square.from_json_string(())
        with self.assertRaises(TypeError):
            Square.from_json_string({})
        with self.assertRaises(TypeError):
            Square.from_json_string(True)
        with self.assertRaises(TypeError):
            Square.from_json_string(2.5)

        json_list = Base.from_json_string(None)
        self.assertEqual(json_list, [])
        json_list = Base.from_json_string("")
        self.assertEqual(json_list, [])
        json_list = Base.from_json_string('["hi"]')
        self.assertEqual(json_list, ["hi"])
        json_list = Rectangle.from_json_string('[{"hi": "world"}]')
        self.assertEqual(json_list, [{'hi': 'world'}])
        json_list = Square.from_json_string('[]')
        self.assertEqual(json_list, [])
 def test_from_json_string_no_args(self):
     with self.assertRaises(TypeError):
         Base.from_json_string()
Example #14
0
 def test_from_json_string_no_arg(self):
     with self.assertRaises(TypeError) as e:
         list_output = Base.from_json_string()
     self.assertEqual(
         "from_json_string() missing 1 required positional " +
         "argument: 'json_string'", str(e.exception))
Example #15
0
 def test_from_json_string_non_dicts(self):
     list_input = [{}, {}]
     json_list_input = Base.to_json_string(list_input)
     list_output = Base.from_json_string(json_list_input)
     self.assertEqual(list_output, [{}, {}])
Example #16
0
 def test_from_json_string_empty_string(self):
     list_output = Base.from_json_string("")
     self.assertEqual(list_output, [])
Example #17
0
 def test_from_json_string_None(self):
     list_output = Base.from_json_string(None)
     self.assertEqual(list_output, [])
Example #18
0
    def test_from_json_string(self):
        """Tests 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 = [{"foobarrooo": 989898}, {"abc": 123}, {"HI": 0}]
        s = '[{"foobarrooo": 989898}, {"abc": 123}, {"HI": 0}]'
        self.assertEqual(Base.from_json_string(s), d)

        d = [{"foobarrooo": 989898}]
        s = '[{"foobarrooo": 989898}]'
        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)
 def test_id_emp(self):
     """ test empty instance """
     self.assertEqual(Base.from_json_string(None), [])
 def test_from_json_string_more_than_one_arg(self):
     with self.assertRaises(TypeError):
         Base.from_json_string([], 1)
 def test_from_json_None(self):
     """ Checking from_json_string is None """
     test = Base.from_json_string(None)
     self.assertEqual(test, [])
Example #22
0
 def test_from_json_string(self):
     """test conversion of json string to python object"""
     obj = [{'a': 1}]
     json_string = Base.to_json_string(obj)
     self.assertEqual(Base.from_json_string(json_string), obj)
 def test_from_json_works(self):
     """ Checking from_json_string works """
     test = Base.from_json_string('[{"id": 89}]')
     self.assertEqual(test, [{"id": 89}])
Example #24
0
    def test_base_from_json_string(self):
        li = [{
            'id': 89,
            'width': 10,
            'height': 4
        }, {
            'id': 7,
            'width': 1,
            'height': 7
        }]
        json = Rectangle.to_json_string(li)
        self.assertEqual(Rectangle.from_json_string(json), li)
        self.assertEqual(Base.from_json_string(""), [])
        self.assertEqual(Base.from_json_string(None), [])

        json = '[{"id": 23}, {"id": -23}, {"id": 45}, {"id": {"id": []}}]'
        li = [{'id': 23}, {'id': -23}, {'id': 45}, {'id': {'id': []}}]
        self.assertEqual(Base.from_json_string(json), li)

        li = [{
            'x': 2,
            'width': 1,
            'id': 41,
            'height': 57,
            'y': 1
        }, {
            'x': 12,
            'width': 210,
            'id': 31,
            'height': 57,
            'y': 0
        }, {
            'x': 23,
            'width': 40,
            'id': 11,
            'height': 8,
            "y": 5
        }]
        json = Base.to_json_string(li)
        self.assertEqual(Base.from_json_string(json), li)

        li = [{
            'x': 2,
            'size': 1,
            'id': 41,
            'y': 1
        }, {
            'x': 12,
            'size': 210,
            'id': 31,
            'y': 0
        }, {
            'x': 23,
            'size': 40,
            'id': 11,
            'y': 5
        }]
        json = Base.to_json_string(li)
        self.assertEqual(Base.from_json_string(json), li)

        r1 = Rectangle(2, 1)
        s1 = Square(3)
        s2 = Square(2)
        li = [r1.to_dictionary()]
        json = Rectangle.to_json_string(li)
        self.assertEqual(Rectangle.from_json_string(json), li)

        li = [s1.to_dictionary(), s2.to_dictionary()]
        json = Square.to_json_string(li)
        self.assertEqual(Square.from_json_string(json), li)

        li = [r1.to_dictionary(), s1.to_dictionary(), s2.to_dictionary()]
        json = Base.to_json_string(li)
        self.assertEqual(Base.from_json_string(json), li)

        with self.assertRaises(TypeError):
            Base.from_json_string()
            Base.from_json_string(None)
            Base.from_json_string(1, 1, 1)
 def test_from_json_string_empty(self):
     """Test to check if it works with empty string or none"""
     self.assertEqual(Base.from_json_string(""), [])
     self.assertEqual(Base.from_json_string(None), [])
 def test_from_json_string_list(self):
     """tests for list from_json_method."""
     with self.assertRaises(TypeError) as e:
         Base.from_json_string([1, 2, 3])
     self.assertEqual("the JSON object must be str, not 'list'",
                      str(e.exception))
 def test_json_string_error(self):
     """Test for from_json_method with int."""
     with self.assertRaises(TypeError):
         Base.from_json_string(39)
Example #28
0
 def test_from_json_none(self):
     self.assertEqual([], Base.from_json_string(None))
 def test_from_string_set(self):
     """tests function from json"""
     with self.assertRaises(TypeError) as e:
         Base.from_json_string({1, 2})
     self.assertEqual("the JSON object must be str, not 'set'",
                      str(e.exception))
Example #30
0
 def test_from_json(self):
     """ test from_json_string method """
     self.assertEqual(Base.from_json_string([]), [])
     self.assertEqual(Base.from_json_string(None), [])