public void testReset() {
   insertData();
   Money initialtotalBalance = account1.getTotalBalance();
   assertEquals(6, count(account1.getId(), null));
   account1.reset(null, Account.EXPORT_HANDLE_DELETED_UPDATE_BALANCE, null);
   Account.clear();
   assertEquals(0, count(account1.getId(), null));
   Account resetAccount = Account.getInstanceFromDb(account1.getId());
   assertEquals(initialtotalBalance, resetAccount.getTotalBalance());
 }
 public void testResetWithFilterUpdateBalance() {
   insertData();
   Money initialtotalBalance = account1.getTotalBalance();
   assertEquals(6, count(account1.getId(), null));
   WhereFilter filter = WhereFilter.empty();
   filter.put(0, new CategoryCriteria(TEST_CAT, catId));
   account1.reset(filter, Account.EXPORT_HANDLE_DELETED_UPDATE_BALANCE, null);
   Account.clear();
   assertEquals(5, count(account1.getId(), null)); // 1 Transaction deleted
   Account resetAccount = Account.getInstanceFromDb(account1.getId());
   assertEquals(initialtotalBalance, resetAccount.getTotalBalance());
 }
 public void testBalanceWithReset() {
   insertData();
   Money initialclearedBalance = account1.getClearedBalance();
   assertFalse(initialclearedBalance.equals(account1.getReconciledBalance()));
   assertEquals(
       4, count(account1.getId(), KEY_CR_STATUS + " = '" + CrStatus.CLEARED.name() + "'"));
   assertEquals(
       0, count(account1.getId(), KEY_CR_STATUS + " = '" + CrStatus.RECONCILED.name() + "'"));
   account1.balance(true);
   assertEquals(
       0, count(account1.getId(), KEY_CR_STATUS + " != '" + CrStatus.UNRECONCILED.name() + "'"));
   assertEquals(
       2, count(account1.getId(), KEY_CR_STATUS + " = '" + CrStatus.UNRECONCILED.name() + "'"));
   assertEquals(initialclearedBalance, account1.getReconciledBalance());
 }
 public void testResetWithFilterCreateHelper() {
   insertData();
   Money initialtotalBalance = account1.getTotalBalance();
   assertEquals(6, count(account1.getId(), null));
   assertEquals(1, count(account1.getId(), KEY_CATID + "=" + catId));
   assertEquals(0, count(account1.getId(), KEY_STATUS + "=" + STATUS_HELPER));
   WhereFilter filter = WhereFilter.empty();
   filter.put(0, new CategoryCriteria(TEST_CAT, catId));
   account1.reset(filter, Account.EXPORT_HANDLE_DELETED_CREATE_HELPER, null);
   Account.clear();
   assertEquals(6, count(account1.getId(), null)); // -1 Transaction deleted;+1 helper
   assertEquals(0, count(account1.getId(), KEY_CATID + "=" + catId));
   assertEquals(1, count(account1.getId(), KEY_STATUS + "=" + STATUS_HELPER));
   Account resetAccount = Account.getInstanceFromDb(account1.getId());
   assertEquals(initialtotalBalance, resetAccount.getTotalBalance());
 }
 public Transaction toTransaction(Account a) {
   Transaction t;
   Money m = new Money(a.currency, amount);
   if (isSplit()) {
     t = new SplitTransaction(a.getId(), m);
   } else if (isTransfer()) {
     t = new Transfer(a.getId(), m);
   } else {
     t = new Transaction(a.getId(), m);
   }
   if (date != null) {
     t.setDate(date);
   }
   t.comment = memo;
   t.crStatus = Transaction.CrStatus.fromQifName(status);
   t.referenceNumber = number;
   return t;
 }
 public void testAccount() throws RemoteException, OperationApplicationException {
   Account account, restored = null;
   Long openingBalance = (long) 100;
   account = new Account("TestAccount", openingBalance, "Testing with Junit");
   account.setCurrency("EUR");
   assertEquals("EUR", account.currency.getCurrencyCode());
   account.save();
   assertTrue(account.getId() > 0);
   restored = Account.getInstanceFromDb(account.getId());
   assertEquals(account, restored);
   Long trAmount = (long) 100;
   Transaction op1 = Transaction.getNewInstance(account.getId());
   op1.setAmount(new Money(account.currency, trAmount));
   op1.comment = "test transaction";
   op1.save();
   assertEquals(account.getTotalBalance().getAmountMinor().longValue(), openingBalance + trAmount);
   Account.delete(account.getId());
   assertNull(
       "Account deleted, but can still be retrieved", Account.getInstanceFromDb(account.getId()));
   assertNull(
       "Account delete should delete transaction, but operation can still be retrieved",
       Transaction.getInstanceFromDb(op1.getId()));
 }
 private void insertData() {
   Transaction op;
   account1 = new Account("Account 1", openingBalance, "Account 1");
   account1.save();
   account2 = new Account("Account 2", openingBalance, "Account 2");
   account2.save();
   catId = Category.write(0, TEST_CAT, null);
   op = Transaction.getNewInstance(account1.getId());
   op.setAmount(new Money(account1.currency, -expense1));
   op.crStatus = CrStatus.CLEARED;
   op.save();
   op.setAmount(new Money(account1.currency, -expense2));
   op.saveAsNew();
   op.setAmount(new Money(account1.currency, income1));
   op.saveAsNew();
   op.setAmount(new Money(account1.currency, income2));
   op.setCatId(catId);
   op.saveAsNew();
   Transfer op1 = Transfer.getNewInstance(account1.getId(), account2.getId());
   op1.setAmount(new Money(account1.currency, transferP));
   op1.save();
   op1.setAmount(new Money(account1.currency, -transferN));
   op1.saveAsNew();
 }
  /**
   * we test if the db calculates the aggregate sums correctly this is rather a test of the cursor
   * exposed through the content provider but set up is easier through models
   */
  public void testAggregates() {
    Cursor cursor =
        getMockContentResolver()
            .query(
                TransactionProvider.ACCOUNTS_URI, // the URI for the main data table
                null, // get all the columns
                null, // no selection columns, get all the records
                null, // no selection criteria
                null // use default the sort order
                );
    // the database setup creates the default account
    assertEquals(1, cursor.getCount());
    insertData();
    cursor =
        getMockContentResolver()
            .query(
                TransactionProvider.ACCOUNTS_URI, // the URI for the main data table
                Account.PROJECTION_FULL, // get all the columns
                null, // no selection columns, get all the records
                null, // no selection criteria
                null // use default the sort order
                );

    assertEquals(3, cursor.getCount());

    cursor =
        getMockContentResolver()
            .query(
                TransactionProvider.ACCOUNTS_URI, // the URI for the main data table
                Account.PROJECTION_FULL, // get all the columns
                KEY_ROWID + "=" + account1.getId(),
                null, // no selection criteria
                null // use default the sort order
                );

    assertTrue(cursor.moveToFirst());

    // Since no projection was used, get the column indexes of the returned columns
    int incomeIndex = cursor.getColumnIndex(KEY_SUM_INCOME);
    int expensesIndex = cursor.getColumnIndex(KEY_SUM_EXPENSES);
    int transferIndex = cursor.getColumnIndex(KEY_SUM_TRANSFERS);
    int balanceIndex = cursor.getColumnIndex(KEY_CURRENT_BALANCE);
    assertEquals(income1 + income2, cursor.getLong(incomeIndex));
    assertEquals(-expense1 - expense2, cursor.getLong(expensesIndex));
    assertEquals(transferP - transferN, cursor.getLong(transferIndex));
    assertEquals(
        openingBalance + income1 + income2 - expense1 - expense2 + transferP - transferN,
        cursor.getLong(balanceIndex));
    cursor =
        getMockContentResolver()
            .query(
                TransactionProvider.ACCOUNTS_URI, // the URI for the main data table
                Account.PROJECTION_FULL, // get all the columns
                KEY_ROWID + "=" + account2.getId(),
                null, // no selection criteria
                null // use default the sort order
                );

    assertTrue(cursor.moveToFirst());
    assertEquals(0L, cursor.getLong(incomeIndex));
    assertEquals(0L, cursor.getLong(expensesIndex));
    assertEquals(transferN - transferP, cursor.getLong(transferIndex));
    assertEquals(openingBalance + transferN - transferP, cursor.getLong(balanceIndex));
  }
 @Override
 public Loader<Cursor> onCreateLoader(int id, Bundle arg1) {
   CursorLoader cursorLoader = null;
   String selection;
   String[] selectionArgs;
   if (mAccount.getId() < 0) {
     selection =
         KEY_ACCOUNTID
             + " IN "
             + "(SELECT "
             + KEY_ROWID
             + " from "
             + TABLE_ACCOUNTS
             + " WHERE "
             + KEY_CURRENCY
             + " = ?)";
     selectionArgs = new String[] {mAccount.currency.getCurrencyCode()};
   } else {
     selection = KEY_ACCOUNTID + " = ?";
     selectionArgs = new String[] {String.valueOf(mAccount.getId())};
   }
   switch (id) {
     case TRANSACTION_CURSOR:
       Uri uri =
           TransactionProvider.TRANSACTIONS_URI
               .buildUpon()
               .appendQueryParameter("extended", "1")
               .build();
       cursorLoader =
           new CursorLoader(
               getActivity(),
               uri,
               null,
               selection + " AND " + KEY_PARENTID + " is null",
               selectionArgs,
               null);
       break;
       // TODO: probably we can get rid of SUM_CURSOR, if we also aggregate unmapped transactions
     case SUM_CURSOR:
       cursorLoader =
           new CursorLoader(
               getActivity(),
               TransactionProvider.TRANSACTIONS_URI,
               new String[] {MAPPED_CATEGORIES},
               selection + " AND " + WHERE_NOT_SPLIT,
               selectionArgs,
               null);
       break;
     case GROUPING_CURSOR:
       Builder builder = TransactionProvider.TRANSACTIONS_URI.buildUpon();
       builder.appendPath("groups").appendPath(mAccount.grouping.name());
       // the selectionArg is used in a subquery used by the content provider
       // this will change once filters are implemented
       if (mAccount.getId() < 0) {
         builder.appendQueryParameter(KEY_CURRENCY, mAccount.currency.getCurrencyCode());
       } else {
         builder.appendQueryParameter(KEY_ACCOUNTID, String.valueOf(mAccount.getId()));
       }
       cursorLoader = new CursorLoader(getActivity(), builder.build(), null, null, null, null);
       break;
   }
   return cursorLoader;
 }