Skip to content

Commit

Permalink
Added and improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
codinguser committed Jun 16, 2015
2 parents 1ff057b + bba4af3 commit 892e53b
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,14 @@ public void testExport(ExportFormat format){

@Test
public void testDeleteTransactionsAfterExport(){
assertThat(mTransactionsDbAdapter.getAllTransactionsCount()).isGreaterThan(0);
assertThat(mTransactionsDbAdapter.getRecordsCount()).isGreaterThan(0);

PreferenceManager.getDefaultSharedPreferences(getActivity()).edit()
.putBoolean(mAcccountsActivity.getString(R.string.key_delete_transactions_after_export), true).commit();

testExport(ExportFormat.QIF);

assertThat(mTransactionsDbAdapter.getAllTransactionsCount()).isEqualTo(0);
assertThat(mTransactionsDbAdapter.getRecordsCount()).isEqualTo(0);
PreferenceManager.getDefaultSharedPreferences(getActivity()).edit()
.putBoolean(mAcccountsActivity.getString(R.string.key_delete_transactions_after_export), false).commit();
}
Expand Down
17 changes: 1 addition & 16 deletions app/src/main/java/org/gnucash/android/db/AccountsDbAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ public String getName(long accountID) {
* @return List of {@link Account}s in the database
*/
public List<Account> getAllAccounts(){
LinkedList<Account> accounts = new LinkedList<Account>();
LinkedList<Account> accounts = new LinkedList<>();
Cursor c = fetchAllRecords();
try {
while (c.moveToNext()) {
Expand Down Expand Up @@ -970,21 +970,6 @@ public int getSubAccountCount(String accountUID){
return count;
}

/**
* Returns the number of accounts in the database
* @return Number of accounts in the database
*/
public int getTotalAccountCount() {
String queryCount = "SELECT COUNT(*) FROM " + AccountEntry.TABLE_NAME;
Cursor cursor = mDb.rawQuery(queryCount, null);
try {
cursor.moveToFirst();
return cursor.getInt(0);
} finally {
cursor.close();
}
}

/**
* Returns currency code of account with database ID <code>id</code>
* @param uid GUID of the account
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/org/gnucash/android/db/DatabaseAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.support.annotation.NonNull;
import android.util.Log;

Expand Down Expand Up @@ -446,6 +447,16 @@ public String getAttribute(@NonNull String recordUID, @NonNull String columnName
}
}

/**
* Returns the number of records in the database table backed by this adapter
* @return Total number of records in the database
*/
public long getRecordsCount(){
String sql = "SELECT COUNT(*) FROM " + mTableName;
SQLiteStatement statement = mDb.compileStatement(sql);
return statement.simpleQueryForLong();
}

/**
* Expose mDb.beginTransaction()
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,17 +491,6 @@ public int getTransactionsCount(String accountUID){
return count;
}

/**
* Returns the total number of transactions in the database
* regardless of what account they belong to
* @return Number of transaction in the database
*/
public long getAllTransactionsCount() {
String sql = "SELECT COUNT(*) FROM " + TransactionEntry.TABLE_NAME;
SQLiteStatement statement = mDb.compileStatement(sql);
return statement.simpleQueryForLong();
}

/**
* Returns the number of template transactions in the database
* @return Number of template transactions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ public void generateExport(Writer writer) throws ExporterException{
//account count
xmlSerializer.startTag(null, GncXmlHelper.TAG_COUNT_DATA);
xmlSerializer.attribute(null, GncXmlHelper.ATTR_KEY_CD_TYPE, "account");
xmlSerializer.text(mAccountsDbAdapter.getTotalAccountCount() + "");
xmlSerializer.text(mAccountsDbAdapter.getRecordsCount() + "");
xmlSerializer.endTag(null, GncXmlHelper.TAG_COUNT_DATA);
//transaction count
xmlSerializer.startTag(null, GncXmlHelper.TAG_COUNT_DATA);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import org.assertj.core.data.Index;
import org.gnucash.android.BuildConfig;
import org.gnucash.android.db.AccountsDbAdapter;
import org.gnucash.android.db.ScheduledActionDbAdapter;
import org.gnucash.android.db.SplitsDbAdapter;
import org.gnucash.android.db.TransactionsDbAdapter;
import org.gnucash.android.model.Account;
import org.gnucash.android.model.AccountType;
import org.gnucash.android.model.Money;
import org.gnucash.android.model.ScheduledAction;
import org.gnucash.android.model.Split;
import org.gnucash.android.model.Transaction;
import org.gnucash.android.model.TransactionType;
import org.gnucash.android.test.unit.util.GnucashTestRunner;
import org.gnucash.android.test.unit.util.ShadowCrashlytics;
import org.junit.After;
Expand All @@ -18,11 +21,12 @@
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;

import java.math.BigDecimal;
import java.util.Currency;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

@RunWith(GnucashTestRunner.class)
@Config(constants = BuildConfig.class, shadows = {ShadowCrashlytics.class})
Expand All @@ -46,7 +50,7 @@ public void setUp() throws Exception {
* Test that the list of accounts is always returned sorted alphabetically
*/
@Test
public void testAlphabeticalSorting(){
public void shouldBeAlphabeticallySortedByDefault(){
Account first = new Account(ALPHA_ACCOUNT_NAME);
Account second = new Account(BRAVO_ACCOUNT_NAME);
//purposefully added the second after the first
Expand All @@ -61,26 +65,30 @@ public void testAlphabeticalSorting(){
}

@Test
public void testAddAccountWithTransaction(){
public void shouldAddAccountsToDatabase(){
Account account1 = new Account("AlphaAccount");
Account account2 = new Account("BetaAccount");
Transaction transaction = new Transaction("MyTransaction");
Split split = new Split(Money.getZeroInstance(), account1.getUID());
transaction.addSplit(split);
transaction.addSplit(split.createPair(account2.getUID()));
account1.addTransaction(transaction);
account2.addTransaction(transaction);

long id1 = mAccountsDbAdapter.addAccount(account1);
long id2 = mAccountsDbAdapter.addAccount(account2);

assertTrue(id1 > 0);
assertTrue(id2 > 0);
assertThat(id1).isGreaterThan(0);
assertThat(id2).isGreaterThan(0);

assertThat(mTransactionsDbAdapter.getRecordsCount()).isEqualTo(1);
}

/**
* Tests the foreign key constraint "ON DELETE CASCADE" between accounts and splits
*/
@Test
public void testDeletingAccountShouldDeleteSplits(){
public void shouldDeleteSplitsWhenAccountDeleted(){
Account first = new Account(ALPHA_ACCOUNT_NAME);
first.setUID(ALPHA_ACCOUNT_NAME);
Account second = new Account(BRAVO_ACCOUNT_NAME);
Expand All @@ -95,13 +103,13 @@ public void testDeletingAccountShouldDeleteSplits(){
transaction.addSplit(split.createPair(BRAVO_ACCOUNT_NAME));

long id = mTransactionsDbAdapter.addTransaction(transaction);
assertTrue(id > 0);
assertThat(id).isGreaterThan(0);

mAccountsDbAdapter.deleteRecord(ALPHA_ACCOUNT_NAME);

Transaction trxn = mTransactionsDbAdapter.getTransaction(transaction.getUID());
assertEquals(1, trxn.getSplits().size());
assertEquals(BRAVO_ACCOUNT_NAME, trxn.getSplits().get(0).getAccountUID());
assertThat(trxn.getSplits().size()).isEqualTo(1);
assertThat(trxn.getSplits().get(0).getAccountUID()).isEqualTo(BRAVO_ACCOUNT_NAME);
}

/**
Expand All @@ -111,7 +119,7 @@ public void testDeletingAccountShouldDeleteSplits(){
public void shouldCreateDefaultRootAccount(){
Account account = new Account("Some account");
mAccountsDbAdapter.addAccount(account);
assertThat(2).isEqualTo(mAccountsDbAdapter.getTotalAccountCount());
assertThat(mAccountsDbAdapter.getRecordsCount()).isEqualTo(2L);

List<Account> accounts = mAccountsDbAdapter.getSimpleAccountList();
assertThat(accounts).extracting("mAccountType").contains(AccountType.ROOT);
Expand Down Expand Up @@ -140,6 +148,153 @@ public void shouldUpdateFullNameAfterParentChange(){
assertThat(child.getFullName()).isEqualTo("Test:Child");
}

@Test
public void shouldAddTransactionsAndSplitsWhenAddingAccounts(){
Account account = new Account("Test");
mAccountsDbAdapter.addAccount(account);

Transaction transaction = new Transaction("Test description");
Split split = new Split(Money.getZeroInstance(), account.getUID());
transaction.addSplit(split);
Account account1 = new Account("Transfer account");
transaction.addSplit(split.createPair(account1.getUID()));
account1.addTransaction(transaction);

mAccountsDbAdapter.addAccount(account1);

assertThat(mTransactionsDbAdapter.getRecordsCount()).isEqualTo(1);
assertThat(mSplitsDbAdapter.getRecordsCount()).isEqualTo(2);
assertThat(mAccountsDbAdapter.getRecordsCount()).isEqualTo(3); //ROOT account automatically added

}

@Test
public void shouldClearAllTablesWhenDeletingAllAccounts(){
Account account = new Account("Test");
Transaction transaction = new Transaction("Test description");
Split split = new Split(Money.getZeroInstance(), account.getUID());
transaction.addSplit(split);
Account account2 = new Account("Transfer account");
transaction.addSplit(split.createPair(account2.getUID()));

mAccountsDbAdapter.addAccount(account);
mAccountsDbAdapter.addAccount(account2);

ScheduledAction scheduledAction = new ScheduledAction(ScheduledAction.ActionType.BACKUP);
scheduledAction.setActionUID("Test-uid");
ScheduledActionDbAdapter scheduledActionDbAdapter = ScheduledActionDbAdapter.getInstance();

scheduledActionDbAdapter.addScheduledAction(scheduledAction);

mAccountsDbAdapter.deleteAllRecords();

assertThat(mAccountsDbAdapter.getRecordsCount()).isZero();
assertThat(mTransactionsDbAdapter.getRecordsCount()).isZero();
assertThat(mSplitsDbAdapter.getRecordsCount()).isZero();
assertThat(scheduledActionDbAdapter.getRecordsCount()).isZero();
}

@Test
public void simpleAccountListShouldNotContainTransactions(){
Account account = new Account("Test");
Transaction transaction = new Transaction("Test description");
Split split = new Split(Money.getZeroInstance(), account.getUID());
transaction.addSplit(split);
Account account1 = new Account("Transfer");
transaction.addSplit(split.createPair(account1.getUID()));

mAccountsDbAdapter.addAccount(account);
mAccountsDbAdapter.addAccount(account1);

List<Account> accounts = mAccountsDbAdapter.getSimpleAccountList();
for (Account testAcct : accounts) {
assertThat(testAcct.getTransactionCount()).isZero();
}
}

@Test
public void shouldComputeAccountBalanceCorrectly(){
Account account = new Account("Test", Currency.getInstance("USD"));
account.setAccountType(AccountType.ASSET); //debit normal account balance
Account transferAcct = new Account("Transfer");

mAccountsDbAdapter.addAccount(account);
mAccountsDbAdapter.addAccount(transferAcct);

Transaction transaction = new Transaction("Test description");
mTransactionsDbAdapter.addTransaction(transaction);
Split split = new Split(new Money(BigDecimal.TEN, Currency.getInstance("USD")), account.getUID());
split.setTransactionUID(transaction.getUID());
split.setType(TransactionType.DEBIT);
mSplitsDbAdapter.addSplit(split);

split = new Split(new Money("4.99", "USD"), account.getUID());
split.setTransactionUID(transaction.getUID());
split.setType(TransactionType.DEBIT);
mSplitsDbAdapter.addSplit(split);

split = new Split(new Money("1.19", "USD"), account.getUID());
split.setTransactionUID(transaction.getUID());
split.setType(TransactionType.CREDIT);
mSplitsDbAdapter.addSplit(split);

split = new Split(new Money("3.49", "EUR"), account.getUID());
split.setTransactionUID(transaction.getUID());
split.setType(TransactionType.DEBIT);
mSplitsDbAdapter.addSplit(split);

split = new Split(new Money("8.39", "USD"), transferAcct.getUID());
split.setTransactionUID(transaction.getUID());
mSplitsDbAdapter.addSplit(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 creating an account hierarchy by specifying fully qualified name
*/
@Test
public void shouldCreateAccountHierarchy(){
String uid = mAccountsDbAdapter.createAccountHierarchy("Assets:Current Assets:Cash in Wallet", AccountType.ASSET);

List<Account> accounts = mAccountsDbAdapter.getAllAccounts();
assertThat(accounts).hasSize(3);
assertThat(accounts).extracting("mUID").contains(uid);
}

@Test
public void shouldRecursivelyDeleteAccount(){
Account account = new Account("Parent");
Account account2 = new Account("Child");
account2.setParentUID(account.getUID());

Transaction transaction = new Transaction("Random");
account2.addTransaction(transaction);

Split split = new Split(Money.getZeroInstance(), account.getUID());
transaction.addSplit(split);
transaction.addSplit(split.createPair(account2.getUID()));

mAccountsDbAdapter.addAccount(account);
mAccountsDbAdapter.addAccount(account2);

assertThat(mAccountsDbAdapter.getRecordsCount()).isEqualTo(3);
assertThat(mTransactionsDbAdapter.getRecordsCount()).isEqualTo(1);
assertThat(mSplitsDbAdapter.getRecordsCount()).isEqualTo(2);

boolean result = mAccountsDbAdapter.recursiveDeleteAccount(mAccountsDbAdapter.getID(account.getUID()));
assertThat(result).isTrue();

assertThat(mAccountsDbAdapter.getRecordsCount()).isEqualTo(1); //the root account
assertThat(mTransactionsDbAdapter.getRecordsCount()).isZero();
assertThat(mSplitsDbAdapter.getRecordsCount()).isZero();

}

@After
public void tearDown() throws Exception {
mAccountsDbAdapter.deleteAllRecords();
Expand Down

0 comments on commit 892e53b

Please sign in to comment.