-
Notifications
You must be signed in to change notification settings - Fork 0
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
Cache test #10
Changes from 7 commits
9f32134
a0caded
e7fdbcc
d73fde1
d53e13b
408286d
f0e3ac0
f1b9e25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
plugins { | ||
androidLib | ||
} | ||
|
||
dependencies { | ||
implementation(Library.room) | ||
kapt(Library.roomCompiler) | ||
|
||
testImplementation( | ||
Library.androidXTest, | ||
Library.robolectric | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.example.expenselogger.cache.entity | ||
|
||
import java.util.Date | ||
|
||
internal object DummyData { | ||
|
||
val expenseEntity: ExpenseEntity = ExpenseEntity( | ||
name = "Valentine outing", | ||
amount = 13_500.00, | ||
date = Date(1613311218000).time, // February 14th 2021 | ||
info = "Valentine outing with bae" | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.example.expenselogger.cache.repository | ||
|
||
import androidx.room.Room | ||
import androidx.test.core.app.ApplicationProvider | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.example.expenselogger.cache.ExpenseDatabase | ||
import com.example.expenselogger.cache.entity.DummyData | ||
import com.google.common.truth.Truth.assertThat | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
internal class ExpenseRepositoryTest { | ||
|
||
private lateinit var expenseRepository: ExpenseRepository | ||
private lateinit var expenseDatabase: ExpenseDatabase | ||
|
||
@Before | ||
fun setup() { | ||
expenseDatabase = Room.inMemoryDatabaseBuilder( | ||
ApplicationProvider.getApplicationContext(), | ||
ExpenseDatabase::class.java | ||
).allowMainThreadQueries().build() | ||
|
||
expenseRepository = ExpenseRepositoryImpl( | ||
expenseDatabase.expenseDao | ||
) | ||
} | ||
|
||
@After | ||
fun `tearDown()`() { | ||
expenseDatabase.close() | ||
} | ||
|
||
@Test | ||
fun `verify that insertExpense inserts Expense into database`(): Unit = | ||
runBlocking { | ||
val expenseEntity = DummyData.expenseEntity | ||
val id = expenseRepository.insertExpense(expenseEntity) | ||
val actual = expenseRepository.getExpense(id) | ||
assertThat(actual).isEqualTo(expenseEntity.copy(id = id)) | ||
} | ||
|
||
@Test | ||
fun `verify that getExpenses gets list of expenses`(): Unit = runBlocking { | ||
val expense = DummyData.expenseEntity | ||
val id = expenseRepository.insertExpense(expense) | ||
val id2 = expenseRepository.insertExpense(expense) | ||
Comment on lines
+62
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, all clear now. Thank you |
||
val actual = expenseRepository.getExpenses() | ||
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.copy(id = id)) | ||
} | ||
|
||
@Test | ||
fun `verify that deleteExpense deletes an expense`(): Unit = runBlocking { | ||
val expenseEntity = DummyData.expenseEntity | ||
val id = expenseRepository.insertExpense(expenseEntity) | ||
expenseRepository.deleteExpense(id) | ||
val actual = expenseRepository.getExpense(id) | ||
assertThat(actual).isNull() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try not to add module specific dependencies to the general Android Library plugin
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, thank you