示例#1
0
 def test_decodeNullBroken(self):
     input = "n"
     try:
         ultrajson.decode(input)
         assert False, "Expected exception!"
     except(ValueError):
         return
     assert False, "Wrong exception"
示例#2
0
 def test_decodeWithTrailingNonWhitespaces(self):
     try:
         input = "{}\n\t a"
         ultrajson.decode(input)
     except ValueError:
         pass
     else:
         assert False, "expected ValueError"
示例#3
0
 def test_decodeObjectDepthTooBig(self):
     input = '{' * (1024 * 1024)
     try:
         ultrajson.decode(input)
         assert False, "Expected exception!"
     except(ValueError):
         return
     assert False, "Wrong exception"
示例#4
0
 def test_decodeArrayUnmatchedBracketFail(self):
     input = "[]]"
     try:
         ultrajson.decode(input)
     except ValueError:
         pass
     else:
         assert False, "expected ValueError"
示例#5
0
 def test_decodeBrokenObjectStart(self):
     input = "{"
     try:
         ultrajson.decode(input)
         assert False, "Expected exception!"
     except(ValueError):
         return
     assert False, "Wrong exception"
示例#6
0
 def test_decodeArrayOnlyCommaFail(self):
     input = "[,]"
     try:
         ultrajson.decode(input)
     except ValueError:
         pass
     else:
         assert False, "expected ValueError"
示例#7
0
 def test_decodeVeryTooSmallValue(self):
     try:
         input = "-90223372036854775809"
         ultrajson.decode(input)
     except ValueError:
         pass
     else:
         assert False, "expected ValueError"
示例#8
0
 def test_decodeJibberish(self):
     input = "fdsa sda v9sa fdsa"
     try:
         ultrajson.decode(input)
         assert False, "Expected exception!"
     except(ValueError):
         return
     assert False, "Wrong exception"
示例#9
0
 def test_decodeVeryTooBigValue(self):
     try:
         input = "18446744073709551616"
         ultrajson.decode(input)
     except ValueError:
         pass
     else:
         assert False, "expected ValueError"
示例#10
0
 def test_decodeStringBadEscape(self):
     input = "\"TESTING\\\""
     try:
         ultrajson.decode(input)
         assert False, "Expected exception!"
     except(ValueError):
         return
     assert False, "Wrong exception"
示例#11
0
 def test_decodeStringUnterminated(self):
     input = "\"TESTING"
     try:
         ultrajson.decode(input)
         assert False, "Expected exception!"
     except(ValueError):
         return
     assert False, "Wrong exception"
示例#12
0
    def test_decodeDictWithNoValue(self):
        input = "{{{{\"key\":}}}}"
        try:
            ultrajson.decode(input)
            assert False, "Expected exception!"
        except(ValueError):
            return

        assert False, "Wrong exception"
示例#13
0
    def test_encodeLongNegConversion(self):
        input = -9223372036854775808
        output = ultrajson.encode(input)

        outputjson = json.loads(output)
        outputultrajson = ultrajson.decode(output)

        self.assertEqual(input, json.loads(output))
        self.assertEqual(output, json.dumps(input))
        self.assertEqual(input, ultrajson.decode(output))
示例#14
0
    def test_decodeBrokenDictKeyTypeLeakTest(self):
        input = '{{1337:""}}'
        for x in range(1000):
            try:
                ultrajson.decode(input)
                assert False, "Expected exception!"
            except(ValueError) as e:
                continue

            assert False, "Wrong exception"
示例#15
0
    def test_decodeBrokenListLeakTest(self):
        input = '[[[true'
        for x in range(1000):
            try:
                ultrajson.decode(input)
                assert False, "Expected exception!"
            except(ValueError):
                continue

            assert False, "Wrong exception"
示例#16
0
    def test_doublePrecisionTest(self):
        input = 30.012345678901234
        output = ultrajson.encode(input, double_precision = 15)
        self.assertEqual(input, json.loads(output))
        self.assertEqual(input, ultrajson.decode(output))

        output = ultrajson.encode(input, double_precision = 9)
        self.assertEqual(round(input, 9), json.loads(output))
        self.assertEqual(round(input, 9), ultrajson.decode(output))

        output = ultrajson.encode(input, double_precision = 3)
        self.assertEqual(round(input, 3), json.loads(output))
        self.assertEqual(round(input, 3), ultrajson.decode(output))
示例#17
0
    def test_encodeNullCharacter(self):
        input = "31337 \x00 1337"
        output = ultrajson.encode(input)
        self.assertEqual(input, json.loads(output))
        self.assertEqual(output, json.dumps(input))
        self.assertEqual(input, ultrajson.decode(output))

        input = "\x00"
        output = ultrajson.encode(input)
        self.assertEqual(input, json.loads(output))
        self.assertEqual(output, json.dumps(input))
        self.assertEqual(input, ultrajson.decode(output))

        self.assertEqual('"  \\u0000\\r\\n "', ultrajson.dumps("  \u0000\r\n "))
示例#18
0
    def test_encodeUnicode4BytesUTF8(self):
        input = "\xf0\x91\x80\xb0TRAILINGNORMAL"
        enc = ultrajson.encode(input)
        dec = ultrajson.decode(enc)

        self.assertEqual(enc, json_unicode(input))
        self.assertEqual(dec, json.loads(enc))
示例#19
0
    def test_encodeArrayInArray(self):
        input = [[[[]]]]
        output = ultrajson.encode(input)

        self.assertEqual(input, json.loads(output))
        self.assertEqual(output, json.dumps(input))
        self.assertEqual(input, ultrajson.decode(output))
示例#20
0
    def test_encodeLongUnsignedConversion(self):
        input = 18446744073709551615
        output = ultrajson.encode(input)

        self.assertEqual(input, json.loads(output))
        self.assertEqual(output, json.dumps(input))
        self.assertEqual(input, ultrajson.decode(output))
示例#21
0
 def test_encodeDatetimeConversion(self):
     ts = time.time()
     input = datetime.datetime.fromtimestamp(ts)
     output = ultrajson.encode(input)
     expected = calendar.timegm(input.utctimetuple())
     self.assertEqual(int(expected), json.loads(output))
     self.assertEqual(int(expected), ultrajson.decode(output))
示例#22
0
    def test_encodeUnicodeSurrogatePair(self):
        input = "\xf0\x90\x8d\x86"
        enc = ultrajson.encode(input)
        dec = ultrajson.decode(enc)

        self.assertEqual(enc, json_unicode(input))
        self.assertEqual(dec, json.loads(enc))
示例#23
0
    def test_encodeUnicode4BytesUTF8Highest(self):
        input = "\xf3\xbf\xbf\xbfTRAILINGNORMAL"
        enc = ultrajson.encode(input)
        dec = ultrajson.decode(enc)

        self.assertEqual(enc, json_unicode(input))
        self.assertEqual(dec, json.loads(enc))
示例#24
0
    def test_encodeSet(self):
        s = set([1,2,3,4,5,6,7,8,9])
        enc = ultrajson.encode(s)
        dec = ultrajson.decode(enc)

        for v in dec:
            self.assertTrue(v in s)
示例#25
0
    def test_invalidDoublePrecision(self):
        input = 30.12345678901234567890
        output = ultrajson.encode(input, double_precision = 20)
        # should snap to the max, which is 15
        self.assertEqual(round(input, 15), json.loads(output))
        self.assertEqual(round(input, 15), ultrajson.decode(output))

        output = ultrajson.encode(input, double_precision = -1)
        # also should snap to the max, which is 15
        self.assertEqual(round(input, 15), json.loads(output))
        self.assertEqual(round(input, 15), ultrajson.decode(output))

        # will throw typeError
        self.assertRaises(TypeError, ultrajson.encode, input, double_precision = '9')
        # will throw typeError
        self.assertRaises(TypeError, ultrajson.encode, input, double_precision = None)
示例#26
0
 def test_doubleLongIssue(self):
     sut = {'a': -4342969734183514}
     encoded = json.dumps(sut)
     decoded = json.loads(encoded)
     self.assertEqual(sut, decoded)
     encoded = ultrajson.encode(sut, double_precision=100)
     decoded = ultrajson.decode(encoded)
     self.assertEqual(sut, decoded)
示例#27
0
 def test_doubleLongDecimalIssue(self):
     sut = {'a': -12345678901234.56789012}
     encoded = json.dumps(sut)
     decoded = json.loads(encoded)
     self.assertEqual(sut, decoded)
     encoded = ultrajson.encode(sut, double_precision=100)
     decoded = ultrajson.decode(encoded)
     self.assertEqual(sut, decoded)
示例#28
0
    def test_encodeDateConversion(self):
        ts = time.time()
        input = datetime.date.fromtimestamp(ts)

        output = ultrajson.encode(input)
        tup = ( input.year, input.month, input.day, 0, 0, 0 )

        expected = calendar.timegm(tup)
        self.assertEqual(int(expected), json.loads(output))
        self.assertEqual(int(expected), ultrajson.decode(output))
示例#29
0
 def test_decodeBigEscape(self):
     for x in range(10):
         if PY3:
             base = '\\u00e5'.encode('utf-8')
             quote = "\"".encode()
         else:
             base = "\xc3\xa5"
             quote = "\""
         input = quote + (base * 1024 * 1024 * 2) + quote
         output = ultrajson.decode(input)
示例#30
0
    def test_toDict(self):
        d = {"key": 31337}

        class DictTest:
            def toDict(self):
                return d

        o = DictTest()
        output = ultrajson.encode(o)
        dec = ultrajson.decode(output)
        self.assertEqual(dec, d)