예제 #1
0
def test_object_with_json_attribute_error():
    # If __json__ raises an error, make sure python actually raises it.
    class JSONTest:
        def __json__(self):
            raise AttributeError

    d = {"key": JSONTest()}
    with pytest.raises(AttributeError):
        sd_ujson.encode(d)
예제 #2
0
def test_object_with_json_type_error():
    # __json__ must return a string, otherwise it should raise an error.
    for return_value in (None, 1234, 12.34, True, {}):

        class JSONTest:
            def __json__(self):
                return return_value

        d = {"key": JSONTest()}
        with pytest.raises(TypeError):
            sd_ujson.encode(d)
예제 #3
0
def test_encode_null_character():
    test_input = "31337 \x00 1337"
    output = sd_ujson.encode(test_input)
    assert test_input == sdjson.loads(output)
    assert output == sdjson.dumps(test_input)
    assert test_input == sd_ujson.decode(output)

    test_input = "\x00"
    output = sd_ujson.encode(test_input)
    assert test_input == sdjson.loads(output)
    assert output == sdjson.dumps(test_input)
    assert test_input == sd_ujson.decode(output)

    assert '"  \\u0000\\r\\n "' == sd_ujson.dumps("  \u0000\r\n ")
예제 #4
0
def test_encode_to_utf8():
    test_input = b"\xe6\x97\xa5\xd1\x88"
    test_input = test_input.decode("utf-8")
    enc = sd_ujson.encode(test_input, ensure_ascii=False)
    dec = sd_ujson.decode(enc)
    assert enc == sdjson.dumps(test_input, ensure_ascii=False)
    assert dec == sdjson.loads(enc)
예제 #5
0
def test_double_long_decimal_issue():
    sut = {"a": -12345678901234.56789012}
    encoded = sdjson.dumps(sut)
    decoded = sdjson.loads(encoded)
    assert sut == decoded
    encoded = sd_ujson.encode(sut)
    decoded = sd_ujson.decode(encoded)
    assert sut == decoded
예제 #6
0
def test_double_long_issue():
    sut = {"a": -4342969734183514}
    encoded = sdjson.dumps(sut)
    decoded = sdjson.loads(encoded)
    assert sut == decoded
    encoded = sd_ujson.encode(sut)
    decoded = sd_ujson.decode(encoded)
    assert sut == decoded
예제 #7
0
 def helper(expected_output, **encode_kwargs):
     if "escape_forward_slashes" not in encode_kwargs:
         encode_kwargs["escape_forward_slashes"] = True,
     output = sd_ujson.encode(test_input, **encode_kwargs)
     assert output == expected_output
     if encode_kwargs.get("escape_forward_slashes", True):
         assert test_input == sdjson.loads(output)
         assert test_input == sd_ujson.decode(output)
예제 #8
0
def test_encode_recursion_max():
    # 8 is the max recursion depth
    class O2:
        member = 0

        def toDict(self):
            return {"member": self.member}

    class O1:
        member = 0

        def toDict(self):
            return {"member": self.member}

    test_input = O1()
    test_input.member = O2()
    test_input.member.member = test_input
    with pytest.raises(OverflowError):
        sd_ujson.encode(test_input)
예제 #9
0
def test_encode_long_neg_conversion():
    test_input = -9223372036854775808
    output = sd_ujson.encode(test_input)

    sdjson.loads(output)
    sd_ujson.decode(output)

    assert test_input == sdjson.loads(output)
    assert output == sdjson.dumps(test_input)
    assert test_input == sd_ujson.decode(output)
예제 #10
0
def test_object_with_json():
    # If __json__ returns a string, then that string
    # will be used as a raw JSON snippet in the object.
    output_text = "this is the correct output"

    class JSONTest:
        def __json__(self):
            return '"' + output_text + '"'

    d = {"key": JSONTest()}
    output = sd_ujson.encode(d)
    dec = sd_ujson.decode(output)
    assert dec == {"key": output_text}
예제 #11
0
def test_object_with_complex_json():
    # If __json__ returns a string, then that string
    # will be used as a raw JSON snippet in the object.
    obj = {"foo": ["bar", "baz"]}

    class JSONTest:
        def __json__(self):
            return sd_ujson.encode(obj)

    d = {"key": JSONTest()}
    output = sd_ujson.encode(d)
    dec = sd_ujson.decode(output)
    assert dec == {"key": obj}
예제 #12
0
def test_to_dict():
    d = {"key": 31337}

    class DictTest:
        def toDict(self):
            return d

        def __json__(self):
            return '"json defined"'  # Fallback and shouldn't be called.

    o = DictTest()
    output = sd_ujson.encode(o)
    dec = sd_ujson.decode(output)
    assert dec == d
예제 #13
0
def test_encode_raises(test_input, expected):
    with pytest.raises(expected):
        sd_ujson.encode(test_input)
예제 #14
0
def test_encode_long_conversion(test_input):
    output = sd_ujson.encode(test_input)

    assert test_input == sdjson.loads(output)
    assert output == sdjson.dumps(test_input)
    assert test_input == sd_ujson.decode(output)
예제 #15
0
def test_encode_array_of_doubles():
    test_input = [31337.31337, 31337.31337, 31337.31337, 31337.31337] * 10
    output = sd_ujson.encode(test_input)
    assert test_input == sdjson.loads(output)
    # assert output == json.dumps(test_input)
    assert test_input == sd_ujson.decode(output)
예제 #16
0
def test_encode_no_assert(test_input):
    sd_ujson.encode(test_input)
예제 #17
0
def test_encode(test_input, expected):
    assert sd_ujson.encode(test_input) == expected
예제 #18
0
def test_encode_control_escaping():
    test_input = "\x19"
    enc = sd_ujson.encode(test_input)
    dec = sd_ujson.decode(enc)
    assert test_input == dec
    assert enc == sdjson.dumps(test_input)
예제 #19
0
def test_encode_string_conversion2():
    test_input = "A string \\ / \b \f \n \r \t"
    output = sd_ujson.encode(test_input, escape_forward_slashes=True)
    assert test_input == sdjson.loads(output)
    assert output == '"A string \\\\ \\/ \\b \\f \\n \\r \\t"'
    assert test_input == sd_ujson.decode(output)
예제 #20
0
def test_encode_list_conversion():
    test_input = [1, 2, 3, 4]
    output = sd_ujson.encode(test_input)
    assert test_input == sdjson.loads(output)
    assert test_input == sd_ujson.decode(output)
예제 #21
0
 def __json__(self):
     return sd_ujson.encode(obj)
예제 #22
0
def test_encode_dict_conversion():
    test_input = {"k1": 1, "k2": 2, "k3": 3, "k4": 4}
    output = sd_ujson.encode(test_input)
    assert test_input == sdjson.loads(output)
    assert test_input == sd_ujson.decode(output)
    assert test_input == sd_ujson.decode(output)
예제 #23
0
def test_encode_array_of_nested_arrays():
    test_input = [[[[]]]] * 20
    output = sd_ujson.encode(test_input)
    assert test_input == sdjson.loads(output)
    # assert output == json.dumps(test_input)
    assert test_input == sd_ujson.decode(output)
예제 #24
0
def test_encode_decimal():
    sut = decimal.Decimal("1337.1337")
    encoded = sd_ujson.encode(sut)
    decoded = sd_ujson.decode(encoded)
    assert decoded == 1337.1337
예제 #25
0
def test_encode_decode(test_input):
    output = sd_ujson.encode(test_input)

    assert test_input == sdjson.loads(output)
    assert output == sdjson.dumps(test_input)
    assert test_input == sd_ujson.decode(output)
예제 #26
0
def test_encode_unicode_4_bytes_utf8_fail():
    test_input = b"\xfd\xbf\xbf\xbf\xbf\xbf"
    with pytest.raises(OverflowError):
        sd_ujson.encode(test_input)
예제 #27
0
def test_encode_unicode(test_input):
    enc = sd_ujson.encode(test_input)
    dec = sd_ujson.decode(enc)

    assert enc == sdjson.dumps(test_input)
    assert dec == sdjson.loads(enc)
예제 #28
0
def test_encode_big_escape():
    for x in range(10):
        base = "\u00e5".encode()
        test_input = base * 1024 * 1024 * 2
        sd_ujson.encode(test_input)
예제 #29
0
def test_decimal_decode_test():
    sut = {"a": 4.56}
    encoded = sd_ujson.encode(sut)
    decoded = sd_ujson.decode(encoded)
    assert_almost_equal(sut["a"], decoded["a"])
예제 #30
0
def test_encode_double_neg_conversion():
    test_input = -math.pi
    output = sd_ujson.encode(test_input)

    assert round(test_input, 5) == round(sdjson.loads(output), 5)
    assert round(test_input, 5) == round(sd_ujson.decode(output), 5)