Implement BAT-009 manual asset CRUD and snapshot close

This commit is contained in:
Jarca
2026-07-01 10:55:34 +09:00
parent beddff7661
commit 02380a88ab
24 changed files with 1129 additions and 24 deletions
+61 -1
View File
@@ -16,7 +16,7 @@ def test_asset_snapshot_schema_is_additive_and_idempotent(tmp_path):
snapshot_cols = {row[1] for row in conn.execute("PRAGMA table_info(asset_snapshots)")}
assert {"id", "name", "valuation_amount", "valuation_date", "liquidity_bucket", "source_note", "memo", "active"} <= manual_cols
assert {"id", "basis_date", "source_type", "source_id", "name", "amount", "liquidity_bucket", "valuation_source", "source_note", "memo"} <= snapshot_cols
assert {"id", "basis_date", "source_type", "source_id", "name", "amount", "liquidity_bucket", "valuation_source", "source_note", "memo", "snapshot_run_id", "correction_of_run_id", "correction_reason", "is_correction", "closed_at"} <= snapshot_cols
def test_manual_assets_snapshot_report_and_net_worth_formula(tmp_path):
@@ -83,3 +83,63 @@ def test_asset_liquidity_report_empty_new_tables_preserves_req005_fallback(tmp_p
assert report["manual_asset_count"] == 0
assert report["source"] == "accounts.last_balance"
assert [item["source_type"] for item in report["accounts"]] == ["account", "account", "account"]
def test_manual_asset_update_deactivate_preview_and_close_correction_policy(tmp_path):
db_path = str(tmp_path / "ledger.sqlite3")
account = service.create_ledger_account("월말 현금", last_balance=1_000_000, last_synced_at="2026-06-30", liquidity_bucket="cash", db_path=db_path)
asset = service.create_manual_asset("전세보증금", 300_000_000, "2026-06-30", "real_estate", source_note="synthetic", db_path=db_path)
loan = service.create_manual_asset("대출", 120_000_000, "2026-06-30", "loan", source_note="synthetic", db_path=db_path)
dormant = service.create_manual_asset("해지 예금", 5_000_000, "2026-06-30", "deposit", source_note="synthetic", db_path=db_path)
assert account["success"] and asset["success"] and loan["success"] and dormant["success"]
updated = service.update_manual_asset(asset["manual_asset"]["id"], valuation_amount=310_000_000, memo="updated synthetic", db_path=db_path)
assert updated["success"] is True
assert updated["manual_asset"]["valuation_amount"] == 310_000_000
deactivated = service.deactivate_manual_asset(dormant["manual_asset"]["id"], db_path=db_path)
assert deactivated["success"] is True
assert deactivated["manual_asset"]["active"] is False
before = service.get_ledger_db_status(db_path, include_hash=False, include_integrity=False)["table_counts"].get("asset_snapshots", 0)
preview = service.preview_asset_snapshot_close("2026-06-30", db_path=db_path)
after = service.get_ledger_db_status(db_path, include_hash=False, include_integrity=False)["table_counts"].get("asset_snapshots", 0)
assert preview["success"] is True
assert preview["source"] == "live_authority"
assert preview["is_closed_snapshot"] is False
assert before == after
assert preview["total_assets"] == 311_000_000
assert preview["total_liabilities"] == 120_000_000
assert preview["net_worth"] == 191_000_000
closed = service.close_asset_snapshot("2026-06-30", source_note="synthetic close", db_path=db_path)
assert closed["success"] is True
assert closed["source"] == "snapshot"
assert closed["is_closed_snapshot"] is True
assert closed["is_correction"] is False
assert closed["inserted_count"] == 3
first_run = closed["snapshot_run_id"]
report = service.get_asset_snapshot_report("2026-06-30", db_path=db_path)
assert report["success"] is True
assert report["is_closed_snapshot"] is True
assert report["net_worth"] == closed["net_worth"]
assert report["close_metadata"]["snapshot_run_id"] == first_run
blocked = service.close_asset_snapshot("2026-06-30", db_path=db_path)
assert blocked["success"] is False
assert blocked["error_code"] == "validation_error"
service.update_manual_asset(loan["manual_asset"]["id"], valuation_amount=110_000_000, db_path=db_path)
corrected = service.close_asset_snapshot("2026-06-30", source_note="synthetic correction", correction_reason="synthetic correction test", db_path=db_path)
assert corrected["success"] is True
assert corrected["is_correction"] is True
assert corrected["correction_of_run_id"] == first_run
assert corrected["net_worth"] == 201_000_000
corrected_report = service.get_asset_snapshot_report("2026-06-30", db_path=db_path)
assert corrected_report["net_worth"] == 201_000_000
assert corrected_report["close_metadata"]["snapshot_run_id"] == corrected["snapshot_run_id"]
with sqlite3.connect(db_path) as conn:
row_count = conn.execute("SELECT COUNT(*) FROM asset_snapshots WHERE basis_date='2026-06-30'").fetchone()[0]
assert row_count == 6