@davoodmehraban89 / Aram-1 / commits / c5379b5
Fork 0

test(statements): cover balanced BS, sign policy, source trace, misuse and blocking cases

davoodmehraban89 committed Oct 31, 2026 at 00:34 UTC c5379b5f14929dc717846330419d6cac90e9f3bb
1 file changed +241
backend/tests/test_balance_sheet_candidate.py new
+241
@@ -0,0 +1,241 @@
1 +"""Synthetic tests of non-official, explicit-policy balance sheet presentation."""
2 +from __future__ import annotations
3 +
4 +import json
5 +from io import BytesIO
6 +
7 +import pytest
8 +from openpyxl import Workbook
9 +
10 +from aram_backend.balance_sheet_candidate import (
11 + BalanceSheetLinePolicy, build_balance_sheet_candidate,
12 +)
13 +from aram_backend.coverage_preview import CoverageError
14 +from aram_backend.pilot_workbook import SHEET_HEADERS
15 +from aram_backend.reconciliation_review import NoteReference
16 +
17 +
18 +def workbook(*, liability="-60", equity="-40", omit_mapping=False, add_income=False):
19 + book = Workbook()
20 + book.remove(book.active)
21 + for name, headers in SHEET_HEADERS.items():
22 + book.create_sheet(name).append(list(headers))
23 +
24 + def add(name, **record):
25 + book[name].append([record.get(column) for column in SHEET_HEADERS[name]])
26 +
27 + add("Company", company_code="001", company_name="Synthetic only",
28 + fiscal_year=1404, status="Draft")
29 + amounts = [("A", "100", "A1", "Assets"), ("L", liability, "L1", "Liabilities"),
30 + ("E", equity, "E1", "Equity")]
31 + if add_income:
32 + amounts.append(("I", "0", "I1", "Revenue"))
33 + for i, (code, amount, note, section) in enumerate(amounts, 1):
34 + add("Accounts", company_code="001", fiscal_year=1404,
35 + general_code=code, general_name=section, account_full_code=code,
36 + account_name=section + " Synthetic", opening=amount,
37 + debit="0", credit="0", closing=amount, source_row=i + 1,
38 + row_status="Draft", closing_delta="0")
39 + statement = "IS" if code == "I" else "BS"
40 + add("Notes", company_code="001", fiscal_year=1404,
41 + statement=statement, statement_section=section,
42 + note_number=note, note_title=section)
43 + if omit_mapping and code == "E":
44 + continue
45 + add("Mapping", mapping_id="M" + code, company_code="001",
46 + fiscal_year=1404, account_full_code=code, account_level="General",
47 + general_code=code, target_statement=statement,
48 + statement_section=section, note_number=note, mapping_version=1,
49 + approval_status="Approved", reviewer="Synthetic reviewer",
50 + review_date="2026-10-31", validation_status="Valid", source="Synthetic")
51 + output = BytesIO()
52 + book.save(output)
53 + book.close()
54 + return output.getvalue()
55 +
56 +
57 +def references(*, liability="-60", equity="-40", add_income=False):
58 + rows = [
59 + NoteReference("BS", "Assets", "A1", "", "100", "SYNTH-TB-ASSET"),
60 + NoteReference("BS", "Liabilities", "L1", "", liability, "SYNTH-TB-DEBT"),
61 + NoteReference("BS", "Equity", "E1", "", equity, "SYNTH-TB-EQUITY"),
62 + ]
63 + if add_income:
64 + rows.append(NoteReference("IS", "Revenue", "I1", "", "0", "SYNTH-IS"))
65 + return rows
66 +
67 +
68 +def policies(*, liabilities_sign=-1, equity_sign=-1):
69 + return [
70 + BalanceSheetLinePolicy("Assets", "A1", "", "ASSETS", 1,
71 + "Synthetic assets", "SYNTH-POLICY-A"),
72 + BalanceSheetLinePolicy("Liabilities", "L1", "", "LIABILITIES", liabilities_sign,
73 + "Synthetic liabilities", "SYNTH-POLICY-L"),
74 + BalanceSheetLinePolicy("Equity", "E1", "", "EQUITY", equity_sign,
75 + "Synthetic equity", "SYNTH-POLICY-E"),
76 + ]
77 +
78 +
79 +def build(*, source=None, refs=None, policy=None):
80 + return build_balance_sheet_candidate(
81 + source if source is not None else workbook(),
82 + company_code="001", fiscal_year=1404,
83 + references=references() if refs is None else refs,
84 + policies=policies() if policy is None else policy,
85 + )
86 +
87 +
88 +def test_balanced_candidate_is_not_an_approved_financial_statement():
89 + result = build()
90 + assert result.eligible_for_analyst_review
91 + assert result.arithmetically_balanced
92 + assert result.assets_total == "100"
93 + assert result.liabilities_total == "60"
94 + assert result.equity_total == "40"
95 + assert result.balance_difference == "0"
96 + assert not result.findings
97 + assert len(result.lines) == 3
98 + assert not result.official_financial_statement
99 + assert not result.accounting_policy_authorized
100 + assert not result.independently_reviewed
101 + assert not result.export_authorized
102 + assert not result.releasable
103 + assert all(item.policy_evidence_ref for item in result.lines)
104 +
105 +
106 +def test_mismatched_accounting_equation_is_flagged():
107 + result = build(source=workbook(liability="-59"), refs=references(liability="-59"))
108 + assert not result.eligible_for_analyst_review
109 + assert not result.arithmetically_balanced
110 + assert result.balance_difference == "1"
111 + assert "BALANCE_SHEET_DOES_NOT_BALANCE" in result.findings
112 +
113 +
114 +def test_missing_presentation_policy_is_blocking():
115 + result = build(policy=policies()[:2])
116 + assert not result.eligible_for_analyst_review
117 + assert "MISSING_EXPLICIT_PRESENTATION_POLICY" in result.findings
118 + assert "NO_EQUITY_LINES" in result.findings
119 +
120 +
121 +def test_extra_policy_is_blocking_even_if_equation_balances():
122 + p = policies()
123 + p.append(BalanceSheetLinePolicy("Equity", "Z9", "", "EQUITY", 1, "Ghost", "SYNTH"))
124 + result = build(policy=p)
125 + assert not result.eligible_for_analyst_review
126 + assert "ORPHAN_PRESENTATION_POLICY" in result.findings
127 + assert result.arithmetically_balanced
128 +
129 +
130 +def test_unmapped_account_makes_statement_candidate_ineligible():
131 + result = build(source=workbook(omit_mapping=True),
132 + refs=references()[:2])
133 + assert not result.eligible_for_analyst_review
134 + assert "UPSTREAM_RECONCILIATION_BLOCKED" in result.findings
135 +
136 +
137 +def test_unmatched_reference_always_blocks_presentation():
138 + r = references()
139 + r[0] = NoteReference("BS", "Assets", "A1", "", "100.01", "SYNTH-OTHER")
140 + result = build(refs=r)
141 + assert not result.eligible_for_analyst_review
142 + assert "UPSTREAM_RECONCILIATION_BLOCKED" in result.findings
143 +
144 +
145 +def test_wrong_sign_is_not_silently_corrected():
146 + result = build(policy=policies(liabilities_sign=1))
147 + assert not result.eligible_for_analyst_review
148 + assert result.liabilities_total == "-60"
149 + assert "NEGATIVE_PRESENTATION_AMOUNT" in result.findings
150 + assert "BALANCE_SHEET_DOES_NOT_BALANCE" in result.findings
151 +
152 +
153 +def test_non_bs_notes_do_not_become_bs_lines_but_must_be_reconciled():
154 + result = build(source=workbook(add_income=True),
155 + refs=references(add_income=True))
156 + assert result.eligible_for_analyst_review
157 + assert len(result.lines) == 3
158 + assert all(line.note_number != "I1" for line in result.lines)
159 +
160 +
161 +def test_non_bs_missing_reference_still_blocks_the_gate():
162 + result = build(source=workbook(add_income=True))
163 + assert not result.eligible_for_analyst_review
164 + assert "UPSTREAM_RECONCILIATION_BLOCKED" in result.findings
165 +
166 +
167 +def test_identical_policies_in_different_order_hash_identically():
168 + source = workbook()
169 + p = policies()
170 + r = build(source=source, policy=p)
171 + reverse = build(source=source, policy=list(reversed(p)))
172 + assert r.presentation_policies_sha256 == reverse.presentation_policies_sha256
173 + assert r.lines == reverse.lines
174 + assert r.source_sha256 == reverse.source_sha256
175 +
176 +
177 +def test_policy_change_changes_provenance_and_flags_wrong_sign():
178 + original = build()
179 + edited = build(policy=policies(equity_sign=1))
180 + assert original.presentation_policies_sha256 != edited.presentation_policies_sha256
181 + assert not edited.eligible_for_analyst_review
182 +
183 +
184 +def test_source_bytes_never_modified():
185 + data = workbook()
186 + original = bytes(data)
187 + build(source=data)
188 + assert data == original
189 +
190 +
191 +@pytest.mark.parametrize("invalid", [
192 + "not a sequence", [object()], [None],
193 + [policies()[0]] * 10001,
194 +])
195 +def test_invalid_policy_collection_is_rejected(invalid):
196 + with pytest.raises(CoverageError):
197 + build(policy=invalid)
198 +
199 +
200 +@pytest.mark.parametrize("bad", [
201 + BalanceSheetLinePolicy("Assets", "A1", "", "ASSETS", True, "Synthetic assets", "P"),
202 + BalanceSheetLinePolicy("Assets", "A1", "", "ASSETS", 0, "Synthetic assets", "P"),
203 + BalanceSheetLinePolicy("Assets", "A1", "", "OTHER", 1, "Synthetic assets", "P"),
204 + BalanceSheetLinePolicy("Assets", "", "", "ASSETS", 1, "Synthetic assets", "P"),
205 + BalanceSheetLinePolicy("Assets", "A1", "", "ASSETS", 1, "Bad\\nLine", "P"),
206 + BalanceSheetLinePolicy("Assets", "A1", "", "ASSETS", 1, " ", "P"),
207 + BalanceSheetLinePolicy("Assets", "A1", "", "ASSETS", 1, "Synthetic assets", ""),
208 +])
209 +def test_invalid_presentation_policy_rejected(bad):
210 + with pytest.raises(CoverageError):
211 + build(policy=[bad])
212 +
213 +
214 +def test_duplicate_policies_rejected_even_when_identical():
215 + with pytest.raises(CoverageError) as e:
216 + build(policy=[policies()[0], policies()[0]])
217 + assert e.value.code == "DUPLICATE_PRESENTATION_POLICY"
218 +
219 +
220 +def test_json_only_contains_string_amounts_and_no_approved_flags():
221 + candidate = json.loads(json.dumps(build().to_dict()))
222 + assert candidate["kind"] == "UNAPPROVED_BALANCE_SHEET_PRESENTATION_CANDIDATE"
223 + assert candidate["assets_total"] == "100"
224 + assert candidate["official_financial_statement"] is False
225 + assert candidate["releasable"] is False
226 +
227 +
228 +def test_wrong_company_or_year_fails_closed():
229 + with pytest.raises(CoverageError):
230 + build_balance_sheet_candidate(workbook(), company_code="999",
231 + fiscal_year=1404,
232 + references=references(), policies=policies())
233 + with pytest.raises(CoverageError):
234 + build_balance_sheet_candidate(workbook(), company_code="001",
235 + fiscal_year=1403,
236 + references=references(), policies=policies())
237 +
238 +
239 +def test_missing_source_fails_closed():
240 + with pytest.raises(CoverageError):
241 + build(source=b"not a valid workbook")