@Test public void deletingTransactionsShouldDeleteSplits() { Transaction transaction = new Transaction(""); Split split = new Split(Money.getZeroInstance(), alphaAccount.getUID()); transaction.addSplit(split); mTransactionsDbAdapter.addRecord(transaction); assertThat(mSplitsDbAdapter.getSplitsForTransaction(transaction.getUID())).hasSize(1); mTransactionsDbAdapter.deleteRecord(transaction.getUID()); assertThat(mSplitsDbAdapter.getSplitsForTransaction(transaction.getUID())).hasSize(0); }
@Test public void shouldBalanceTransactionsOnSave() { Transaction transaction = new Transaction("Auto balance"); Split split = new Split( new Money(BigDecimal.TEN, Currency.getInstance(Money.DEFAULT_CURRENCY_CODE)), alphaAccount.getUID()); transaction.addSplit(split); mTransactionsDbAdapter.addRecord(transaction); Transaction trn = mTransactionsDbAdapter.getRecord(transaction.getUID()); assertThat(trn.getSplits()).hasSize(2); String imbalanceAccountUID = mAccountsDbAdapter.getImbalanceAccountUID( Currency.getInstance(Money.DEFAULT_CURRENCY_CODE)); assertThat(trn.getSplits()).extracting("mAccountUID").contains(imbalanceAccountUID); }
@Test public void shouldComputeAccountBalanceCorrectly() { Account account = new Account("Test", Commodity.USD); account.setAccountType(AccountType.ASSET); // debit normal account balance Account transferAcct = new Account("Transfer"); mAccountsDbAdapter.addRecord(account); mAccountsDbAdapter.addRecord(transferAcct); Transaction transaction = new Transaction("Test description"); mTransactionsDbAdapter.addRecord(transaction); Split split = new Split(new Money(BigDecimal.TEN, Commodity.USD), account.getUID()); split.setTransactionUID(transaction.getUID()); split.setType(TransactionType.DEBIT); mSplitsDbAdapter.addRecord(split); split = new Split(new Money("4.99", "USD"), account.getUID()); split.setTransactionUID(transaction.getUID()); split.setType(TransactionType.DEBIT); mSplitsDbAdapter.addRecord(split); split = new Split(new Money("1.19", "USD"), account.getUID()); split.setTransactionUID(transaction.getUID()); split.setType(TransactionType.CREDIT); mSplitsDbAdapter.addRecord(split); split = new Split(new Money("3.49", "EUR"), account.getUID()); split.setTransactionUID(transaction.getUID()); split.setType(TransactionType.DEBIT); mSplitsDbAdapter.addRecord(split); split = new Split(new Money("8.39", "USD"), transferAcct.getUID()); split.setTransactionUID(transaction.getUID()); mSplitsDbAdapter.addRecord(split); // balance computation ignores the currency of the split Money balance = mAccountsDbAdapter.getAccountBalance(account.getUID()); Money expectedBalance = new Money("17.29", "USD"); // EUR splits should be ignored assertThat(balance).isEqualTo(expectedBalance); }
@Test public void testComputeBalance() { Transaction transaction = new Transaction("Compute"); Money firstSplitAmount = new Money("4.99", DEFAULT_CURRENCY.getCurrencyCode()); Split split = new Split(firstSplitAmount, alphaAccount.getUID()); transaction.addSplit(split); Money secondSplitAmount = new Money("3.50", DEFAULT_CURRENCY.getCurrencyCode()); split = new Split(secondSplitAmount, bravoAccount.getUID()); transaction.addSplit(split); mTransactionsDbAdapter.addRecord(transaction); // balance is negated because the CASH account has inverse normal balance transaction = mTransactionsDbAdapter.getRecord(transaction.getUID()); Money savedBalance = transaction.getBalance(alphaAccount.getUID()); assertThat(savedBalance).isEqualTo(firstSplitAmount.negate()); savedBalance = transaction.getBalance(bravoAccount.getUID()); assertThat(savedBalance.getNumerator()).isEqualTo(secondSplitAmount.negate().getNumerator()); assertThat(savedBalance.getCurrency()).isEqualTo(secondSplitAmount.getCurrency()); }
@Test public void testTransactionsAreTimeSorted() { Transaction t1 = new Transaction("T800"); t1.setTime(System.currentTimeMillis() - 10000); Split split = new Split(Money.getZeroInstance(), alphaAccount.getUID()); t1.addSplit(split); t1.addSplit(split.createPair(bravoAccount.getUID())); Transaction t2 = new Transaction("T1000"); t2.setTime(System.currentTimeMillis()); Split split2 = new Split(new Money("23.50"), bravoAccount.getUID()); t2.addSplit(split2); t2.addSplit(split2.createPair(alphaAccount.getUID())); mTransactionsDbAdapter.addRecord(t1); mTransactionsDbAdapter.addRecord(t2); List<Transaction> transactionsList = mTransactionsDbAdapter.getAllTransactionsForAccount(alphaAccount.getUID()); assertThat(transactionsList).contains(t2, Index.atIndex(0)); assertThat(transactionsList).contains(t1, Index.atIndex(1)); }
/** Tests the foreign key constraint "ON DELETE CASCADE" between accounts and splits */ @Test public void shouldDeleteSplitsWhenAccountDeleted() { Account first = new Account(ALPHA_ACCOUNT_NAME); first.setUID(ALPHA_ACCOUNT_NAME); Account second = new Account(BRAVO_ACCOUNT_NAME); second.setUID(BRAVO_ACCOUNT_NAME); mAccountsDbAdapter.addRecord(second); mAccountsDbAdapter.addRecord(first); Transaction transaction = new Transaction("TestTrn"); Split split = new Split(Money.getZeroInstance(), ALPHA_ACCOUNT_NAME); transaction.addSplit(split); transaction.addSplit(split.createPair(BRAVO_ACCOUNT_NAME)); mTransactionsDbAdapter.addRecord(transaction); mAccountsDbAdapter.deleteRecord(ALPHA_ACCOUNT_NAME); Transaction trxn = mTransactionsDbAdapter.getRecord(transaction.getUID()); assertThat(trxn.getSplits().size()).isEqualTo(1); assertThat(trxn.getSplits().get(0).getAccountUID()).isEqualTo(BRAVO_ACCOUNT_NAME); }