示例#1
0
    def test_to_json_when_is_passed_object_then_return_string_of_his_json_representation(
            self):
        expr = Panda(name='Ivo', age=20)
        expected_result = """{
    "dict": {
        "age": 20,
        "name": "Ivo"
    },
    "type": "Panda"
}"""
        # expected_result = ''
        self.assertEqual(expr.to_json(), expected_result)
示例#2
0
    def test_when_to_json_then_return_json_representation_of_object(self):
        expected_result = '''{
    "type": "Panda",
    "dict": {
        "name": "Ivo",
        "age": 23,
        "food": "bamboo",
        "weight": 67
    }
}'''
        test_panda = Panda(name='Ivo', age=23, food='bamboo', weight=67)
        self.assertEqual(test_panda.to_json(), expected_result)
示例#3
0
    def test_from_json_when_is_passed_json_string_then_return_class_object(
            self):
        expected_result = Panda(name='Ivo', age=20)
        expr = """{
    "dict": {
        "age": 20,
        "name": "Ivo"
    },
    "type": "Panda"
}"""

        self.assertEqual(Panda.from_json(expr), expected_result)
示例#4
0
    def test_when_from_json_then_instace_object_from_json_string(self):
        test_json_string = '''{
    "type": "Panda",
    "dict": {
        "name": "Tony",
        "age": 23,
        "food": "bamboo",
        "weight": 67
    }
}'''
        expected_result = Panda(name='Tony', age=23, food='bamboo', weight=67)
        self.assertEqual(Panda.from_json(test_json_string), expected_result)
示例#5
0
class MixinsTest(unittest.TestCase):
    def setUp(self):
        self.panda_name = 'AaA'
        self.test_panda = Panda(name=self.panda_name)
        self.test_person = Person(name=self.panda_name)

    def test_to_json(self):
        expected_value = """{
    "dict": {
        "name": "AaA"
    },
    "class_name": "Panda"
}"""
        result = self.test_panda.to_json(indent=4)
        self.assertEqual(expected_value, result)

    def test_to_xml(self):
        expected_value = f"<Panda><name>{self.panda_name}</name></Panda>"
        result = self.test_panda.to_xml()
        self.assertCountEqual(expected_value, result)

    def test_to_json_from_json(self):
        """ Convert the panda object to a json and create a new panda object from that json"""
        json_panda = self.test_panda.to_json()
        new_panda = self.test_panda.from_json(json_panda)
        self.assertEqual(self.test_panda, new_panda)

    def test_to_xml_from_xml(self):
        """ Conver the Panda object to an XML and create the new Panda object from that XML"""
        xml_panda = self.test_panda.to_xml()
        new_panda = self.test_panda.from_xml(xml_panda)
        self.assertEqual(self.test_panda, new_panda)

    def test_panda_to_xml_from_person_xml(self):
        """ Try to create a Panda object from a Person XML"""
        xml_person = self.test_person.to_xml()
        with self.assertRaises(ValueError):
            self.test_panda.from_xml(xml_person)

    def test_panda_to_json_from_person_json(self):
        """ Try to create a Panda object from a Person JSON """
        json_person = self.test_person.to_json()
        with self.assertRaises(ValueError):
            self.test_panda.from_json(json_person)
示例#6
0
 def test_when_from_xml_then_instace_object_from_xml_string(self):
     expected_result = Panda(name='Tony', age=23, food='bamboo', weight=67)
     test_xml_string = '<Panda><name>Tony</name><age>23</age><food>bamboo</food><weight>67</weight></Panda>'
     self.assertEqual(Panda.from_xml(test_xml_string), expected_result)
示例#7
0
 def test_when_to_xml_then_return_xml_representation_of_object(self):
     expected_result = '<Panda><name>Tony</name><age>23</age><food>bamboo</food><weight>67</weight></Panda>'
     test_panda = Panda(name='Tony', age=23, food='bamboo', weight=67)
     self.assertEqual(test_panda.to_xml(), expected_result)
示例#8
0
 def setUp(self):
     self.panda_name = 'AaA'
     self.test_panda = Panda(name=self.panda_name)
     self.test_person = Person(name=self.panda_name)