Mirrored from https://github.com/davoodmehraban89/Aram.git
Synced Dec 3, 18:15 UTC
test(coverage): define read-only mapping exposure contract and misuse cases
davoodmehraban89 committed
Oct 31, 2026 at 00:15 UTC
07d2b681519b75fc75f74052250cbad9152fa186
1 file changed
+171
backend/tests/test_coverage_preview.py
new
+171
@@ -0,0 +1,171 @@
1
+"""Read-only Mapping coverage preview, using synthetic Pilot XLSX inputs only."""
2
+from __future__ import annotations
3
+
4
+from io import BytesIO
5
+
6
+import pytest
7
+from openpyxl import Workbook
8
+
9
+from aram_backend.coverage_preview import CoverageError, preview_mapping_coverage
10
+from aram_backend.pilot_workbook import SHEET_HEADERS
11
+
12
+
13
+def sample_workbook(*, extra_mapping=False, approved=True):
14
+ book = Workbook()
15
+ book.remove(book.active)
16
+ sheets = {}
17
+ for name, headers in SHEET_HEADERS.items():
18
+ sheet = book.create_sheet(name)
19
+ sheet.append(list(headers))
20
+ sheets[name] = sheet
21
+
22
+ def add(sheet_name, **fields):
23
+ headers = SHEET_HEADERS[sheet_name]
24
+ sheets[sheet_name].append([fields.get(header) for header in headers])
25
+
26
+ add("Company", company_code="001", company_name="Synthetic Company",
27
+ fiscal_year=1404, status="Draft")
28
+ for code, amount in (("0010", "110.25"), ("0020", "15.75")):
29
+ add("Accounts", company_code="001", fiscal_year=1404,
30
+ general_code=code, general_name="General",
31
+ account_full_code=code, account_name="Synthetic " + code,
32
+ opening=amount, debit="0", credit="0", closing=amount,
33
+ source_row=10, row_status="Draft", closing_delta="0")
34
+ add("Notes", company_code="001", fiscal_year=1404, statement="BS",
35
+ statement_section="Assets", note_number="N01", note_title="Cash")
36
+ options = {
37
+ "company_code": "001", "fiscal_year": 1404,
38
+ "account_full_code": "0010", "account_level": "General",
39
+ "general_code": "0010", "target_statement": "BS",
40
+ "statement_section": "Assets", "note_number": "N01",
41
+ "mapping_version": 1, "approval_status": "Approved" if approved else "Draft",
42
+ "source": "Manual", "validation_status": "Valid",
43
+ "reviewer": "Synthetic reviewer" if approved else None,
44
+ "review_date": "2026-10-31" if approved else None,
45
+ }
46
+ add("Mapping", mapping_id="M001", **options)
47
+ if extra_mapping:
48
+ add("Mapping", mapping_id="M002", **options)
49
+ stream = BytesIO()
50
+ book.save(stream)
51
+ return stream.getvalue()
52
+
53
+
54
+def make_change(content, change):
55
+ book = __import__("openpyxl").load_workbook(BytesIO(content))
56
+ change(book)
57
+ result = BytesIO()
58
+ book.save(result)
59
+ book.close()
60
+ return result.getvalue()
61
+
62
+
63
+def preview(content):
64
+ return preview_mapping_coverage(content, company_code="001", fiscal_year=1404)
65
+
66
+
67
+def test_includes_only_file_marked_approved_unique_mapping():
68
+ result = preview(sample_workbook())
69
+ assert result.account_count == 2
70
+ assert result.candidate_mapped_accounts == 1
71
+ assert result.blocked_accounts == 1
72
+ assert result.source_closing_exposure == "126.00"
73
+ assert result.candidate_closing_exposure == "110.25"
74
+ assert result.blocked_closing_exposure == "15.75"
75
+ assert len(result.notes) == 1
76
+ assert result.notes[0].note_number == "N01"
77
+ assert result.notes[0].account_codes == ("0010",)
78
+ assert [(issue.code, issue.account_code) for issue in result.exceptions] == [
79
+ ("UNMAPPED_ACCOUNT", "0020")
80
+ ]
81
+ assert not result.financial_statement
82
+ assert not result.trusted_approvals
83
+ assert not result.releasable
84
+
85
+
86
+def test_multiple_mapping_rows_are_blocked_not_silently_deduplicated():
87
+ result = preview(sample_workbook(extra_mapping=True))
88
+ assert result.candidate_mapped_accounts == 0
89
+ assert result.blocked_accounts == 2
90
+ assert {issue.code for issue in result.exceptions} == {
91
+ "AMBIGUOUS_ACCOUNT_MAPPING", "UNMAPPED_ACCOUNT"
92
+ }
93
+ match = next(e for e in result.exceptions if e.code == "AMBIGUOUS_ACCOUNT_MAPPING")
94
+ assert match.mapping_excel_rows == (2, 3)
95
+
96
+
97
+def test_unapproved_file_label_is_not_evidence_of_authorization():
98
+ result = preview(sample_workbook(approved=False))
99
+ assert result.candidate_mapped_accounts == 0
100
+ assert result.notes == ()
101
+ assert any(e.code == "MAPPING_NOT_FILE_MARKED_APPROVED" for e in result.exceptions)
102
+
103
+
104
+@pytest.mark.parametrize("kwargs", [
105
+ {"company_code": "", "fiscal_year": 1404},
106
+ {"company_code": "001", "fiscal_year": 0},
107
+ {"company_code": "001", "fiscal_year": True},
108
+ {"company_code": "001", "fiscal_year": 1403},
109
+])
110
+def test_scope_is_explicit_and_no_fallback(kwargs):
111
+ with pytest.raises(CoverageError):
112
+ preview_mapping_coverage(sample_workbook(), **kwargs)
113
+
114
+
115
+def test_malformed_workbook_fails_closed():
116
+ with pytest.raises(CoverageError) as exc:
117
+ preview(b"invalid file")
118
+ assert exc.value.code in {"WORKBOOK_INVALID", "WORKBOOK_ARCHIVE_INVALID"}
119
+
120
+
121
+def test_wrong_headers_fail_closed():
122
+ content = make_change(sample_workbook(), lambda book: setattr(book["Accounts"]["A1"], "value", "other"))
123
+ with pytest.raises(CoverageError) as exc:
124
+ preview(content)
125
+ assert exc.value.code == "WORKBOOK_INVALID"
126
+
127
+
128
+@pytest.mark.parametrize("alter", [
129
+ lambda book: setattr(book["Mapping"].row_dimensions[2], "hidden", True),
130
+ lambda book: setattr(book["Accounts"].column_dimensions["Q"], "hidden", True),
131
+ lambda book: book["Mapping"].merge_cells("C2:D2"),
132
+ lambda book: setattr(book["Notes"], "sheet_state", "hidden"),
133
+])
134
+def test_hidden_or_merged_inputs_fail_closed(alter):
135
+ content = make_change(sample_workbook(), alter)
136
+ with pytest.raises(CoverageError) as exc:
137
+ preview(content)
138
+ assert exc.value.code in {"HIDDEN_INPUT", "MERGED_INPUT"}
139
+
140
+
141
+def test_original_bytes_never_mutated():
142
+ source = sample_workbook()
143
+ before = bytes(source)
144
+ result = preview(source)
145
+ assert source == before
146
+ assert len(result.source_sha256) == 64
147
+
148
+
149
+def test_result_serializable_without_float_amounts():
150
+ import json
151
+ result = preview(sample_workbook())
152
+ encoded = json.dumps(result.to_dict(), sort_keys=True)
153
+ assert '"110.25"' in encoded
154
+ assert '"releasable": false' in encoded
155
+
156
+
157
+def test_rejects_nonbytes_and_large_inputs():
158
+ with pytest.raises(CoverageError):
159
+ preview_mapping_coverage("not-bytes", company_code="001", fiscal_year=1404)
160
+ with pytest.raises(CoverageError):
161
+ preview(b"x" * (20 * 1024 * 1024 + 1))
162
+
163
+
164
+def test_empty_scoped_accounts_fail_closed():
165
+ content = make_change(sample_workbook(), lambda book: (
166
+ book["Accounts"].delete_rows(2, 2),
167
+ book["Mapping"].delete_rows(2, 1),
168
+ ))
169
+ with pytest.raises(CoverageError) as exc:
170
+ preview(content)
171
+ assert exc.value.code == "NO_ACCOUNTS_IN_SCOPE"