Mirrored from https://github.com/davoodmehraban89/Aram.git
Synced Dec 3, 20:45 UTC
test(statements): cover direct cash flow pair validation, activity totals and cash rollforward
davoodmehraban89 committed
Oct 31, 2026 at 08:23 UTC
370ad3e14b89593e6fb0f072ed0cc2404fb26b2f
1 file changed
+323
backend/tests/test_cash_flow_direct_candidate.py
new
+323
@@ -0,0 +1,323 @@
1
+"""Synthetic-only direct cash flow candidate tests; no customer data."""
2
+from __future__ import annotations
3
+
4
+from dataclasses import replace
5
+from io import BytesIO
6
+import json
7
+
8
+import pytest
9
+from openpyxl import Workbook, load_workbook
10
+
11
+from aram_backend.cash_flow_direct_candidate import (
12
+ CashAccountPolicy, CashCounterpartPolicy, CashFlowEntry,
13
+ build_direct_cash_flow_candidate,
14
+)
15
+from aram_backend.coverage_preview import CoverageError
16
+from aram_backend.pilot_workbook import SHEET_HEADERS
17
+
18
+
19
+ACCOUNT_ROWS = (
20
+ ("CASH1", "Cash account one", "100", "40", "40", "100"),
21
+ ("CASH2", "Cash account two", "20", "20", "10", "30"),
22
+ ("REV", "Sales counterpart", "0", "0", "40", "-40"),
23
+ ("EXP1", "Operating expense", "0", "20", "0", "20"),
24
+ ("ASSET", "Equipment", "0", "10", "0", "10"),
25
+ ("CAP", "Capital", "0", "0", "10", "-10"),
26
+ ("EXP2", "Other operating expense", "0", "10", "0", "10"),
27
+)
28
+
29
+
30
+def workbook(*, mutate=None):
31
+ book = Workbook()
32
+ book.remove(book.active)
33
+ for name, headers in SHEET_HEADERS.items():
34
+ book.create_sheet(name).append(list(headers))
35
+
36
+ def add(name, **values):
37
+ book[name].append([values.get(key) for key in SHEET_HEADERS[name]])
38
+
39
+ add("Company", company_code="001", company_name="Synthetic only",
40
+ fiscal_year=1404, currency="IRR", unit="Rial", status="Draft")
41
+ for index, (code, name, opening, debit, credit, closing) in enumerate(ACCOUNT_ROWS, 2):
42
+ add("Accounts", company_code="001", fiscal_year=1404,
43
+ general_code=code, general_name=name, account_full_code=code,
44
+ account_name=name, opening=opening, debit=debit, credit=credit,
45
+ closing=closing, source_row=index + 20, row_status="Draft",
46
+ closing_delta="0")
47
+ if mutate:
48
+ mutate(book)
49
+ stream = BytesIO()
50
+ book.save(stream)
51
+ book.close()
52
+ return stream.getvalue()
53
+
54
+
55
+def cash_accounts():
56
+ return [
57
+ CashAccountPolicy("CASH1", "SYNTH-CASH-POLICY-1"),
58
+ CashAccountPolicy("CASH2", "SYNTH-CASH-POLICY-2"),
59
+ ]
60
+
61
+
62
+def counterpart_policies():
63
+ return [
64
+ CashCounterpartPolicy("REV", "OPERATING", "SYNTH-REV"),
65
+ CashCounterpartPolicy("EXP1", "OPERATING", "SYNTH-EXP1"),
66
+ CashCounterpartPolicy("ASSET", "INVESTING", "SYNTH-ASSET"),
67
+ CashCounterpartPolicy("CAP", "FINANCING", "SYNTH-CAP"),
68
+ CashCounterpartPolicy("EXP2", "OPERATING", "SYNTH-EXP2"),
69
+ ]
70
+
71
+
72
+def transactions():
73
+ common = {"company_code": "001", "fiscal_year": 1404}
74
+ return [
75
+ CashFlowEntry(**common, line_id="L1", transaction_id="V1",
76
+ posting_date="1404-01-01", cash_account_code="CASH1",
77
+ counterpart_account_code="REV", cash_debit="40", cash_credit="0",
78
+ counterpart_debit="0", counterpart_credit="40", evidence_ref="SYNTH-T1"),
79
+ CashFlowEntry(**common, line_id="L2", transaction_id="V2",
80
+ posting_date="1404-01-02", cash_account_code="CASH1",
81
+ counterpart_account_code="EXP1", cash_debit="0", cash_credit="20",
82
+ counterpart_debit="20", counterpart_credit="0", evidence_ref="SYNTH-T2"),
83
+ CashFlowEntry(**common, line_id="L3", transaction_id="V3",
84
+ posting_date="1404-01-03", cash_account_code="CASH1",
85
+ counterpart_account_code="ASSET", cash_debit="0", cash_credit="10",
86
+ counterpart_debit="10", counterpart_credit="0", evidence_ref="SYNTH-T3"),
87
+ CashFlowEntry(**common, line_id="L4", transaction_id="V4",
88
+ posting_date="1404-01-04", cash_account_code="CASH1",
89
+ counterpart_account_code="CASH2", cash_debit="0", cash_credit="10",
90
+ counterpart_debit="10", counterpart_credit="0", evidence_ref="SYNTH-T4"),
91
+ CashFlowEntry(**common, line_id="L5", transaction_id="V5",
92
+ posting_date="1404-01-05", cash_account_code="CASH2",
93
+ counterpart_account_code="CAP", cash_debit="10", cash_credit="0",
94
+ counterpart_debit="0", counterpart_credit="10", evidence_ref="SYNTH-T5"),
95
+ CashFlowEntry(**common, line_id="L6", transaction_id="V6",
96
+ posting_date="1404-01-06", cash_account_code="CASH2",
97
+ counterpart_account_code="EXP2", cash_debit="0", cash_credit="10",
98
+ counterpart_debit="10", counterpart_credit="0", evidence_ref="SYNTH-T6"),
99
+ ]
100
+
101
+
102
+def build(*, data=None, cash=None, policies=None, entries=None,
103
+ company="001", year=1404, currency="IRR", unit="Rial"):
104
+ return build_direct_cash_flow_candidate(
105
+ workbook() if data is None else data,
106
+ company_code=company, fiscal_year=year, currency=currency, unit=unit,
107
+ cash_accounts=cash_accounts() if cash is None else cash,
108
+ counterpart_policies=counterpart_policies() if policies is None else policies,
109
+ transactions=transactions() if entries is None else entries,
110
+ )
111
+
112
+
113
+def edit(data, change):
114
+ book = load_workbook(BytesIO(data))
115
+ change(book)
116
+ stream = BytesIO()
117
+ book.save(stream)
118
+ book.close()
119
+ return stream.getvalue()
120
+
121
+
122
+def test_direct_cash_flow_classification_and_cash_rollforward():
123
+ result = build()
124
+ assert result.eligible_for_analyst_review
125
+ assert not result.findings
126
+ assert result.opening_cash_and_equivalents == "120"
127
+ assert result.operating_inflows == "40"
128
+ assert result.operating_outflows == "30"
129
+ assert result.operating_net_cash_flow == "10"
130
+ assert result.investing_inflows == "0"
131
+ assert result.investing_outflows == "10"
132
+ assert result.investing_net_cash_flow == "-10"
133
+ assert result.financing_inflows == "10"
134
+ assert result.financing_outflows == "0"
135
+ assert result.financing_net_cash_flow == "10"
136
+ assert result.internal_transfer_inflows == "10"
137
+ assert result.internal_transfer_outflows == "10"
138
+ assert result.net_change_in_cash == "10"
139
+ assert result.closing_cash_and_equivalents == "130"
140
+ assert result.rollforward_difference == "0"
141
+ assert result.cash_account_count == 2
142
+ assert result.transaction_count == 6
143
+ assert len(result.cash_flow_lines) == 6
144
+ assert all(a.debit_difference == "0" and a.credit_difference == "0"
145
+ for a in result.account_reconciliations)
146
+ assert result.kind == "UNAPPROVED_DIRECT_CASH_FLOW_CANDIDATE"
147
+ assert not result.cash_account_scope_owner_approved
148
+ assert not result.transaction_ledger_completeness_verified
149
+ assert not result.accounting_policy_authorized
150
+ assert not result.official_financial_statement
151
+ assert not result.independently_reviewed
152
+ assert not result.export_authorized
153
+ assert not result.releasable
154
+
155
+
156
+def test_missing_transaction_line_blocks_turnover_reconciliation():
157
+ result = build(entries=transactions()[:-1])
158
+ assert not result.eligible_for_analyst_review
159
+ assert "CASH_ACCOUNT_CREDIT_TURNOVER_MISMATCH" in result.findings
160
+
161
+
162
+def test_missing_counterpart_classification_blocks_candidate():
163
+ policies = counterpart_policies()[:-1]
164
+ result = build(policies=policies)
165
+ assert not result.eligible_for_analyst_review
166
+ assert "MISSING_COUNTERPART_ACTIVITY_POLICY" in result.findings
167
+ assert "CASH_FLOW_CASH_BALANCE_ROLLFORWARD_MISMATCH" in result.findings
168
+
169
+
170
+def test_orphan_counterpart_policy_blocks_candidate():
171
+ policies = counterpart_policies() + [
172
+ CashCounterpartPolicy("UNUSED", "OPERATING", "SYNTH-UNUSED")
173
+ ]
174
+ result = build(policies=policies)
175
+ assert not result.eligible_for_analyst_review
176
+ assert "ORPHAN_COUNTERPART_ACTIVITY_POLICY" in result.findings
177
+
178
+
179
+def test_internal_cash_transfer_is_excluded_from_activity_categories():
180
+ result = build()
181
+ assert result.internal_transfer_inflows == result.internal_transfer_outflows
182
+ assert result.operating_net_cash_flow == "10"
183
+ assert result.investing_net_cash_flow == "-10"
184
+ assert result.financing_net_cash_flow == "10"
185
+
186
+
187
+def test_unbalanced_cash_counterpart_pair_rejected():
188
+ entries = transactions()
189
+ entries[0] = replace(entries[0], counterpart_credit="39")
190
+ with pytest.raises(CoverageError) as error:
191
+ build(entries=entries)
192
+ assert error.value.code == "CASH_FLOW_COUNTERPART_NOT_BALANCED"
193
+
194
+
195
+def test_duplicate_line_id_rejected():
196
+ entries = transactions()
197
+ entries[1] = replace(entries[1], line_id="L1")
198
+ with pytest.raises(CoverageError) as error:
199
+ build(entries=entries)
200
+ assert error.value.code == "DUPLICATE_CASH_FLOW_LINE_ID"
201
+
202
+
203
+def test_wrong_company_or_year_transaction_rejected():
204
+ entries = transactions()
205
+ entries[0] = replace(entries[0], company_code="002")
206
+ with pytest.raises(CoverageError) as error:
207
+ build(entries=entries)
208
+ assert error.value.code == "CASH_FLOW_TRANSACTION_SCOPE_MISMATCH"
209
+ entries = transactions()
210
+ entries[0] = replace(entries[0], fiscal_year=1403)
211
+ with pytest.raises(CoverageError):
212
+ build(entries=entries)
213
+
214
+
215
+def test_invalid_posting_date_rejected():
216
+ entries = transactions()
217
+ entries[0] = replace(entries[0], posting_date="1404-13-01")
218
+ with pytest.raises(CoverageError) as error:
219
+ build(entries=entries)
220
+ assert error.value.code == "INVALID_CASH_FLOW_POSTING_DATE"
221
+
222
+
223
+def test_currency_or_unit_mismatch_rejected():
224
+ with pytest.raises(CoverageError) as error:
225
+ build(currency="USD")
226
+ assert error.value.code == "CASH_FLOW_CURRENCY_UNIT_MISMATCH"
227
+ with pytest.raises(CoverageError):
228
+ build(unit="Thousand Rial")
229
+
230
+
231
+def test_cash_account_missing_from_source_rejected():
232
+ with pytest.raises(CoverageError) as error:
233
+ build(cash=[CashAccountPolicy("MISSING", "SYNTH")])
234
+ assert error.value.code == "CASH_ACCOUNT_NOT_FOUND_IN_SOURCE"
235
+
236
+
237
+def test_transaction_unknown_cash_or_counterpart_account_rejected():
238
+ entries = transactions()
239
+ entries[0] = replace(entries[0], cash_account_code="UNKNOWN")
240
+ with pytest.raises(CoverageError):
241
+ build(entries=entries)
242
+ entries = transactions()
243
+ entries[0] = replace(entries[0], counterpart_account_code="UNKNOWN")
244
+ with pytest.raises(CoverageError):
245
+ build(entries=entries)
246
+
247
+
248
+def test_negative_or_two_sided_cash_posting_rejected():
249
+ entries = transactions()
250
+ entries[0] = replace(entries[0], cash_debit="-40")
251
+ with pytest.raises(CoverageError):
252
+ build(entries=entries)
253
+ entries = transactions()
254
+ entries[0] = replace(entries[0], cash_credit="1")
255
+ with pytest.raises(CoverageError):
256
+ build(entries=entries)
257
+
258
+
259
+def test_duplicate_cash_or_counterpart_policies_rejected():
260
+ with pytest.raises(CoverageError):
261
+ build(cash=cash_accounts() + [cash_accounts()[0]])
262
+ with pytest.raises(CoverageError):
263
+ build(policies=counterpart_policies() + [counterpart_policies()[0]])
264
+
265
+
266
+def test_invalid_activity_or_cash_counterpart_policy_rejected():
267
+ bad = counterpart_policies()
268
+ bad[0] = replace(bad[0], activity="CASH_TRANSFER")
269
+ with pytest.raises(CoverageError):
270
+ build(policies=bad)
271
+ bad = counterpart_policies() + [
272
+ CashCounterpartPolicy("CASH1", "OPERATING", "SYNTH-BAD")
273
+ ]
274
+ with pytest.raises(CoverageError):
275
+ build(policies=bad)
276
+
277
+
278
+def test_all_zero_cash_balances_and_no_transactions_are_valid():
279
+ def zero(book):
280
+ for row in book["Accounts"].iter_rows(min_row=2):
281
+ for col in (16, 17, 18, 19):
282
+ row[col].value = "0"
283
+ data = edit(workbook(), zero)
284
+ result = build(data=data, entries=[])
285
+ assert result.eligible_for_analyst_review
286
+ assert result.opening_cash_and_equivalents == "0"
287
+ assert result.net_change_in_cash == "0"
288
+ assert result.closing_cash_and_equivalents == "0"
289
+
290
+
291
+def test_source_cash_rollforward_invalid_rejected():
292
+ data = edit(workbook(), lambda book: setattr(book["Accounts"]["T2"], "value", "101"))
293
+ with pytest.raises(CoverageError):
294
+ build(data=data)
295
+
296
+
297
+def test_wrong_company_year_and_currency_source_scope_rejected():
298
+ with pytest.raises(CoverageError):
299
+ build(company="999")
300
+ with pytest.raises(CoverageError):
301
+ build(year=1403)
302
+ data = edit(workbook(), lambda book: setattr(book["Company"]["D2"], "value", "USD"))
303
+ with pytest.raises(CoverageError):
304
+ build(data=data)
305
+
306
+
307
+def test_policy_and_transaction_hashes_are_stable_and_source_unchanged():
308
+ data = workbook()
309
+ original = bytes(data)
310
+ first = build(data=data)
311
+ second = build(data=data, entries=list(reversed(transactions())))
312
+ assert data == original
313
+ assert first.transaction_ledger_sha256 == second.transaction_ledger_sha256
314
+ assert first.classification_policies_sha256 == second.classification_policies_sha256
315
+ assert len(first.source_sha256) == 64
316
+ assert not first.releasable
317
+
318
+
319
+def test_input_size_and_transaction_count_are_bounded():
320
+ with pytest.raises(CoverageError):
321
+ build(data=b"x" * (20 * 1024 * 1024 + 1))
322
+ with pytest.raises(CoverageError):
323
+ build(entries=transactions() * 5001)