codex/aram-gate3-coverage-target-guard-20261030
add-index-html-10623938925177053894
arena/094f314c-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
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 | import pytest |
| 2 | |
| 3 | from aram_backend.mapping_coverage import inspect_mapping_coverage |
| 4 | from aram_backend.mapping_service import MappingValidationError |
| 5 | from aram_backend.models import ( |
| 6 | AccountVersion, Company, FiscalYear, MappingRule, NoteDefinition, |
| 7 | ) |
| 8 | |
| 9 | |
| 10 | def year_with_accounts(session, code, codes=("001", "002", "003")): |
| 11 | company = Company(code=code, name="Coverage test") |
| 12 | year = FiscalYear(company=company, year=1404) |
| 13 | session.add(year) |
| 14 | session.flush() |
| 15 | for account_code in codes: |
| 16 | session.add(AccountVersion(year_id=year.id, account_code=account_code, |
| 17 | account_name=account_code, version=1)) |
| 18 | notes = [NoteDefinition(year_id=year.id, note_number=str(i), title=str(i), version=1) |
| 19 | for i in (1, 2)] |
| 20 | session.add_all(notes) |
| 21 | session.commit() |
| 22 | return year, notes |
| 23 | |
| 24 | |
| 25 | def rule(session, year, key, code, note_id, version=1): |
| 26 | session.add(MappingRule(year_id=year.id, rule_key=key, account_code=code, |
| 27 | target_note_id=note_id, mapping_type="direct", version=version)) |
| 28 | |
| 29 | |
| 30 | def test_empty_queue_without_mappings(session): |
| 31 | year, _ = year_with_accounts(session, "COVER-A") |
| 32 | coverage = inspect_mapping_coverage(session, year_id=year.id) |
| 33 | assert coverage.total_accounts == 3 |
| 34 | assert coverage.mapped_accounts == () |
| 35 | assert coverage.unmapped_accounts == ("001", "002", "003") |
| 36 | assert coverage.ambiguous_accounts == () |
| 37 | |
| 38 | |
| 39 | def test_mapped_and_ambiguous_queues(session): |
| 40 | year, notes = year_with_accounts(session, "COVER-B") |
| 41 | rule(session, year, "a", "001", notes[0].id) |
| 42 | rule(session, year, "b", "002", notes[0].id) |
| 43 | rule(session, year, "c", "002", notes[1].id) |
| 44 | session.commit() |
| 45 | coverage = inspect_mapping_coverage(session, year_id=year.id) |
| 46 | assert coverage.mapped_accounts == ("001",) |
| 47 | assert coverage.ambiguous_accounts == ("002",) |
| 48 | assert coverage.unmapped_accounts == ("003",) |
| 49 | assert coverage.total_accounts == 3 |
| 50 | |
| 51 | |
| 52 | def test_multiple_rules_same_note_are_not_ambiguous(session): |
| 53 | year, notes = year_with_accounts(session, "COVER-C", ("001",)) |
| 54 | rule(session, year, "a", "001", notes[0].id) |
| 55 | rule(session, year, "b", "001", notes[0].id) |
| 56 | session.commit() |
| 57 | assert inspect_mapping_coverage(session, year_id=year.id).mapped_accounts == ("001",) |
| 58 | |
| 59 | |
| 60 | def test_latest_rule_version_supersedes_prior_version(session): |
| 61 | year, notes = year_with_accounts(session, "COVER-D", ("001",)) |
| 62 | rule(session, year, "a", "001", notes[0].id, version=1) |
| 63 | rule(session, year, "a", "001", notes[1].id, version=2) |
| 64 | session.commit() |
| 65 | coverage = inspect_mapping_coverage(session, year_id=year.id) |
| 66 | assert coverage.mapped_accounts == ("001",) |
| 67 | assert coverage.ambiguous_accounts == () |
| 68 | |
| 69 | |
| 70 | def test_latest_account_version_counted_once(session): |
| 71 | year, _ = year_with_accounts(session, "COVER-E", ("001",)) |
| 72 | session.add(AccountVersion(year_id=year.id, account_code="001", account_name="Updated", version=2)) |
| 73 | session.commit() |
| 74 | coverage = inspect_mapping_coverage(session, year_id=year.id) |
| 75 | assert coverage.total_accounts == 1 |
| 76 | assert coverage.unmapped_accounts == ("001",) |
| 77 | |
| 78 | |
| 79 | def test_orphan_rule_and_cross_year_isolation(session): |
| 80 | year, notes = year_with_accounts(session, "COVER-F", ("001",)) |
| 81 | other, other_notes = year_with_accounts(session, "COVER-G", ("001",)) |
| 82 | rule(session, year, "orphan", "999", notes[0].id) |
| 83 | rule(session, other, "other", "001", other_notes[0].id) |
| 84 | session.commit() |
| 85 | coverage = inspect_mapping_coverage(session, year_id=year.id) |
| 86 | assert coverage.orphan_rule_keys == ("orphan",) |
| 87 | assert coverage.unmapped_accounts == ("001",) |
| 88 | assert coverage.mapped_accounts == () |
| 89 | |
| 90 | |
| 91 | @pytest.mark.parametrize("year_id", [0, -1, True, "1", None, 99999]) |
| 92 | def test_invalid_or_unknown_year_rejected(session, year_id): |
| 93 | with pytest.raises(MappingValidationError): |
| 94 | inspect_mapping_coverage(session, year_id=year_id) |
| 95 | |
| 96 | |
| 97 | def test_coverage_does_not_change_rows(session): |
| 98 | year, notes = year_with_accounts(session, "COVER-H") |
| 99 | rule(session, year, "a", "001", notes[0].id) |
| 100 | session.commit() |
| 101 | before = session.query(MappingRule).count() |
| 102 | inspect_mapping_coverage(session, year_id=year.id) |
| 103 | assert session.query(MappingRule).count() == before |
| 104 | |
| 105 | |
| 106 | def test_diagnostic_does_not_flush_pending_unrelated_writes(session): |
| 107 | year, _ = year_with_accounts(session, "COVER-I", ("001",)) |
| 108 | pending = AccountVersion(year_id=year.id, account_code="PENDING", account_name="Pending", version=1) |
| 109 | session.add(pending) |
| 110 | coverage = inspect_mapping_coverage(session, year_id=year.id) |
| 111 | assert pending in session.new |
| 112 | assert pending.id is None |
| 113 | assert coverage.total_accounts == 1 |
| 114 | assert coverage.unmapped_accounts == ("001",) |
| 115 | |
| 116 | |
| 117 | def test_stale_same_year_note_is_not_counted_as_mapped(session): |
| 118 | year, notes = year_with_accounts(session, "STALE-A", ("001",)) |
| 119 | rule(session, year, "old_note", "001", notes[0].id) |
| 120 | session.add(NoteDefinition( |
| 121 | year_id=year.id, note_number=notes[0].note_number, |
| 122 | title="Current version", version=2, |
| 123 | )) |
| 124 | session.commit() |
| 125 | |
| 126 | result = inspect_mapping_coverage(session, year_id=year.id) |
| 127 | |
| 128 | assert result.mapped_accounts == () |
| 129 | assert result.unmapped_accounts == ("001",) |
| 130 | assert result.invalid_target_rules == (("old_note", "001", "target_note_stale"),) |
| 131 | |
| 132 | |
| 133 | def test_cross_year_note_target_is_not_counted_as_mapped(session): |
| 134 | from sqlalchemy import update |
| 135 | |
| 136 | year, notes = year_with_accounts(session, "FOREIGN-A", ("001",)) |
| 137 | foreign, foreign_notes = year_with_accounts(session, "FOREIGN-B", ("001",)) |
| 138 | rule(session, year, "foreign", "001", notes[0].id) |
| 139 | session.commit() |
| 140 | # This is intentionally an ORM event bypass via SQLAlchemy Core SQL. |
| 141 | session.execute( |
| 142 | update(MappingRule) |
| 143 | .where(MappingRule.year_id == year.id, MappingRule.rule_key == "foreign") |
| 144 | .values(target_note_id=foreign_notes[0].id) |
| 145 | ) |
| 146 | session.commit() |
| 147 | session.expire_all() |
| 148 | |
| 149 | result = inspect_mapping_coverage(session, year_id=year.id) |
| 150 | |
| 151 | assert result.mapped_accounts == () |
| 152 | assert result.unmapped_accounts == ("001",) |
| 153 | assert result.invalid_target_rules == (("foreign", "001", "target_note_wrong_year"),) |
| 154 | |
| 155 | |
| 156 | def test_one_valid_and_one_stale_rule_does_not_count_as_clean_mapping(session): |
| 157 | year, notes = year_with_accounts(session, "MIXED-A", ("001",)) |
| 158 | rule(session, year, "valid", "001", notes[1].id) |
| 159 | rule(session, year, "stale", "001", notes[0].id) |
| 160 | session.add(NoteDefinition( |
| 161 | year_id=year.id, note_number=notes[0].note_number, |
| 162 | title="Current version", version=2, |
| 163 | )) |
| 164 | session.commit() |
| 165 | |
| 166 | result = inspect_mapping_coverage(session, year_id=year.id) |
| 167 | |
| 168 | assert result.mapped_accounts == () |
| 169 | assert result.unmapped_accounts == ("001",) |
| 170 | assert result.ambiguous_accounts == () |
| 171 | assert result.invalid_target_rules == (("stale", "001", "target_note_stale"),) |
| 172 | |
| 173 | |
| 174 | def test_latest_note_version_target_is_clean_and_read_only(session): |
| 175 | year, notes = year_with_accounts(session, "LATEST-A", ("001",)) |
| 176 | current = NoteDefinition(year_id=year.id, note_number=notes[0].note_number, title="Current", version=2) |
| 177 | session.add(current) |
| 178 | session.flush() |
| 179 | rule(session, year, "ok", "001", current.id) |
| 180 | session.commit() |
| 181 | |
| 182 | pending = AccountVersion(year_id=year.id, account_code="X", account_name="Pending", version=1) |
| 183 | session.add(pending) |
| 184 | result = inspect_mapping_coverage(session, year_id=year.id) |
| 185 | |
| 186 | assert result.mapped_accounts == ("001",) |
| 187 | assert result.invalid_target_rules == () |
| 188 | assert pending in session.new |
| 189 | assert pending.id is None |