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:
76 files changed
+15564
-80
No files matched your search
@@ -0,0 +1,284 @@
|
||||
# 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.
|
||||
|
||||
"""Whole-document sentence alignment (spec/05 §5.6)."""
|
||||
|
||||
import pytest
|
||||
from hypothesis import given, settings, strategies as st
|
||||
|
||||
from helpers_lib1 import vectors
|
||||
from paperspec import align
|
||||
from paperspec.hashing import OBJ, lossy_from_strict
|
||||
|
||||
PROPERTIES = settings(derandomize=True, max_examples=150, database=None, deadline=None)
|
||||
|
||||
LONG = [
|
||||
"The first long sentence about bond constants.",
|
||||
"A second sentence describing the fitting procedure.",
|
||||
"The third sentence reports how charges were derived.",
|
||||
"Another sentence that covers the validation set used.",
|
||||
"Finally this sentence closes the paragraph for good.",
|
||||
]
|
||||
|
||||
|
||||
def item(ident, text, container="p1", context="paragraph", complex_keys=()):
|
||||
"""A reading item whose strict text is `text`."""
|
||||
return {
|
||||
"id": ident,
|
||||
"strict": text,
|
||||
"lossy": lossy_from_strict(text),
|
||||
"complex": list(complex_keys),
|
||||
"container": container,
|
||||
"context": context,
|
||||
}
|
||||
|
||||
|
||||
def reading(prefix, texts, **kw):
|
||||
"""A reading of one paragraph from texts, with IDs `<prefix>1`, `<prefix>2`, ..."""
|
||||
return [item(f"{prefix}{i}", t, **kw) for i, t in enumerate(texts, 1)]
|
||||
|
||||
|
||||
def pairs_of(result):
|
||||
return [(p["a"], p["b"]) for p in result["pairs"]]
|
||||
|
||||
|
||||
def kinds_of(result):
|
||||
return [(s["kind"], s["a"], s["b"]) for s in result["suspects"]]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("case", vectors("alignment"))
|
||||
def test_alignment_vector(case):
|
||||
assert align.align(case["a"], case["b"]) == case["expect"]
|
||||
|
||||
|
||||
def test_identical_readings_agree_everywhere():
|
||||
a = reading("a", LONG)
|
||||
result = align.align(a, reading("b", LONG))
|
||||
assert pairs_of(result) == [(f"a{i}", f"b{i}") for i in range(1, 6)]
|
||||
assert all(p["agree"] for p in result["pairs"])
|
||||
assert result["suspects"] == []
|
||||
|
||||
|
||||
def test_two_empty_readings_give_nothing():
|
||||
assert align.align([], []) == {"pairs": [], "suspects": []}
|
||||
|
||||
|
||||
def test_a_sentence_only_in_the_first_reading_is_dropped():
|
||||
result = align.align(reading("a", LONG), reading("b", LONG[:2] + LONG[3:]))
|
||||
assert kinds_of(result) == [("dropped", "a3", None)]
|
||||
assert len(result["pairs"]) == 4
|
||||
|
||||
|
||||
def test_a_sentence_only_in_the_second_reading_is_added():
|
||||
result = align.align(reading("a", LONG[:2] + LONG[3:]), reading("b", LONG))
|
||||
assert kinds_of(result) == [("added", None, "b3")]
|
||||
|
||||
|
||||
def test_everything_is_dropped_or_added_when_nothing_is_shared():
|
||||
a = reading("a", ["Alpha beta gamma delta epsilon zeta eta."])
|
||||
b = reading("b", ["Completely unrelated words fill this row."])
|
||||
result = align.align(a, b)
|
||||
assert result["pairs"] == []
|
||||
assert kinds_of(result) == [("dropped", "a1", None), ("added", None, "b1")]
|
||||
|
||||
|
||||
def test_a_changed_number_is_a_number_suspect():
|
||||
a = reading("a", ["The fitted rate is 103 per second at room temperature."])
|
||||
b = reading("b", ["The fitted rate is 108 per second at room temperature."])
|
||||
result = align.align(a, b)
|
||||
assert result["pairs"] == [{"a": "a1", "b": "b1", "agree": False}]
|
||||
assert kinds_of(result) == [("number", "a1", "b1")]
|
||||
|
||||
|
||||
def test_a_changed_word_is_a_word_suspect():
|
||||
a = reading("a", ["The solvent was heated slowly to the boiling point."])
|
||||
b = reading("b", ["The solvent was heated quickly to the boiling point."])
|
||||
assert kinds_of(align.align(a, b)) == [("word", "a1", "b1")]
|
||||
|
||||
|
||||
def test_a_changed_equation_with_the_same_text_is_an_equation_suspect():
|
||||
text = f"where {OBJ} is the bond length in the model."
|
||||
a = [item("a1", text, complex_keys=["tex:\\frac{a}{b}"])]
|
||||
b = [item("b1", text, complex_keys=["tex:\\frac{c}{d}"])]
|
||||
result = align.align(a, b)
|
||||
assert result["pairs"] == [{"a": "a1", "b": "b1", "agree": False}]
|
||||
assert kinds_of(result) == [("equation", "a1", "b1")]
|
||||
|
||||
|
||||
def test_a_changed_context_is_a_structure_suspect_even_when_the_text_agrees():
|
||||
a = reading("a", LONG[:1], context="paragraph")
|
||||
b = reading("b", LONG[:1], context="heading")
|
||||
result = align.align(a, b)
|
||||
assert result["pairs"][0]["agree"] is True
|
||||
assert kinds_of(result) == [("structure", "a1", "b1")]
|
||||
|
||||
|
||||
def test_a_paragraph_break_that_appears_or_vanishes_is_a_join_suspect():
|
||||
a = [item("a1", LONG[0], "p1"), item("a2", LONG[1], "p1")]
|
||||
b = [item("b1", LONG[0], "q1"), item("b2", LONG[1], "q2")]
|
||||
assert kinds_of(align.align(a, b)) == [("join", "a2", "b2")]
|
||||
assert kinds_of(align.align(b, a)) == [("join", "b2", "a2")]
|
||||
|
||||
|
||||
def test_neighbours_in_matching_containers_are_not_a_join():
|
||||
a = [item("a1", LONG[0], "p1"), item("a2", LONG[1], "p2")]
|
||||
b = [item("b1", LONG[0], "q1"), item("b2", LONG[1], "q2")]
|
||||
assert kinds_of(align.align(a, b)) == []
|
||||
|
||||
|
||||
def test_short_sentences_pair_only_by_equal_text():
|
||||
a = reading("a", ["Rate is 10.", "Next one."])
|
||||
b = reading("b", ["Rate is 11.", "Next one."])
|
||||
result = align.align(a, b)
|
||||
assert pairs_of(result) == [("a2", "b2")]
|
||||
assert kinds_of(result) == [("dropped", "a1", None), ("added", None, "b1")]
|
||||
|
||||
|
||||
def test_a_sentence_that_changed_a_little_is_paired_and_one_that_changed_a_lot_is_not():
|
||||
base = "Every bond was scanned in steps of small size around its minimum."
|
||||
little = base.replace("small", "tiny")
|
||||
a = reading("a", [LONG[0], base, LONG[2]])
|
||||
near = align.align(a, reading("b", [LONG[0], little, LONG[2]]))
|
||||
assert pairs_of(near) == [("a1", "b1"), ("a2", "b2"), ("a3", "b3")]
|
||||
far = align.align(
|
||||
a, reading("b", [LONG[0], "Zebras graze quietly beyond.....", LONG[2]])
|
||||
)
|
||||
assert pairs_of(far) == [("a1", "b1"), ("a3", "b3")]
|
||||
assert kinds_of(far) == [("dropped", "a2", None), ("added", None, "b2")]
|
||||
|
||||
|
||||
def test_a_moved_sentence_is_a_drop_and_an_add_not_a_pair():
|
||||
a = reading("a", LONG[:3])
|
||||
b = reading("b", [LONG[1], LONG[2], LONG[0]])
|
||||
result = align.align(a, b)
|
||||
assert pairs_of(result) == [("a2", "b1"), ("a3", "b2")]
|
||||
assert kinds_of(result) == [("dropped", "a1", None), ("added", None, "b3")]
|
||||
|
||||
|
||||
def test_a_repeated_sentence_does_not_anchor_but_still_pairs():
|
||||
a = reading("a", [LONG[0], LONG[1], LONG[1], LONG[2]])
|
||||
b = reading("b", [LONG[0], LONG[1], LONG[1], LONG[2]])
|
||||
result = align.align(a, b)
|
||||
assert pairs_of(result) == [(f"a{i}", f"b{i}") for i in range(1, 5)]
|
||||
assert result["suspects"] == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("a", "b", "kind"),
|
||||
[
|
||||
({"strict": "x", "complex": []}, {"strict": "x", "complex": []}, None),
|
||||
({"strict": "x 1", "complex": []}, {"strict": "x 2", "complex": []}, "number"),
|
||||
(
|
||||
{"strict": "x_{1}", "complex": []},
|
||||
{"strict": "x^{1}", "complex": []},
|
||||
"number",
|
||||
),
|
||||
({"strict": "1.5", "complex": []}, {"strict": "15", "complex": []}, "number"),
|
||||
(
|
||||
{"strict": "a \N{MINUS SIGN} 1", "complex": []},
|
||||
{"strict": "a 1", "complex": []},
|
||||
"number",
|
||||
),
|
||||
({"strict": "ab", "complex": []}, {"strict": "ac", "complex": []}, "word"),
|
||||
(
|
||||
{"strict": "ab", "complex": ["k"]},
|
||||
{"strict": "ab", "complex": []},
|
||||
"equation",
|
||||
),
|
||||
(
|
||||
{"strict": "ab 1", "complex": ["k"]},
|
||||
{"strict": "ac 1", "complex": []},
|
||||
"equation",
|
||||
),
|
||||
(
|
||||
{"strict": "a 1", "complex": ["k"]},
|
||||
{"strict": "a 2", "complex": ["k"]},
|
||||
"number",
|
||||
),
|
||||
({"strict": "٣", "complex": []}, {"strict": "3", "complex": []}, "number"),
|
||||
],
|
||||
)
|
||||
def test_classify(a, b, kind):
|
||||
assert align.classify(a, b) == kind
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("a", "b", "length"),
|
||||
[
|
||||
("", "", 0),
|
||||
("abc", "", 0),
|
||||
("abc", "abc", 3),
|
||||
("abcbdab", "bdcaba", 4),
|
||||
("AGGTAB", "GXTXAYB", 4),
|
||||
("abc", "xyz", 0),
|
||||
("aaaa", "aa", 2),
|
||||
("\U0001d465y", "y\U0001d465", 1),
|
||||
],
|
||||
)
|
||||
def test_lcs_length(a, b, length):
|
||||
assert align.lcs_length(a, b) == align.lcs_length(b, a) == length
|
||||
|
||||
|
||||
def _lcs_table(a, b):
|
||||
table = [[0] * (len(b) + 1) for _ in range(len(a) + 1)]
|
||||
for i in range(len(a) - 1, -1, -1):
|
||||
for j in range(len(b) - 1, -1, -1):
|
||||
if a[i] == b[j]:
|
||||
table[i][j] = table[i + 1][j + 1] + 1
|
||||
else:
|
||||
table[i][j] = max(table[i + 1][j], table[i][j + 1])
|
||||
return table[0][0]
|
||||
|
||||
|
||||
@given(st.text(alphabet="abc", max_size=14), st.text(alphabet="abc", max_size=14))
|
||||
@PROPERTIES
|
||||
def test_lcs_length_is_the_textbook_dynamic_programme(a, b):
|
||||
assert align.lcs_length(a, b) == _lcs_table(a, b)
|
||||
|
||||
|
||||
@given(st.text(max_size=30), st.text(max_size=30))
|
||||
@PROPERTIES
|
||||
def test_similarity_is_symmetric_and_between_zero_and_one(a, b):
|
||||
assert align.similarity(a, b) == align.similarity(b, a)
|
||||
assert 0.0 <= align.similarity(a, b) <= 1.0
|
||||
assert align.similarity(a, a) == 1.0
|
||||
|
||||
|
||||
def test_similarity_is_twice_the_common_length_over_the_total():
|
||||
assert align.similarity("", "") == 1.0
|
||||
assert align.similarity("abc", "") == 0.0
|
||||
assert align.similarity("abcd", "abxd") == 0.75
|
||||
assert align.similarity("kitten", "sitting") == pytest.approx(2 * 4 / 13)
|
||||
|
||||
|
||||
SENTENCES = [*LONG, "Short.", "Also short.", "Rate 10 here.", "Rate 11 here."]
|
||||
|
||||
|
||||
@given(
|
||||
st.lists(st.sampled_from(SENTENCES), max_size=7),
|
||||
st.lists(st.sampled_from(SENTENCES), max_size=7),
|
||||
)
|
||||
@PROPERTIES
|
||||
def test_pairs_increase_in_both_readings_and_every_sentence_is_accounted_for(ta, tb):
|
||||
a, b = reading("a", ta), reading("b", tb)
|
||||
result = align.align(a, b)
|
||||
ai = [int(p["a"][1:]) for p in result["pairs"]]
|
||||
bi = [int(p["b"][1:]) for p in result["pairs"]]
|
||||
assert ai == sorted(set(ai))
|
||||
assert bi == sorted(set(bi))
|
||||
dropped = {s["a"] for s in result["suspects"] if s["kind"] == "dropped"}
|
||||
added = {s["b"] for s in result["suspects"] if s["kind"] == "added"}
|
||||
assert dropped == {x["id"] for x in a} - {p["a"] for p in result["pairs"]}
|
||||
assert added == {x["id"] for x in b} - {p["b"] for p in result["pairs"]}
|
||||
|
||||
|
||||
@given(st.lists(st.sampled_from(SENTENCES), max_size=7))
|
||||
@PROPERTIES
|
||||
def test_a_reading_aligned_with_itself_has_no_suspects(texts):
|
||||
result = align.align(reading("a", texts), reading("b", texts))
|
||||
assert len(result["pairs"]) == len(texts)
|
||||
assert all(p["agree"] for p in result["pairs"])
|
||||
assert result["suspects"] == []
|
||||
Reference in new issue
Block a user