Implement client cost attribution

This commit is contained in:
tegwick 2026-08-10 20:32:09 +02:00
parent d2b9bc4b32
commit 33883e0977
15 changed files with 362 additions and 21 deletions

View file

@ -1,3 +1,4 @@
import sqlite3
from pathlib import Path
import pytest
@ -36,6 +37,53 @@ def test_ledger_import_skips_unchanged_file(tmp_path: Path):
assert second.rows_imported == 0
def test_ledger_import_preserves_client_attribution(tmp_path: Path):
source = tmp_path / "attributed.csv"
source.write_text(
"product,amount,currency,invoice_date,client_id,application_id,app_instance_id\n"
"Managed cluster,42.00,EUR,2026-07-01,acme,portal,prod-01\n",
encoding="utf-8",
)
ledger = tmp_path / "ledger.db"
import_csv(source, "hosteurope", ledger_path=ledger)
with sqlite3.connect(ledger) as conn:
row = conn.execute(
"SELECT client_id, application_id, app_instance_id, cost_attribution_key "
"FROM ledger_entries"
).fetchone()
assert row == (
"acme",
"portal",
"prod-01",
"client:acme|app:portal|instance:prod-01",
)
def test_existing_ledger_schema_is_migrated_additively(tmp_path: Path):
ledger = tmp_path / "legacy.db"
with sqlite3.connect(ledger) as conn:
conn.execute(
"CREATE TABLE ledger_entries ("
"id INTEGER PRIMARY KEY, source_type TEXT NOT NULL, category TEXT NOT NULL, "
"label TEXT NOT NULL, amount REAL NOT NULL, currency TEXT NOT NULL, "
"period_month TEXT NOT NULL, incurred_on TEXT, source_path TEXT NOT NULL, "
"imported_at TEXT NOT NULL)"
)
monthly_summary(ledger_path=ledger)
with sqlite3.connect(ledger) as conn:
columns = {row[1] for row in conn.execute("PRAGMA table_info(ledger_entries)")}
assert {
"client_id",
"application_id",
"app_instance_id",
"cost_attribution_key",
}.issubset(columns)
def test_evaluate_runway_from_ledger(tmp_path: Path):
ledger = tmp_path / "ledger.db"
seed_fixture_ledger(ledger_path=ledger, opening_balance=12000.0)
@ -57,4 +105,4 @@ def test_build_runway_report_and_write_evidence(tmp_path: Path):
opening_balance=12000.0,
)
assert output.exists()
assert "runway-dogfood-" in output.name
assert "runway-dogfood-" in output.name