121 lines
4.7 KiB
Python
121 lines
4.7 KiB
Python
"""The fidelity instrument: it must be able to see a loss before a 100 % means anything.
|
|
|
|
This module exists because the arm A report of 2026-08-29 published `docx`
|
|
193/196 and `xlsx` 193/193 without shipping the command that produced them.
|
|
Those figures could not be re-measured against a new converter version, against
|
|
the product path, or at all -- so a later run reporting the same numbers would
|
|
have been agreement with a memory rather than with a measurement.
|
|
|
|
The load-bearing test here is the NEGATIVE CONTROL: an instrument that returns
|
|
100 % on text with strings deliberately removed is an instrument that returns
|
|
100 % on everything, and every green figure it ever produced would be
|
|
worthless. `test_a_dropped_string_is_counted_as_lost` is what makes the rest
|
|
of the numbers mean something.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
import zipfile
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tools"))
|
|
|
|
import okf_fidelity # noqa: E402
|
|
|
|
DOCUMENT_XML = """<?xml version="1.0"?>
|
|
<w:document xmlns:w="x"><w:body>
|
|
<w:p><w:r><w:t>Krav til seksjonering av bygget</w:t></w:r></w:p>
|
|
<w:p><w:r><w:t>Navn tilbyder:</w:t></w:r></w:p>
|
|
<w:p><w:r><w:t>Roemningsveier skal vaere uavhengige</w:t></w:r></w:p>
|
|
<w:p><w:r><w:t>ok</w:t></w:r></w:p>
|
|
</w:body></w:document>
|
|
"""
|
|
|
|
SHARED_XML = """<?xml version="1.0"?>
|
|
<sst xmlns="x"><si><t>Prisskjema for entreprisen</t></si><si><t>Sum eks mva</t></si></sst>
|
|
"""
|
|
|
|
|
|
def docx(tmp_path: Path) -> Path:
|
|
path = tmp_path / "d.docx"
|
|
with zipfile.ZipFile(path, "w") as archive:
|
|
archive.writestr("word/document.xml", DOCUMENT_XML)
|
|
return path
|
|
|
|
|
|
def xlsx(tmp_path: Path) -> Path:
|
|
path = tmp_path / "s.xlsx"
|
|
with zipfile.ZipFile(path, "w") as archive:
|
|
archive.writestr("xl/sharedStrings.xml", SHARED_XML)
|
|
return path
|
|
|
|
|
|
def test_the_fasit_comes_from_the_file_not_from_a_converter(tmp_path: Path) -> None:
|
|
"""A fasit derived from one converter would score it on its own homework."""
|
|
strings = okf_fidelity.source_strings(docx(tmp_path))
|
|
assert "krav til seksjonering av bygget" in strings
|
|
# Below the floor: a two-character string appears in almost any output by
|
|
# accident and would inflate every converter's score equally.
|
|
assert "ok" not in strings
|
|
|
|
|
|
def test_full_coverage_is_reported_when_every_string_survives(tmp_path: Path) -> None:
|
|
path = docx(tmp_path)
|
|
text = "\n".join(okf_fidelity.source_strings(path))
|
|
result = okf_fidelity.score(path, text)
|
|
assert (result.covered, result.total) == (3, 3)
|
|
|
|
|
|
def test_a_dropped_string_is_counted_as_lost(tmp_path: Path) -> None:
|
|
"""THE negative control. An instrument that cannot see a loss measures nothing.
|
|
|
|
Without this test every 100 % above is compatible with a matcher that
|
|
always says yes, and the whole K2 figure would be decoration.
|
|
"""
|
|
path = docx(tmp_path)
|
|
kept = list(okf_fidelity.source_strings(path))[:-1]
|
|
result = okf_fidelity.score(path, "\n".join(kept))
|
|
assert result.covered == 2
|
|
assert result.total == 3
|
|
assert result.covered < result.total
|
|
|
|
|
|
def test_the_converters_own_markup_is_not_counted_as_a_loss(tmp_path: Path) -> None:
|
|
"""Measured on the corpus: markdown escapes read as 47 missing strings.
|
|
|
|
The question is whether the STRING survived, not whether the markup
|
|
matches -- a converter is entitled to escape a bracket and to wrap a bold
|
|
run, and scoring that as data loss would attribute a formatting choice to
|
|
the pipeline.
|
|
"""
|
|
path = docx(tmp_path)
|
|
escaped = "\\[Krav til seksjonering av bygget\\] **Navn tilbyder:** value\nRoemningsveier skal vaere uavhengige"
|
|
result = okf_fidelity.score(path, escaped)
|
|
assert result.covered == 3
|
|
|
|
|
|
def test_pairing_needs_a_value_beside_the_label_not_just_the_label(tmp_path: Path) -> None:
|
|
"""A label alone on a line is the failure the criterion exists to catch."""
|
|
path = docx(tmp_path)
|
|
alone = "Navn tilbyder:\nKrav til seksjonering av bygget"
|
|
beside = "Navn tilbyder: Entreprenoer AS\nKrav til seksjonering av bygget"
|
|
assert okf_fidelity.score(path, alone).paired == 0
|
|
assert okf_fidelity.score(path, beside).paired == 1
|
|
assert okf_fidelity.score(path, beside).pairable == 1
|
|
|
|
|
|
def test_a_workbooks_shared_strings_are_read(tmp_path: Path) -> None:
|
|
strings = okf_fidelity.source_strings(xlsx(tmp_path))
|
|
assert set(strings) == {"prisskjema for entreprisen", "sum eks mva"}
|
|
|
|
|
|
def test_a_type_with_no_reader_is_refused_rather_than_scored_zero(tmp_path: Path) -> None:
|
|
"""Zero coverage and "no instrument" are different facts."""
|
|
path = tmp_path / "x.pptx"
|
|
with zipfile.ZipFile(path, "w") as archive:
|
|
archive.writestr("a", "b")
|
|
with pytest.raises(ValueError):
|
|
okf_fidelity.source_strings(path)
|