Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache test #10

Merged
merged 8 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.example.expenselogger.cache.entity.ExpenseEntity

@Dao
internal interface ExpenseDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertExpense(expense: ExpenseEntity): Long

@Update
suspend fun updateExpense(expense: ExpenseEntity)

@Query("SELECT * FROM expense WHERE id = :id")
suspend fun getExpense(id: Long): ExpenseEntity?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ public data class ExpenseEntity(
@ColumnInfo(name = "date")
val date: Long,
@ColumnInfo(name = "info")
val info: String
) {
val info: String,
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
var id: Long = 0L
}
val id: Long = 0L
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package com.example.expenselogger.cache.repository
import com.example.expenselogger.cache.entity.ExpenseEntity

public interface ExpenseRepository {

public suspend fun insertExpense(expenseEntity: ExpenseEntity): Long
public suspend fun updateExpense(expenseEntity: ExpenseEntity)
public suspend fun getExpense(id: Long): ExpenseEntity?
public suspend fun getExpenses(): List<ExpenseEntity>
public suspend fun deleteExpense(id: Long)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ internal class ExpenseRepositoryImpl @Inject constructor(
expenseEntity: ExpenseEntity
): Long = expenseDao.insertExpense(expenseEntity)

override suspend fun updateExpense(expenseEntity: ExpenseEntity) {
expenseDao.updateExpense(expenseEntity)
}

override suspend fun getExpense(
id: Long
): ExpenseEntity? = expenseDao.getExpense(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,38 +41,28 @@ internal class ExpenseRepositoryTest {
val expenseEntity = DummyData.expenseEntity
val id = expenseRepository.insertExpense(expenseEntity)
val actual = expenseRepository.getExpense(id)
assertThat(actual).isEqualTo(expenseEntity)
assertThat(actual).isEqualTo(expenseEntity.copy(id = id))
}

@Test
fun `verify that getExpenses gets list of expenses`(): Unit = runBlocking {
val expense = DummyData.expenseEntity
expenseRepository.insertExpense(expense)
val id = expenseRepository.insertExpense(expense)
val id2 = expenseRepository.insertExpense(expense)
Comment on lines +62 to +63
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand inserting the expense into the database twice. Is it like to sort of simulate multiple entries?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, all clear now. Thank you

val actual = expenseRepository.getExpenses()
assertThat(actual).isEqualTo(listOf(expense))
val expected = listOf(
expense.copy(id = id),
expense.copy(id = id2)
)
assertThat(actual).isEqualTo(expected)
}

@Test
fun `verify that getExpense gets an expense`(): Unit = runBlocking {
val expenseEntity = DummyData.expenseEntity
val id = expenseRepository.insertExpense(expenseEntity)
val actual = expenseRepository.getExpense(id)
assertThat(actual).isEqualTo(expenseEntity)
}

@Test
fun `verify that updateExpense updates an expense`(): Unit = runBlocking {
val expenseEntity = DummyData.expenseEntity
val id = expenseRepository.insertExpense(expenseEntity)
val newExpenseEntity = expenseRepository.getExpense(id)
val newInfo = "Valentine outing with now ex bae"
val newExpenseEntityCopy = newExpenseEntity?.copy(info = newInfo)
newExpenseEntityCopy?.let {
it.id = id
expenseRepository.updateExpense(it)
}
val actual = expenseRepository.getExpense(id)
assertThat(actual?.info).isEqualTo(newInfo)
assertThat(actual).isEqualTo(expenseEntity.copy(id = id))
}

@Test
Expand Down