Harden resource cost evidence contract
This commit is contained in:
parent
080f756fff
commit
00343307fd
22 changed files with 1623 additions and 130 deletions
|
|
@ -1,4 +1,6 @@
|
|||
import sqlite3
|
||||
import os
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
|
@ -11,6 +13,7 @@ from fin_hub.services.ledger import (
|
|||
monthly_burn_series,
|
||||
monthly_summary,
|
||||
record_engagement_price,
|
||||
reverse_financial_fact,
|
||||
set_opening_balance,
|
||||
)
|
||||
|
||||
|
|
@ -44,6 +47,86 @@ def test_ledger_import_skips_unchanged_file(tmp_path: Path):
|
|||
assert second.rows_imported == 0
|
||||
|
||||
|
||||
def test_ledger_import_is_idempotent_when_file_is_renamed_or_touched(tmp_path: Path):
|
||||
ledger = tmp_path / "ledger.db"
|
||||
source = tmp_path / "costs.csv"
|
||||
shutil.copy(FIXTURES / "cloud-costs.csv", source)
|
||||
renamed = tmp_path / "renamed.csv"
|
||||
|
||||
first = import_csv(source, "cloud", ledger_path=ledger)
|
||||
os.utime(source, (source.stat().st_atime, source.stat().st_mtime + 1))
|
||||
touched = import_csv(source, "cloud", ledger_path=ledger)
|
||||
shutil.copy(source, renamed)
|
||||
delivered_again = import_csv(renamed, "cloud", ledger_path=ledger)
|
||||
|
||||
assert first.rows_imported == 3
|
||||
assert touched.rows_imported == 0
|
||||
assert delivered_again.rows_imported == 0
|
||||
assert monthly_summary(ledger_path=ledger)[0].entry_count == 3
|
||||
|
||||
|
||||
def test_changed_fact_requires_and_applies_one_correction(tmp_path: Path):
|
||||
ledger = tmp_path / "ledger.db"
|
||||
source = tmp_path / "costs.csv"
|
||||
source.write_text(
|
||||
"service,amount,currency,period_month\ncompute,10.00,EUR,2026-07\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
import_csv(source, "cloud", ledger_path=ledger)
|
||||
source.write_text(
|
||||
"service,amount,currency,period_month\ncompute,12.00,EUR,2026-07\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError, match="explicit correction"):
|
||||
import_csv(source, "cloud", ledger_path=ledger)
|
||||
corrected = import_csv(source, "cloud", ledger_path=ledger, force=True)
|
||||
duplicate = import_csv(source, "cloud", ledger_path=ledger, force=True)
|
||||
|
||||
assert corrected.rows_imported == 1
|
||||
assert duplicate.rows_imported == 0
|
||||
rollup = monthly_summary(ledger_path=ledger)[0]
|
||||
assert rollup.total == 12.0
|
||||
assert rollup.entry_count == 1
|
||||
with sqlite3.connect(ledger) as conn:
|
||||
facts = conn.execute(
|
||||
"SELECT correction_of, is_current FROM ledger_entries ORDER BY id"
|
||||
).fetchall()
|
||||
assert facts[0][1] == 0
|
||||
assert facts[1][0] is not None
|
||||
assert facts[1][1] == 1
|
||||
|
||||
|
||||
def test_financial_fact_reversal_is_append_only_and_zeroes_effective_total(tmp_path: Path):
|
||||
ledger = tmp_path / "ledger.db"
|
||||
source = tmp_path / "costs.csv"
|
||||
source.write_text(
|
||||
"service,amount,currency,period_month\ncompute,10.00,EUR,2026-07\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
import_csv(source, "cloud", ledger_path=ledger)
|
||||
with sqlite3.connect(ledger) as conn:
|
||||
fact_id = conn.execute(
|
||||
"SELECT financial_fact_id FROM ledger_entries"
|
||||
).fetchone()[0]
|
||||
|
||||
reversal_id = reverse_financial_fact(
|
||||
fact_id, source="provider-credit-note", ledger_path=ledger
|
||||
)
|
||||
|
||||
rollup = monthly_summary(ledger_path=ledger)[0]
|
||||
assert rollup.total == 0.0
|
||||
with sqlite3.connect(ledger) as conn:
|
||||
rows = conn.execute(
|
||||
"SELECT financial_fact_id, correction_of, adjustment_kind, is_current "
|
||||
"FROM ledger_entries ORDER BY id"
|
||||
).fetchall()
|
||||
assert rows == [
|
||||
(fact_id, None, "charge", 0),
|
||||
(reversal_id, fact_id, "reversal", 1),
|
||||
]
|
||||
|
||||
|
||||
def test_ledger_import_preserves_client_attribution(tmp_path: Path):
|
||||
source = tmp_path / "attributed.csv"
|
||||
source.write_text(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue