import orjson
import pytest
from etoon import dumps
PAYLOAD = orjson.dumps({"t": [1, 2, 3], "k": "\x01"})
def _dumps_bytes():
from etoon._etoon import dumps_bytes
return dumps_bytes
def _all_defaults():
return {
"delimiter": ",",
"key_folding": False,
"flatten_depth": None,
"empty_array_bare": True,
"escape_controls": True,
"max_depth": 1000,
"max_input_bytes": 0,
}
def test_dumps_default_delimiter_is_comma():
x = {"t": [1, 2, 3]}
assert dumps(x) == dumps(x, delimiter=",")
def test_dumps_default_fold_keys_is_false():
x = {"a": {"b": 1}}
assert dumps(x) == dumps(x, fold_keys=False)
def test_dumps_default_flatten_depth_is_unlimited():
x = {"a": {"b": {"c": 1}}}
got = dumps(x, fold_keys=True)
assert got == dumps(x, fold_keys=True, flatten_depth=None)
assert got == "a.b.c: 1"
def test_dumps_default_empty_array_bare_is_true():
x = {"k": []}
assert dumps(x) == dumps(x, empty_array_bare=True)
def test_dumps_default_escape_controls_is_true():
x = {"k": "\x01"}
assert dumps(x) == dumps(x, escape_controls=True)
def test_dumps_default_max_depth_is_1000():
def nested(n):
return ('{"a":' * n + "1" + "}" * n).encode()
with pytest.raises(ValueError, match="max_depth"):
dumps(nested(1001))
assert isinstance(dumps(nested(1000)), str)
def test_dumps_default_max_input_bytes_is_unlimited():
big = b'{"k":"' + b"x" * 200_000 + b'"}'
assert isinstance(dumps(big), str)
def test_binding_no_kwargs_equals_all_explicit_defaults():
db = _dumps_bytes()
assert db(PAYLOAD) == db(PAYLOAD, **_all_defaults())
def test_binding_rust_default_delimiter():
db = _dumps_bytes()
assert db(PAYLOAD) == db(PAYLOAD, delimiter=",")
def test_binding_rust_default_escape_controls():
db = _dumps_bytes()
assert db(PAYLOAD) == db(PAYLOAD, escape_controls=True)
def test_binding_rust_default_max_input_bytes_disabled():
big = b'{"k":"' + b"x" * 200_000 + b'"}'
assert isinstance(_dumps_bytes()(big), type(_dumps_bytes()(big, max_input_bytes=0)))
def test_binding_rust_default_key_folding_is_false():
db = _dumps_bytes()
x = orjson.dumps({"a": {"b": 1}})
assert db(x) == db(x, key_folding=False) == "a:\n b: 1"
def test_binding_rust_default_empty_array_bare_is_true():
db = _dumps_bytes()
x = orjson.dumps({"k": []})
assert db(x) == db(x, empty_array_bare=True) == "k: []"
def test_binding_rust_default_max_depth_is_1000():
db = _dumps_bytes()
def nested(n):
return ('{"a":' * n + "1" + "}" * n).encode()
assert isinstance(db(nested(1000)), str)
with pytest.raises(ValueError, match="max_depth"):
db(nested(1001))
def test_binding_rejects_unknown_kwargs():
with pytest.raises(TypeError, match="unexpected keyword"):
_dumps_bytes()(b"{}", nonsense=True)