Exemplo n.º 1
0
 def test_dir_with_members(self):
     prop = Json({"__members__": 1})
     dir(prop)
     self.assertTrue("__members__" in prop.keys())
Exemplo n.º 2
0
class JsonTestCase(unittest.TestCase):
    def setUp(self):
        self.d = Json((("a", 2), (3, 4), ("d", {"d2": 4})))

    def testConstructor(self):
        self.assertEqual(Json('{"a":2,"4":"5"}'), {"a": 2, "4": "5"})

    def testBase(self):
        self.assertEqual(self.d.a, 2)
        self.assertEqual(self.d["a"], 2)
        self.assertEqual(self.d.b.c, {})
        self.assertEqual(self.d.d.d2, 4)
        self.assertIsInstance(self.d.keys(), slist)
        self.assertIsInstance(self.d.iterkeys(), stream)
        self.assertIsInstance(self.d.itervalues(), stream)

    def test_strReturnsSortedMap(self):
        self.assertEqual('{"4":3,"a":"4"}', str(Json({"a": "4", 4: 3})))

    def test_strBasics(self):
        self.assertEqual(json.dumps({"a": "4"}, separators=(",", ":")), str(Json({"a": "4"})))
        self.assertEqual(
            json.dumps(dict((("a", 2), (3, 4), ("d", {"d2": 4}))), separators=(",", ":"), sort_keys=True),
            str(Json((("a", 2), (3, 4), ("d", {"d2": 4})))),
        )
        self.assertEqual(
            json.dumps(dict((("a", 2), (3, 4), ("d", {"d2": 4}))), separators=(",", ":"), sort_keys=True), str(self.d)
        )

    def test_repr_from_dict(self):
        d = {"a": "a"}
        j = Json(d)
        self.assertEqual(repr(j), repr(d))

    def test_repr_used_setattr(self):
        j = Json()
        j.a = "a"
        self.assertEqual(repr(j), repr({"a": "a"}))

    def test_forbiden_attrs(self):
        j = Json()
        with self.assertRaises(AttributeError):
            j.__methods__()

    def testUpdateItems(self):
        d = Json((("a", 2), (3, 4), ("d", {"d2": 4})))
        d.d.d2 = 3
        self.assertEqual(d.d.d2, 3)

    def testSpecialKeys(self):
        d = Json((("__init__", 2), (3, 4), ("d", {"d2": 4})))
        self.assertEqual(d["__init__"], 2)
        self.assertNotEquals(d.__init__, 2)
        self.assertIsInstance(d.__init__, types.MethodType)

    def testIteritems(self):
        b = self.d.iteritems().toList()
        self.assertEqual(self.d.iterkeys().toList(), self.d.toList())
        self.assertEqual(b[2][1].d2, 4)
        self.assertIsInstance(b[2][1], Json)
        self.assertIsInstance(self.d.iteritems(), stream)
        self.assertEqual(self.d.iteritems().toList(), [("a", 2), (3, 4), ("d", {"d2": 4})])
        self.assertEqual(self.d.iteritems()[2][1].d2, 4)
        self.assertIsInstance(self.d.iteritems(), stream)
        self.assertEquals(self.d.iteritems().sorted().toList(), [(3, 4), ("a", 2), ("d", {"d2": 4})])
        self.assertEqual(Json({1: 1, 2: 2, 3: 3}).itervalues().sum(), 6)

    def testJsonList(self):
        jlist = Json({"a": [1, 2, {"b": [{"c": 3}, {"d": 4}]}]})
        self.assertEqual(jlist.a[2], {"b": [{"c": 3}, {"d": 4}]})
        self.assertEqual(jlist.a[2].b[1].d, 4)

    def testJsonSetValues(self):
        self.d.c = "set"
        self.assertEqual(self.d.c, "set")

    def test_toOrigNominal(self):
        j = Json()
        j.a = Json({"b": "c"})
        j.toString()
        j.toOrig()
        repr(j)
        d = j.toOrig()

        self.assertIsInstance(d, sdict)
        self.assertDictEqual(d, {"a": {"b": "c"}})

    def test_NoneValueRemainsNone(self):
        j = Json({"a": None})
        self.assertIs(j.a, None)

    def test_ConvertSetToList(self):
        j = Json()
        j.st = set((1, 2))
        d = j.toOrig()
        self.assertIsInstance(d, sdict)
        self.assertDictEqual({"st": set([1, 2])}, d)

    def test_serializeDeserialize(self):
        serialized = '{"command":"put","details":{"cookie":"cookie1","platform":"fb"}}'
        j = Json(serialized)
        self.assertEqual(serialized, j.toString())