|
3 | 3 |
|
4 | 4 | """Tests for ``SaveHandler._derive_aggregate_scores``. |
5 | 5 |
|
6 | | -Covers the score-availability semantics: |
7 | | -- valid scores flow through verbatim |
8 | | -- missing per-field signal yields ``None`` (rendered as "N/A" in the UI) |
9 | | -- a genuine zero is preserved as ``0`` (rendered as "0%") |
10 | | -- failed processing (no comparison items) yields ``None`` |
| 6 | +Covers the score-derivation contract: |
| 7 | +- probabilistic confidence flows through verbatim when available |
| 8 | +- structural completeness fallback fires for Completed runs without logprobs |
| 9 | + (e.g. reasoning models / image-only flow) instead of emitting a misleading 0% |
| 10 | +- a genuine zero is preserved as ``0.0`` |
| 11 | +- failed/empty runs return ``0.0`` |
11 | 12 | """ |
12 | 13 |
|
13 | 14 | from __future__ import annotations |
@@ -35,7 +36,7 @@ def _make_result( |
35 | 36 | ) |
36 | 37 |
|
37 | 38 |
|
38 | | -class TestDeriveAggregateScores: |
| 39 | +class TestProbabilisticPath: |
39 | 40 | def test_valid_scores_flow_through(self): |
40 | 41 | """A normal evaluate-step result must produce numeric scores.""" |
41 | 42 | items = [ |
@@ -63,75 +64,174 @@ def test_valid_scores_flow_through(self): |
63 | 64 | assert schema == round(2 / 3, 3) |
64 | 65 | assert min_score == 0.0 |
65 | 66 |
|
66 | | - def test_missing_per_field_signal_returns_none(self): |
67 | | - """Reasoning-model / image-only flow: no signal → ``None`` everywhere.""" |
68 | | - items: list[ExtractionComparisonItem] = [] |
| 67 | + def test_all_fields_above_threshold(self): |
| 68 | + items = [ |
| 69 | + ExtractionComparisonItem( |
| 70 | + Field="a", Extracted="x", Confidence="95.00%", IsAboveThreshold="True" |
| 71 | + ), |
| 72 | + ExtractionComparisonItem( |
| 73 | + Field="b", Extracted="y", Confidence="90.00%", IsAboveThreshold="True" |
| 74 | + ), |
| 75 | + ] |
69 | 76 | confidence = { |
70 | | - "total_evaluated_fields_count": 0, |
71 | | - "overall_confidence": 0.0, |
72 | | - "min_extracted_field_confidence": 0.0, |
| 77 | + "total_evaluated_fields_count": 2, |
| 78 | + "overall_confidence": 0.925, |
| 79 | + "min_extracted_field_confidence": 0.9, |
73 | 80 | "zero_confidence_fields_count": 0, |
74 | 81 | } |
75 | 82 | entity, schema, min_score = SaveHandler._derive_aggregate_scores( |
76 | 83 | _make_result(items=items, confidence=confidence) |
77 | 84 | ) |
78 | | - assert entity is None |
79 | | - assert schema is None |
80 | | - assert min_score is None |
| 85 | + assert entity == 0.925 |
| 86 | + assert schema == 1.0 |
| 87 | + assert min_score == 0.9 |
| 88 | + |
81 | 89 |
|
82 | | - def test_no_comparison_items_returns_none(self): |
83 | | - """Even if confidence claims fields exist, an empty comparison list is unknown.""" |
| 90 | +class TestStructuralFallback: |
| 91 | + """When logprobs are unavailable (reasoning model / image-only) but |
| 92 | + extraction succeeded, the Completed file must still get a meaningful |
| 93 | + numeric score based on schema completeness.""" |
| 94 | + |
| 95 | + def test_all_fields_filled_yields_one(self): |
| 96 | + items = [ |
| 97 | + ExtractionComparisonItem( |
| 98 | + Field="a", Extracted="x", Confidence="0.00%", IsAboveThreshold="False" |
| 99 | + ), |
| 100 | + ExtractionComparisonItem( |
| 101 | + Field="b", Extracted="y", Confidence="0.00%", IsAboveThreshold="False" |
| 102 | + ), |
| 103 | + ExtractionComparisonItem( |
| 104 | + Field="c", Extracted=42, Confidence="0.00%", IsAboveThreshold="False" |
| 105 | + ), |
| 106 | + ] |
| 107 | + # No probabilistic signal: total_evaluated_fields_count == 0 |
84 | 108 | confidence = { |
85 | | - "total_evaluated_fields_count": 5, |
86 | | - "overall_confidence": 0.9, |
87 | | - "min_extracted_field_confidence": 0.5, |
| 109 | + "total_evaluated_fields_count": 0, |
| 110 | + "overall_confidence": 0.0, |
| 111 | + "min_extracted_field_confidence": 0.0, |
88 | 112 | "zero_confidence_fields_count": 0, |
89 | 113 | } |
90 | 114 | entity, schema, min_score = SaveHandler._derive_aggregate_scores( |
91 | | - _make_result(items=[], confidence=confidence) |
| 115 | + _make_result(items=items, confidence=confidence) |
92 | 116 | ) |
93 | | - assert entity is None |
94 | | - assert schema is None |
95 | | - assert min_score is None |
| 117 | + assert entity == 1.0 |
| 118 | + assert schema == 1.0 |
| 119 | + assert min_score == 1.0 |
96 | 120 |
|
97 | | - def test_genuine_zero_score_preserved(self): |
98 | | - """A real ``0`` confidence (e.g. all fields below threshold) must NOT become ``None``.""" |
| 121 | + def test_partial_fill_yields_ratio(self): |
99 | 122 | items = [ |
100 | 123 | ExtractionComparisonItem( |
101 | 124 | Field="a", Extracted="x", Confidence="0.00%", IsAboveThreshold="False" |
102 | 125 | ), |
| 126 | + ExtractionComparisonItem( |
| 127 | + Field="b", Extracted=None, Confidence="0.00%", IsAboveThreshold="False" |
| 128 | + ), |
| 129 | + ExtractionComparisonItem( |
| 130 | + Field="c", Extracted="", Confidence="0.00%", IsAboveThreshold="False" |
| 131 | + ), |
| 132 | + ExtractionComparisonItem( |
| 133 | + Field="d", Extracted="z", Confidence="0.00%", IsAboveThreshold="False" |
| 134 | + ), |
| 135 | + ] |
| 136 | + confidence = {"total_evaluated_fields_count": 0} |
| 137 | + entity, schema, min_score = SaveHandler._derive_aggregate_scores( |
| 138 | + _make_result(items=items, confidence=confidence) |
| 139 | + ) |
| 140 | + # 2 of 4 fields actually filled → 0.5 |
| 141 | + assert entity == 0.5 |
| 142 | + assert schema == 0.5 |
| 143 | + assert min_score == 0.5 |
| 144 | + |
| 145 | + def test_all_fields_empty_yields_zero(self): |
| 146 | + """Genuine-empty extraction: structural fallback collapses to ``0.0``.""" |
| 147 | + items = [ |
| 148 | + ExtractionComparisonItem( |
| 149 | + Field="a", Extracted=None, Confidence="0.00%", IsAboveThreshold="False" |
| 150 | + ), |
| 151 | + ExtractionComparisonItem( |
| 152 | + Field="b", Extracted="", Confidence="0.00%", IsAboveThreshold="False" |
| 153 | + ), |
| 154 | + ExtractionComparisonItem( |
| 155 | + Field="c", Extracted=" ", Confidence="0.00%", IsAboveThreshold="False" |
| 156 | + ), |
103 | 157 | ] |
| 158 | + confidence = {"total_evaluated_fields_count": 0} |
| 159 | + entity, schema, min_score = SaveHandler._derive_aggregate_scores( |
| 160 | + _make_result(items=items, confidence=confidence) |
| 161 | + ) |
| 162 | + assert entity == 0.0 |
| 163 | + assert schema == 0.0 |
| 164 | + assert min_score == 0.0 |
| 165 | + |
| 166 | + |
| 167 | +class TestZeroPath: |
| 168 | + def test_no_comparison_items_returns_zero(self): |
| 169 | + """No extraction data at all (failed pipeline) → ``0.0``.""" |
104 | 170 | confidence = { |
105 | | - "total_evaluated_fields_count": 1, |
| 171 | + "total_evaluated_fields_count": 0, |
106 | 172 | "overall_confidence": 0.0, |
107 | 173 | "min_extracted_field_confidence": 0.0, |
108 | | - "zero_confidence_fields_count": 1, |
| 174 | + "zero_confidence_fields_count": 0, |
109 | 175 | } |
110 | 176 | entity, schema, min_score = SaveHandler._derive_aggregate_scores( |
111 | | - _make_result(items=items, confidence=confidence) |
| 177 | + _make_result(items=[], confidence=confidence) |
112 | 178 | ) |
113 | 179 | assert entity == 0.0 |
114 | 180 | assert schema == 0.0 |
115 | 181 | assert min_score == 0.0 |
116 | 182 |
|
117 | | - def test_all_fields_above_threshold(self): |
| 183 | + def test_genuine_zero_probabilistic_score_preserved(self): |
| 184 | + """A real ``0`` confidence (every field below threshold) must NOT be |
| 185 | + replaced by the structural fallback — it's genuinely 0%.""" |
118 | 186 | items = [ |
119 | 187 | ExtractionComparisonItem( |
120 | | - Field="a", Extracted="x", Confidence="95.00%", IsAboveThreshold="True" |
121 | | - ), |
122 | | - ExtractionComparisonItem( |
123 | | - Field="b", Extracted="y", Confidence="90.00%", IsAboveThreshold="True" |
| 188 | + Field="a", Extracted="x", Confidence="0.00%", IsAboveThreshold="False" |
124 | 189 | ), |
125 | 190 | ] |
126 | 191 | confidence = { |
127 | | - "total_evaluated_fields_count": 2, |
128 | | - "overall_confidence": 0.925, |
129 | | - "min_extracted_field_confidence": 0.9, |
130 | | - "zero_confidence_fields_count": 0, |
| 192 | + "total_evaluated_fields_count": 1, |
| 193 | + "overall_confidence": 0.0, |
| 194 | + "min_extracted_field_confidence": 0.0, |
| 195 | + "zero_confidence_fields_count": 1, |
131 | 196 | } |
132 | 197 | entity, schema, min_score = SaveHandler._derive_aggregate_scores( |
133 | 198 | _make_result(items=items, confidence=confidence) |
134 | 199 | ) |
135 | | - assert entity == 0.925 |
136 | | - assert schema == 1.0 |
137 | | - assert min_score == 0.9 |
| 200 | + assert entity == 0.0 |
| 201 | + assert schema == 0.0 |
| 202 | + assert min_score == 0.0 |
| 203 | + |
| 204 | + |
| 205 | +class TestIsFilledValue: |
| 206 | + """Coverage for the ``_is_filled_value`` helper used by the structural fallback.""" |
| 207 | + |
| 208 | + def test_none_is_empty(self): |
| 209 | + assert SaveHandler._is_filled_value(None) is False |
| 210 | + |
| 211 | + def test_empty_string_is_empty(self): |
| 212 | + assert SaveHandler._is_filled_value("") is False |
| 213 | + assert SaveHandler._is_filled_value(" ") is False |
| 214 | + |
| 215 | + def test_non_empty_string_is_filled(self): |
| 216 | + assert SaveHandler._is_filled_value("x") is True |
| 217 | + |
| 218 | + def test_zero_int_is_filled(self): |
| 219 | + # A literal ``0`` is a valid extracted value (e.g. count fields). |
| 220 | + assert SaveHandler._is_filled_value(0) is True |
| 221 | + |
| 222 | + def test_bool_is_filled(self): |
| 223 | + assert SaveHandler._is_filled_value(False) is True |
| 224 | + assert SaveHandler._is_filled_value(True) is True |
| 225 | + |
| 226 | + def test_empty_container_is_empty(self): |
| 227 | + assert SaveHandler._is_filled_value([]) is False |
| 228 | + assert SaveHandler._is_filled_value({}) is False |
| 229 | + |
| 230 | + def test_nested_all_null_is_empty(self): |
| 231 | + assert SaveHandler._is_filled_value({"a": None, "b": ""}) is False |
| 232 | + assert SaveHandler._is_filled_value([None, "", {"c": None}]) is False |
| 233 | + |
| 234 | + def test_nested_with_value_is_filled(self): |
| 235 | + assert SaveHandler._is_filled_value({"a": None, "b": "x"}) is True |
| 236 | + assert SaveHandler._is_filled_value([None, "x"]) is True |
| 237 | + |
0 commit comments