Mirrored from https://github.com/davoodmehraban89/Aram.git
Synced Dec 4, 14:15 UTC
dependabot/github_actions/actions/setup-node-6
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 | """D-06 acceptance: content-addressed source storage + hash-verified re-import.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import hashlib |
| 6 | import io |
| 7 | |
| 8 | import pytest |
| 9 | from openpyxl import Workbook |
| 10 | |
| 11 | from aram_backend.ingestion import register_source_document |
| 12 | from aram_backend.ledger_parser import parse_general_ledger, parse_ledger |
| 13 | from aram_backend.models import Company, FiscalYear, SourceFile |
| 14 | from aram_backend.services.source_store import SourceStore, SourceStoreError |
| 15 | |
| 16 | |
| 17 | def _xlsx_bytes(): |
| 18 | wb = Workbook() |
| 19 | ws = wb.active |
| 20 | ws.title = "Source" |
| 21 | ws.append(["source ledger"]) |
| 22 | ws.append( |
| 23 | [ |
| 24 | "حساب", |
| 25 | "kol", |
| 26 | "ACCOUNT_NAME", |
| 27 | "year", |
| 28 | "نوع", |
| 29 | "T1_CODE", |
| 30 | "T2_CODE", |
| 31 | "T3_CODE", |
| 32 | "T4_CODE", |
| 33 | "OPENING", |
| 34 | "Debit", |
| 35 | "Credit", |
| 36 | "CLOSING", |
| 37 | ] |
| 38 | ) |
| 39 | ws.append(["0101", "14", "نمونه", 1404, "O", None, None, None, None, 0, 100, 0, 100]) |
| 40 | buf = io.BytesIO() |
| 41 | wb.save(buf) |
| 42 | wb.close() |
| 43 | return buf.getvalue() |
| 44 | |
| 45 | |
| 46 | def _make_year(session): |
| 47 | company = Company(code="SRC", name="شرکت") |
| 48 | session.add(company) |
| 49 | session.flush() |
| 50 | year = FiscalYear(company_id=company.id, year=1404) |
| 51 | session.add(year) |
| 52 | session.flush() |
| 53 | return year |
| 54 | |
| 55 | |
| 56 | class TestSourceStore: |
| 57 | def test_put_get_roundtrip(self, tmp_path): |
| 58 | store = SourceStore(tmp_path / "store") |
| 59 | content = b"immutable source bytes" |
| 60 | stored = store.put(content) |
| 61 | assert stored.sha256 == hashlib.sha256(content).hexdigest() |
| 62 | assert stored.size == len(content) |
| 63 | assert store.get(stored.sha256) == content |
| 64 | |
| 65 | def test_put_idempotent(self, tmp_path): |
| 66 | store = SourceStore(tmp_path / "store") |
| 67 | a = store.put(b"same") |
| 68 | b = store.put(b"same") |
| 69 | assert a.sha256 == b.sha256 |
| 70 | assert a.stored_path == b.stored_path |
| 71 | |
| 72 | def test_empty_rejected(self, tmp_path): |
| 73 | with pytest.raises(SourceStoreError): |
| 74 | SourceStore(tmp_path).put(b"") |
| 75 | |
| 76 | def test_corruption_detected(self, tmp_path): |
| 77 | store = SourceStore(tmp_path / "store") |
| 78 | stored = store.put(b"original") |
| 79 | # Tamper with the stored bytes; the hash check must fail loudly. |
| 80 | from pathlib import Path |
| 81 | |
| 82 | Path(stored.stored_path).write_bytes(b"tampered") |
| 83 | with pytest.raises(SourceStoreError): |
| 84 | store.get(stored.sha256) |
| 85 | |
| 86 | def test_missing_file_raises(self, tmp_path): |
| 87 | with pytest.raises(SourceStoreError): |
| 88 | SourceStore(tmp_path).get("0" * 64) |
| 89 | |
| 90 | |
| 91 | class TestRegistrationWithStore: |
| 92 | def test_bytes_persisted_and_reimport_matches_hash(self, session, tmp_path): |
| 93 | year = _make_year(session) |
| 94 | store = SourceStore(tmp_path / "store") |
| 95 | content = _xlsx_bytes() |
| 96 | source = register_source_document( |
| 97 | session, |
| 98 | year_id=year.id, |
| 99 | filename="ledger.xlsx", |
| 100 | document_type="general_ledger", |
| 101 | content=content, |
| 102 | store=store, |
| 103 | ) |
| 104 | session.commit() |
| 105 | assert source.source_file_hash == hashlib.sha256(content).hexdigest() |
| 106 | assert not hasattr(source, "content") # metadata-only model (accepted contract) |
| 107 | sf = session.get(SourceFile, source.source_file_hash) |
| 108 | assert sf is not None and sf.size == len(content) |
| 109 | # Re-import from the stored bytes: hash matches and rows parse identically. |
| 110 | retrieved = store.get(source.source_file_hash) |
| 111 | assert hashlib.sha256(retrieved).hexdigest() == source.file_hash |
| 112 | original = list(parse_general_ledger(content)) |
| 113 | again = list(parse_ledger(retrieved, "ledger.xlsx")) |
| 114 | assert again == original |
| 115 | |
| 116 | def test_without_store_backward_compatible(self, session): |
| 117 | year = _make_year(session) |
| 118 | source = register_source_document( |
| 119 | session, |
| 120 | year_id=year.id, |
| 121 | filename="ledger.xlsx", |
| 122 | document_type="general_ledger", |
| 123 | content=_xlsx_bytes(), |
| 124 | ) |
| 125 | session.commit() |
| 126 | assert source.source_file_hash is None |