Esempio n. 1
0
 def test_struct(self):
     stuff = Stuff(aStruct=SomeStruct(anInteger=12, aMap={"hi": 1.5}),
                   aListOfStruct=[
                       SomeStruct(anInteger=10, aMap={"good": 2.0}),
                       SomeStruct(anInteger=11, aMap={"bye": 1.0})
                   ],
                   anotherString="Hey")
     j = writeToJSON(stuff)
     stuff_read = readStuffFromJSON(j)
     self.assertEqual(len(stuff_read.aListOfStruct), 2)
     self.assertEqual(stuff_read.aListOfStruct[0].anInteger, 10)
     self.assertEqual(stuff_read.aListOfStruct[0].aMap["good"], 2.0)
     self.assertEqual(stuff_read.aListOfStruct[1].anInteger, 11)
     self.assertEqual(stuff_read.aListOfStruct[1].aMap["bye"], 1.0)
     self.assertEqual(stuff_read.anotherString, "Hey")
Esempio n. 2
0
 def test_unexpected_field(self):
     ss = SomeStruct(anInteger=1)
     j = '{ "anInteger": 101, "unexpected": 111.1}'
     struct_read = readStuffFromJSON(j, struct_type=SomeStruct)
     self.assertEqual(struct_read.anInteger, 101)
Esempio n. 3
0
    def test_skip_unrecognized_fields(self):
        stuff = Stuff(
            aString="hello",
            aShort=10,
            anInteger=23990,
            aLong=123456789012,
            aDouble=1234567.9,
            aBool=True,
            aBinary=b'\\"hello'.decode("utf-8"),
            aStruct=SomeStruct(anInteger=12, aMap={"hi": 1.5}),
            aList=[
                [[["hello", "world"], ["good", "bye"]]],
                [[["what", "is"], ["going", "on"]]],
            ],
            aMap={
                1: {"hello": [1, 2, 3, 4], "world": [5, 6, 7, 8]},
                2: {"good": [100, 200], "bye": [300, 400]},
            },
            anotherString="Hey",
            aListOfStruct=[
                SomeStruct(anInteger=10, aMap={"good": 2.0}),
                SomeStruct(anInteger=11, aMap={"bye": 1.0}),
            ],
            aListOfSet=[{"hello"}, {"world"}],
            aListOfDouble=[0.0, 1.25, 10.0],
            anotherMap={3: {"foo": 1, "bar": 12}, 4: {"baz": 123, "bazzz": 1234}},
        )

        j = writeToJSON(stuff)
        stuff_read = readStuffFromJSON(j, struct_type=StuffMissing)
        self.assertFalse(hasattr(stuff_read, "aString"))
        self.assertFalse(hasattr(stuff_read, "aShort"))
        self.assertEqual(stuff_read.anInteger, 23990)
        self.assertFalse(hasattr(stuff_read, "aLong"))
        self.assertEqual(stuff_read.aDouble, 1234567.9)
        self.assertFalse(hasattr(stuff_read, "aBool"))
        self.assertEqual(stuff_read.aBinary, b'\\"hello')

        self.assertFalse(hasattr(stuff_read, "aStruct"))

        self.assertEqual(len(stuff_read.aList), 2)
        self.assertEqual(stuff_read.aList[0][0][0], ["hello", "world"])
        self.assertEqual(stuff_read.aList[0][0][1], ["good", "bye"])
        self.assertEqual(stuff_read.aList[1][0][0], ["what", "is"])
        self.assertEqual(stuff_read.aList[1][0][1], ["going", "on"])

        self.assertFalse(hasattr(stuff_read, "aMap"))

        self.assertEqual(stuff_read.anotherString, "Hey")

        self.assertFalse(hasattr(stuff_read, "aListOfStruct"))

        self.assertEqual(len(stuff_read.aListOfSet), 2)
        self.assertEqual(stuff_read.aListOfSet[0], {"hello"})
        self.assertEqual(stuff_read.aListOfSet[1], {"world"})

        self.assertFalse(hasattr(stuff_read, "aListOfDouble"))

        self.assertEqual(len(stuff_read.anotherMap), 2)
        self.assertEqual(stuff_read.anotherMap[3]["foo"], 1)
        self.assertEqual(stuff_read.anotherMap[3]["foo"], 1)
        self.assertEqual(stuff_read.anotherMap[3]["bar"], 12)
        self.assertEqual(stuff_read.anotherMap[4]["baz"], 123)
        self.assertEqual(stuff_read.anotherMap[4]["bazzz"], 1234)

        stuff_read_2 = readStuffFromJSON(j, struct_type=Empty)
        self.assertFalse(hasattr(stuff_read_2, "aString"))
        self.assertFalse(hasattr(stuff_read_2, "aShort"))
        self.assertFalse(hasattr(stuff_read_2, "anInteger"))
        self.assertFalse(hasattr(stuff_read_2, "aLong"))
        self.assertFalse(hasattr(stuff_read_2, "aDouble"))
        self.assertFalse(hasattr(stuff_read_2, "aBool"))
        self.assertFalse(hasattr(stuff_read_2, "aBinary"))
        self.assertFalse(hasattr(stuff_read_2, "aStruct"))
        self.assertFalse(hasattr(stuff_read_2, "aList"))
        self.assertFalse(hasattr(stuff_read_2, "aMap"))
        self.assertFalse(hasattr(stuff_read_2, "anotherString"))
        self.assertFalse(hasattr(stuff_read_2, "aListOfStruct"))
        self.assertFalse(hasattr(stuff_read_2, "aListOfSet"))
        self.assertFalse(hasattr(stuff_read_2, "aListOfDouble"))
        self.assertFalse(hasattr(stuff_read_2, "anotherMap"))