Mirrored from https://github.com/davoodmehraban89/Aram.git
Synced Dec 3, 19:30 UTC
codex/aram-gate3-integration-validation-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 | from sqlalchemy.exc import IntegrityError |
| 3 | |
| 4 | from aram_backend.models import ( |
| 5 | AccountVersion, |
| 6 | Company, |
| 7 | FinancialStatement, |
| 8 | FiscalYear, |
| 9 | FiscalYearStatus, |
| 10 | MappingRule, |
| 11 | NoteDefinition, |
| 12 | NoteRow, |
| 13 | SourceDocument, |
| 14 | StatementLine, |
| 15 | StatementType, |
| 16 | ) |
| 17 | |
| 18 | |
| 19 | def make_year(session, year=1404): |
| 20 | company = Company(code=f"CO-{year}", name="Test Company") |
| 21 | fiscal_year = FiscalYear(company=company, year=year) |
| 22 | session.add(company) |
| 23 | session.commit() |
| 24 | return fiscal_year |
| 25 | |
| 26 | |
| 27 | def test_model_relationships(session): |
| 28 | year = make_year(session) |
| 29 | note = NoteDefinition(year_id=year.id, note_number="10", title="Assets", version=1) |
| 30 | note.rows.append(NoteRow(row_order=0, label="Tangible assets")) |
| 31 | statement = FinancialStatement(year_id=year.id, statement_type=StatementType.BALANCE_SHEET) |
| 32 | statement.lines.append(StatementLine(line_order=0, line_label="Assets", target_note=note)) |
| 33 | session.add_all([note, statement]) |
| 34 | session.commit() |
| 35 | assert note.rows[0].note_id == note.id |
| 36 | assert statement.lines[0].target_note_id == note.id |
| 37 | |
| 38 | |
| 39 | def test_version_uniqueness(session): |
| 40 | year = make_year(session) |
| 41 | session.add(AccountVersion(year_id=year.id, account_code="1000", account_name="Cash", version=1)) |
| 42 | session.commit() |
| 43 | session.add(AccountVersion(year_id=year.id, account_code="1000", account_name="Cash duplicate", version=1)) |
| 44 | with pytest.raises(IntegrityError): |
| 45 | session.commit() |
| 46 | |
| 47 | |
| 48 | def test_source_document_hash_unique_within_year(session): |
| 49 | year = make_year(session) |
| 50 | session.add(SourceDocument(year_id=year.id, filename="a.xlsx", file_hash="abc", document_type="ledger")) |
| 51 | session.commit() |
| 52 | session.add(SourceDocument(year_id=year.id, filename="b.xlsx", file_hash="abc", document_type="ledger")) |
| 53 | with pytest.raises(IntegrityError): |
| 54 | session.commit() |
| 55 | |
| 56 | |
| 57 | def test_fiscal_year_isolation_for_mapping(session): |
| 58 | first = make_year(session, 1403) |
| 59 | second = make_year(session, 1404) |
| 60 | note = NoteDefinition(year_id=first.id, note_number="10", title="Assets") |
| 61 | session.add(note) |
| 62 | session.commit() |
| 63 | session.add(MappingRule( |
| 64 | year_id=second.id, rule_key="rule-1", account_code="1000", |
| 65 | target_note=note, mapping_type="O" |
| 66 | )) |
| 67 | with pytest.raises(ValueError, match="same fiscal year"): |
| 68 | session.commit() |
| 69 | |
| 70 | |
| 71 | def test_fiscal_year_isolation_cannot_be_bypassed_with_raw_ids(session): |
| 72 | first = make_year(session, 1403) |
| 73 | second = make_year(session, 1404) |
| 74 | note = NoteDefinition(year_id=first.id, note_number="10", title="Assets") |
| 75 | session.add(note) |
| 76 | session.commit() |
| 77 | session.add(MappingRule( |
| 78 | year_id=second.id, rule_key="raw-id-rule", account_code="1000", |
| 79 | target_note_id=note.id, mapping_type="O" |
| 80 | )) |
| 81 | with pytest.raises(ValueError, match="same fiscal year"): |
| 82 | session.commit() |
| 83 | |
| 84 | |
| 85 | def test_statement_line_isolation_cannot_be_bypassed_with_raw_ids(session): |
| 86 | first = make_year(session, 1403) |
| 87 | second = make_year(session, 1404) |
| 88 | note = NoteDefinition(year_id=first.id, note_number="10", title="Assets") |
| 89 | statement = FinancialStatement( |
| 90 | year_id=second.id, statement_type=StatementType.BALANCE_SHEET |
| 91 | ) |
| 92 | session.add_all([note, statement]) |
| 93 | session.commit() |
| 94 | session.add(StatementLine( |
| 95 | statement_id=statement.id, line_order=0, line_label="Assets", |
| 96 | target_note_id=note.id |
| 97 | )) |
| 98 | with pytest.raises(ValueError, match="same fiscal year"): |
| 99 | session.commit() |
| 100 | |
| 101 | |
| 102 | def test_locked_year_blocks_mutation(session): |
| 103 | year = make_year(session) |
| 104 | year.status = FiscalYearStatus.APPROVED |
| 105 | session.commit() |
| 106 | year.status = FiscalYearStatus.LOCKED |
| 107 | session.commit() |
| 108 | session.add(AccountVersion(year_id=year.id, account_code="1000", account_name="Cash")) |
| 109 | with pytest.raises(ValueError, match="locked"): |
| 110 | session.commit() |
| 111 | |
| 112 | |
| 113 | def test_locked_year_protection_cannot_be_bypassed_with_raw_parent_id(session): |
| 114 | year = make_year(session) |
| 115 | statement = FinancialStatement( |
| 116 | year_id=year.id, statement_type=StatementType.BALANCE_SHEET |
| 117 | ) |
| 118 | session.add(statement) |
| 119 | session.commit() |
| 120 | year.status = FiscalYearStatus.APPROVED |
| 121 | session.commit() |
| 122 | year.status = FiscalYearStatus.LOCKED |
| 123 | session.commit() |
| 124 | session.add(StatementLine( |
| 125 | statement_id=statement.id, line_order=0, line_label="Blocked" |
| 126 | )) |
| 127 | with pytest.raises(ValueError, match="locked"): |
| 128 | session.commit() |
| 129 | |
| 130 | |
| 131 | def test_approved_year_blocks_silent_overwrite(session): |
| 132 | year = make_year(session) |
| 133 | account = AccountVersion( |
| 134 | year_id=year.id, account_code="1000", account_name="Cash" |
| 135 | ) |
| 136 | session.add(account) |
| 137 | session.commit() |
| 138 | year.status = FiscalYearStatus.APPROVED |
| 139 | session.commit() |
| 140 | account.account_name = "Silently changed cash" |
| 141 | with pytest.raises(ValueError, match="approved"): |
| 142 | session.commit() |
| 143 | |
| 144 | |
| 145 | def test_locked_year_cannot_be_reopened(session): |
| 146 | year = make_year(session) |
| 147 | year.status = FiscalYearStatus.APPROVED |
| 148 | session.commit() |
| 149 | year.status = FiscalYearStatus.LOCKED |
| 150 | session.commit() |
| 151 | year.status = FiscalYearStatus.DRAFT |
| 152 | with pytest.raises(ValueError, match="Invalid fiscal-year transition"): |
| 153 | session.commit() |
| 154 | |
| 155 | |
| 156 | def test_invalid_status_skip_is_rejected(session): |
| 157 | year = make_year(session) |
| 158 | year.status = FiscalYearStatus.LOCKED |
| 159 | with pytest.raises(ValueError, match="Invalid fiscal-year transition"): |
| 160 | session.commit() |
| 161 | |
| 162 | |
| 163 | # These adversarial cases reproduce the V1 audit bypasses; keep them as release gates. |
| 164 | @pytest.mark.parametrize("status", [FiscalYearStatus.APPROVED, FiscalYearStatus.LOCKED]) |
| 165 | def test_protected_year_identity_cannot_be_mutated(session, status): |
| 166 | year = make_year(session) |
| 167 | year.status = FiscalYearStatus.APPROVED |
| 168 | session.commit() |
| 169 | if status is FiscalYearStatus.LOCKED: |
| 170 | year.status = FiscalYearStatus.LOCKED |
| 171 | session.commit() |
| 172 | |
| 173 | year.year = 1405 |
| 174 | with pytest.raises(ValueError, match="identity cannot be mutated"): |
| 175 | session.commit() |
| 176 | |
| 177 | |
| 178 | @pytest.mark.parametrize("status", [FiscalYearStatus.APPROVED, FiscalYearStatus.LOCKED]) |
| 179 | def test_protected_year_cannot_be_deleted(session, status): |
| 180 | year = make_year(session) |
| 181 | year.status = FiscalYearStatus.APPROVED |
| 182 | session.commit() |
| 183 | if status is FiscalYearStatus.LOCKED: |
| 184 | year.status = FiscalYearStatus.LOCKED |
| 185 | session.commit() |
| 186 | |
| 187 | session.delete(year) |
| 188 | with pytest.raises(ValueError, match="cannot be deleted"): |
| 189 | session.commit() |
| 190 | |
| 191 | |
| 192 | def test_company_with_protected_year_cannot_be_mutated(session): |
| 193 | year = make_year(session) |
| 194 | year.status = FiscalYearStatus.APPROVED |
| 195 | session.commit() |
| 196 | |
| 197 | year.company.name = "Changed after approval" |
| 198 | with pytest.raises(ValueError, match="cannot be mutated"): |
| 199 | session.commit() |
| 200 | |
| 201 | |
| 202 | def test_company_with_protected_year_cannot_be_deleted(session): |
| 203 | year = make_year(session) |
| 204 | year.status = FiscalYearStatus.APPROVED |
| 205 | session.commit() |
| 206 | |
| 207 | session.delete(year.company) |
| 208 | with pytest.raises(ValueError, match="cannot be deleted"): |
| 209 | session.commit() |
| 210 | |
| 211 | |
| 212 | def test_protected_year_cannot_be_reparented_through_relationship(session): |
| 213 | year = make_year(session) |
| 214 | replacement = Company(code="REPLACEMENT", name="Replacement Company") |
| 215 | session.add(replacement) |
| 216 | session.commit() |
| 217 | year.status = FiscalYearStatus.APPROVED |
| 218 | session.commit() |
| 219 | |
| 220 | year.company = replacement |
| 221 | with pytest.raises(ValueError, match="cannot be mutated"): |
| 222 | session.commit() |
| 223 | |
| 224 | |
| 225 | def test_draft_year_identity_and_deletion_remain_mutable(session): |
| 226 | year = make_year(session) |
| 227 | year.year = 1405 |
| 228 | session.commit() |
| 229 | assert year.year == 1405 |
| 230 | |
| 231 | session.delete(year) |
| 232 | session.commit() |
| 233 | assert session.get(FiscalYear, year.id) is None |
| 234 | |
| 235 | |
| 236 | @pytest.mark.parametrize("status", [FiscalYearStatus.APPROVED, FiscalYearStatus.LOCKED]) |
| 237 | @pytest.mark.parametrize("expire_owner", [False, True]) |
| 238 | @pytest.mark.parametrize("record_kind", ["account", "source", "note", "statement"]) |
| 239 | def test_record_cannot_leave_protected_year(session, status, expire_owner, record_kind): |
| 240 | original = make_year(session, 1403) |
| 241 | destination = make_year(session, 1404) |
| 242 | records = { |
| 243 | "account": lambda: AccountVersion( |
| 244 | year_id=original.id, account_code="1000", account_name="Cash" |
| 245 | ), |
| 246 | "source": lambda: SourceDocument( |
| 247 | year_id=original.id, filename="synthetic.csv", file_hash="synthetic", |
| 248 | document_type="general_ledger", |
| 249 | ), |
| 250 | "note": lambda: NoteDefinition( |
| 251 | year_id=original.id, note_number="10", title="Assets" |
| 252 | ), |
| 253 | "statement": lambda: FinancialStatement( |
| 254 | year_id=original.id, statement_type=StatementType.BALANCE_SHEET |
| 255 | ), |
| 256 | } |
| 257 | record = records[record_kind]() |
| 258 | session.add(record) |
| 259 | session.commit() |
| 260 | original.status = FiscalYearStatus.APPROVED |
| 261 | session.commit() |
| 262 | if status is FiscalYearStatus.LOCKED: |
| 263 | original.status = FiscalYearStatus.LOCKED |
| 264 | session.commit() |
| 265 | record_id, original_id = record.id, original.id |
| 266 | if expire_owner: |
| 267 | session.expire(record, ["year_id"]) |
| 268 | |
| 269 | record.year_id = destination.id |
| 270 | with pytest.raises(ValueError, match="cannot be mutated"): |
| 271 | session.commit() |
| 272 | session.rollback() |
| 273 | session.expire_all() |
| 274 | assert session.get(type(record), record_id).year_id == original_id |
| 275 | |
| 276 | |
| 277 | @pytest.mark.parametrize("status", [FiscalYearStatus.APPROVED, FiscalYearStatus.LOCKED]) |
| 278 | def test_mapping_cannot_leave_protected_year_with_new_target(session, status): |
| 279 | original = make_year(session, 1403) |
| 280 | destination = make_year(session, 1404) |
| 281 | original_note = NoteDefinition(year_id=original.id, note_number="10", title="Old") |
| 282 | new_note = NoteDefinition(year_id=destination.id, note_number="10", title="New") |
| 283 | session.add_all([original_note, new_note]) |
| 284 | session.commit() |
| 285 | rule = MappingRule( |
| 286 | year_id=original.id, rule_key="cash", account_code="1000", |
| 287 | mapping_type="O", target_note=original_note, |
| 288 | ) |
| 289 | session.add(rule) |
| 290 | session.commit() |
| 291 | original.status = FiscalYearStatus.APPROVED |
| 292 | session.commit() |
| 293 | if status is FiscalYearStatus.LOCKED: |
| 294 | original.status = FiscalYearStatus.LOCKED |
| 295 | session.commit() |
| 296 | original_id, rule_id, note_id = original.id, rule.id, original_note.id |
| 297 | |
| 298 | rule.year_id = destination.id |
| 299 | rule.target_note = new_note |
| 300 | with pytest.raises(ValueError, match="cannot be mutated"): |
| 301 | session.commit() |
| 302 | session.rollback() |
| 303 | session.expire_all() |
| 304 | stored = session.get(MappingRule, rule_id) |
| 305 | assert (stored.year_id, stored.target_note_id) == (original_id, note_id) |
| 306 | |
| 307 | |
| 308 | @pytest.mark.parametrize("status", [FiscalYearStatus.APPROVED, FiscalYearStatus.LOCKED]) |
| 309 | @pytest.mark.parametrize("child_kind", ["note_row", "statement_line"]) |
| 310 | @pytest.mark.parametrize("assignment", ["foreign_key", "relationship"]) |
| 311 | def test_child_cannot_leave_protected_parent(session, status, child_kind, assignment): |
| 312 | original = make_year(session, 1403) |
| 313 | destination = make_year(session, 1404) |
| 314 | if child_kind == "note_row": |
| 315 | old_parent = NoteDefinition(year_id=original.id, note_number="10", title="Old") |
| 316 | new_parent = NoteDefinition(year_id=destination.id, note_number="10", title="New") |
| 317 | child = NoteRow(note=old_parent, row_order=0, label="Cash") |
| 318 | foreign_key, relationship = "note_id", "note" |
| 319 | else: |
| 320 | old_parent = FinancialStatement( |
| 321 | year_id=original.id, statement_type=StatementType.BALANCE_SHEET |
| 322 | ) |
| 323 | new_parent = FinancialStatement( |
| 324 | year_id=destination.id, statement_type=StatementType.BALANCE_SHEET |
| 325 | ) |
| 326 | child = StatementLine(statement=old_parent, line_order=0, line_label="Cash") |
| 327 | foreign_key, relationship = "statement_id", "statement" |
| 328 | session.add_all([old_parent, new_parent, child]) |
| 329 | session.commit() |
| 330 | original.status = FiscalYearStatus.APPROVED |
| 331 | session.commit() |
| 332 | if status is FiscalYearStatus.LOCKED: |
| 333 | original.status = FiscalYearStatus.LOCKED |
| 334 | session.commit() |
| 335 | child_id, parent_id = child.id, old_parent.id |
| 336 | if assignment == "foreign_key": |
| 337 | session.expire(child, [relationship]) |
| 338 | setattr(child, foreign_key, new_parent.id) |
| 339 | else: |
| 340 | setattr(child, relationship, new_parent) |
| 341 | |
| 342 | with pytest.raises(ValueError, match="cannot be mutated"): |
| 343 | session.commit() |
| 344 | session.rollback() |
| 345 | session.expire_all() |
| 346 | assert getattr(session.get(type(child), child_id), foreign_key) == parent_id |
| 347 | |
| 348 | |
| 349 | def test_draft_account_edits_and_owner_change_remain_allowed(session): |
| 350 | original = make_year(session, 1403) |
| 351 | destination = make_year(session, 1404) |
| 352 | account = AccountVersion(year_id=original.id, account_code="1000", account_name="Cash") |
| 353 | session.add(account) |
| 354 | session.commit() |
| 355 | |
| 356 | account.year_id = destination.id |
| 357 | account.account_name = "Reviewed cash" |
| 358 | session.commit() |
| 359 | session.expire_all() |
| 360 | stored = session.get(AccountVersion, account.id) |
| 361 | assert (stored.year_id, stored.account_name) == (destination.id, "Reviewed cash") |
| 362 | |
| 363 | |
| 364 | @pytest.mark.parametrize("same_company", [False, True]) |
| 365 | def test_relationship_only_statement_note_cannot_cross_year(session, same_company): |
| 366 | first = make_year(session, 1403) |
| 367 | if same_company: |
| 368 | second = FiscalYear(company=first.company, year=1404) |
| 369 | session.add(second) |
| 370 | session.commit() |
| 371 | else: |
| 372 | second = make_year(session, 1404) |
| 373 | note = NoteDefinition(year_id=first.id, note_number="10", title="Assets") |
| 374 | statement = FinancialStatement( |
| 375 | year_id=second.id, statement_type=StatementType.BALANCE_SHEET |
| 376 | ) |
| 377 | session.add_all([note, statement]) |
| 378 | session.commit() |
| 379 | statement_id = statement.id |
| 380 | session.add(StatementLine( |
| 381 | statement=statement, target_note=note, line_order=0, line_label="Assets" |
| 382 | )) |
| 383 | |
| 384 | with pytest.raises(ValueError, match="same fiscal year"): |
| 385 | session.commit() |
| 386 | session.rollback() |
| 387 | session.expire_all() |
| 388 | assert session.get(FinancialStatement, statement_id).lines == [] |
| 389 | |
| 390 | |
| 391 | @pytest.mark.parametrize("record_kind", ["mapping", "statement_line"]) |
| 392 | def test_cached_note_cannot_hide_cross_year_foreign_key_change(session, record_kind): |
| 393 | first = make_year(session, 1403) |
| 394 | second = make_year(session, 1404) |
| 395 | old_note = NoteDefinition(year_id=first.id, note_number="10", title="Old") |
| 396 | new_note = NoteDefinition(year_id=second.id, note_number="10", title="New") |
| 397 | statement = FinancialStatement( |
| 398 | year_id=first.id, statement_type=StatementType.BALANCE_SHEET |
| 399 | ) |
| 400 | session.add_all([old_note, new_note, statement]) |
| 401 | session.commit() |
| 402 | if record_kind == "mapping": |
| 403 | record = MappingRule( |
| 404 | year_id=first.id, rule_key="cash", account_code="1000", |
| 405 | target_note=old_note, mapping_type="O", |
| 406 | ) |
| 407 | else: |
| 408 | record = StatementLine( |
| 409 | statement=statement, target_note=old_note, line_order=0, line_label="Cash" |
| 410 | ) |
| 411 | session.add(record) |
| 412 | session.commit() |
| 413 | assert record.target_note is old_note |
| 414 | record_id, old_note_id = record.id, old_note.id |
| 415 | |
| 416 | record.target_note_id = new_note.id |
| 417 | with pytest.raises(ValueError, match="same fiscal year"): |
| 418 | session.commit() |
| 419 | session.rollback() |
| 420 | session.expire_all() |
| 421 | assert session.get(type(record), record_id).target_note_id == old_note_id |
| 422 | |
| 423 | |
| 424 | def test_cached_statement_cannot_hide_move_into_locked_year(session): |
| 425 | first = make_year(session, 1403) |
| 426 | second = make_year(session, 1404) |
| 427 | old_statement = FinancialStatement( |
| 428 | year_id=first.id, statement_type=StatementType.BALANCE_SHEET |
| 429 | ) |
| 430 | new_statement = FinancialStatement( |
| 431 | year_id=second.id, statement_type=StatementType.BALANCE_SHEET |
| 432 | ) |
| 433 | line = StatementLine(statement=old_statement, line_order=0, line_label="Cash") |
| 434 | session.add_all([old_statement, new_statement, line]) |
| 435 | session.commit() |
| 436 | second.status = FiscalYearStatus.APPROVED |
| 437 | session.commit() |
| 438 | second.status = FiscalYearStatus.LOCKED |
| 439 | session.commit() |
| 440 | assert line.statement is old_statement |
| 441 | line_id, original_id = line.id, old_statement.id |
| 442 | |
| 443 | line.statement_id = new_statement.id |
| 444 | with pytest.raises(ValueError, match="cannot be mutated"): |
| 445 | session.commit() |
| 446 | session.rollback() |
| 447 | session.expire_all() |
| 448 | assert session.get(StatementLine, line_id).statement_id == original_id |
| 449 | |
| 450 | |
| 451 | @pytest.mark.parametrize("record_kind", ["mapping", "statement_line"]) |
| 452 | def test_conflicting_target_relationship_and_foreign_key_are_rejected(session, record_kind): |
| 453 | year = make_year(session) |
| 454 | first_note = NoteDefinition(year_id=year.id, note_number="10", title="First") |
| 455 | second_note = NoteDefinition(year_id=year.id, note_number="11", title="Second") |
| 456 | statement = FinancialStatement( |
| 457 | year_id=year.id, statement_type=StatementType.BALANCE_SHEET |
| 458 | ) |
| 459 | session.add_all([first_note, second_note, statement]) |
| 460 | session.commit() |
| 461 | if record_kind == "mapping": |
| 462 | record = MappingRule( |
| 463 | year_id=year.id, rule_key="cash", account_code="1000", mapping_type="O", |
| 464 | target_note=first_note, target_note_id=second_note.id, |
| 465 | ) |
| 466 | else: |
| 467 | record = StatementLine( |
| 468 | statement=statement, line_order=0, line_label="Cash", |
| 469 | target_note=first_note, target_note_id=second_note.id, |
| 470 | ) |
| 471 | session.add(record) |
| 472 | |
| 473 | with pytest.raises(ValueError, match="Conflicting"): |
| 474 | session.commit() |
| 475 | session.rollback() |
| 476 | |
| 477 | |
| 478 | def test_statement_allows_same_year_relationship_and_optional_note(session): |
| 479 | year = make_year(session) |
| 480 | note = NoteDefinition(year_id=year.id, note_number="10", title="Assets") |
| 481 | statement = FinancialStatement( |
| 482 | year_id=year.id, statement_type=StatementType.BALANCE_SHEET |
| 483 | ) |
| 484 | linked = StatementLine( |
| 485 | statement=statement, target_note=note, line_order=0, line_label="Cash" |
| 486 | ) |
| 487 | unlinked = StatementLine(statement=statement, line_order=1, line_label="Subtotal") |
| 488 | session.add_all([note, statement, linked, unlinked]) |
| 489 | session.commit() |
| 490 | note_id, linked_id, unlinked_id = note.id, linked.id, unlinked.id |
| 491 | session.expire_all() |
| 492 | assert session.get(StatementLine, linked_id).target_note_id == note_id |
| 493 | assert session.get(StatementLine, unlinked_id).target_note_id is None |
| 494 | |
| 495 | session.get(StatementLine, linked_id).target_note = None |
| 496 | session.commit() |
| 497 | session.expire_all() |
| 498 | assert session.get(StatementLine, linked_id).target_note_id is None |
| 499 | |
| 500 | |
| 501 | @pytest.mark.parametrize("pending_parent", ["note", "statement", "both"]) |
| 502 | @pytest.mark.parametrize("same_year", [False, True]) |
| 503 | def test_direct_fk_resolves_pending_explicit_id_parents(session, pending_parent, same_year): |
| 504 | first = make_year(session, 1403) |
| 505 | second = make_year(session, 1404) |
| 506 | note = NoteDefinition( |
| 507 | id=900, year_id=first.id if same_year else second.id, |
| 508 | note_number="10", title="Synthetic note", |
| 509 | ) |
| 510 | statement = FinancialStatement( |
| 511 | id=901, year_id=first.id, statement_type=StatementType.BALANCE_SHEET |
| 512 | ) |
| 513 | if pending_parent == "note": |
| 514 | session.add(statement) |
| 515 | session.commit() |
| 516 | elif pending_parent == "statement": |
| 517 | session.add(note) |
| 518 | session.commit() |
| 519 | line = StatementLine( |
| 520 | statement_id=901, target_note_id=900, line_order=0, line_label="Synthetic" |
| 521 | ) |
| 522 | session.add_all([note, statement, line]) |
| 523 | if same_year: |
| 524 | session.commit() |
| 525 | line_id = line.id |
| 526 | session.expire_all() |
| 527 | stored = session.get(StatementLine, line_id) |
| 528 | assert stored.statement.year_id == first.id |
| 529 | assert stored.target_note.year_id == first.id |
| 530 | else: |
| 531 | with pytest.raises(ValueError, match="same fiscal year"): |
| 532 | session.commit() |
| 533 | session.rollback() |
| 534 | session.expire_all() |
| 535 | assert session.query(StatementLine).count() == 0 |
| 536 | |
| 537 | |
| 538 | @pytest.mark.parametrize("same_year", [False, True]) |
| 539 | def test_mapping_direct_fk_resolves_pending_note(session, same_year): |
| 540 | first = make_year(session, 1403) |
| 541 | second = make_year(session, 1404) |
| 542 | note = NoteDefinition( |
| 543 | id=900, year_id=first.id if same_year else second.id, |
| 544 | note_number="10", title="Synthetic note", |
| 545 | ) |
| 546 | mapping = MappingRule( |
| 547 | year_id=first.id, rule_key="synthetic", account_code="1000", |
| 548 | target_note_id=900, mapping_type="O", |
| 549 | ) |
| 550 | session.add_all([note, mapping]) |
| 551 | if same_year: |
| 552 | session.commit() |
| 553 | rule_id = mapping.id |
| 554 | session.expire_all() |
| 555 | stored = session.get(MappingRule, rule_id) |
| 556 | assert stored.target_note.year_id == stored.year_id == first.id |
| 557 | else: |
| 558 | with pytest.raises(ValueError, match="same fiscal year"): |
| 559 | session.commit() |
| 560 | session.rollback() |
| 561 | assert session.query(MappingRule).count() == 0 |
| 562 | |
| 563 | |
| 564 | # Same-flush approval must atomically freeze the entire fiscal-year aggregate. |
| 565 | # Prior to this regression, checking only persisted FiscalYear.status returned DRAFT |
| 566 | # while a pending APPROVED transition was in the same SQLAlchemy flush. |
| 567 | @pytest.mark.parametrize("record_kind", [ |
| 568 | "account", "source", "note", "statement", "mapping", "note_row", "statement_line", |
| 569 | ]) |
| 570 | def test_approval_and_existing_year_record_edit_in_one_flush_are_rejected(session, record_kind): |
| 571 | year = make_year(session, 1403) |
| 572 | note = NoteDefinition(year_id=year.id, note_number="10", title="Existing note") |
| 573 | statement = FinancialStatement( |
| 574 | year_id=year.id, statement_type=StatementType.BALANCE_SHEET |
| 575 | ) |
| 576 | account = AccountVersion( |
| 577 | year_id=year.id, account_code="1000", account_name="Existing account" |
| 578 | ) |
| 579 | source = SourceDocument( |
| 580 | year_id=year.id, filename="synthetic.xlsx", |
| 581 | file_hash="synthetic-sha", document_type="general_ledger", |
| 582 | ) |
| 583 | mapping = MappingRule( |
| 584 | year_id=year.id, rule_key="existing", account_code="1000", |
| 585 | target_note=note, mapping_type="O", |
| 586 | ) |
| 587 | note_row = NoteRow(note=note, row_order=0, label="Existing row") |
| 588 | statement_line = StatementLine( |
| 589 | statement=statement, target_note=note, |
| 590 | line_order=0, line_label="Existing statement row", |
| 591 | ) |
| 592 | session.add_all([ |
| 593 | note, statement, account, source, mapping, note_row, statement_line |
| 594 | ]) |
| 595 | session.commit() |
| 596 | records = { |
| 597 | "account": (account, "account_name"), |
| 598 | "source": (source, "filename"), |
| 599 | "note": (note, "title"), |
| 600 | "statement": (statement, "version"), |
| 601 | "mapping": (mapping, "rule_key"), |
| 602 | "note_row": (note_row, "label"), |
| 603 | "statement_line": (statement_line, "line_label"), |
| 604 | } |
| 605 | record, field = records[record_kind] |
| 606 | original = getattr(record, field) |
| 607 | year_id = year.id |
| 608 | record_id = record.id |
| 609 | year.status = FiscalYearStatus.APPROVED |
| 610 | setattr(record, field, original + "-edited" if isinstance(original, str) else original + 1) |
| 611 | |
| 612 | with pytest.raises(ValueError, match="cannot be mutated"): |
| 613 | session.commit() |
| 614 | session.rollback() |
| 615 | session.expire_all() |
| 616 | assert session.get(FiscalYear, year_id).status is FiscalYearStatus.DRAFT |
| 617 | assert getattr(session.get(type(record), record_id), field) == original |
| 618 | |
| 619 | |
| 620 | def test_approval_and_new_source_registration_in_one_flush_are_rejected(session): |
| 621 | year = make_year(session, 1403) |
| 622 | year_id = year.id |
| 623 | year.status = FiscalYearStatus.APPROVED |
| 624 | session.add(SourceDocument( |
| 625 | year_id=year_id, filename="new.xlsx", file_hash="synthetic-new", |
| 626 | document_type="general_ledger", |
| 627 | )) |
| 628 | |
| 629 | with pytest.raises(ValueError, match="cannot be mutated"): |
| 630 | session.commit() |
| 631 | session.rollback() |
| 632 | session.expire_all() |
| 633 | assert session.get(FiscalYear, year_id).status is FiscalYearStatus.DRAFT |
| 634 | assert session.query(SourceDocument).filter_by(year_id=year_id).count() == 0 |
| 635 | |
| 636 | |
| 637 | def test_approval_and_company_edit_in_one_flush_are_rejected(session): |
| 638 | year = make_year(session, 1403) |
| 639 | company_id = year.company_id |
| 640 | original = year.company.name |
| 641 | year_id = year.id |
| 642 | year.status = FiscalYearStatus.APPROVED |
| 643 | year.company.name = "Company changed during approval" |
| 644 | |
| 645 | with pytest.raises(ValueError, match="cannot be mutated"): |
| 646 | session.commit() |
| 647 | session.rollback() |
| 648 | session.expire_all() |
| 649 | assert session.get(FiscalYear, year_id).status is FiscalYearStatus.DRAFT |
| 650 | assert session.get(Company, company_id).name == original |
| 651 | |
| 652 | |
| 653 | def test_draft_edit_committed_before_approval_is_still_allowed(session): |
| 654 | year = make_year(session, 1403) |
| 655 | account = AccountVersion( |
| 656 | year_id=year.id, account_code="1000", account_name="Initial" |
| 657 | ) |
| 658 | session.add(account) |
| 659 | session.commit() |
| 660 | account.account_name = "Validated" |
| 661 | session.commit() |
| 662 | year.status = FiscalYearStatus.APPROVED |
| 663 | session.commit() |
| 664 | session.expire_all() |
| 665 | assert session.get(AccountVersion, account.id).account_name == "Validated" |
| 666 | assert session.get(FiscalYear, year.id).status is FiscalYearStatus.APPROVED |
| 667 | |
| 668 | |
| 669 | # A parent's fiscal-year movement must not silently invalidate rows that |
| 670 | # were not themselves dirty and therefore escaped the ORM before_flush loop. |
| 671 | @pytest.mark.parametrize("same_company", [False, True]) |
| 672 | @pytest.mark.parametrize("ref_kind", ["mapping", "statement_line"]) |
| 673 | def test_draft_note_move_rejects_clean_cross_year_references(session, same_company, ref_kind): |
| 674 | first = make_year(session, 1403) |
| 675 | if same_company: |
| 676 | second = FiscalYear(company=first.company, year=1404) |
| 677 | session.add(second) |
| 678 | session.commit() |
| 679 | else: |
| 680 | second = make_year(session, 1404) |
| 681 | note = NoteDefinition(year_id=first.id, note_number="10", title="Synthetic note") |
| 682 | if ref_kind == "mapping": |
| 683 | dependent = MappingRule( |
| 684 | year_id=first.id, rule_key="cash", account_code="1000", |
| 685 | mapping_type="O", target_note=note, |
| 686 | ) |
| 687 | else: |
| 688 | statement = FinancialStatement( |
| 689 | year_id=first.id, statement_type=StatementType.BALANCE_SHEET, |
| 690 | ) |
| 691 | dependent = StatementLine( |
| 692 | statement=statement, target_note=note, |
| 693 | line_order=0, line_label="Linked assets", |
| 694 | ) |
| 695 | session.add(statement) |
| 696 | session.add_all([note, dependent]) |
| 697 | session.commit() |
| 698 | original_year_id, note_id, dependent_id = first.id, note.id, dependent.id |
| 699 | |
| 700 | note.year_id = second.id |
| 701 | with pytest.raises(ValueError, match="same fiscal year"): |
| 702 | session.commit() |
| 703 | session.rollback() |
| 704 | session.expire_all() |
| 705 | assert session.get(NoteDefinition, note_id).year_id == original_year_id |
| 706 | assert session.get(type(dependent), dependent_id) is not None |
| 707 | |
| 708 | |
| 709 | @pytest.mark.parametrize("same_company", [False, True]) |
| 710 | def test_draft_statement_move_rejects_clean_note_link(session, same_company): |
| 711 | first = make_year(session, 1403) |
| 712 | if same_company: |
| 713 | second = FiscalYear(company=first.company, year=1404) |
| 714 | session.add(second) |
| 715 | session.commit() |
| 716 | else: |
| 717 | second = make_year(session, 1404) |
| 718 | note = NoteDefinition(year_id=first.id, note_number="10", title="Existing note") |
| 719 | statement = FinancialStatement( |
| 720 | year_id=first.id, statement_type=StatementType.BALANCE_SHEET, |
| 721 | ) |
| 722 | line = StatementLine( |
| 723 | statement=statement, target_note=note, |
| 724 | line_order=0, line_label="Assets", |
| 725 | ) |
| 726 | session.add_all([note, statement, line]) |
| 727 | session.commit() |
| 728 | original_id, statement_id, line_id = first.id, statement.id, line.id |
| 729 | |
| 730 | statement.year_id = second.id |
| 731 | with pytest.raises(ValueError, match="same fiscal year"): |
| 732 | session.commit() |
| 733 | session.rollback() |
| 734 | session.expire_all() |
| 735 | assert session.get(FinancialStatement, statement_id).year_id == original_id |
| 736 | assert session.get(StatementLine, line_id).statement_id == statement_id |
| 737 | |
| 738 | |
| 739 | def test_unreferenced_draft_parents_can_change_year(session): |
| 740 | first = make_year(session, 1403) |
| 741 | second = make_year(session, 1404) |
| 742 | note = NoteDefinition(year_id=first.id, note_number="10", title="Free note") |
| 743 | statement = FinancialStatement( |
| 744 | year_id=first.id, statement_type=StatementType.BALANCE_SHEET, |
| 745 | ) |
| 746 | session.add_all([note, statement]) |
| 747 | session.commit() |
| 748 | note.year_id = second.id |
| 749 | statement.year_id = second.id |
| 750 | session.commit() |
| 751 | session.expire_all() |
| 752 | assert session.get(NoteDefinition, note.id).year_id == second.id |
| 753 | assert session.get(FinancialStatement, statement.id).year_id == second.id |
| 754 | |
| 755 | |
| 756 | def test_draft_note_move_and_mapping_owner_move_in_one_flush(session): |
| 757 | first = make_year(session, 1403) |
| 758 | second = make_year(session, 1404) |
| 759 | note = NoteDefinition(year_id=first.id, note_number="10", title="Synthetic note") |
| 760 | mapping = MappingRule( |
| 761 | year_id=first.id, rule_key="cash", account_code="1000", |
| 762 | mapping_type="O", target_note=note, |
| 763 | ) |
| 764 | session.add_all([note, mapping]) |
| 765 | session.commit() |
| 766 | note.year_id = second.id |
| 767 | mapping.year_id = second.id |
| 768 | session.commit() |
| 769 | session.expire_all() |
| 770 | assert session.get(NoteDefinition, note.id).year_id == second.id |
| 771 | assert session.get(MappingRule, mapping.id).year_id == second.id |
| 772 | |
| 773 | |
| 774 | def test_draft_statement_move_with_clean_unlinked_line_is_allowed(session): |
| 775 | first = make_year(session, 1403) |
| 776 | second = make_year(session, 1404) |
| 777 | statement = FinancialStatement( |
| 778 | year_id=first.id, statement_type=StatementType.BALANCE_SHEET, |
| 779 | ) |
| 780 | line = StatementLine(statement=statement, line_order=0, line_label="Subtotal") |
| 781 | session.add_all([statement, line]) |
| 782 | session.commit() |
| 783 | |
| 784 | statement.year_id = second.id |
| 785 | session.commit() |
| 786 | session.expire_all() |
| 787 | assert session.get(FinancialStatement, statement.id).year_id == second.id |
| 788 | assert session.get(StatementLine, line.id).target_note_id is None |
| 789 | |
| 790 | |
| 791 | @pytest.mark.parametrize("ref_kind", ["mapping", "statement_line"]) |
| 792 | def test_note_year_move_with_explicit_reference_retarget_is_valid(session, ref_kind): |
| 793 | first = make_year(session, 1403) |
| 794 | second = make_year(session, 1404) |
| 795 | moved_note = NoteDefinition( |
| 796 | year_id=first.id, note_number="10", title="Moved" |
| 797 | ) |
| 798 | remaining_note = NoteDefinition( |
| 799 | year_id=first.id, note_number="11", title="Remaining" |
| 800 | ) |
| 801 | session.add_all([moved_note, remaining_note]) |
| 802 | if ref_kind == "mapping": |
| 803 | dependent = MappingRule( |
| 804 | year_id=first.id, rule_key="cash", account_code="1000", |
| 805 | mapping_type="O", target_note=moved_note, |
| 806 | ) |
| 807 | else: |
| 808 | statement = FinancialStatement( |
| 809 | year_id=first.id, statement_type=StatementType.BALANCE_SHEET |
| 810 | ) |
| 811 | dependent = StatementLine( |
| 812 | statement=statement, target_note=moved_note, |
| 813 | line_order=0, line_label="Assets", |
| 814 | ) |
| 815 | session.add(statement) |
| 816 | session.add(dependent) |
| 817 | session.commit() |
| 818 | moved_note.year_id = second.id |
| 819 | dependent.target_note = remaining_note |
| 820 | session.commit() |
| 821 | session.expire_all() |
| 822 | assert session.get(NoteDefinition, moved_note.id).year_id == second.id |
| 823 | assert session.get(type(dependent), dependent.id).target_note_id == remaining_note.id |
| 824 | |
| 825 | |
| 826 | def test_move_note_and_statement_together_preserves_same_year_line(session): |
| 827 | first = make_year(session, 1403) |
| 828 | second = make_year(session, 1404) |
| 829 | note = NoteDefinition(year_id=first.id, note_number="10", title="Assets") |
| 830 | statement = FinancialStatement( |
| 831 | year_id=first.id, statement_type=StatementType.BALANCE_SHEET |
| 832 | ) |
| 833 | line = StatementLine( |
| 834 | statement=statement, target_note=note, line_order=0, line_label="Assets" |
| 835 | ) |
| 836 | session.add_all([note, statement, line]) |
| 837 | session.commit() |
| 838 | note.year_id = second.id |
| 839 | statement.year_id = second.id |
| 840 | session.commit() |
| 841 | session.expire_all() |
| 842 | assert session.get(NoteDefinition, note.id).year_id == second.id |
| 843 | assert session.get(FinancialStatement, statement.id).year_id == second.id |
| 844 | assert session.get(StatementLine, line.id).target_note_id == note.id |