diff --git a/tests/test_ledger.py b/tests/test_ledger.py index b89b27d..d18a491 100644 --- a/tests/test_ledger.py +++ b/tests/test_ledger.py @@ -333,9 +333,10 @@ class LedgerTestCase(unittest.TestCase): def test_kroner_transfer_moves_the_balance(self): - source = create_market("Børs_01") - destination = create_market("Lommebok", kind="wallet") - # Buy 1,0 BTC for 200 000 kr, then sell it for 500 000 kr: 300 000 kr left. + # Funded with exactly the 200 000 kr the buy costs. + source = create_market("Børs_01", kroner_ore=20000000) + destination = create_market("Lommebok", kind="wallet", kroner_ore=0) + # Buy 1,0 BTC for 200 000 kr, then sell it for 500 000 kr: 500 000 kr left. create_transaction(market_id=source, date="2024-01-10", leg_in_sats=100000000, leg_out_ore=20000000) create_transaction(market_id=source, date="2024-02-10", @@ -347,33 +348,42 @@ class LedgerTestCase(unittest.TestCase): leg_in_ore=10000000, to_market_id=source, transfer_group="group-1", generated=1) ledger.rebuild() - self.assertEqual(ledger.kroner_in_market(source, "2024-12-31"), 20000000) + self.assertEqual(ledger.kroner_in_market(source, "2024-12-31"), 40000000) self.assertEqual(ledger.kroner_in_market(destination, "2024-12-31"), 10000000) # Kroner are not coins: the transfer consumed no lots. self.assertEqual(consumptions(transfer), []) def test_a_transfer_cannot_overdraw_kroner(self): - source = create_market("Børs_01") - destination = create_market("Lommebok", kind="wallet") + source = create_market("Børs_01", kroner_ore=20000000) + destination = create_market("Lommebok", kind="wallet", kroner_ore=0) create_transaction(market_id=source, date="2024-01-10", leg_in_sats=100000000, leg_out_ore=20000000) - create_transaction(market_id=source, date="2024-02-10", - leg_in_ore=50000000, leg_out_sats=100000000) - # 300 000 kr on the account, and a transfer of 400 000 kr. + # Nothing left on the account, and a transfer of 400 000 kr. create_transaction(market_id=source, date="2024-03-10", type="transfer_out", leg_out_ore=40000000, to_market_id=destination, transfer_group="group-1") with self.assertRaises(ledger.LedgerError) as caught: ledger.rebuild() - self.assertIn("100 000 kr", str(caught.exception)) + self.assertIn("400 000 kr", str(caught.exception)) - def test_the_bank_stays_at_zero(self): + def test_a_buy_cannot_overdraw_kroner(self): + # No funding at all: the account holds no kroner. + market_id = create_market("Børs_01", kroner_ore=0) + create_transaction(market_id=market_id, date="2024-01-10", + leg_in_sats=100000000, leg_out_ore=20000000) + with self.assertRaises(ledger.LedgerError) as caught: + ledger.rebuild() + self.assertIn("200 000 kr", str(caught.exception)) + + def test_a_cost_leaves_the_bank(self): bank = int(database.get_setting("bank_market_id")) - # One row with both kroner legs, as the server writes an admin_cost. + before = ledger.kroner_in_market(bank, "2024-12-31") cost = create_transaction(market_id=bank, date="2024-05-01", type="admin_cost", - leg_out_ore=50000, leg_in_ore=50000) + leg_out_ore=50000) ledger.rebuild() - self.assertEqual(ledger.kroner_in_market(bank, "2024-12-31"), 0) + self.assertEqual( + ledger.kroner_in_market(bank, "2024-12-31"), before - 50000 + ) self.assertEqual(consumptions(cost), [])