|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +"""Tests for ``SaveHandler._derive_aggregate_scores``. |
| 5 | +
|
| 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`` |
| 12 | +""" |
| 13 | + |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +from libs.pipeline.handlers.logics.evaluate_handler.comparison import ( |
| 17 | + ExtractionComparisonData, |
| 18 | + ExtractionComparisonItem, |
| 19 | +) |
| 20 | +from libs.pipeline.handlers.logics.evaluate_handler.model import DataExtractionResult |
| 21 | +from libs.pipeline.handlers.save_handler import SaveHandler |
| 22 | + |
| 23 | + |
| 24 | +def _make_result( |
| 25 | + *, |
| 26 | + items: list[ExtractionComparisonItem], |
| 27 | + confidence: dict, |
| 28 | +) -> DataExtractionResult: |
| 29 | + return DataExtractionResult( |
| 30 | + extracted_result={}, |
| 31 | + confidence=confidence, |
| 32 | + comparison_result=ExtractionComparisonData(items=items), |
| 33 | + prompt_tokens=0, |
| 34 | + completion_tokens=0, |
| 35 | + execution_time=0, |
| 36 | + ) |
| 37 | + |
| 38 | + |
| 39 | +class TestProbabilisticPath: |
| 40 | + def test_valid_scores_flow_through(self): |
| 41 | + """A normal evaluate-step result must produce numeric scores.""" |
| 42 | + items = [ |
| 43 | + ExtractionComparisonItem( |
| 44 | + Field="a", Extracted="x", Confidence="90.00%", IsAboveThreshold="True" |
| 45 | + ), |
| 46 | + ExtractionComparisonItem( |
| 47 | + Field="b", Extracted="y", Confidence="80.00%", IsAboveThreshold="True" |
| 48 | + ), |
| 49 | + ExtractionComparisonItem( |
| 50 | + Field="c", Extracted="z", Confidence="0.00%", IsAboveThreshold="False" |
| 51 | + ), |
| 52 | + ] |
| 53 | + confidence = { |
| 54 | + "total_evaluated_fields_count": 3, |
| 55 | + "overall_confidence": 0.567, |
| 56 | + "min_extracted_field_confidence": 0.0, |
| 57 | + "zero_confidence_fields_count": 1, |
| 58 | + } |
| 59 | + entity, schema, min_score = SaveHandler._derive_aggregate_scores( |
| 60 | + _make_result(items=items, confidence=confidence) |
| 61 | + ) |
| 62 | + assert entity == 0.567 |
| 63 | + # 2 of 3 fields above threshold → 0.667 |
| 64 | + assert schema == round(2 / 3, 3) |
| 65 | + assert min_score == 0.0 |
| 66 | + |
| 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 | + ] |
| 76 | + confidence = { |
| 77 | + "total_evaluated_fields_count": 2, |
| 78 | + "overall_confidence": 0.925, |
| 79 | + "min_extracted_field_confidence": 0.9, |
| 80 | + "zero_confidence_fields_count": 0, |
| 81 | + } |
| 82 | + entity, schema, min_score = SaveHandler._derive_aggregate_scores( |
| 83 | + _make_result(items=items, confidence=confidence) |
| 84 | + ) |
| 85 | + assert entity == 0.925 |
| 86 | + assert schema == 1.0 |
| 87 | + assert min_score == 0.9 |
| 88 | + |
| 89 | + |
| 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 |
| 108 | + confidence = { |
| 109 | + "total_evaluated_fields_count": 0, |
| 110 | + "overall_confidence": 0.0, |
| 111 | + "min_extracted_field_confidence": 0.0, |
| 112 | + "zero_confidence_fields_count": 0, |
| 113 | + } |
| 114 | + entity, schema, min_score = SaveHandler._derive_aggregate_scores( |
| 115 | + _make_result(items=items, confidence=confidence) |
| 116 | + ) |
| 117 | + assert entity == 1.0 |
| 118 | + assert schema == 1.0 |
| 119 | + assert min_score == 1.0 |
| 120 | + |
| 121 | + def test_partial_fill_yields_ratio(self): |
| 122 | + items = [ |
| 123 | + ExtractionComparisonItem( |
| 124 | + Field="a", Extracted="x", Confidence="0.00%", IsAboveThreshold="False" |
| 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 | + ), |
| 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``.""" |
| 170 | + confidence = { |
| 171 | + "total_evaluated_fields_count": 0, |
| 172 | + "overall_confidence": 0.0, |
| 173 | + "min_extracted_field_confidence": 0.0, |
| 174 | + "zero_confidence_fields_count": 0, |
| 175 | + } |
| 176 | + entity, schema, min_score = SaveHandler._derive_aggregate_scores( |
| 177 | + _make_result(items=[], confidence=confidence) |
| 178 | + ) |
| 179 | + assert entity == 0.0 |
| 180 | + assert schema == 0.0 |
| 181 | + assert min_score == 0.0 |
| 182 | + |
| 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%.""" |
| 186 | + items = [ |
| 187 | + ExtractionComparisonItem( |
| 188 | + Field="a", Extracted="x", Confidence="0.00%", IsAboveThreshold="False" |
| 189 | + ), |
| 190 | + ] |
| 191 | + confidence = { |
| 192 | + "total_evaluated_fields_count": 1, |
| 193 | + "overall_confidence": 0.0, |
| 194 | + "min_extracted_field_confidence": 0.0, |
| 195 | + "zero_confidence_fields_count": 1, |
| 196 | + } |
| 197 | + entity, schema, min_score = SaveHandler._derive_aggregate_scores( |
| 198 | + _make_result(items=items, confidence=confidence) |
| 199 | + ) |
| 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 |
0 commit comments