def test_tdd(): accounts = Ledger('equity') accounts.make_stock_accounts(['cash', 'claims', 'inventory']) accounts.make_flow_accounts(['expenditure', 'revenue', 'cost of goods sold', 'depreciation']) accounts.book( debit=[('cash', 50), ('claims', 50)], credit=[('equity', 100)], text="Start with owners' equity, partially paid in") assert accounts._check_debit_eq_credit() assert accounts.get_total_assets() == 100 assert accounts['cash'].get_balance() == (AccountSide.DEBIT, 50) assert accounts['claims'].get_balance() == (AccountSide.DEBIT, 50) assert accounts['equity'].get_balance() == (AccountSide.CREDIT, 100) print('Initial balance') accounts.print_balance_sheet() print('Some purchases and operating expenses') accounts.book(debit=[('expenditure', 20)], credit=[('cash', 20)],text="General expenses") assert accounts.get_total_assets() == 80, accounts.get_total_assets() accounts.book(debit=[('inventory',20)],credit=[('cash',20)],text="Purchase of equipment") accounts.book(debit=[('depreciation',2)],credit=[('inventory',2)],text="Depreciation") accounts.print_profit_and_loss() print('Balance sheet after first period') accounts.book_end_of_period() accounts.print_balance_sheet() assert accounts['equity'].get_balance() == (AccountSide.CREDIT, 78) assert accounts['cash'].get_balance() == (AccountSide.DEBIT, 10),accounts['cash'].get_balance() print('Profitable sale') accounts.book(debit=[('cash',40)],credit=[('revenue',40)],text="Sale of goods") accounts.book(debit=[('cost of goods sold',10)],credit=[('inventory',10)],text="Sale of goods") accounts.print_profit_and_loss() assert accounts['inventory'].get_balance() == (AccountSide.DEBIT, 8) print('Balance sheet after second period') accounts.book_end_of_period() accounts.print_balance_sheet() assert accounts['equity'].get_balance() == (AccountSide.CREDIT, 108)
""" --------------------------------------------------------------------------- Initialize balance sheets (all symmetric balance sheets with 0 equity, for simplicity) """ fed.book(debit=[('Securities Holdings',1000)],credit=[('Securities belonging to Depository A',500),('Securities belonging to Depository B',500)]) fed.book(debit=[('Assets',2000)],credit=[('Reserves Depository A',1000),('Reserves Depository B',1000)]) depository_A.book(debit=[('Securities Holdings',1000)],credit=[('Securities belonging to Broker M',1000)]) depository_A.book(debit=[('Reserves',1000)],credit=[('Deposits by Broker M',1000)]) depository_B.book(debit=[('Securities Holdings',1000)],credit=[('Securities belonging to Financial Institution J',1000)]) depository_B.book(debit=[('Reserves',1000)],credit=[('Deposits by Financial Institution J',1000)]) broker_M.book(debit=[('Securities Holdings',1000)],credit=[('Securities belonging to Customers',1000)]) broker_M.book(debit=[('Reserves with Depository A',1000)],credit=[('Customer Deposits',1000)]) fi_J.book(debit=[('Securities Holdings',1000)],credit=[('Securities belonging to Customers',1000)]) fi_J.book(debit=[('Reserves with Depository B',1000)],credit=[('Customer Deposits',1000)]) print('Initial balance sheets:') fed.print_balance_sheet() depository_A.print_balance_sheet() depository_B.print_balance_sheet() broker_M.print_balance_sheet() fi_J.print_balance_sheet() """ --------------------------------------------------------------------------- Transfer of a security from a customer of Broker M to a customer of Financial Institution J The booking statements on the customers' balance sheet are left out for brevity, they would be completely analogous """ broker_M.book(debit=[('Securities belonging to Customers',500)],credit=[('Securities Holdings',500)]) depository_A.book(debit=[('Securities belonging to Broker M',500)],credit=[('Securities Holdings',500)]) fed.book(debit=[('Securities belonging to Depository A',500)],credit=[('Securities belonging to Depository B',500)]) depository_B.book(debit=[('Securities Holdings',500)],credit=[('Securities belonging to Financial Institution J',500)]) fi_J.book(debit=[('Securities Holdings',500)],credit=[('Securities belonging to Customers',500)])
private_agent.book(debit=[('Currency', 500)], credit=[('Equity', 500)]) bank.book(debit=[('Currency', 50)], credit=[('Equity', 50)]) central_bank.book(debit=[('Assets', 550)], credit=[('Currency', 550)]) private_agent.book(debit=[('Deposits', 500)], credit=[('Currency', 500)]) bank.book(debit=[('Currency', 500)], credit=[('Deposits', 500)]) bank.book(debit=[('Reserves', 550)], credit=[('Currency', 550)]) central_bank.book(debit=[('Currency', 550)], credit=[('Reserves', 550)]) """ --------------------------------------------------------------------------- Bank buys a government bond """ government.book(debit=[('Reserves', 400)], credit=[('Bonds', 400)]) bank.book(debit=[('Bonds', 400)], credit=[('Reserves', 400)]) central_bank.book(debit=[('Reserves', 400)], credit=[('Government Reserves', 400)]) """ --------------------------------------------------------------------------- Private agent buys the bond from the bank """ private_agent.book(debit=[('Bonds', 100)], credit=[('Deposits', 100)]) bank.book(debit=[('Deposits', 100)], credit=[('Bonds', 100)]) """ --------------------------------------------------------------------------- Take out a loan """ private_agent.book(debit=[('Deposits', 550)], credit=[('Loans', 550)]) bank.book(debit=[('Loans', 550)], credit=[('Deposits', 550)]) private_agent.print_balance_sheet() bank.print_balance_sheet() central_bank.print_balance_sheet() government.print_balance_sheet()
household.book(debit=[('loan liabilities',100)],credit=[('money holdings',100)],text='Principal repayment') """ --------------------------------------------------------------------------- Dividend payment """ bank.book(debit=[('equity',5)],credit=[('deposits',5)],text='Dividend payout') bank_owner.book(debit=[('money holdings',5)],credit=[('income',5)],text='Dividend income') bank.book_end_of_period() household.book_end_of_period() bank_owner.book_end_of_period() print('Bank initial balance sheet') bank.print_balance_sheet() print('Household initial balance sheet') household.print_balance_sheet() print('Bank owner initial balance sheet') bank_owner.print_balance_sheet() print('Bank PnL after interest payment') bank.print_profit_and_loss() print('Household PnL after interest payment') household.print_profit_and_loss() bank.book_end_of_period() household.book_end_of_period() print('Bank balance sheet after dividend payment') bank.print_balance_sheet() print('Household balance sheet after dividend payment')
def test_banking(): bank = Ledger(residual_account_name="equity") household = Ledger(residual_account_name="equity") bank.make_stock_accounts(['reserves','claims','security holdings','deposits','wholesale refinancing']) household.make_stock_accounts(['money holdings','loan liabilities']) bank.make_flow_accounts(['interest income','interest expense']) household.make_flow_accounts(['income','expenses']) """ --------------------------------------------------------------------------- Initialize balance sheets (outside money endowments) """ household.book(debit=[('money holdings',100)],credit=[('equity',100)],text='Initial endowment') bank.book(debit=[('reserves',100)],credit=([('deposits',100)]),text='Initial endowment') print('Bank initial balance sheet') bank.print_balance_sheet() print('Household initial balance sheet') household.print_balance_sheet() """ --------------------------------------------------------------------------- Granting of a loan """ bank.book(debit=[('claims',10)],credit=[('deposits',10)],text='Loan') household.book(debit=[('money holdings',10)],credit=[('loan liabilities',10)],text='Loan') print('Bank balance sheet after granting a loan') bank.print_balance_sheet() print('Household balance sheet after granting a loan') household.print_balance_sheet() """ --------------------------------------------------------------------------- Interest payment """ bank.book(debit=[('deposits',1)],credit=[('interest income',1)],text='Interest payment') household.book(debit=[('expenses',1)],credit=[('money holdings',1)],text='Interest payment') print('Bank PnL after interest payment') bank.print_profit_and_loss() print('Household PnL after interest payment') household.print_profit_and_loss() bank.book_end_of_period() household.book_end_of_period() print('Bank balance sheet after interest payment') bank.print_balance_sheet() print('Household balance sheet after interest payment') household.print_balance_sheet() """ --------------------------------------------------------------------------- Principal repayment """ bank.book(debit=[('deposits',10)],credit=[('claims',10)],text='Principal repayment') household.book(debit=[('loan liabilities',10)],credit=[('money holdings',10)],text='Principal repayment') print('Bank balance sheet after principal repayment') bank.print_balance_sheet() print('Household balance sheet after principal repayment') household.print_balance_sheet() """ --------------------------------------------------------------------------- Dividend payment """ bank.book(debit=[('equity',1)],credit=[('deposits',1)],text='Dividend payment') household.book(debit=[('money holdings',1)],credit=[('income',1)],text='Dividend payment') print('Bank PnL after interest payment') bank.print_profit_and_loss() print('Household PnL after interest payment') household.print_profit_and_loss() bank.book_end_of_period() household.book_end_of_period() print('Bank balance sheet after dividend payment') bank.print_balance_sheet() print('Household balance sheet after dividend payment') household.print_balance_sheet()