Skip to content

Commit

Permalink
Added tests for recent issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mtotschnig committed Nov 11, 2024
1 parent acf97f0 commit 1934dab
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class CriterionReachedTest : BaseExpenseEditTest() {
doTheTestWithNewTransaction(5000, 40, null)
}

@Test
fun savingGoalReachedSaveAndNew() {
doTheTestWithNewTransaction(5000, 40, R.string.saving_goal_reached, repeat = 2)
}

@Test
fun creditIsLargerThanSavingGoal() {
doTheTestWithNewTransaction(5000, -60, null)
Expand Down Expand Up @@ -177,14 +182,20 @@ class CriterionReachedTest : BaseExpenseEditTest() {
amount: Int,
expectedTitle: Int?,
openingBalance: Long = 0,
repeat: Int = 1,
) {
fixture(criterion, openingBalance)
launchForResult(intentForNewTransaction.apply {
putExtra(ExpenseEdit.KEY_INCOME, amount > 0)
putExtra(Transactions.OPERATION_TYPE, Transactions.TYPE_TRANSACTION)
}).use {
setAmount(amount.absoluteValue)
clickFab()
if (repeat > 1) {
clickMenuItem(R.id.SAVE_AND_NEW_COMMAND)
}
repeat(repeat) {
setAmount(amount.absoluteValue)
clickFab()
}
if (expectedTitle != null) {
composeTestRule.onNodeWithText(getString(expectedTitle)).isDisplayed()
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import com.google.common.truth.Truth
import org.hamcrest.Matchers.allOf
import org.hamcrest.Matchers.containsString
import org.hamcrest.Matchers.`is`
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.totschnig.myexpenses.R
Expand All @@ -23,7 +23,8 @@ import org.totschnig.myexpenses.model.AccountType
import org.totschnig.myexpenses.model.CurrencyUnit
import org.totschnig.myexpenses.model.Template
import org.totschnig.myexpenses.model2.Account
import org.totschnig.myexpenses.provider.DatabaseConstants
import org.totschnig.myexpenses.provider.DatabaseConstants.KEY_ACCOUNTID
import org.totschnig.myexpenses.provider.DatabaseConstants.KEY_TEMPLATEID
import org.totschnig.myexpenses.testutils.Espresso.checkEffectiveGone
import org.totschnig.myexpenses.testutils.Espresso.checkEffectiveVisible
import org.totschnig.myexpenses.testutils.cleanup
Expand Down Expand Up @@ -85,7 +86,7 @@ class ExpenseEditTest : BaseExpenseEditTest() {
fun statusIsShownWhenBankAccountIsSelected() {
launch(intent.apply {
putExtra(Transactions.OPERATION_TYPE, Transactions.TYPE_TRANSACTION)
putExtra(DatabaseConstants.KEY_ACCOUNTID, account2.id)
putExtra(KEY_ACCOUNTID, account2.id)
}).use {
checkEffectiveVisible(R.id.Status)
}
Expand All @@ -94,7 +95,7 @@ class ExpenseEditTest : BaseExpenseEditTest() {
@Test
fun amountInputWithFractionDigitLessCurrency() {
launch(intent.apply {
putExtra(DatabaseConstants.KEY_ACCOUNTID, yenAccount.id)
putExtra(KEY_ACCOUNTID, yenAccount.id)
}).use {
setAmount(100)
}
Expand Down Expand Up @@ -169,7 +170,7 @@ class ExpenseEditTest : BaseExpenseEditTest() {
for (a in allAccounts) {
val i = intent.apply {
putExtra(Transactions.OPERATION_TYPE, Transactions.TYPE_TRANSACTION)
putExtra(DatabaseConstants.KEY_ACCOUNTID, a.id)
putExtra(KEY_ACCOUNTID, a.id)
}
launch(i).use {
checkAccount(a.label)
Expand All @@ -179,25 +180,26 @@ class ExpenseEditTest : BaseExpenseEditTest() {

@Test
fun saveAsNewWorksMultipleTimesInARow() {
//We test with an account that is not sorted first, in order to verify that account is kept
//after save and new fab is clicked
launch(intent.apply {
putExtra(Transactions.OPERATION_TYPE, Transactions.TYPE_TRANSACTION)
putExtra(DatabaseConstants.KEY_ACCOUNTID, account1.id)
putExtra(KEY_ACCOUNTID, account2.id)
}).use {
val success = getString(R.string.save_transaction_and_new_success)
val times = 5
val amount = 2
clickMenuItem(R.id.SAVE_AND_NEW_COMMAND, false) //toggle save and new on
clickMenuItem(R.id.SAVE_AND_NEW_COMMAND) //toggle save and new on
repeat(times) {
setAmount(amount)
clickFab()
checkAccount(account2.label)
onView(withText(success)).check(matches(isDisplayed()))
}
//we assume two fraction digits
assertEquals(
"Transaction sum does not match saved transactions",
repository.getTransactionSum(account1),
(-amount * times * 100).toLong()
)
Truth.assertWithMessage("Transaction sum does not match saved transactions")
.that(repository.getTransactionSum(account2))
.isEqualTo(-amount * times * 100L)
}
}

Expand All @@ -209,14 +211,14 @@ class ExpenseEditTest : BaseExpenseEditTest() {
template.title = "Test template"
template.save(contentResolver)
launch(intent.apply {
putExtra(DatabaseConstants.KEY_TEMPLATEID, template.id)
putExtra(KEY_TEMPLATEID, template.id)
}).use {
val amount = 2
setAmount(amount)
clickFab()
val restored = Template.getInstanceFromDb(contentResolver, template.id)
assertEquals(Transactions.TYPE_TRANSFER, restored!!.operationType())
assertEquals((-amount * 100).toLong(), restored.amount.amountMinor)
val restored = Template.getInstanceFromDb(contentResolver, template.id)!!
Truth.assertThat(restored.operationType()).isEqualTo(Transactions.TYPE_TRANSFER)
Truth.assertThat(restored.amount.amountMinor).isEqualTo(-amount * 100L)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.contrib.RecyclerViewActions
import androidx.test.espresso.matcher.ViewMatchers.hasChildCount
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withSpinnerText
import androidx.test.espresso.matcher.ViewMatchers.withText
import com.adevinta.android.barista.interaction.BaristaScrollInteractions
import com.adevinta.android.barista.internal.matcher.HelperMatchers.menuIdMatcher
import com.adevinta.android.barista.internal.viewaction.NestedEnabledScrollToAction.nestedScrollToAction
import com.google.common.truth.Truth.assertThat
import org.hamcrest.CoreMatchers
import org.hamcrest.CoreMatchers.allOf
import org.hamcrest.CoreMatchers.instanceOf
import org.junit.After
Expand All @@ -29,8 +28,10 @@ import org.junit.Test
import org.totschnig.myexpenses.R
import org.totschnig.myexpenses.activity.ExpenseEdit
import org.totschnig.myexpenses.adapter.IdHolder
import org.totschnig.myexpenses.adapter.SplitPartRVAdapter
import org.totschnig.myexpenses.contract.TransactionsContract.Transactions
import org.totschnig.myexpenses.db2.deleteAccount
import org.totschnig.myexpenses.provider.DatabaseConstants
import org.totschnig.myexpenses.provider.DatabaseConstants.KEY_ROWID
import org.totschnig.myexpenses.provider.DatabaseConstants.KEY_STATUS
import org.totschnig.myexpenses.provider.DatabaseConstants.STATUS_UNCOMMITTED
Expand All @@ -41,6 +42,7 @@ import org.totschnig.myexpenses.testutils.withAccount

class SplitEditTest : BaseExpenseEditTest() {
private val accountLabel1 = "Test label 1"
private val accountLabel2 = "Test label 2"

companion object {
@BeforeClass
Expand Down Expand Up @@ -241,13 +243,13 @@ class SplitEditTest : BaseExpenseEditTest() {
fun create_and_save() {
launch()
createParts(1)
clickMenuItem(R.id.SAVE_AND_NEW_COMMAND, false) //toggle save and new on
clickMenuItem(R.id.SAVE_AND_NEW_COMMAND) //toggle save and new on
clickFab()
onView(withId(com.google.android.material.R.id.snackbar_text))
.check(matches(withText(R.string.save_transaction_and_new_success)))
waitForSnackbarDismissed()
createParts(1)
clickMenuItem(R.id.SAVE_AND_NEW_COMMAND, false) //toggle save and new off
clickMenuItem(R.id.SAVE_AND_NEW_COMMAND) //toggle save and new off
closeKeyboardAndSave()
assertFinishing()
}
Expand All @@ -258,7 +260,27 @@ class SplitEditTest : BaseExpenseEditTest() {
@Test
fun createPartsWhichFlipSign() {
launch()
createParts(1, 50, false)
createParts(1, 50)
createParts(1, 100, true, initialChildCount = 1)
}

@Test
fun createPartsAndDelete() {
account1 = buildAccount(accountLabel1)
val account2 = buildAccount(accountLabel2)
launchForResult(baseIntent.apply {
putExtra(DatabaseConstants.KEY_ACCOUNTID, account2.id)
})
createParts(2, 50)
checkAccount(accountLabel2)
onView(withId(R.id.list)).perform(RecyclerViewActions.actionOnItemAtPosition<SplitPartRVAdapter.ViewHolder>(0, click()))
onData(menuIdMatcher(R.id.DELETE_COMMAND)).perform(click())
checkAccount(accountLabel2)
createParts(2, 50, initialChildCount = 1)
checkAccount(accountLabel2)
clickFab()
cleanup {
repository.deleteAccount(account2.id)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ import org.totschnig.myexpenses.model.PreDefinedPaymentMethod.Companion.translat
import org.totschnig.myexpenses.model.Sort
import org.totschnig.myexpenses.model.Sort.Companion.fromCommandId
import org.totschnig.myexpenses.preference.PrefKey
import org.totschnig.myexpenses.preference.enumValueOrDefault
import org.totschnig.myexpenses.provider.CheckSealedHandler
import org.totschnig.myexpenses.provider.DataBaseAccount.Companion.HOME_AGGREGATE_ID
import org.totschnig.myexpenses.provider.DataBaseAccount.Companion.isAggregate
Expand Down Expand Up @@ -242,9 +243,6 @@ abstract class BaseMyExpenses : LaunchActivity(), OnDialogResultListener, Contri
@Inject
lateinit var reviewManager: ReviewManager

@Inject
lateinit var viewIntentProvider: ViewIntentProvider

@Inject
lateinit var modelClass: Class<out MyExpensesViewModel>

Expand Down Expand Up @@ -1477,13 +1475,8 @@ abstract class BaseMyExpenses : LaunchActivity(), OnDialogResultListener, Contri
}
}

private fun readAccountSortFromPref() = try {
Sort.valueOf(
prefHandler.requireString(PrefKey.SORT_ORDER_ACCOUNTS, Sort.USAGES.name)
)
} catch (e: IllegalArgumentException) {
Sort.USAGES
}
private fun readAccountSortFromPref() =
prefHandler.enumValueOrDefault(PrefKey.SORT_ORDER_ACCOUNTS, Sort.USAGES)

private fun closeDrawer() {
binding.drawer?.closeDrawers()
Expand Down

0 comments on commit 1934dab

Please sign in to comment.