@davoodmehraban89 / Aram-2 Mirror · read-only / commits / bb72e1a
Fork 0

Mirrored from https://github.com/davoodmehraban89/Aram.git

Synced Dec 3, 17:00 UTC

test(integration): exercise all five statement builders from one synthetic ledger

davoodmehraban89 committed Oct 31, 2026 at 11:26 UTC bb72e1ab633c80f0aa356598a354d2e7b9358c7f
1 file changed +286
backend/tests/test_five_statement_pipeline_integration.py new
+286
@@ -0,0 +1,286 @@
1 +"""End-to-end synthetic integration tests for all five ARAM statement engines."""
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.balance_sheet_candidate import (
10 + BalanceSheetLinePolicy, build_balance_sheet_candidate,
11 +)
12 +from aram_backend.cash_flow_direct_candidate import (
13 + CashAccountPolicy, CashCounterpartPolicy, CashFlowEntry,
14 + build_direct_cash_flow_candidate,
15 +)
16 +from aram_backend.cross_statement_reconciliation import (
17 + CashBalanceNoteSelection, reconcile_cross_statements,
18 +)
19 +from aram_backend.equity_changes_candidate import (
20 + EquityMovementPolicy, build_equity_changes_candidate,
21 +)
22 +from aram_backend.income_statement_candidate import (
23 + IncomeStatementLinePolicy, build_income_statement_candidate,
24 +)
25 +from aram_backend.pilot_workbook import SHEET_HEADERS
26 +from aram_backend.reconciliation_review import NoteReference
27 +from aram_backend.trial_balance_diagnostic import analyze_trial_balance
28 +
29 +
30 +COMPANY = "001"
31 +YEAR = 1404
32 +
33 +
34 +def source_workbook() -> bytes:
35 + """One debit-net balanced ledger shared by every candidate builder."""
36 + book = Workbook()
37 + book.remove(book.active)
38 + for name, headers in SHEET_HEADERS.items():
39 + book.create_sheet(name).append(list(headers))
40 +
41 + def add(sheet: str, **values: object) -> None:
42 + book[sheet].append([values.get(column) for column in SHEET_HEADERS[sheet]])
43 +
44 + add(
45 + "Company", company_code=COMPANY, company_name="Synthetic integration only",
46 + fiscal_year=YEAR, currency="IRR", unit="Rial", prepared_by="Test fixture",
47 + status="Draft",
48 + )
49 + ledger = (
50 + # code, name, opening, debit, credit, closing
51 + ("CASH", "Cash", "40", "30", "20", "50"),
52 + ("AR", "Receivables", "50", "0", "0", "50"),
53 + ("AP", "Payables", "-20", "10", "0", "-10"),
54 + ("CAP", "Share capital", "-70", "0", "0", "-70"),
55 + ("REV", "Revenue", "0", "0", "40", "-40"),
56 + ("EXP", "Cost of revenue", "0", "20", "0", "20"),
57 + )
58 + for index, (code, name, opening, debit, credit, closing) in enumerate(ledger, 101):
59 + add(
60 + "Accounts", company_code=COMPANY, fiscal_year=YEAR,
61 + general_code=code, general_name=name, account_full_code=code,
62 + account_name=name, opening=opening, debit=debit, credit=credit,
63 + closing=closing, source_row=index, row_status="Draft",
64 + closing_delta="0",
65 + )
66 +
67 + notes = (
68 + ("BS", "Current assets", "C01", "Cash"),
69 + ("BS", "Current assets", "A01", "Receivables"),
70 + ("BS", "Current liabilities", "L01", "Payables"),
71 + ("BS", "Equity", "E01", "Equity"),
72 + ("IS", "Revenue", "R01", "Revenue"),
73 + ("IS", "Cost of revenue", "X01", "Cost of revenue"),
74 + ("EQ", "Share capital", "E01", "Share capital"),
75 + ("EQ", "Current year result", "P01", "Current year result"),
76 + )
77 + for statement, section, note, title in notes:
78 + add(
79 + "Notes", company_code=COMPANY, fiscal_year=YEAR,
80 + statement=statement, statement_section=section,
81 + note_number=note, note_title=title, subnote_code="",
82 + subnote_title="",
83 + )
84 +
85 + mappings = (
86 + ("CASH", "BS", "Current assets", "C01"),
87 + ("AR", "BS", "Current assets", "A01"),
88 + ("AP", "BS", "Current liabilities", "L01"),
89 + ("CAP", "BS", "Equity", "E01"),
90 + ("CAP", "EQ", "Share capital", "E01"),
91 + ("REV", "IS", "Revenue", "R01"),
92 + ("REV", "BS", "Equity", "E01"),
93 + ("REV", "EQ", "Current year result", "P01"),
94 + ("EXP", "IS", "Cost of revenue", "X01"),
95 + ("EXP", "BS", "Equity", "E01"),
96 + ("EXP", "EQ", "Current year result", "P01"),
97 + )
98 + for index, (account, statement, section, note) in enumerate(mappings, 1):
99 + add(
100 + "Mapping", mapping_id=f"SYNTH-M-{index:02d}",
101 + company_code=COMPANY, fiscal_year=YEAR,
102 + account_full_code=account, account_level="General",
103 + general_code=account, target_statement=statement,
104 + statement_section=section, note_number=note, subnote_code="",
105 + mapping_version=1, approval_status="Approved",
106 + reviewer="Synthetic fixture only", review_date="2026-10-31",
107 + source="Synthetic test", notes="Not an authenticated approval",
108 + validation_status="Valid",
109 + )
110 +
111 + output = BytesIO()
112 + book.save(output)
113 + book.close()
114 + return output.getvalue()
115 +
116 +
117 +def references() -> tuple[NoteReference, ...]:
118 + return (
119 + NoteReference("BS", "Current assets", "C01", "", "50", "SYNTH-BS-CASH"),
120 + NoteReference("BS", "Current assets", "A01", "", "50", "SYNTH-BS-AR"),
121 + NoteReference("BS", "Current liabilities", "L01", "", "-10", "SYNTH-BS-AP"),
122 + NoteReference("BS", "Equity", "E01", "", "-90", "SYNTH-BS-EQ"),
123 + NoteReference("IS", "Revenue", "R01", "", "-40", "SYNTH-IS-REV"),
124 + NoteReference("IS", "Cost of revenue", "X01", "", "20", "SYNTH-IS-COST"),
125 + NoteReference("EQ", "Share capital", "E01", "", "-70", "SYNTH-EQ-CAP"),
126 + NoteReference("EQ", "Current year result", "P01", "", "-20", "SYNTH-EQ-PROFIT"),
127 + )
128 +
129 +
130 +def cash_transactions() -> tuple[CashFlowEntry, ...]:
131 + return (
132 + CashFlowEntry(
133 + company_code=COMPANY, fiscal_year=YEAR, line_id="CF-L1",
134 + transaction_id="CF-V1", posting_date="1404-01-01",
135 + cash_account_code="CASH", counterpart_account_code="REV",
136 + cash_debit="30", cash_credit="0", counterpart_debit="0",
137 + counterpart_credit="30", evidence_ref="SYNTH-TXN-1",
138 + ),
139 + CashFlowEntry(
140 + company_code=COMPANY, fiscal_year=YEAR, line_id="CF-L2",
141 + transaction_id="CF-V2", posting_date="1404-01-02",
142 + cash_account_code="CASH", counterpart_account_code="EXP",
143 + cash_debit="0", cash_credit="20", counterpart_debit="20",
144 + counterpart_credit="0", evidence_ref="SYNTH-TXN-2",
145 + ),
146 + )
147 +
148 +
149 +def build_all(data: bytes | None = None, *, transactions=None):
150 + source = source_workbook() if data is None else data
151 + refs = references()
152 + balance_sheet = build_balance_sheet_candidate(
153 + source, company_code=COMPANY, fiscal_year=YEAR, references=refs,
154 + policies=(
155 + BalanceSheetLinePolicy(
156 + "Current assets", "C01", "", "ASSETS", 1,
157 + "Cash and cash equivalents", "SYNTH-POL-BS-CASH",
158 + ),
159 + BalanceSheetLinePolicy(
160 + "Current assets", "A01", "", "ASSETS", 1,
161 + "Trade receivables", "SYNTH-POL-BS-AR",
162 + ),
163 + BalanceSheetLinePolicy(
164 + "Current liabilities", "L01", "", "LIABILITIES", -1,
165 + "Trade payables", "SYNTH-POL-BS-AP",
166 + ),
167 + BalanceSheetLinePolicy(
168 + "Equity", "E01", "", "EQUITY", -1,
169 + "Total equity", "SYNTH-POL-BS-EQ",
170 + ),
171 + ),
172 + )
173 + income_statement = build_income_statement_candidate(
174 + source, company_code=COMPANY, fiscal_year=YEAR, references=refs,
175 + policies=(
176 + IncomeStatementLinePolicy(
177 + "Revenue", "R01", "", "REVENUE", -1,
178 + "Revenue", "SYNTH-POL-IS-REV",
179 + ),
180 + IncomeStatementLinePolicy(
181 + "Cost of revenue", "X01", "", "COST_OF_REVENUE", 1,
182 + "Cost of revenue", "SYNTH-POL-IS-COST",
183 + ),
184 + ),
185 + )
186 + equity_changes = build_equity_changes_candidate(
187 + source, company_code=COMPANY, fiscal_year=YEAR, references=refs,
188 + policies=(
189 + EquityMovementPolicy(
190 + "Share capital", "E01", "", "SHARE_CAPITAL",
191 + "CAPITAL_CONTRIBUTION", -1, "Share capital",
192 + "SYNTH-POL-EQ-CAP",
193 + ),
194 + EquityMovementPolicy(
195 + "Current year result", "P01", "", "RETAINED_EARNINGS",
196 + "NET_PROFIT_LOSS", -1, "Current year profit",
197 + "SYNTH-POL-EQ-PROFIT",
198 + ),
199 + ),
200 + )
201 + cash_flow = build_direct_cash_flow_candidate(
202 + source, company_code=COMPANY, fiscal_year=YEAR,
203 + currency="IRR", unit="Rial",
204 + cash_accounts=(CashAccountPolicy("CASH", "SYNTH-CASH-SCOPE"),),
205 + counterpart_policies=(
206 + CashCounterpartPolicy("REV", "OPERATING", "SYNTH-REV-CLASS"),
207 + CashCounterpartPolicy("EXP", "OPERATING", "SYNTH-EXP-CLASS"),
208 + ),
209 + transactions=cash_transactions() if transactions is None else transactions,
210 + )
211 + trial_balance = analyze_trial_balance(
212 + source, company_code=COMPANY, fiscal_year=YEAR,
213 + )
214 + result = reconcile_cross_statements(
215 + balance_sheet=balance_sheet, income_statement=income_statement,
216 + equity_changes=equity_changes, cash_flow=cash_flow,
217 + trial_balance=trial_balance,
218 + cash_balance_notes=(
219 + CashBalanceNoteSelection(
220 + "Current assets", "C01", "", "SYNTH-CASH-NOTE-SELECTION",
221 + ),
222 + ),
223 + )
224 + return (
225 + balance_sheet, income_statement, equity_changes,
226 + cash_flow, trial_balance, result,
227 + )
228 +
229 +
230 +def test_all_five_real_builders_reconcile_from_one_immutable_workbook():
231 + bs, income, equity, cash, trial, result = build_all()
232 +
233 + assert bs.eligible_for_analyst_review and not bs.findings
234 + assert income.eligible_for_analyst_review and not income.findings
235 + assert equity.eligible_for_analyst_review and not equity.findings
236 + assert cash.eligible_for_analyst_review and not cash.findings
237 + assert trial.balanced_for_analyst_review and not trial.findings
238 + assert len({item.source_sha256 for item in (bs, income, equity, cash, trial)}) == 1
239 + assert len({
240 + bs.note_references_sha256,
241 + income.note_references_sha256,
242 + equity.note_references_sha256,
243 + }) == 1
244 +
245 + assert bs.assets_total == "100"
246 + assert bs.liabilities_total == "10"
247 + assert bs.equity_total == "90"
248 + assert income.net_result == "20"
249 + assert equity.opening_equity_total == "70"
250 + assert equity.net_profit_loss_movements == "20"
251 + assert equity.closing_equity_total == "90"
252 + assert cash.opening_cash_and_equivalents == "40"
253 + assert cash.net_change_in_cash == "10"
254 + assert cash.closing_cash_and_equivalents == "50"
255 + assert trial.opening_debit_net_total == "0"
256 + assert trial.debit_turnover_total == "60"
257 + assert trial.credit_turnover_total == "60"
258 + assert trial.closing_debit_net_total == "0"
259 +
260 + assert result.eligible_for_analyst_review
261 + assert result.findings == ()
262 + assert result.balance_sheet_equity_difference == "0"
263 + assert result.net_profit_equity_difference == "0"
264 + assert result.balance_sheet_cash_difference == "0"
265 + assert result.cash_accounts_match_trial_balance
266 + assert result.cash_account_totals_valid
267 + assert not result.source_ledger_completeness_verified
268 + assert not result.accounting_policy_authorized
269 + assert not result.independently_reviewed
270 + assert not result.official_financial_statement
271 + assert not result.export_authorized
272 + assert not result.releasable
273 +
274 +
275 +def test_missing_cash_transaction_blocks_real_five_statement_pipeline():
276 + bs, income, equity, cash, trial, result = build_all(
277 + transactions=cash_transactions()[:-1],
278 + )
279 + assert bs.eligible_for_analyst_review
280 + assert income.eligible_for_analyst_review
281 + assert equity.eligible_for_analyst_review
282 + assert trial.balanced_for_analyst_review
283 + assert not cash.eligible_for_analyst_review
284 + assert not result.eligible_for_analyst_review
285 + assert "CASH_FLOW_CANDIDATE_BLOCKED" in result.findings
286 + assert "CASH_FLOW_BRIDGE_INVALID" in result.findings or "CASH_ACCOUNT_TURNOVER_OR_TOTALS_INVALID" in result.findings