-
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 #9 from larmie56/develop
Add cache module and setup database
- Loading branch information
Showing
23 changed files
with
244 additions
and
61 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
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
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,3 @@ | ||
plugins { | ||
androidLib | ||
} |
Empty file.
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,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle.kts. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
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,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.example.expenselogger.cache"> | ||
|
||
</manifest> |
23 changes: 23 additions & 0 deletions
23
cache/src/main/java/com/example/expenselogger/cache/ExpenseDatabase.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,23 @@ | ||
package com.example.expenselogger.cache | ||
|
||
import android.content.Context | ||
import androidx.room.Database | ||
import androidx.room.Room | ||
import androidx.room.RoomDatabase | ||
import com.example.expenselogger.cache.dao.ExpenseDao | ||
import com.example.expenselogger.cache.entity.ExpenseEntity | ||
|
||
@Database(entities = [ExpenseEntity::class], version = 1, exportSchema = false) | ||
internal abstract class ExpenseDatabase : RoomDatabase() { | ||
|
||
abstract val expenseDao: ExpenseDao | ||
|
||
companion object { | ||
private const val DATABASE_NAME: String = "expense_db" | ||
fun build(context: Context): ExpenseDatabase = Room.databaseBuilder( | ||
context.applicationContext, | ||
ExpenseDatabase::class.java, | ||
DATABASE_NAME | ||
).build() | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
cache/src/main/java/com/example/expenselogger/cache/dao/ExpenseDao.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,25 @@ | ||
package com.example.expenselogger.cache.dao | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Insert | ||
import androidx.room.Query | ||
import androidx.room.Update | ||
import com.example.expenselogger.cache.entity.ExpenseEntity | ||
|
||
@Dao | ||
internal interface ExpenseDao { | ||
@Insert | ||
fun insertExpense(expense: ExpenseEntity) | ||
|
||
@Update | ||
fun updateExpense(expense: ExpenseEntity) | ||
|
||
@Query("SELECT * FROM expense WHERE id = :id") | ||
fun getExpense(id: Long): ExpenseEntity | ||
|
||
@Query("SELECT * FROM expense ORDER BY date ASC") | ||
public fun getExpenses(): List<ExpenseEntity> | ||
|
||
@Query("DELETE FROM expense WHERE id = :id") | ||
fun deleteExpense(id: Long) | ||
} |
33 changes: 33 additions & 0 deletions
33
cache/src/main/java/com/example/expenselogger/cache/di/CacheModule.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,33 @@ | ||
package com.example.expenselogger.cache.di | ||
|
||
import android.content.Context | ||
import com.example.expenselogger.cache.ExpenseDatabase | ||
import com.example.expenselogger.cache.dao.ExpenseDao | ||
import com.example.expenselogger.cache.repository.ExpenseRepository | ||
import com.example.expenselogger.cache.repository.ExpenseRepositoryImpl | ||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@[Module InstallIn(SingletonComponent::class)] | ||
internal interface CacheModule { | ||
|
||
@get:Binds | ||
val ExpenseRepositoryImpl.expenseRepository: ExpenseRepository | ||
|
||
companion object { | ||
@[Provides Singleton] | ||
fun providesExpenseDatabase( | ||
@ApplicationContext context: Context | ||
): ExpenseDatabase = ExpenseDatabase.build(context) | ||
|
||
@[Provides Singleton] | ||
fun providesExpenseDao( | ||
expenseDatabase: ExpenseDatabase | ||
): ExpenseDao = expenseDatabase.expenseDao | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
cache/src/main/java/com/example/expenselogger/cache/entity/ExpenseEntity.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,20 @@ | ||
package com.example.expenselogger.cache.entity | ||
|
||
import androidx.room.ColumnInfo | ||
import androidx.room.Entity | ||
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") | ||
val amount: Double, | ||
@ColumnInfo(name = "date") | ||
val date: Long, | ||
@ColumnInfo(name = "info") | ||
val info: String | ||
) |
12 changes: 12 additions & 0 deletions
12
cache/src/main/java/com/example/expenselogger/cache/repository/ExpenseRepository.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,12 @@ | ||
package com.example.expenselogger.cache.repository | ||
|
||
import com.example.expenselogger.cache.entity.ExpenseEntity | ||
|
||
public interface ExpenseRepository { | ||
|
||
public fun insertExpense(expenseEntity: ExpenseEntity) | ||
public fun updateExpense(expenseEntity: ExpenseEntity) | ||
public fun getExpense(id: Long): ExpenseEntity? | ||
public fun getExpenses(): List<ExpenseEntity> | ||
public fun deleteExpense(id: Long) | ||
} |
30 changes: 30 additions & 0 deletions
30
cache/src/main/java/com/example/expenselogger/cache/repository/ExpenseRepositoryImpl.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,30 @@ | ||
package com.example.expenselogger.cache.repository | ||
|
||
import com.example.expenselogger.cache.dao.ExpenseDao | ||
import com.example.expenselogger.cache.entity.ExpenseEntity | ||
import javax.inject.Inject | ||
|
||
internal class ExpenseRepositoryImpl @Inject constructor( | ||
private val expenseDao: ExpenseDao | ||
) : ExpenseRepository { | ||
|
||
override fun insertExpense(expenseEntity: ExpenseEntity) { | ||
expenseDao.insertExpense(expenseEntity) | ||
} | ||
|
||
override fun updateExpense(expenseEntity: ExpenseEntity) { | ||
expenseDao.updateExpense(expenseEntity) | ||
} | ||
|
||
override fun getExpense(id: Long): ExpenseEntity? { | ||
return expenseDao.getExpense(id) | ||
} | ||
|
||
override fun getExpenses(): List<ExpenseEntity> { | ||
return expenseDao.getExpenses() | ||
} | ||
|
||
override fun deleteExpense(id: Long) { | ||
expenseDao.deleteExpense(id) | ||
} | ||
} |
31 changes: 17 additions & 14 deletions
31
...e/src/test/java/com/example/expenselogger/lib_expense/domain/usecase/DeleteExpenseTest.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 |
---|---|---|
@@ -1,26 +1,29 @@ | ||
package com.example.expenselogger.lib_expense.domain.usecase | ||
|
||
import com.example.expenselogger.lib_expense.domain.fakes.FakeExpenseContract | ||
import com.example.expenselogger.lib_expense.domain.contract.ExpenseContract | ||
import com.example.expenselogger.lib_expense.domain.model.DummyData | ||
import com.example.expenselogger_test_utils.TestAsyncExecutor | ||
import com.google.common.truth.Truth.assertThat | ||
import kotlinx.coroutines.test.runBlockingTest | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.mockito.Mockito.verify | ||
import org.mockito.kotlin.mock | ||
|
||
public class DeleteExpenseTest { | ||
|
||
private val expenseContract = FakeExpenseContract() | ||
private val deleteExpense = DeleteExpense( | ||
expenseContract, | ||
TestAsyncExecutor() | ||
) | ||
private lateinit var expenseContract: ExpenseContract | ||
private lateinit var deleteExpense: DeleteExpense | ||
|
||
@Before | ||
public fun setup() { | ||
expenseContract = mock() | ||
deleteExpense = DeleteExpense(expenseContract, TestAsyncExecutor()) | ||
} | ||
|
||
@Test | ||
public fun `verify that deleteExpense usecase deletes an expense if it exists`(): Unit = | ||
runBlockingTest { | ||
val expense = DummyData.expense | ||
expenseContract.saveExpense(expense) | ||
deleteExpense.invoke(expense) | ||
assertThat(expenseContract.getExpenses().size).isEqualTo(0) | ||
} | ||
public fun `verify that deleteExpense usecase deletes an expense`(): Unit = runBlockingTest { | ||
val expense = DummyData.expense | ||
deleteExpense.invoke(expense) | ||
verify(expenseContract).deleteExpense(expense) | ||
} | ||
} |
24 changes: 11 additions & 13 deletions
24
...ense/src/test/java/com/example/expenselogger/lib_expense/domain/usecase/GetExpenseTest.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 |
---|---|---|
@@ -1,26 +1,24 @@ | ||
package com.example.expenselogger.lib_expense.domain.usecase | ||
|
||
import com.example.expenselogger.lib_expense.domain.fakes.FakeExpenseContract | ||
import com.example.expenselogger.lib_expense.domain.contract.ExpenseContract | ||
import com.example.expenselogger.lib_expense.domain.model.DummyData | ||
import com.example.expenselogger_test_utils.TestAsyncExecutor | ||
import com.google.common.truth.Truth.assertThat | ||
import kotlinx.coroutines.test.runBlockingTest | ||
import org.junit.Test | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.whenever | ||
|
||
public class GetExpenseTest { | ||
|
||
private val expenseContract = FakeExpenseContract() | ||
private val getExpense = GetExpense( | ||
expenseContract, | ||
TestAsyncExecutor() | ||
) | ||
|
||
@Test | ||
public fun `verify that getExpense usecase returns an expense`(): Unit = | ||
runBlockingTest { | ||
val expense = DummyData.expense | ||
expenseContract.saveExpense(expense) | ||
val expectedExpense = getExpense.invoke(expense.id) | ||
assertThat(expectedExpense).isEqualTo(expense) | ||
public fun `verify that getExpense usecase gets an expense`(): Unit = runBlockingTest { | ||
val expense = DummyData.expense | ||
val expenseContract = mock<ExpenseContract>().apply { | ||
whenever(getExpense(0)).thenReturn(expense) | ||
} | ||
val getExpense = GetExpense(expenseContract, TestAsyncExecutor()) | ||
val expectedExpense = getExpense.invoke(0) | ||
assertThat(expectedExpense).isEqualTo(expense) | ||
} | ||
} |
21 changes: 10 additions & 11 deletions
21
...nse/src/test/java/com/example/expenselogger/lib_expense/domain/usecase/GetExpensesTest.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 |
---|---|---|
@@ -1,25 +1,24 @@ | ||
package com.example.expenselogger.lib_expense.domain.usecase | ||
|
||
import com.example.expenselogger.lib_expense.domain.fakes.FakeExpenseContract | ||
import com.example.expenselogger.lib_expense.domain.contract.ExpenseContract | ||
import com.example.expenselogger.lib_expense.domain.model.DummyData | ||
import com.example.expenselogger_test_utils.TestAsyncExecutor | ||
import com.google.common.truth.Truth.assertThat | ||
import kotlinx.coroutines.test.runBlockingTest | ||
import org.junit.Test | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.whenever | ||
|
||
public class GetExpensesTest { | ||
|
||
private val expenseContract = FakeExpenseContract() | ||
private val getExpenses = GetExpenses( | ||
expenseContract, | ||
TestAsyncExecutor() | ||
) | ||
|
||
@Test | ||
public fun `verify that expenses returns a list of expenses`(): Unit = runBlockingTest { | ||
val expense = DummyData.expense | ||
expenseContract.saveExpense(expense) | ||
public fun `verify that getExpenses usecase gets a list of expenses`(): Unit = runBlockingTest { | ||
val expenses = listOf(DummyData.expense) | ||
val expenseContract = mock<ExpenseContract>().apply { | ||
whenever(getExpenses()).thenReturn(expenses) | ||
} | ||
val getExpenses = GetExpenses(expenseContract, TestAsyncExecutor()) | ||
val expectedExpenses = getExpenses.invoke() | ||
assertThat(expectedExpenses.size).isEqualTo(1) | ||
assertThat(expectedExpenses).isEqualTo(expenses) | ||
} | ||
} |
Oops, something went wrong.