Reject invalid balances on account updates

This commit is contained in:
2026-10-07 22:09:02 +00:00
parent 3bf9b77c6c
commit 31d1c11dcf
2 changed files with 82 additions and 3 deletions
@@ -121,6 +121,78 @@ def test_create_rejects_whitespace_or_non_integer_dated_balance_without_mutation
assert service.list_account_valuations(baseline["account"]["id"], db_path=path)["valuations"] == before_valuations
def test_update_rejects_bool_or_float_dated_balance_without_mutation(tmp_path):
path = str(tmp_path / "ledger.sqlite3")
account = service.create_ledger_account(
"baseline",
last_balance=900,
last_balance_basis_date="2026-07-01",
db_path=path,
)["account"]["id"]
before = service.list_ledger_accounts(active_only=False, db_path=path)["accounts"][0]
before_valuations = service.list_account_valuations(account, db_path=path)["valuations"]
for invalid_balance in (True, False, 1.2, -1.2):
rejected = service.update_ledger_account(
account,
last_balance=invalid_balance,
last_balance_basis_date="2026-07-02",
db_path=path,
)
assert rejected["success"] is False
assert rejected["error_code"] == "validation_error"
assert service.list_ledger_accounts(active_only=False, db_path=path)["accounts"][0] == before
assert service.list_account_valuations(account, db_path=path)["valuations"] == before_valuations
def test_update_rejects_empty_undated_balance_without_mutation(tmp_path):
path = str(tmp_path / "ledger.sqlite3")
account = service.create_ledger_account(
"baseline",
last_balance=900,
last_balance_basis_date="2026-07-01",
db_path=path,
)["account"]["id"]
before = service.list_ledger_accounts(active_only=False, db_path=path)["accounts"][0]
before_valuations = service.list_account_valuations(account, db_path=path)["valuations"]
rejected = service.update_ledger_account(account, last_balance="", db_path=path)
assert rejected["success"] is False
assert rejected["error_code"] == "validation_error"
assert service.list_ledger_accounts(active_only=False, db_path=path)["accounts"][0] == before
assert service.list_account_valuations(account, db_path=path)["valuations"] == before_valuations
def test_update_preserves_zero_and_signed_integer_balance_semantics(tmp_path):
path = str(tmp_path / "ledger.sqlite3")
account = service.create_ledger_account("baseline", db_path=path)["account"]["id"]
zero = service.update_ledger_account(
account,
last_balance=" +0 ",
last_balance_basis_date="2026-07-01",
db_path=path,
)
signed = service.update_ledger_account(
account,
last_balance="-12",
last_balance_basis_date="2026-07-02",
db_path=path,
)
assert zero["success"] is True
assert zero["account"]["last_balance"] == 0
assert signed["success"] is True
assert signed["account"]["last_balance"] == -12
assert signed["account"]["last_balance_basis_date"] == "2026-07-02"
assert [(item["basis_date"], item["amount"]) for item in service.list_account_valuations(account, db_path=path)["valuations"]] == [
("2026-07-01", 0),
("2026-07-02", -12),
]
def test_zero_dated_balance_and_valid_import_recovery(tmp_path, monkeypatch):
path = str(tmp_path / "ledger.sqlite3")
zero = service.create_ledger_account(