def test_get_balance_with_five_transactions(self): account_name = "john" account = Account(account_name) payer = "john" payee = "mary" account = Account(account_name) transaction = Transaction("2020-01-01", payer, payee, 20) account.add_transaction(transaction) transaction = Transaction("2020-01-03", payer, payee, 20) account.add_transaction(transaction) transaction = Transaction("2020-01-05", payer, payee, 20) account.add_transaction(transaction) transaction = Transaction("2020-01-22", payer, payee, 20) account.add_transaction(transaction) transaction = Transaction("2020-01-30", payer, payee, 20) account.add_transaction(transaction) self.assertEqual(account.get_balance("2020-01-02"), -20) self.assertEqual(account.get_balance("2020-01-05"), -60) self.assertEqual(account.get_balance("2020-01-06"), -60) self.assertEqual(account.get_balance("2020-01-07"), -60) self.assertEqual(account.get_balance("2020-01-21"), -60) self.assertEqual(account.get_balance("2020-01-22"), -80) self.assertEqual(account.get_balance("2020-01-23"), -80) self.assertEqual(account.get_balance("2020-01-29"), -80) self.assertEqual(account.get_balance("2020-01-30"), -100) self.assertEqual(account.get_balance("2020-01-31"), -100)
def test_get_transactions(self): self.ledger.create_account('101', 'Cash', 'asset') self.ledger.create_account('102', 'Equipment', 'asset') self.ledger.create_account('301', 'Share Capital', 'equity') self.ledger.record_transaction(date(2016, 9, 1), "Record the funder's investment", [('101', 500000), ('301', -500000)]) self.ledger.record_transaction(date(2016, 9, 2), "Buy a laptop", [('101', -100000), ('102', 100000)]) self.assertEqual([ Transaction(date(2016, 9, 1), "Record the funder's investment", [('101', 500000), ('301', -500000)]), Transaction(date(2016, 9, 2), "Buy a laptop", [('101', -100000), ('102', 100000)]), ], self.ledger.get_transactions())
def test_get_balance_with_two_transactions(self): account_name = "john" account = Account(account_name) payer = "john" payee = "mary" account = Account(account_name) transaction = Transaction("2020-01-22", payer, payee, 20) account.add_transaction(transaction) transaction = Transaction("2020-01-25", payer, payee, 20) account.add_transaction(transaction) self.assertEqual(account.get_balance("2020-01-22"), -20) # Check balance between transactions is as expected self.assertEqual(account.get_balance("2020-01-23"), -20) # Check final balance as expected self.assertEqual(account.get_balance("2020-01-25"), -40)
def test_add_invalid_name_transaction(self): account_name = "john" account = Account(account_name) payer = "mary" payee = "insurance" account = Account(account_name) transaction = Transaction("2020-01-22", payer, payee, 20) with self.assertRaises(AccountError): account.add_transaction(transaction)
def test_record_transaction(self): self.ledger.create_account('101', 'Cash', 'asset') self.ledger.create_account('301', 'Share Capital', 'equity') tx_id = self.ledger.record_transaction( date(2016, 9, 1), "Record the funder's investment", [('101', 500000), ('301', -500000)]) self.assertEqual( Transaction(date(2016, 9, 1), "Record the funder's investment", [('101', 500000), ('301', -500000)]), self.ledger.get_transaction(tx_id))
def read(inFile): with inFile.open("r") as f: lines = f.readlines() topAccount, accounts = getAccountsFromLines(lines) transactions = [ Transaction.deserialize(line, accounts) for line in lines ] ledger_ = ledger.Ledger() ledger_.topAccount = topAccount for transaction in transactions: ledger_.addTransaction(transaction) x = ledger_.topAccount.reset() return ledger_
def test_get_balance_with_one_transaction(self): account_name = "john" account = Account(account_name) payer = "john" payee = "mary" account = Account(account_name) transaction = Transaction("2020-01-22", payer, payee, 20) account.add_transaction(transaction) self.assertEqual(account.balance, -20) # Balance on date of transaction should be -20 self.assertEqual(account.get_balance("2020-01-22"), -20) # Balance after date of transaction should be -20 self.assertEqual(account.get_balance("2020-01-25"), -20) # Balance before first transaction - assume open and 0 self.assertEqual(account.get_balance("2015-01-21"), 0)
def test_create_transaction(self): transaction = Transaction("2020-01-22", "john", "mary", 20) self.assertEqual(str(transaction), "2020-01-22, john, mary, 20")
def test_add_transaction(in_date: str, in_amount: float, in_from: str, in_to: str): t= Transaction(in_date,\ in_amount,\ in_from,\ in_to)
def getTransaction(ledger_, str_transaction): sourceAccount = ledger.getAccountFromStr(str_transaction.sourceAccount) targetAccount = ledger.getAccountFromStr(str_transaction.targetAccount) return Transaction(str_transaction.amount, sourceAccount, targetAccount, str_transaction.originator, str_transaction.date, str_transaction.usage)
if category.startswith("From '"): continue if not expenseacct in monefy_account_shorthands: print("%s not in %s" % (expenseacct,monefy_account_shorthands)) assert(False) targetacct = monefy_account_shorthands[expenseacct] description, *commentlist = csvdescription.split(";") description = description.strip() comment = " ".join(commentlist).strip() if comment == "": comment = None new_transaction = Transaction(description, date).addPosting(Posting(targetacct,Amount(amountCmp,currency))) if not comment is None and len(comment) > 0: new_transaction tm = transfer_regex_.search(category) if not tm is None: sourceacct = tm.group(1) assert(sourceacct in monefy_account_shorthands) sourceacct = monefy_account_shorthands[sourceacct] if description == "Assert": new_transaction = Transaction(description, date).addPosting(Posting(sourceacct,Amount(0,currency)).addPostPostingAssertAmount(Amount(-1*amountCmp,currency))).addTag("monefy") transactions.append(new_transaction) continue else: if not category in monefy_category_shorthands: print("unknown category:",category,file=sys.stderr)
def processHledgerAwareExpense(amountCmp, fulltext): global hledger_accounts_, hledger_aware_booking_account_shorthands_ext, hledger_accounts_longest_first hledger_accounts_ = ( queryHledgerForAccountList(hledger_mainledgerpath_) if not "hledger_accounts_" in globals() else hledger_accounts_ ) hledger_aware_booking_account_shorthands_ext = ( populateShorthandDictWithHledgerAccounts(hledger_accounts_, hledger_aware_booking_account_shorthands) if not "hledger_aware_booking_account_shorthands_ext" in globals() else hledger_aware_booking_account_shorthands_ext ) hledger_accounts_longest_first = ( sorted(hledger_accounts_, key=len, reverse=True) if not "hledger_accounts_longest_first" in globals() is None else hledger_accounts_longest_first ) dm = elba_extrainfo_neu_.match(fulltext) if not dm is None: text = dm.group(1) else: text = fulltext tout = Transaction() postings = map( lambda x: re_booking_w_hledger_info.match(x).group(1, 2), filter( re_booking_w_hledger_info.match, text.split(default_currency_)[1:] ), # safely drop first list item which is empty text or the sender information ) rest = None amntsum = 0.0 for (amnttxt, accttxt) in postings: acct = None accttxt = accttxt.strip() for shorthand in hledger_aware_booking_account_shorthands_ext.keys(): if accttxt == shorthand or (accttxt.startswith(shorthand) and accttxt[len(shorthand)] in " \t\n"): acct = hledger_aware_booking_account_shorthands_ext[shorthand] accttxt = accttxt[len(shorthand) :].strip() break if acct is None: for hacct in hledger_accounts_longest_first: if accttxt == hacct or ( accttxt.startswith(hacct) and len(accttxt) > len(hacct) and accttxt[len(hacct)] in " \t\n" ): acct = hacct accttxt = accttxt[len(acct) :].strip() break if acct is None: acct = accttxt.split(" ")[0] accttxt = accttxt[len(acct) :].strip() if len(accttxt) > 0: tout.setName(" ".join(filter(len, [accttxt, tout.name]))) if amnttxt == "REST" or amnttxt == "ALL": if rest: raise Exception("can only have ONE REST or ALL") rest = acct amnt = None else: amnt = float(amnttxt) amntsum += amnt tout.addPosting(Posting(acct, Amount(amnt, default_currency_))) if rest: amnt = -1 * round(amntsum + amountCmp, 4) tout.addPosting(Posting(rest, Amount(amnt, default_currency_))) else: assert amountCmp == -1 * amntsum if len(tout.name) == 0: tout.setName("ELBA parsed expense") return tout
if category.startswith("From '"): continue if not expenseacct in monefy_account_shorthands: print("%s not in %s" % (expenseacct, monefy_account_shorthands)) assert (False) targetacct = monefy_account_shorthands[expenseacct] description, *commentlist = csvdescription.split(";") description = description.strip() comment = " ".join(commentlist).strip() if comment == "": comment = None new_transaction = Transaction(description, date).addPosting( Posting(targetacct, Amount(amountCmp, currency))) if not comment is None and len(comment) > 0: new_transaction tm = transfer_regex_.search(category) if not tm is None: sourceacct = tm.group(1) if not sourceacct in monefy_account_shorthands: print("unknown sourceacct:", sourceacct, file=sys.stderr) assert (False) sourceacct = monefy_account_shorthands[sourceacct] if description == "Assert": new_transaction = Transaction(description, date).addPosting( Posting(sourceacct, Amount(0, currency)).addPostPostingAssertAmount( Amount(-1 * amountCmp, currency))).addTag("monefy")