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

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

Synced Dec 4, 14:15 UTC

dependabot/github_actions/actions/upload-artifact-7
add-index-html-10623938925177053894 arena/094f314c-aram arena/2d4eda07-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 dependabot/github_actions/actions/checkout-7 dependabot/github_actions/actions/setup-node-6 dependabot/github_actions/actions/setup-python-7 dependabot/github_actions/actions/upload-artifact-7 dependabot/npm_and_yarn/frontend/eslint-plugin-react-hooks-7.1.1 dependabot/npm_and_yarn/frontend/globals-17.15.0 dependabot/npm_and_yarn/frontend/vite-8.4.0 dependabot/npm_and_yarn/frontend/vitest-5.1.2 dependabot/npm_and_yarn/frontend/vitest/coverage-v8-5.1.2 dependabot/pip/backend/mypy-gte-1.13-and-lt-3 dependabot/pip/backend/pytest-cov-gte-6.0-and-lt-8 dependabot/pip/backend/setuptools-gte-75-and-lt-85 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 124 lines 4.94 KB
Raw
1 """Phase 3 acceptance: note engine evaluates formulas against real balances."""
2
3 from __future__ import annotations
4
5 import pytest
6
7 from aram_backend.models import NoteRow
8 from aram_backend.services.note_engine import NoteEngineError, evaluate_note, evaluate_year_notes
9 from test_statement_build import _balance, _note, _setup
10
11
12 def _row(session, note, order, label, formula):
13 r = NoteRow(note_id=note.id, row_order=order, label=label, formula_logic=formula)
14 session.add(r)
15 session.flush()
16 return r
17
18
19 def _seed(session):
20 year, source = _setup(session)
21 _balance(session, year, source, "148348", 100, 3)
22 _balance(session, year, source, "241001", -60, 4)
23 session.flush()
24 return year
25
26
27 class TestNoteEngine:
28 def test_rows_with_account_and_subtotal(self, session):
29 year = _seed(session)
30 note = _note(session, year, "6", "نقد", "BS", "Assets")
31 _row(session, note, 0, "حساب نقد", 'acct("148348")')
32 _row(session, note, 1, "پرداختنی", 'acct("241001")')
33 _row(session, note, 2, "جمع", "sum(row(0), row(1))")
34 session.flush()
35 result = evaluate_note(session, note.id)
36 values = [r["value"] for r in result["rows"]]
37 assert values == ["100", "-60", "40"]
38 assert result["rows"][0]["accounts_referenced"] == ["148348"]
39
40 def test_cross_note_reference(self, session):
41 year = _seed(session)
42 n6 = _note(session, year, "6", "نقد", "BS", "Assets")
43 _row(session, n6, 0, "نقد", 'acct("148348")')
44 n7 = _note(session, year, "7", "مشتق", "BS", "Assets")
45 _row(session, n7, 0, "مشتق", 'note("6", 0) + 5')
46 session.flush()
47 result = evaluate_note(session, n7.id)
48 assert result["rows"][0]["value"] == "105"
49
50 def test_circular_reference_detected(self, session):
51 year = _seed(session)
52 n6 = _note(session, year, "6", "الف", "BS", "Assets")
53 _row(session, n6, 0, "a", 'note("7", 0)')
54 n7 = _note(session, year, "7", "ب", "BS", "Assets")
55 _row(session, n7, 0, "b", 'note("6", 0)')
56 session.flush()
57 with pytest.raises(NoteEngineError, match="circular"):
58 evaluate_note(session, n6.id)
59
60 def test_forward_row_reference_rejected(self, session):
61 year = _seed(session)
62 note = _note(session, year, "6", "نقد", "BS", "Assets")
63 _row(session, note, 0, "bad", "row(1)")
64 _row(session, note, 1, "x", 'acct("148348")')
65 session.flush()
66 with pytest.raises(NoteEngineError):
67 evaluate_note(session, note.id)
68
69 def test_unknown_account_rejected(self, session):
70 year = _seed(session)
71 note = _note(session, year, "6", "نقد", "BS", "Assets")
72 _row(session, note, 0, "bad", 'acct("999999")')
73 session.flush()
74 with pytest.raises(NoteEngineError):
75 evaluate_note(session, note.id)
76
77 def test_evaluate_year_notes(self, session):
78 year = _seed(session)
79 note = _note(session, year, "6", "نقد", "BS", "Assets")
80 _row(session, note, 0, "نقد", 'acct("148348")')
81 session.flush()
82 results = evaluate_year_notes(session, year.id)
83 assert len(results) == 1
84 assert results[0]["rows"][0]["value"] == "100"
85
86
87 class TestNoteEngineApi:
88 def test_create_rows_and_evaluate(self, api_client):
89 c = api_client
90 year_id = c.post(
91 "/api/v1/companies", json={"code": "N3", "name": "x", "year": 1404}
92 ).json()["year_id"]
93 csv = (
94 "حساب,kol,ACCOUNT_NAME,year,نوع,T1_CODE,T2_CODE,T3_CODE,T4_CODE,OPENING,Debit,Credit,CLOSING\n"
95 "8348,14,نقد,1404,O,,,,,0,100,0,100\n"
96 ).encode()
97 c.post(f"/api/v1/years/{year_id}/ledger", files={"file": ("l.csv", csv, "text/csv")})
98 note_id = c.post(
99 f"/api/v1/years/{year_id}/notes",
100 json={"note_number": "6", "title": "نقد", "statement": "BS", "section": "Assets"},
101 ).json()["note_id"]
102 r = c.post(
103 f"/api/v1/years/{year_id}/notes/{note_id}/rows",
104 json={"row_order": 0, "label": "نقد", "formula_logic": 'acct("148348")'},
105 )
106 assert r.status_code == 200, r.text
107 r = c.get(f"/api/v1/notes/{note_id}/evaluate")
108 assert r.status_code == 200, r.text
109 assert r.json()["rows"][0]["value"] == "100"
110
111 def test_invalid_formula_rejected_on_create(self, api_client):
112 c = api_client
113 year_id = c.post(
114 "/api/v1/companies", json={"code": "N4", "name": "x", "year": 1404}
115 ).json()["year_id"]
116 note_id = c.post(
117 f"/api/v1/years/{year_id}/notes",
118 json={"note_number": "6", "title": "نقد", "statement": "BS", "section": "Assets"},
119 ).json()["note_id"]
120 r = c.post(
121 f"/api/v1/years/{year_id}/notes/{note_id}/rows",
122 json={"row_order": 0, "label": "bad", "formula_logic": "__import__('os')"},
123 )
124 assert r.status_code == 422