Skip to content

Commit

Permalink
Fixed: editing account deletes transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
codinguser committed Jul 16, 2015
1 parent 045dc30 commit 909b773
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.gnucash.android.model.Account;
import org.gnucash.android.model.AccountType;
import org.gnucash.android.model.Money;
import org.gnucash.android.model.Split;
import org.gnucash.android.model.Transaction;
import org.gnucash.android.receivers.AccountCreator;
import org.gnucash.android.ui.account.AccountsActivity;
import org.gnucash.android.ui.account.AccountsListFragment;
Expand All @@ -45,6 +47,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;

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

Expand All @@ -71,6 +74,7 @@
@RunWith(AndroidJUnit4.class)
public class AccountsActivityTest extends ActivityInstrumentationTestCase2<AccountsActivity> {
private static final String DUMMY_ACCOUNT_CURRENCY_CODE = "USD";
private static final Currency DUMMY_ACCOUNT_CURRENCY = Currency.getInstance(DUMMY_ACCOUNT_CURRENCY_CODE);
private static final String DUMMY_ACCOUNT_NAME = "Dummy account";
public static final String DUMMY_ACCOUNT_UID = "dummy-account";
private DatabaseHelper mDbHelper;
Expand Down Expand Up @@ -226,7 +230,7 @@ public void testChangeParentAccount() {
*/
@Test
public void shouldHideParentAccountViewWhenNoParentsExist(){
onView(withText(DUMMY_ACCOUNT_NAME)).perform(click());
onView(allOf(withText(DUMMY_ACCOUNT_NAME), isDisplayed())).perform(click());
onView(withId(R.id.fragment_transaction_list)).perform(swipeRight());
onView(withText(R.string.label_create_account)).check(matches(isDisplayed())).perform(click());
sleep(1000);
Expand Down Expand Up @@ -264,6 +268,31 @@ public void testEditAccount(){
assertThat(latest.getCurrency().getCurrencyCode()).isEqualTo(DUMMY_ACCOUNT_CURRENCY_CODE);
}

@Test
public void editingAccountShouldNotDeleteTransactions(){
onView(allOf(withText(DUMMY_ACCOUNT_NAME), isDisplayed()))
.perform(longClick());
Account account = new Account("Transfer Account");

Transaction transaction = new Transaction("Simple trxn");
Split split = new Split(new Money(BigDecimal.TEN, DUMMY_ACCOUNT_CURRENCY), account.getUID());
transaction.addSplit(split);
transaction.addSplit(split.createPair(DUMMY_ACCOUNT_UID));
account.addTransaction(transaction);
mAccountsDbAdapter.addAccount(account);

assertThat(mAccountsDbAdapter.getAccount(DUMMY_ACCOUNT_UID).getTransactionCount()).isEqualTo(1);
assertThat(mSplitsDbAdapter.getSplitsForTransaction(transaction.getUID())).hasSize(2);

onView(withId(R.id.context_menu_edit_accounts)).perform(click());

onView(withId(R.id.menu_save)).perform(click());
assertThat(mAccountsDbAdapter.getAccount(DUMMY_ACCOUNT_UID).getTransactionCount()).isEqualTo(1);
assertThat(mSplitsDbAdapter.fetchSplitsForAccount(DUMMY_ACCOUNT_UID).getCount()).isEqualTo(1);
assertThat(mSplitsDbAdapter.getSplitsForTransaction(transaction.getUID())).hasSize(2);

}

/**
* Sleep the thread for a specified period
* @param millis Duration to sleep in milliseconds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public long addAccount(Account account){
* @return number of rows inserted
*/
public long bulkAddAccounts(List<Account> accountList){
List<Transaction> transactionList = new ArrayList<>(accountList.size()*2);
long nRow = 0;
try {
mDb.beginTransaction();
Expand Down Expand Up @@ -187,12 +188,17 @@ public long bulkAddAccounts(List<Account> accountList){
//Log.d(LOG_TAG, "Replacing account in db");
replaceStatement.execute();
nRow ++;
transactionList.addAll(account.getTransactions());
}
mDb.setTransactionSuccessful();
}
finally {
mDb.endTransaction();
}

if (nRow > 0 && !transactionList.isEmpty()){
mTransactionsAdapter.bulkAddTransactions(transactionList);
}
return nRow;
}
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.SimpleCursorAdapter;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -701,8 +702,9 @@ public void onDestroy() {
* Reads the fields from the account form and saves as a new account
*/
private void saveAccount() {
Log.i("AccountFormFragment", "Saving account");
// accounts to update, in case we're updating full names of a sub account tree
ArrayList<Account> accountsToUpdate = new ArrayList<Account>();
ArrayList<Account> accountsToUpdate = new ArrayList<>();
boolean nameChanged = false;
if (mAccount == null){
String name = getEnteredName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.robolectric.annotation.Config;

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

Expand Down Expand Up @@ -64,6 +65,30 @@ public void shouldBeAlphabeticallySortedByDefault(){
assertThat(accountsList).contains(second, Index.atIndex(1));
}

@Test
public void bulkAddAccountsShouldNotModifyTransactions(){
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);

List<Account> accounts = new ArrayList<>();
accounts.add(account1);
accounts.add(account2);

mAccountsDbAdapter.bulkAddAccounts(accounts);

SplitsDbAdapter splitsDbAdapter = SplitsDbAdapter.getInstance();
assertThat(splitsDbAdapter.getSplitsForTransactionInAccount(transaction.getUID(), account1.getUID())).hasSize(1);
assertThat(splitsDbAdapter.getSplitsForTransactionInAccount(transaction.getUID(), account2.getUID())).hasSize(1);

assertThat(mAccountsDbAdapter.getAccount(account1.getUID()).getTransactions()).hasSize(1);
}

@Test
public void shouldAddAccountsToDatabase(){
Account account1 = new Account("AlphaAccount");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import org.junit.runners.model.InitializationError;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.internal.bytecode.ClassInfo;
import org.robolectric.internal.bytecode.InstrumentingClassLoaderConfig;
import org.robolectric.internal.bytecode.ShadowMap;
import org.robolectric.manifest.AndroidManifest;

import java.util.Arrays;
import java.util.Collections;
Expand All @@ -29,6 +31,11 @@ protected ShadowMap createShadowMap() {
.newBuilder().addShadowClass(ShadowCrashlytics.class).build();
}

@Override
protected AndroidManifest getAppManifest(Config config) {
return super.getAppManifest(config);
}

@Override
public InstrumentingClassLoaderConfig createSetup() {
return new InstrumenterConfig();
Expand Down

0 comments on commit 909b773

Please sign in to comment.