forked from cursorman/coffer
65 lines
2.6 KiB
Python
65 lines
2.6 KiB
Python
import unittest
|
|
|
|
from coffer.ledger import CreditKind, InsufficientCredit, Ledger
|
|
from coffer.units import to_subunits
|
|
|
|
L, O = "agent:leora", "company:orbit"
|
|
|
|
|
|
def ledger(promo, funded):
|
|
lg = Ledger()
|
|
if promo:
|
|
lg.credit_subunits(L, to_subunits(promo), CreditKind.PROMOTIONAL, "signup-grant")
|
|
if funded:
|
|
lg.credit_subunits(L, to_subunits(funded), CreditKind.FUNDED, "top-up")
|
|
return lg
|
|
|
|
|
|
class FundedOnlyTest(unittest.TestCase):
|
|
def test_40_promo_60_funded_cannot_invest_100(self):
|
|
lg = ledger(40, 60)
|
|
n = len(lg.transactions)
|
|
with self.assertRaises(InsufficientCredit):
|
|
lg.transfer(L, O, to_subunits(100), "investment:x", tx_kind="investment")
|
|
b = lg.account(L).balance_subunits()
|
|
self.assertEqual(b["promotional"], 4000)
|
|
self.assertEqual(b["funded"], 6000)
|
|
self.assertEqual(len(lg.transactions), n)
|
|
|
|
def test_contract_payment_is_funded_only(self):
|
|
lg = ledger(40, 60)
|
|
with self.assertRaises(InsufficientCredit):
|
|
lg.transfer(L, O, to_subunits(70), "c", tx_kind="contract_payment")
|
|
|
|
def test_investment_draws_funded_only_and_leaves_promo(self):
|
|
lg = ledger(40, 200)
|
|
tx = lg.transfer(L, O, to_subunits(100), "investment:x", tx_kind="investment")
|
|
self.assertTrue(all(d["kind"] == "funded" for d in tx.meta["draws"]))
|
|
b = lg.account(L).balance_subunits()
|
|
self.assertEqual(b["promotional"], 4000)
|
|
self.assertEqual(b["funded"], 10000)
|
|
|
|
def test_plain_transfer_keeps_promo_first(self):
|
|
lg = ledger(40, 60)
|
|
tx = lg.transfer(L, O, to_subunits(50), "t")
|
|
self.assertEqual(tx.meta["draws"][0], {"kind": "promotional", "amount": 4000})
|
|
|
|
def test_reversal_restores_original_draw_kinds(self):
|
|
lg = ledger(40, 60)
|
|
bad = lg.transfer(L, O, to_subunits(100), "inv", tx_kind="investment", funded_only=False)
|
|
revs = lg.reverse_tx(bad, "correction:inv")
|
|
self.assertEqual([(t.kind, t.amount, t.meta["credit_kind"]) for t in revs],
|
|
[("investment_reversal", 4000, "promotional"),
|
|
("investment_reversal", 6000, "funded")])
|
|
self.assertTrue(all(t.meta["reverses_seq"] == bad.seq for t in revs))
|
|
self.assertTrue(all((t.debit_account, t.credit_account) == (O, L) for t in revs))
|
|
b = lg.account(L).balance_subunits()
|
|
self.assertEqual((b["promotional"], b["funded"]), (4000, 6000))
|
|
self.assertEqual(lg.account(O).balance_subunits()["spendable"], 0)
|
|
with self.assertRaises(ValueError):
|
|
lg.reverse_tx(bad, "again")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|