import pytest
import json
import hyperjson
import string
from io import StringIO
simple_types = [1, 1.0, -1, None, "str", True, False]
@pytest.mark.parametrize("payload", simple_types)
def test_simple_types(payload):
assert json.dumps(payload) == hyperjson.dumps(payload)
def ignore_whitespace(a):
WHITE_MAP = dict.fromkeys(ord(c) for c in string.whitespace)
return a.translate(WHITE_MAP)
simple_dicts = [
({"a": 1, "b": 2}, ['{"a":1,"b":2}', '{"b":2,"a":1}']),
({1: "a", 2: "b"}, ['{"1":"a","2":"b"}', '{"2":"b","1":"a"}']),
]
@pytest.mark.parametrize("d,allowed", simple_dicts)
def test_simple_dicts(d, allowed):
actual = ignore_whitespace(hyperjson.dumps(d))
assert actual in allowed
complex_dicts = [
{"complex": [4, 5, 6]},
{"complex": [1, (23, 42)]}
]
@pytest.mark.parametrize("d", complex_dicts)
def test_complex_dicts(d):
assert ignore_whitespace(json.dumps(
d)) == ignore_whitespace(hyperjson.dumps(d))
def test_dict_of_arrays_of_dict_string_int_pairs():
payload = {
'9.865710069007799': [
{
'19.37384331792742': 315795
}
],
'5.076904844489237': [
{
'0.479301331636357': 460144
}
]
}
assert hyperjson.loads(hyperjson.dumps(payload)) == payload