-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from larmie56/develop
Cache test
- Loading branch information
Showing
12 changed files
with
152 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
cache/src/test/java/com/example/expenselogger/cache/entity/DummyData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) | ||
} |
88 changes: 88 additions & 0 deletions
88
cache/src/test/java/com/example/expenselogger/cache/repository/ExpenseRepositoryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
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 updateExpense updates an Expense in the database`(): Unit = | ||
runBlocking { | ||
val expenseEntity = DummyData.expenseEntity | ||
val id = expenseRepository.insertExpense(expenseEntity) | ||
val newInfo = "Valentine outing with now ex bae" | ||
val expenseEntityUpdate = expenseEntity.copy(id = id, info = newInfo) | ||
expenseRepository.updateExpense(expenseEntityUpdate) | ||
val actual = expenseRepository.getExpense(id) | ||
assertThat(actual).isEqualTo(expenseEntityUpdate) | ||
} | ||
|
||
@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) | ||
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() | ||
} | ||
} |