def test_decodeNullBroken(self): input = "n" try: ultrajson.decode(input) assert False, "Expected exception!" except(ValueError): return assert False, "Wrong exception"
def test_decodeWithTrailingNonWhitespaces(self): try: input = "{}\n\t a" ultrajson.decode(input) except ValueError: pass else: assert False, "expected ValueError"
def test_decodeObjectDepthTooBig(self): input = '{' * (1024 * 1024) try: ultrajson.decode(input) assert False, "Expected exception!" except(ValueError): return assert False, "Wrong exception"
def test_decodeArrayUnmatchedBracketFail(self): input = "[]]" try: ultrajson.decode(input) except ValueError: pass else: assert False, "expected ValueError"
def test_decodeBrokenObjectStart(self): input = "{" try: ultrajson.decode(input) assert False, "Expected exception!" except(ValueError): return assert False, "Wrong exception"
def test_decodeArrayOnlyCommaFail(self): input = "[,]" try: ultrajson.decode(input) except ValueError: pass else: assert False, "expected ValueError"
def test_decodeVeryTooSmallValue(self): try: input = "-90223372036854775809" ultrajson.decode(input) except ValueError: pass else: assert False, "expected ValueError"
def test_decodeJibberish(self): input = "fdsa sda v9sa fdsa" try: ultrajson.decode(input) assert False, "Expected exception!" except(ValueError): return assert False, "Wrong exception"
def test_decodeVeryTooBigValue(self): try: input = "18446744073709551616" ultrajson.decode(input) except ValueError: pass else: assert False, "expected ValueError"
def test_decodeStringBadEscape(self): input = "\"TESTING\\\"" try: ultrajson.decode(input) assert False, "Expected exception!" except(ValueError): return assert False, "Wrong exception"
def test_decodeStringUnterminated(self): input = "\"TESTING" try: ultrajson.decode(input) assert False, "Expected exception!" except(ValueError): return assert False, "Wrong exception"
def test_decodeDictWithNoValue(self): input = "{{{{\"key\":}}}}" try: ultrajson.decode(input) assert False, "Expected exception!" except(ValueError): return assert False, "Wrong exception"
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))
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"
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"
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))
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 "))
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))
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))
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))
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))
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))
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))
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)
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)
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)
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)
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))
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)
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)