codex/gate-2a-pilot-workbook-6864754244772568979
add-index-html-10623938925177053894 arena/094f314c-aram chatgpt-phase1b2-autopilot codex/adr-future-connectors-pi-v1 codex/aram-account-note-source-trace-20261031 codex/aram-actions-node24-maintenance-20261031 codex/aram-analyst-review-workbook-20261031 codex/aram-bs-candidate-statement-20261031 codex/aram-cash-flow-direct-candidate-20261031 codex/aram-clean-reference-year-integrity-20261030 codex/aram-cross-statement-controls-20261031 codex/aram-equity-changes-candidate-20261031 codex/aram-five-statement-cli-20261031 codex/aram-five-statement-integration-20261031 codex/aram-five-statement-orchestrator-20261031 codex/aram-gate3-coverage-target-guard-20261030 codex/aram-gate3-hidden-xlsx-guard-20261030 codex/aram-gate3-integration-validation-20261030 codex/aram-gate3-mapping-change-preview-20261030 codex/aram-gate3-stale-note-guard-20261030 codex/aram-income-statement-candidate-20261031 codex/aram-manifest-referential-integrity-20261031 codex/aram-mapping-source-parser-20261101 codex/aram-multidimensional-mapping-20261101 codex/aram-multistatement-mapping-20261031 codex/aram-pipeline-manifest-schema-20261031 codex/aram-readonly-mapping-coverage-20261031 codex/aram-reconciliation-review-gate-20261031 codex/aram-same-flush-fiscal-lock-20261030 codex/aram-synthetic-windows-demo-20261031 codex/aram-trial-balance-diagnostic-20261031 codex/aram-windows-background-detail-search-20261031 codex/aram-windows-detail-review-20261031 codex/aram-windows-five-statement-pilot-20261031 codex/aram-windows-statement-figures-20261031 codex/gate-2a-pilot-workbook codex/gate-2a-pilot-workbook-6864754244772568979 codex/gate3-authenticated-principal-boundary-20261030 codex/gate3-authored-draft-staging-20261030 codex/gate3-carry-forward-preview-20261023 codex/gate3-draft-staging-audit-20261030 codex/gate3-mapping-batch-freshness-20261030 codex/gate3-mapping-batch-review-20261030 codex/gate3-mapping-core codex/gate3-mapping-coverage-20261023 codex/gate3-mapping-excel-20261023 codex/gate3-note-mapping-review-20261023 codex/gate3-signed-year-scope-20261030 codex/gate3-unapproved-proposal-storage-20261030 codex/payvast-mapping-discovery-20261023 codex/phase-2a-backend-foundation codex/phase-2b-post-merge-record codex/phase-2b-source-registration codex/phase-2b-source-registration-12955190277775369594 codex/post-gate2a-control-reconciliation codex/post-pr18-acceptance-record codex/project-control-foundation codex/t1-repo-hygiene-20261023 codex/t2-ci-hardening-20261023 codex/t3-explicit-migrations-20261023 codex/t6a-readonly-diagnostics-20261023 codex/v1-01-safety-gate-hardening codex/v1-year-isolation-followup-20261023 codex/windows-native-pilot-shell-20261030 docs/aram-master-control-plan-20261111 docs/aram-roadmap-dashboard-20261030 docs/financial-statement-reconciliation-1402-1404-20261110 feat/gate3-governed-mapping-20261101 feat/gate3-package9-1-security-20261103 feat/gate3-package9-isolated-20261103 fix/password-unicode-validation-20261111 governance/aram-5-1-3-controls-20261030 governance/role-based-operating-model jules-1168663913887856558-73b8a885 jules-gate-2b-mapping-workflow-foundation-4137724387867065330 phase-1b1-audit-report-7365389436990903239 phase-2a-backend-foundation-10237597358138521261 phase1b2-postmerge-metadata-fix refactor/drg-parent-year-id-15088443704684163304 source-data-cleanup
py 267 lines 10.5 KB
Raw
1 import pytest
2 from pathlib import Path
3 from decimal import Decimal
4 from openpyxl import Workbook
5 from io import BytesIO
6
7 from aram_backend.pilot_workbook import validate_pilot_workbook, REQUIRED_SHEETS, EXPECTED_HEADERS
8
9
10 def test_validate_generated_template():
11 # Verify the committed blank template is valid (except it has no data, so it should just pass structural checks)
12 template_path = Path(__file__).parent.parent.parent / "samples" / "Aram_Pilot_Input_and_Mapping_Template_v0.1.xlsx"
13 if template_path.exists():
14 with open(template_path, "rb") as f:
15 res = validate_pilot_workbook(f.read())
16 assert res.is_valid, f"Template should be valid, got issues: {res.issues}"
17
18 def create_in_memory_workbook(modifications: dict = None) -> bytes:
19 wb = Workbook()
20 if "Sheet" in wb.sheetnames:
21 wb.remove(wb["Sheet"])
22
23 for sheet_name in REQUIRED_SHEETS:
24 sheet = wb.create_sheet(title=sheet_name)
25 sheet.append(EXPECTED_HEADERS.get(sheet_name, ["dummy"]))
26
27 # Add valid minimal data
28 wb["Company"].append(["C1", "Company 1", 1403, "Draft"])
29 wb["Accounts"].append(["C1", 1403, "1001", "Cash", None, None, None, None, None, 100, 50, 20, 130])
30 wb["Notes"].append(["C1", 1403, "N1", "Note 1", 1, "Label"])
31 wb["Mapping"].append(["C1", 1403, "M1", "1001", "N1", "EXACT"])
32
33 if modifications:
34 for sheet_name, mod_func in modifications.items():
35 if sheet_name in wb.sheetnames:
36 mod_func(wb[sheet_name])
37
38 out = BytesIO()
39 wb.save(out)
40 return out.getvalue()
41
42 def test_minimal_valid_workbook():
43 data = create_in_memory_workbook()
44 res = validate_pilot_workbook(data)
45 assert res.is_valid
46 assert len(res.issues) == 0
47
48 def test_missing_sheets():
49 wb = Workbook()
50 wb.create_sheet(title="Company")
51 out = BytesIO()
52 wb.save(out)
53
54 res = validate_pilot_workbook(out.getvalue())
55 assert not res.is_valid
56 missing_codes = [i.code for i in res.issues]
57 assert "MISSING_SHEET" in missing_codes
58
59 def test_incorrect_header_order():
60 def modify_headers(sheet):
61 # Swap first two headers
62 headers = list(sheet.iter_rows(min_row=1, max_row=1, values_only=True))[0]
63 new_headers = [headers[1], headers[0]] + list(headers[2:])
64 sheet.delete_rows(1)
65 sheet.insert_rows(1)
66 for col_idx, val in enumerate(new_headers, start=1):
67 sheet.cell(row=1, column=col_idx, value=val)
68
69 data = create_in_memory_workbook({"Company": modify_headers})
70 res = validate_pilot_workbook(data)
71 assert not res.is_valid
72 assert any(i.code == "INVALID_HEADERS" and i.sheet == "Company" for i in res.issues)
73
74 def test_numeric_code_rejection():
75 def mod(sheet):
76 # Change account code to numeric
77 sheet.cell(row=2, column=3, value=1001)
78
79 data = create_in_memory_workbook({"Accounts": mod})
80 res = validate_pilot_workbook(data)
81 assert not res.is_valid
82 assert any(i.code == "NUMERIC_CODE" and i.field == "account_code" for i in res.issues)
83
84 def mod2(sheet):
85 # Change note number to numeric
86 sheet.cell(row=2, column=3, value=1)
87
88 data = create_in_memory_workbook({"Notes": mod2})
89 res = validate_pilot_workbook(data)
90 assert not res.is_valid
91 assert any(i.code == "NUMERIC_CODE" and i.field == "note_number" for i in res.issues)
92
93 def test_duplicate_rows():
94 def mod(sheet):
95 # Add duplicate account
96 sheet.append(["C1", 1403, "1001", "Cash2", None, None, None, None, None, 0, 0, 0, 0])
97
98 data = create_in_memory_workbook({"Accounts": mod})
99 res = validate_pilot_workbook(data)
100 assert not res.is_valid
101 assert any(i.code == "DUPLICATE_KEY" and i.sheet == "Accounts" for i in res.issues)
102
103 def test_unknown_mapping_account():
104 def mod(sheet):
105 # Map to unknown account
106 sheet.cell(row=2, column=4, value="9999")
107
108 data = create_in_memory_workbook({"Mapping": mod})
109 res = validate_pilot_workbook(data)
110 assert not res.is_valid
111 assert any(i.code == "UNKNOWN_ACCOUNT" and i.sheet == "Mapping" for i in res.issues)
112
113 def test_invalid_lifecycle_values():
114 def mod(sheet):
115 sheet.cell(row=2, column=4, value="UnknownStatus")
116
117 data = create_in_memory_workbook({"Company": mod})
118 res = validate_pilot_workbook(data)
119 assert not res.is_valid
120 assert any(i.code == "INVALID_STATUS" and i.sheet == "Company" for i in res.issues)
121
122 def test_non_zero_closing_delta():
123 def mod(sheet):
124 # 100 + 50 - 20 = 130, change closing to 140 to force delta
125 sheet.cell(row=2, column=13, value=140)
126
127 data = create_in_memory_workbook({"Accounts": mod})
128 res = validate_pilot_workbook(data)
129 assert not res.is_valid
130 assert any(i.code == "NON_ZERO_DELTA" for i in res.issues)
131
132 def test_nan_and_infinity_rejection():
133 def mod_nan(sheet):
134 sheet.cell(row=2, column=10, value="NaN")
135
136 data = create_in_memory_workbook({"Accounts": mod_nan})
137 res = validate_pilot_workbook(data)
138 assert not res.is_valid
139 assert any(i.code == "INVALID_TYPE" and i.field == "opening" for i in res.issues)
140
141 def mod_inf(sheet):
142 sheet.cell(row=2, column=11, value="Infinity")
143
144 data = create_in_memory_workbook({"Accounts": mod_inf})
145 res = validate_pilot_workbook(data)
146 assert not res.is_valid
147 assert any(i.code == "INVALID_TYPE" and i.field == "debit" for i in res.issues)
148
149 def test_simultaneous_multi_error_collection_and_order():
150 def mod_company(sheet):
151 sheet.cell(row=2, column=4, value="Bad") # Row 2, INVALID_STATUS
152 def mod_accounts(sheet):
153 sheet.cell(row=2, column=3, value=123) # Row 2, NUMERIC_CODE
154 sheet.cell(row=2, column=13, value=1) # Row 2, NON_ZERO_DELTA
155
156 def mod_mapping(sheet):
157 sheet.cell(row=2, column=1, value=1)
158 sheet.cell(row=2, column=3, value=1)
159 sheet.cell(row=2, column=5, value=1)
160
161 data = create_in_memory_workbook({"Company": mod_company, "Accounts": mod_accounts, "Mapping": mod_mapping})
162 res = validate_pilot_workbook(data)
163 assert not res.is_valid
164
165 assert any(i.code == "NUMERIC_CODE" and i.sheet == "Mapping" and i.field == "company_code" for i in res.issues)
166 assert any(i.code == "NUMERIC_CODE" and i.sheet == "Mapping" and i.field == "mapping_id" for i in res.issues)
167 assert any(i.code == "NUMERIC_CODE" and i.sheet == "Mapping" and i.field == "note_number" for i in res.issues)
168
169 # We also get an UNKNOWN_ACCOUNT error in Mapping because '1001' is no longer in accounts_set (it was parsed as numeric 123)
170 # The numeric code changes the a_code, so the mapping for "1001" fails. We just filter to the errors we care about for the test.
171 relevant_issues = [i for i in res.issues if i.code in {"NUMERIC_CODE", "NON_ZERO_DELTA", "INVALID_STATUS"} and i.sheet in {"Company", "Accounts"}]
172 assert len(relevant_issues) == 3
173
174 # Check deterministic ordering (sheet, row, field, code)
175 assert relevant_issues[0].sheet == "Accounts"
176 assert relevant_issues[0].code == "NUMERIC_CODE" # account_code
177 assert relevant_issues[1].sheet == "Accounts"
178 assert relevant_issues[1].code == "NON_ZERO_DELTA" # closing
179 assert relevant_issues[2].sheet == "Company"
180 assert relevant_issues[2].code == "INVALID_STATUS"
181
182 def test_missing_cross_sheet():
183 def mod_mapping(sheet):
184 # M1 references account 9999 which does not exist
185 sheet.cell(row=2, column=4, value="9999")
186 data = create_in_memory_workbook({"Mapping": mod_mapping})
187 res = validate_pilot_workbook(data)
188 assert not res.is_valid
189 assert any(i.code == "UNKNOWN_ACCOUNT" and i.sheet == "Mapping" for i in res.issues)
190
191 def test_cross_company_year_references():
192 def mod_mapping(sheet):
193 # Map to an account that belongs to a different company year
194 sheet.cell(row=2, column=2, value=1404)
195
196 def mod_company(sheet):
197 # Add year 1404
198 sheet.append(["C1", "Company 1", 1404, "Draft"])
199
200 def mod_accounts(sheet):
201 # Account is only in 1403
202 pass
203
204 data = create_in_memory_workbook({"Company": mod_company, "Mapping": mod_mapping})
205 res = validate_pilot_workbook(data)
206 assert not res.is_valid
207 # The mapping should say account not found since it's looking for C1-1404-1001
208 assert any(i.code == "UNKNOWN_ACCOUNT" and i.sheet == "Mapping" for i in res.issues)
209
210 def test_preserve_leading_zero():
211 def mod_accounts(sheet):
212 # Change account code to a text string with leading zero
213 sheet.cell(row=2, column=3, value="0123")
214
215 def mod_mapping(sheet):
216 sheet.cell(row=2, column=4, value="0123")
217
218 data = create_in_memory_workbook({"Accounts": mod_accounts, "Mapping": mod_mapping})
219 res = validate_pilot_workbook(data)
220 # The leading zero is preserved as text
221 assert res.is_valid
222
223 def test_incomplete_row():
224 def mod_accounts(sheet):
225 # Change required field account_code to empty string so it's not completely skipped by the empty row check
226 sheet.cell(row=2, column=3, value="")
227
228 data = create_in_memory_workbook({"Accounts": mod_accounts})
229 res = validate_pilot_workbook(data)
230 assert not res.is_valid
231 assert any(i.code == "INCOMPLETE_ROW" and i.sheet == "Accounts" for i in res.issues)
232
233 def mod_accounts_none(sheet):
234 # The base row already has 1001 in column 3, setting to None doesn't overwrite it properly for iter_rows sometimes
235 # We delete the entire row and re-insert an incomplete one
236 sheet.delete_rows(2)
237 sheet.append(["C1", 1403, None, "Cash", None, None, None, None, None, 100, 50, 20, 130])
238
239 data = create_in_memory_workbook({"Accounts": mod_accounts_none})
240 res = validate_pilot_workbook(data)
241 assert not res.is_valid
242 assert any(i.code == "INCOMPLETE_ROW" and i.sheet == "Accounts" for i in res.issues)
243
244 def test_no_persistence(monkeypatch):
245 import aram_backend.pilot_workbook as pw
246
247 data = create_in_memory_workbook()
248
249 # We monkeypatch load_workbook to track if it's called
250 load_called = False
251 orig_load = pw.load_workbook
252 def mock_load(*args, **kwargs):
253 nonlocal load_called
254 load_called = True
255 # Ensure we only use data_only and read_only
256 assert kwargs.get("data_only") is True
257 assert kwargs.get("read_only") is True
258 return orig_load(*args, **kwargs)
259
260 monkeypatch.setattr(pw, "load_workbook", mock_load)
261
262 res = pw.validate_pilot_workbook(data)
263 assert load_called
264 assert res.is_valid
265
266 # It takes bytes, returns object, doesn't interact with DB.
267 # The read_only=True parameter on openpyxl guarantees no file writes possible to the BytesIO stream.