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
2 changes: 2 additions & 0 deletions buildSrc/src/main/kotlin/Library.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ object Library {

// AndroidXTest
const val androidXTest: String = "androidx.test.ext:junit:${Version.testExt}"
const val runner: String = "androidx.test:runner:${Version.runner}"
const val robolectric: String = "org.robolectric:robolectric:${Version.robolectric}"
}

object Project {
Expand Down
2 changes: 2 additions & 0 deletions buildSrc/src/main/kotlin/Version.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ object Version {

// AndroidXTest
const val testExt: String = "1.1.3"
const val robolectric: String = "4.5.1"
const val runner: String = "1.2.0"

// ktlint
const val ktlint: String = "0.42.1"
Expand Down
9 changes: 2 additions & 7 deletions buildSrc/src/main/kotlin/plugin/AndroidLibraryPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,15 @@ class AndroidLibraryPlugin : BasePlugin() {
get() = {
implementation(
Library.daggerHiltAndroid,
Library.coroutines,
Library.room
Library.coroutines
)
testImplementation(
Library.junit,
Library.truth,
Library.mockito,
Library.androidXTest,
Library.coroutinesTest
)
kapt(
Library.daggerHiltCompiler,
Library.roomCompiler
Copy link
Collaborator

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

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, thank you

)
kapt(Library.daggerHiltCompiler)
}

override val extensions: Array<ProjectExtension>
Expand Down
12 changes: 12 additions & 0 deletions cache/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
plugins {
androidLib
}

dependencies {
implementation(Library.room)
kapt(Library.roomCompiler)

testImplementation(
Library.androidXTest,
Library.runner,
Library.androidXTest,
Library.robolectric
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ package com.example.expenselogger.cache.dao

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
suspend fun insertExpense(expense: ExpenseEntity)
@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
suspend fun getExpense(id: Long): ExpenseEntity?

@Query("SELECT * FROM expense ORDER BY date ASC")
suspend fun getExpenses(): List<ExpenseEntity>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import androidx.room.PrimaryKey

@Entity(tableName = "expense")
public data class ExpenseEntity(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
val id: Long,
@ColumnInfo(name = "name")
val name: String,
@ColumnInfo(name = "amount")
Expand All @@ -17,4 +14,8 @@ public data class ExpenseEntity(
val date: Long,
@ColumnInfo(name = "info")
val info: String
)
) {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
var id: Long = 0L
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.example.expenselogger.cache.entity.ExpenseEntity

public interface ExpenseRepository {

public suspend fun insertExpense(expenseEntity: ExpenseEntity)
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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@ internal class ExpenseRepositoryImpl @Inject constructor(
private val expenseDao: ExpenseDao
) : ExpenseRepository {

override suspend fun insertExpense(expenseEntity: ExpenseEntity) {
expenseDao.insertExpense(expenseEntity)
}
override suspend fun insertExpense(
expenseEntity: ExpenseEntity
): Long = expenseDao.insertExpense(expenseEntity)

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

override suspend fun getExpense(id: Long): ExpenseEntity? {
return expenseDao.getExpense(id)
}
override suspend fun getExpense(
id: Long
): ExpenseEntity? = expenseDao.getExpense(id)

override suspend fun getExpenses(): List<ExpenseEntity> {
return expenseDao.getExpenses()
}
override suspend fun getExpenses(): List<ExpenseEntity> = expenseDao.getExpenses()

override suspend fun deleteExpense(id: Long) {
expenseDao.deleteExpense(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package com.example.expenselogger.cache.entity

import java.util.Date

public object DummyData {
internal object DummyData {

public val expenseEntity: ExpenseEntity = ExpenseEntity(
id = 0,
val expenseEntity: ExpenseEntity = ExpenseEntity(
name = "Valentine outing",
amount = 13_500.00,
date = Date(1613311218000).time, // February 14th 2021
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ 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.test.runBlockingTest
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
public class ExpenseRepositoryImplTest {
internal class ExpenseRepositoryTest {

private lateinit var expenseRepository: ExpenseRepository
private lateinit var expenseDatabase: ExpenseDatabase

@Before
public fun setup() {
fun setup() {
expenseDatabase = Room.inMemoryDatabaseBuilder(
ApplicationProvider.getApplicationContext(),
ExpenseDatabase::class.java
Expand All @@ -30,33 +30,33 @@ public class ExpenseRepositoryImplTest {
)
}

@After
fun `tearDown()`() {
expenseDatabase.close()
}

@Test
public fun `verify that insertExpense inserts Expense into database`(): Unit =
runBlockingTest {
fun `verify that insertExpense inserts Expense into database`(): Unit =
runBlocking {
val expenseEntity = DummyData.expenseEntity
expenseRepository.insertExpense(expenseEntity)
val expectedExpenseEntity = expenseRepository.getExpense(0)
assertThat(expectedExpenseEntity).isEqualTo(expenseEntity)
val id = expenseRepository.insertExpense(expenseEntity)
val actual = expenseRepository.getExpense(id)
assertThat(actual).isEqualTo(expenseEntity)
}

@Test
public fun `verify that getExpenses gets list of expenses`(): Unit = runBlockingTest {
fun `verify that getExpenses gets list of expenses`(): Unit = runBlocking {
val expense = DummyData.expenseEntity
expenseRepository.insertExpense(expense)
val expectedExpenses = expenseRepository.getExpenses().first()
assertThat(expense).isEqualTo(expectedExpenses)
val actual = expenseRepository.getExpenses()
assertThat(actual).isEqualTo(listOf(expense))
}

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

@After
public fun `tearDown()`() {
expenseDatabase.close()
val id = expenseRepository.insertExpense(expenseEntity)
val actual = expenseRepository.getExpense(id)
assertThat(actual).isEqualTo(expenseEntity)
}
}