Mirrored from https://github.com/davoodmehraban89/Aram.git
Synced Dec 4, 15:30 UTC
arena/2d4eda07-aram
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
| 1 | """Exact-money tests (P-02, D-03, OD-03), including Hypothesis property tests.""" |
| 2 | |
| 3 | from decimal import Decimal |
| 4 | |
| 5 | import pytest |
| 6 | from hypothesis import given, settings |
| 7 | from hypothesis import strategies as st |
| 8 | from sqlalchemy.exc import StatementError |
| 9 | |
| 10 | from aram_backend.domain.money import ( |
| 11 | Rial, |
| 12 | allocate_remainder, |
| 13 | round_half_up, |
| 14 | ) |
| 15 | |
| 16 | rial = Rial() |
| 17 | |
| 18 | |
| 19 | class TestRialRoundTrip: |
| 20 | @given(st.integers(min_value=-(10**18 - 1), max_value=10**18 - 1)) |
| 21 | @settings(max_examples=300) |
| 22 | def test_exact_roundtrip_up_to_18_digits(self, n): |
| 23 | value = Decimal(n) |
| 24 | stored = rial.process_bind_param(value, dialect=None) |
| 25 | assert isinstance(stored, int) |
| 26 | assert rial.process_result_value(stored, dialect=None) == value |
| 27 | |
| 28 | def test_none_roundtrip(self): |
| 29 | assert rial.process_bind_param(None, dialect=None) is None |
| 30 | assert rial.process_result_value(None, dialect=None) is None |
| 31 | |
| 32 | def test_float_rejected(self): |
| 33 | with pytest.raises(TypeError): |
| 34 | rial.process_bind_param(1.5, dialect=None) |
| 35 | |
| 36 | def test_fractional_rial_rejected(self): |
| 37 | with pytest.raises(ValueError): |
| 38 | rial.process_bind_param(Decimal("1.5"), dialect=None) |
| 39 | |
| 40 | def test_non_finite_rejected(self): |
| 41 | with pytest.raises(ValueError): |
| 42 | rial.process_bind_param(Decimal("NaN"), dialect=None) |
| 43 | |
| 44 | def test_int_and_text_accepted(self): |
| 45 | assert rial.process_bind_param(100, dialect=None) == 100 |
| 46 | assert rial.process_bind_param("100", dialect=None) == 100 |
| 47 | |
| 48 | def test_large_rial_no_silent_rounding(self): |
| 49 | big = Decimal("10823163409740108") |
| 50 | assert ( |
| 51 | rial.process_result_value(rial.process_bind_param(big, dialect=None), dialect=None) |
| 52 | == big |
| 53 | ) |
| 54 | |
| 55 | |
| 56 | class TestRoundHalfUp: |
| 57 | def test_half_up(self): |
| 58 | assert round_half_up(Decimal("2.5")) == Decimal("3") |
| 59 | assert round_half_up(Decimal("2.4")) == Decimal("2") |
| 60 | assert round_half_up(Decimal("-2.5")) == Decimal("-3") |
| 61 | |
| 62 | def test_places(self): |
| 63 | assert round_half_up(Decimal("1.2345"), 2) == Decimal("1.23") |
| 64 | assert round_half_up(Decimal("1.2355"), 2) == Decimal("1.24") |
| 65 | |
| 66 | |
| 67 | class TestAllocateRemainder: |
| 68 | def test_rows_sum_to_rounded_total(self): |
| 69 | values = [Decimal("0.5"), Decimal("0.5"), Decimal("0.5")] |
| 70 | total = sum(values) |
| 71 | result = allocate_remainder(values, total, 0) |
| 72 | assert sum(result) == round_half_up(total, 0) |
| 73 | |
| 74 | def test_empty(self): |
| 75 | assert allocate_remainder([], Decimal(0), 0) == [] |
| 76 | |
| 77 | @given( |
| 78 | st.lists( |
| 79 | st.integers(min_value=-(10**9), max_value=10**9).map(lambda x: Decimal(x) / 100), |
| 80 | min_size=1, |
| 81 | max_size=25, |
| 82 | ) |
| 83 | ) |
| 84 | @settings(max_examples=200) |
| 85 | def test_property_sum_matches_rounded_total(self, values): |
| 86 | total = sum(values) |
| 87 | for places in (0, 2): |
| 88 | result = allocate_remainder(values, total, places) |
| 89 | assert sum(result) == round_half_up(total, places) |
| 90 | assert len(result) == len(values) |
| 91 | |
| 92 | |
| 93 | class TestP02NoSilentRoundingGuard: |
| 94 | """Owner-audit regression: fractional rial must fail loudly, never round.""" |
| 95 | |
| 96 | @pytest.mark.parametrize("bad", ["1.5", "100.5", "100.0001", "0.1", "-2.25"]) |
| 97 | def test_fractional_text_rejected_not_rounded(self, bad): |
| 98 | with pytest.raises(ValueError): |
| 99 | rial.process_bind_param(bad, dialect=None) |
| 100 | |
| 101 | def test_boundary_half_rial_rejected(self): |
| 102 | # 100.5 must NOT become 100 or 101; it must raise. |
| 103 | with pytest.raises(ValueError): |
| 104 | rial.process_bind_param(Decimal("100.5"), dialect=None) |
| 105 | |
| 106 | def test_fractional_through_orm_rejected(self, session): |
| 107 | from aram_backend.ingestion import register_source_document |
| 108 | from aram_backend.models import Company, FiscalYear, LedgerBalance |
| 109 | |
| 110 | company = Company(code="P02", name="x") |
| 111 | session.add(company) |
| 112 | session.flush() |
| 113 | year = FiscalYear(company_id=company.id, year=1404) |
| 114 | session.add(year) |
| 115 | session.flush() |
| 116 | source = register_source_document( |
| 117 | session, |
| 118 | year_id=year.id, |
| 119 | filename="l.xlsx", |
| 120 | document_type="general_ledger", |
| 121 | content=b"p02", |
| 122 | ) |
| 123 | session.flush() |
| 124 | session.add( |
| 125 | LedgerBalance( |
| 126 | year_id=year.id, |
| 127 | source_document_id=source.id, |
| 128 | source_sheet="Source", |
| 129 | source_row=3, |
| 130 | account_full_code="148348", |
| 131 | account_name="x", |
| 132 | closing=Decimal("100.5"), |
| 133 | ) |
| 134 | ) |
| 135 | with pytest.raises(StatementError, match="fractional rial"): |
| 136 | session.flush() |
| 137 | session.rollback() |
| 138 | |
| 139 | def test_fractional_csv_import_fails_loudly(self, session): |
| 140 | from aram_backend.ingestion import register_source_document |
| 141 | from aram_backend.ledger_parser import parse_general_ledger_csv |
| 142 | from aram_backend.models import Company, FiscalYear |
| 143 | from aram_backend.services.ledger_import import import_ledger_balances |
| 144 | |
| 145 | company = Company(code="P02C", name="x") |
| 146 | session.add(company) |
| 147 | session.flush() |
| 148 | year = FiscalYear(company_id=company.id, year=1404) |
| 149 | session.add(year) |
| 150 | session.flush() |
| 151 | source = register_source_document( |
| 152 | session, |
| 153 | year_id=year.id, |
| 154 | filename="l.csv", |
| 155 | document_type="general_ledger", |
| 156 | content=b"p02c", |
| 157 | ) |
| 158 | session.flush() |
| 159 | csv = ( |
| 160 | "حساب,kol,ACCOUNT_NAME,year,نوع,T1_CODE,T2_CODE,T3_CODE,T4_CODE,OPENING,Debit,Credit,CLOSING\n" |
| 161 | "8348,14,x,1404,O,,,,,0,100.5,0,100.5\n" |
| 162 | ).encode() |
| 163 | records = list(parse_general_ledger_csv(csv)) |
| 164 | with pytest.raises(StatementError, match="fractional rial"): |
| 165 | import_ledger_balances( |
| 166 | session, year_id=year.id, source_document_id=source.id, records=records |
| 167 | ) |
| 168 | session.rollback() |