Add the pytest suite and fix the bugs it found

The suite takes its data from the vectors and fixtures: 3635 tests, 97.9% branch
coverage, and paperspec check and paperspec stress give the same results as before.
Fixed: receipts.verify_signature raised KeyError for a record without a format;
traces.DOC_TAG matched a tag followed by a line feed; paperspec load and paperspec check
left SQLite connections open; the site builder copied the output folder's subfolders into
itself.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B8cL4zN4XpaQF5cBtASzfq
This commit is contained in:
alexmandClaude committed 2026-10-19 04:32:44 -04:00
1 parent 30d310f64d
commit a8fc7237ae
76 files changed
+15564 -80

No files matched your search

+240
View File
@@ -0,0 +1,240 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2026 Scienting
#
# Source Code: https://git.scient.ing/papers/paperspec
# See the LICENSE.md file for full license terms.
"""Canonical JSON: the vectors, the refusals of spec/01 §1.1, and round-trip properties."""
import json
import math
import re
import pytest
from hypothesis import given, settings, strategies as st
from helpers_lib1 import parse_doubles, vectors
from paperspec import jcs
PROPERTIES = settings(derandomize=True, max_examples=200, database=None, deadline=None)
JSON_VALUES = st.recursive(
st.none()
| st.booleans()
| st.integers(min_value=-(2**53), max_value=2**53)
| st.floats(allow_nan=False, allow_infinity=False)
| st.text(),
lambda children: (
st.lists(children, max_size=4)
| st.dictionaries(st.text(), children, max_size=4)
),
max_leaves=12,
)
@pytest.mark.parametrize("case", vectors("canonical-json"))
def test_canonical_json_vector(case):
assert jcs.dumps(parse_doubles(case["input_json"])) == case["output"]
@pytest.mark.parametrize(
("number", "text"),
[
(0.0, "0"),
(1.0, "1"),
(100.0, "100"),
(1.5, "1.5"),
(-2.5, "-2.5"),
(0.1 + 0.2, "0.30000000000000004"),
(4.35, "4.35"),
(1e20, "100000000000000000000"),
(1e21, "1e+21"),
(1.2345678901234568e22, "1.2345678901234568e+22"),
(123456789012345680000.0, "123456789012345680000"),
(0.000001, "0.000001"),
(1e-7, "1e-7"),
(1.5e-7, "1.5e-7"),
(-2.5e-5, "-0.000025"),
(5e-324, "5e-324"),
(1.7976931348623157e308, "1.7976931348623157e+308"),
(2.0**53, "9007199254740992"),
(2.0**60, "1152921504606847000"),
],
)
def test_floats_are_written_as_ecmascript_writes_them(number, text):
assert jcs.dumps(number) == text
assert jcs.dumps([number, "x"]) == f'[{text},"x"]'
def test_negative_zero_is_written_as_zero():
assert jcs.dumps(-0.0) == "0"
assert jcs.dumps([-0.0, "x"]) == '[0,"x"]'
@pytest.mark.parametrize("number", [2**53, -(2**53), 0, -1, 12345])
def test_integers_in_the_exact_range_are_written_in_full(number):
assert jcs.dumps(number) == str(number)
@pytest.mark.parametrize("number", [2**53 + 1, -(2**53) - 1, 10**30])
def test_integers_beyond_the_exact_range_are_refused(number):
with pytest.raises(ValueError, match="integer outside the IEEE 754 exact range"):
jcs.dumps(number)
with pytest.raises(ValueError, match="integer outside the IEEE 754 exact range"):
jcs.dumps([1.5, number])
@pytest.mark.parametrize("number", [math.nan, math.inf, -math.inf])
def test_nan_and_infinity_have_no_canonical_form(number):
with pytest.raises(ValueError, match="NaN and Infinity are not JSON"):
jcs.dumps(number)
with pytest.raises(ValueError, match="NaN and Infinity are not JSON"):
jcs.dumps({"a": [number]})
@pytest.mark.parametrize(
"lone",
[chr(0xD800), chr(0xDFFF), "a" + chr(0xDC00) + "b"],
ids=["high", "low", "inside"],
)
def test_a_lone_surrogate_has_no_canonical_form(lone):
with pytest.raises(ValueError, match="lone surrogate"):
jcs.dumps(lone)
with pytest.raises(ValueError, match="lone surrogate"):
jcs.dumps({lone: 1})
with pytest.raises(ValueError, match="lone surrogate"):
jcs.dumps([1.5, lone])
with pytest.raises(ValueError, match="lone surrogate"):
jcs.dumpb([lone])
def test_a_surrogate_pair_held_as_two_code_points_is_two_lone_surrogates():
with pytest.raises(ValueError, match="lone surrogate"):
jcs.dumps(chr(0xD83D) + chr(0xDE00))
def test_object_keys_must_be_strings():
with pytest.raises(TypeError, match="object keys must be strings"):
jcs.dumps({1: "a"})
with pytest.raises(TypeError, match="object keys must be strings"):
jcs.dumps({"a": 1.5, 2: 3})
@pytest.mark.parametrize("value", [{1, 2}, b"bytes", object(), 1 + 2j])
def test_values_outside_json_are_refused(value):
with pytest.raises(TypeError, match="not JSON"):
jcs.dumps(value)
with pytest.raises(TypeError, match="not JSON"):
jcs.dumps([1.5, value])
def test_literals_and_containers():
assert jcs.dumps(None) == "null"
assert jcs.dumps(True) == "true"
assert jcs.dumps(False) == "false"
assert jcs.dumps([]) == "[]"
assert jcs.dumps({}) == "{}"
assert jcs.dumps((1, "a")) == '[1,"a"]'
assert jcs.dumps([1.5, True, None, False]) == "[1.5,true,null,false]"
assert jcs.dumps([1.5, (2, 3)]) == "[1.5,[2,3]]"
def test_members_are_sorted_by_utf16_code_units_not_code_points():
# U+FF5E sorts after the astral U+1F600 in code points, before it in UTF-16.
value = {"\U0001f600": 1, "\N{FULLWIDTH TILDE}": 2, "": 3}
assert jcs.dumps(value) == '{"\U0001f600":1,"":3,"\N{FULLWIDTH TILDE}":2}'
assert sorted(value) == ["", "\N{FULLWIDTH TILDE}", "\U0001f600"]
def test_members_are_sorted_in_nested_objects_and_with_floats_present():
value = {"b": {"z": 1, "a": 2.5}, "a": [{"y": 1, "x": 2}]}
assert jcs.dumps(value) == '{"a":[{"x":2,"y":1}],"b":{"a":2.5,"z":1}}'
def test_strings_use_the_short_escapes_and_lower_case_unicode_escapes():
s = '\b\t\n\f\r"\\\x01\x1f\x7f/\N{LINE SEPARATOR}'
expected = '"\\b\\t\\n\\f\\r\\"\\\\\\u0001\\u001f\x7f/\N{LINE SEPARATOR}"'
assert jcs.dumps(s) == expected
assert jcs.dumps([1.5, s]) == "[1.5," + expected + "]"
def test_non_ascii_text_is_written_as_it_is():
assert jcs.dumps("café \U0001f600") == '"café \U0001f600"'
assert jcs.dumpb("é") == b'"\xc3\xa9"'
def test_dumpb_is_the_utf8_of_dumps():
value = {"k": ["é", 1.25]}
assert jcs.dumpb(value) == jcs.dumps(value).encode("utf-8")
@given(JSON_VALUES)
@PROPERTIES
def test_canonical_text_parses_back_to_the_value(value):
assert parse_doubles(jcs.dumps(value)) == value
@given(JSON_VALUES)
@PROPERTIES
def test_canonical_json_is_a_fixed_point(value):
once = jcs.dumps(value)
assert jcs.dumps(parse_doubles(once)) == once
@given(st.text())
@PROPERTIES
def test_strings_agree_whichever_path_writes_them(s):
assert jcs.dumps([1.5, s]) == "[1.5," + jcs.dumps(s) + "]"
@given(st.dictionaries(st.text(), st.integers(min_value=0, max_value=9), max_size=6))
@PROPERTIES
def test_members_come_out_in_utf16_order(value):
keys = list(json.loads(jcs.dumps(value)))
assert keys == sorted(value, key=lambda k: k.encode("utf-16-be"))
@given(st.floats(allow_nan=False, allow_infinity=False))
@PROPERTIES
def test_a_float_is_written_in_the_shortest_form_that_reads_back_exactly(x):
text = jcs.dumps(x)
assert float(text) == x
assert re.fullmatch(r"-?(0|[1-9][0-9]*)(\.[0-9]+)?(e[+-][0-9]+)?", text)
@given(st.floats(min_value=-(2.0**53), max_value=2.0**53).filter(float.is_integer))
@PROPERTIES
def test_a_whole_float_is_written_like_an_integer(x):
assert jcs.dumps(x) == str(int(x))
def test_loads_line_reads_canonical_lines():
line = b'{"a":[1,2.5,null],"b":"\xc3\xa9"}'
assert jcs.loads_line(line) == {"a": [1, 2.5, None], "b": "é"}
@pytest.mark.parametrize(
"line",
[
b'{"b":1,"a":2}',
b'{"a": 1}',
b'{"a":1.0}',
b'{"a":1e2}',
b'{"a":"\\u00e9"}',
b'{"a":"\\/"}',
b" 1",
],
)
def test_loads_line_refuses_a_line_that_is_not_canonical(line):
with pytest.raises(ValueError, match="not canonical JSON"):
jcs.loads_line(line)
def test_loads_line_refuses_bytes_that_are_not_utf8():
with pytest.raises(UnicodeDecodeError):
jcs.loads_line(b'"\xff"')
def test_loads_line_refuses_text_that_is_not_json():
with pytest.raises(json.JSONDecodeError):
jcs.loads_line(b"{")