33import org .assertj .core .data .Index ;
44import org .gnucash .android .BuildConfig ;
55import org .gnucash .android .db .AccountsDbAdapter ;
6+ import org .gnucash .android .db .ScheduledActionDbAdapter ;
67import org .gnucash .android .db .SplitsDbAdapter ;
78import org .gnucash .android .db .TransactionsDbAdapter ;
89import org .gnucash .android .model .Account ;
910import org .gnucash .android .model .AccountType ;
1011import org .gnucash .android .model .Money ;
12+ import org .gnucash .android .model .ScheduledAction ;
1113import org .gnucash .android .model .Split ;
1214import org .gnucash .android .model .Transaction ;
15+ import org .gnucash .android .model .TransactionType ;
1316import org .gnucash .android .test .unit .util .GnucashTestRunner ;
1417import org .gnucash .android .test .unit .util .ShadowCrashlytics ;
1518import org .junit .After ;
1821import org .junit .runner .RunWith ;
1922import org .robolectric .annotation .Config ;
2023
24+ import java .math .BigDecimal ;
25+ import java .util .Currency ;
2126import java .util .List ;
2227
2328import static org .assertj .core .api .Assertions .assertThat ;
2429import 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