Skip to content

Commit 892e53b

Browse files
committed
Added and improved tests
2 parents 1ff057b + bba4af3 commit 892e53b

File tree

6 files changed

+180
-40
lines changed

6 files changed

+180
-40
lines changed

app/src/androidTest/java/org/gnucash/android/test/ui/ExportTransactionsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,14 @@ public void testExport(ExportFormat format){
160160

161161
@Test
162162
public void testDeleteTransactionsAfterExport(){
163-
assertThat(mTransactionsDbAdapter.getAllTransactionsCount()).isGreaterThan(0);
163+
assertThat(mTransactionsDbAdapter.getRecordsCount()).isGreaterThan(0);
164164

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

168168
testExport(ExportFormat.QIF);
169169

170-
assertThat(mTransactionsDbAdapter.getAllTransactionsCount()).isEqualTo(0);
170+
assertThat(mTransactionsDbAdapter.getRecordsCount()).isEqualTo(0);
171171
PreferenceManager.getDefaultSharedPreferences(getActivity()).edit()
172172
.putBoolean(mAcccountsActivity.getString(R.string.key_delete_transactions_after_export), false).commit();
173173
}

app/src/main/java/org/gnucash/android/db/AccountsDbAdapter.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ public String getName(long accountID) {
515515
* @return List of {@link Account}s in the database
516516
*/
517517
public List<Account> getAllAccounts(){
518-
LinkedList<Account> accounts = new LinkedList<Account>();
518+
LinkedList<Account> accounts = new LinkedList<>();
519519
Cursor c = fetchAllRecords();
520520
try {
521521
while (c.moveToNext()) {
@@ -970,21 +970,6 @@ public int getSubAccountCount(String accountUID){
970970
return count;
971971
}
972972

973-
/**
974-
* Returns the number of accounts in the database
975-
* @return Number of accounts in the database
976-
*/
977-
public int getTotalAccountCount() {
978-
String queryCount = "SELECT COUNT(*) FROM " + AccountEntry.TABLE_NAME;
979-
Cursor cursor = mDb.rawQuery(queryCount, null);
980-
try {
981-
cursor.moveToFirst();
982-
return cursor.getInt(0);
983-
} finally {
984-
cursor.close();
985-
}
986-
}
987-
988973
/**
989974
* Returns currency code of account with database ID <code>id</code>
990975
* @param uid GUID of the account

app/src/main/java/org/gnucash/android/db/DatabaseAdapter.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import android.content.ContentValues;
2020
import android.database.Cursor;
2121
import android.database.sqlite.SQLiteDatabase;
22+
import android.database.sqlite.SQLiteStatement;
2223
import android.support.annotation.NonNull;
2324
import android.util.Log;
2425

@@ -446,6 +447,16 @@ public String getAttribute(@NonNull String recordUID, @NonNull String columnName
446447
}
447448
}
448449

450+
/**
451+
* Returns the number of records in the database table backed by this adapter
452+
* @return Total number of records in the database
453+
*/
454+
public long getRecordsCount(){
455+
String sql = "SELECT COUNT(*) FROM " + mTableName;
456+
SQLiteStatement statement = mDb.compileStatement(sql);
457+
return statement.simpleQueryForLong();
458+
}
459+
449460
/**
450461
* Expose mDb.beginTransaction()
451462
*/

app/src/main/java/org/gnucash/android/db/TransactionsDbAdapter.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -491,17 +491,6 @@ public int getTransactionsCount(String accountUID){
491491
return count;
492492
}
493493

494-
/**
495-
* Returns the total number of transactions in the database
496-
* regardless of what account they belong to
497-
* @return Number of transaction in the database
498-
*/
499-
public long getAllTransactionsCount() {
500-
String sql = "SELECT COUNT(*) FROM " + TransactionEntry.TABLE_NAME;
501-
SQLiteStatement statement = mDb.compileStatement(sql);
502-
return statement.simpleQueryForLong();
503-
}
504-
505494
/**
506495
* Returns the number of template transactions in the database
507496
* @return Number of template transactions

app/src/main/java/org/gnucash/android/export/xml/GncXmlExporter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ public void generateExport(Writer writer) throws ExporterException{
652652
//account count
653653
xmlSerializer.startTag(null, GncXmlHelper.TAG_COUNT_DATA);
654654
xmlSerializer.attribute(null, GncXmlHelper.ATTR_KEY_CD_TYPE, "account");
655-
xmlSerializer.text(mAccountsDbAdapter.getTotalAccountCount() + "");
655+
xmlSerializer.text(mAccountsDbAdapter.getRecordsCount() + "");
656656
xmlSerializer.endTag(null, GncXmlHelper.TAG_COUNT_DATA);
657657
//transaction count
658658
xmlSerializer.startTag(null, GncXmlHelper.TAG_COUNT_DATA);

app/src/test/java/org/gnucash/android/test/unit/db/AccountsDbAdapterTest.java

Lines changed: 165 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
import org.assertj.core.data.Index;
44
import org.gnucash.android.BuildConfig;
55
import org.gnucash.android.db.AccountsDbAdapter;
6+
import org.gnucash.android.db.ScheduledActionDbAdapter;
67
import org.gnucash.android.db.SplitsDbAdapter;
78
import org.gnucash.android.db.TransactionsDbAdapter;
89
import org.gnucash.android.model.Account;
910
import org.gnucash.android.model.AccountType;
1011
import org.gnucash.android.model.Money;
12+
import org.gnucash.android.model.ScheduledAction;
1113
import org.gnucash.android.model.Split;
1214
import org.gnucash.android.model.Transaction;
15+
import org.gnucash.android.model.TransactionType;
1316
import org.gnucash.android.test.unit.util.GnucashTestRunner;
1417
import org.gnucash.android.test.unit.util.ShadowCrashlytics;
1518
import org.junit.After;
@@ -18,11 +21,12 @@
1821
import org.junit.runner.RunWith;
1922
import org.robolectric.annotation.Config;
2023

24+
import java.math.BigDecimal;
25+
import java.util.Currency;
2126
import java.util.List;
2227

2328
import static org.assertj.core.api.Assertions.assertThat;
2429
import static org.junit.Assert.assertEquals;
25-
import static org.junit.Assert.assertTrue;
2630

2731
@RunWith(GnucashTestRunner.class)
2832
@Config(constants = BuildConfig.class, shadows = {ShadowCrashlytics.class})
@@ -46,7 +50,7 @@ public void setUp() throws Exception {
4650
* Test that the list of accounts is always returned sorted alphabetically
4751
*/
4852
@Test
49-
public void testAlphabeticalSorting(){
53+
public void shouldBeAlphabeticallySortedByDefault(){
5054
Account first = new Account(ALPHA_ACCOUNT_NAME);
5155
Account second = new Account(BRAVO_ACCOUNT_NAME);
5256
//purposefully added the second after the first
@@ -61,26 +65,30 @@ public void testAlphabeticalSorting(){
6165
}
6266

6367
@Test
64-
public void testAddAccountWithTransaction(){
68+
public void shouldAddAccountsToDatabase(){
6569
Account account1 = new Account("AlphaAccount");
6670
Account account2 = new Account("BetaAccount");
6771
Transaction transaction = new Transaction("MyTransaction");
6872
Split split = new Split(Money.getZeroInstance(), account1.getUID());
6973
transaction.addSplit(split);
7074
transaction.addSplit(split.createPair(account2.getUID()));
75+
account1.addTransaction(transaction);
76+
account2.addTransaction(transaction);
7177

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

75-
assertTrue(id1 > 0);
76-
assertTrue(id2 > 0);
81+
assertThat(id1).isGreaterThan(0);
82+
assertThat(id2).isGreaterThan(0);
83+
84+
assertThat(mTransactionsDbAdapter.getRecordsCount()).isEqualTo(1);
7785
}
7886

7987
/**
8088
* Tests the foreign key constraint "ON DELETE CASCADE" between accounts and splits
8189
*/
8290
@Test
83-
public void testDeletingAccountShouldDeleteSplits(){
91+
public void shouldDeleteSplitsWhenAccountDeleted(){
8492
Account first = new Account(ALPHA_ACCOUNT_NAME);
8593
first.setUID(ALPHA_ACCOUNT_NAME);
8694
Account second = new Account(BRAVO_ACCOUNT_NAME);
@@ -95,13 +103,13 @@ public void testDeletingAccountShouldDeleteSplits(){
95103
transaction.addSplit(split.createPair(BRAVO_ACCOUNT_NAME));
96104

97105
long id = mTransactionsDbAdapter.addTransaction(transaction);
98-
assertTrue(id > 0);
106+
assertThat(id).isGreaterThan(0);
99107

100108
mAccountsDbAdapter.deleteRecord(ALPHA_ACCOUNT_NAME);
101109

102110
Transaction trxn = mTransactionsDbAdapter.getTransaction(transaction.getUID());
103-
assertEquals(1, trxn.getSplits().size());
104-
assertEquals(BRAVO_ACCOUNT_NAME, trxn.getSplits().get(0).getAccountUID());
111+
assertThat(trxn.getSplits().size()).isEqualTo(1);
112+
assertThat(trxn.getSplits().get(0).getAccountUID()).isEqualTo(BRAVO_ACCOUNT_NAME);
105113
}
106114

107115
/**
@@ -111,7 +119,7 @@ public void testDeletingAccountShouldDeleteSplits(){
111119
public void shouldCreateDefaultRootAccount(){
112120
Account account = new Account("Some account");
113121
mAccountsDbAdapter.addAccount(account);
114-
assertThat(2).isEqualTo(mAccountsDbAdapter.getTotalAccountCount());
122+
assertThat(mAccountsDbAdapter.getRecordsCount()).isEqualTo(2L);
115123

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

151+
@Test
152+
public void shouldAddTransactionsAndSplitsWhenAddingAccounts(){
153+
Account account = new Account("Test");
154+
mAccountsDbAdapter.addAccount(account);
155+
156+
Transaction transaction = new Transaction("Test description");
157+
Split split = new Split(Money.getZeroInstance(), account.getUID());
158+
transaction.addSplit(split);
159+
Account account1 = new Account("Transfer account");
160+
transaction.addSplit(split.createPair(account1.getUID()));
161+
account1.addTransaction(transaction);
162+
163+
mAccountsDbAdapter.addAccount(account1);
164+
165+
assertThat(mTransactionsDbAdapter.getRecordsCount()).isEqualTo(1);
166+
assertThat(mSplitsDbAdapter.getRecordsCount()).isEqualTo(2);
167+
assertThat(mAccountsDbAdapter.getRecordsCount()).isEqualTo(3); //ROOT account automatically added
168+
169+
}
170+
171+
@Test
172+
public void shouldClearAllTablesWhenDeletingAllAccounts(){
173+
Account account = new Account("Test");
174+
Transaction transaction = new Transaction("Test description");
175+
Split split = new Split(Money.getZeroInstance(), account.getUID());
176+
transaction.addSplit(split);
177+
Account account2 = new Account("Transfer account");
178+
transaction.addSplit(split.createPair(account2.getUID()));
179+
180+
mAccountsDbAdapter.addAccount(account);
181+
mAccountsDbAdapter.addAccount(account2);
182+
183+
ScheduledAction scheduledAction = new ScheduledAction(ScheduledAction.ActionType.BACKUP);
184+
scheduledAction.setActionUID("Test-uid");
185+
ScheduledActionDbAdapter scheduledActionDbAdapter = ScheduledActionDbAdapter.getInstance();
186+
187+
scheduledActionDbAdapter.addScheduledAction(scheduledAction);
188+
189+
mAccountsDbAdapter.deleteAllRecords();
190+
191+
assertThat(mAccountsDbAdapter.getRecordsCount()).isZero();
192+
assertThat(mTransactionsDbAdapter.getRecordsCount()).isZero();
193+
assertThat(mSplitsDbAdapter.getRecordsCount()).isZero();
194+
assertThat(scheduledActionDbAdapter.getRecordsCount()).isZero();
195+
}
196+
197+
@Test
198+
public void simpleAccountListShouldNotContainTransactions(){
199+
Account account = new Account("Test");
200+
Transaction transaction = new Transaction("Test description");
201+
Split split = new Split(Money.getZeroInstance(), account.getUID());
202+
transaction.addSplit(split);
203+
Account account1 = new Account("Transfer");
204+
transaction.addSplit(split.createPair(account1.getUID()));
205+
206+
mAccountsDbAdapter.addAccount(account);
207+
mAccountsDbAdapter.addAccount(account1);
208+
209+
List<Account> accounts = mAccountsDbAdapter.getSimpleAccountList();
210+
for (Account testAcct : accounts) {
211+
assertThat(testAcct.getTransactionCount()).isZero();
212+
}
213+
}
214+
215+
@Test
216+
public void shouldComputeAccountBalanceCorrectly(){
217+
Account account = new Account("Test", Currency.getInstance("USD"));
218+
account.setAccountType(AccountType.ASSET); //debit normal account balance
219+
Account transferAcct = new Account("Transfer");
220+
221+
mAccountsDbAdapter.addAccount(account);
222+
mAccountsDbAdapter.addAccount(transferAcct);
223+
224+
Transaction transaction = new Transaction("Test description");
225+
mTransactionsDbAdapter.addTransaction(transaction);
226+
Split split = new Split(new Money(BigDecimal.TEN, Currency.getInstance("USD")), account.getUID());
227+
split.setTransactionUID(transaction.getUID());
228+
split.setType(TransactionType.DEBIT);
229+
mSplitsDbAdapter.addSplit(split);
230+
231+
split = new Split(new Money("4.99", "USD"), account.getUID());
232+
split.setTransactionUID(transaction.getUID());
233+
split.setType(TransactionType.DEBIT);
234+
mSplitsDbAdapter.addSplit(split);
235+
236+
split = new Split(new Money("1.19", "USD"), account.getUID());
237+
split.setTransactionUID(transaction.getUID());
238+
split.setType(TransactionType.CREDIT);
239+
mSplitsDbAdapter.addSplit(split);
240+
241+
split = new Split(new Money("3.49", "EUR"), account.getUID());
242+
split.setTransactionUID(transaction.getUID());
243+
split.setType(TransactionType.DEBIT);
244+
mSplitsDbAdapter.addSplit(split);
245+
246+
split = new Split(new Money("8.39", "USD"), transferAcct.getUID());
247+
split.setTransactionUID(transaction.getUID());
248+
mSplitsDbAdapter.addSplit(split);
249+
250+
//balance computation ignores the currency of the split
251+
Money balance = mAccountsDbAdapter.getAccountBalance(account.getUID());
252+
Money expectedBalance = new Money("17.29", "USD"); //EUR splits should be ignored
253+
254+
assertThat(balance).isEqualTo(expectedBalance);
255+
}
256+
257+
/**
258+
* Test creating an account hierarchy by specifying fully qualified name
259+
*/
260+
@Test
261+
public void shouldCreateAccountHierarchy(){
262+
String uid = mAccountsDbAdapter.createAccountHierarchy("Assets:Current Assets:Cash in Wallet", AccountType.ASSET);
263+
264+
List<Account> accounts = mAccountsDbAdapter.getAllAccounts();
265+
assertThat(accounts).hasSize(3);
266+
assertThat(accounts).extracting("mUID").contains(uid);
267+
}
268+
269+
@Test
270+
public void shouldRecursivelyDeleteAccount(){
271+
Account account = new Account("Parent");
272+
Account account2 = new Account("Child");
273+
account2.setParentUID(account.getUID());
274+
275+
Transaction transaction = new Transaction("Random");
276+
account2.addTransaction(transaction);
277+
278+
Split split = new Split(Money.getZeroInstance(), account.getUID());
279+
transaction.addSplit(split);
280+
transaction.addSplit(split.createPair(account2.getUID()));
281+
282+
mAccountsDbAdapter.addAccount(account);
283+
mAccountsDbAdapter.addAccount(account2);
284+
285+
assertThat(mAccountsDbAdapter.getRecordsCount()).isEqualTo(3);
286+
assertThat(mTransactionsDbAdapter.getRecordsCount()).isEqualTo(1);
287+
assertThat(mSplitsDbAdapter.getRecordsCount()).isEqualTo(2);
288+
289+
boolean result = mAccountsDbAdapter.recursiveDeleteAccount(mAccountsDbAdapter.getID(account.getUID()));
290+
assertThat(result).isTrue();
291+
292+
assertThat(mAccountsDbAdapter.getRecordsCount()).isEqualTo(1); //the root account
293+
assertThat(mTransactionsDbAdapter.getRecordsCount()).isZero();
294+
assertThat(mSplitsDbAdapter.getRecordsCount()).isZero();
295+
296+
}
297+
143298
@After
144299
public void tearDown() throws Exception {
145300
mAccountsDbAdapter.deleteAllRecords();

0 commit comments

Comments
 (0)