Skip to content

Commit

Permalink
Add TrendingRepoDatabase and test last request provider with robolect…
Browse files Browse the repository at this point in the history
…ric help
  • Loading branch information
edwnmrtnz committed Jun 6, 2023
1 parent 6b00212 commit 500d898
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 8 deletions.
15 changes: 15 additions & 0 deletions app/src/main/java/com/edwnmrtnz/trendingrepo/app/di/DataModule.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.edwnmrtnz.trendingrepo.app.di

import android.content.Context
import com.edwnmrtnz.trendingrepo.app.DefaultInteractorHandler
import com.edwnmrtnz.trendingrepo.app.TrendyApplication
import com.edwnmrtnz.trendingrepo.core.data.DefaultGithubReposRepository
import com.edwnmrtnz.trendingrepo.core.data.DefaultLastRequestProvider
import com.edwnmrtnz.trendingrepo.core.data.GithubAPI
import com.edwnmrtnz.trendingrepo.core.data.LastRequestDao
import com.edwnmrtnz.trendingrepo.core.data.TrendingRepoDatabase
import com.edwnmrtnz.trendingrepo.core.domain.GithubRepoGateway
import com.edwnmrtnz.trendingrepo.core.domain.LastRequestProvider
import com.edwnmrtnz.trendingrepo.core.domain.interactor.InteractorHandler
Expand All @@ -13,6 +16,7 @@ import dagger.Module
import dagger.Provides
import dagger.Reusable
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
import retrofit2.Retrofit
Expand Down Expand Up @@ -44,6 +48,17 @@ abstract class DataModule {
.build()
}

@Singleton
@Provides
fun provideDatabase(@ApplicationContext context: Context): TrendingRepoDatabase {
return TrendingRepoDatabase.getInstance(context)
}

@Provides
fun provideLastRequestDao(database: TrendingRepoDatabase): LastRequestDao {
return database.lastRequestDao()
}

@Provides
@Reusable
fun provideGithubAPI(retrofit: Retrofit): GithubAPI {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package com.edwnmrtnz.trendingrepo.core.data

import android.text.format.DateUtils
import com.edwnmrtnz.trendingrepo.core.domain.LastRequestProvider
import java.util.Date
import javax.inject.Inject

class DefaultLastRequestProvider @Inject constructor() : LastRequestProvider {

private var lastRequest = Date()
class DefaultLastRequestProvider @Inject constructor(
private val dao: LastRequestDao
) : LastRequestProvider {

override suspend fun isToday(): Boolean {
return DateUtils.isToday(lastRequest.time)
val lastRequest = dao.get()?.toDate()
return if (lastRequest == null) false else DateUtils.isToday(lastRequest.time)
}

override suspend fun update() {
lastRequest = Date()
dao.save(LastRequestRow.create())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.edwnmrtnz.trendingrepo.core.data

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Transaction

@Dao
interface LastRequestDao {

@Query("SELECT * FROM last_request LIMIT 1")
fun get(): LastRequestRow?

@Query("DELETE FROM last_request")
fun clear()

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(row: LastRequestRow)

@Transaction
suspend fun save(row: LastRequestRow) {
clear()
insert(row)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.edwnmrtnz.trendingrepo.core.data

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import java.util.Date

@Entity(tableName = "last_request")
class LastRequestRow(
@PrimaryKey
@ColumnInfo(name = "key")
val key: String,
@ColumnInfo(name = "time")
val time: Long
) {

companion object {
private const val DEFAULT_KEY = "key_last_request_row"
fun create(): LastRequestRow {
return LastRequestRow(DEFAULT_KEY, time = Date().time)
}
}

fun toDate(): Date {
return Date(time)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.edwnmrtnz.trendingrepo.core.data

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

@Database(
entities = [
LastRequestRow::class
],
version = 1,
exportSchema = false
)
abstract class TrendingRepoDatabase : RoomDatabase() {

abstract fun lastRequestDao(): LastRequestDao

companion object {
@Volatile
private var INSTANCE: TrendingRepoDatabase? = null
fun getInstance(context: Context): TrendingRepoDatabase {
return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
TrendingRepoDatabase::class.java,
"trendingrepo.db"
).fallbackToDestructiveMigration()
.allowMainThreadQueries()
.build()
INSTANCE = instance
instance
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.edwnmrtnz.trendingrepo.core.data

import android.app.Application
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import com.edwnmrtnz.trendingrepo.core.domain.LastRequestProvider
import com.google.common.truth.Truth
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

@RunWith(RobolectricTestRunner::class)
@Config(manifest = Config.NONE, sdk = [Config.OLDEST_SDK], application = Application::class)
class DefaultLastRequestProviderTest {

private lateinit var db: TrendingRepoDatabase

private lateinit var sut: LastRequestProvider

@Before
fun setup() {
db = Room.inMemoryDatabaseBuilder(
ApplicationProvider.getApplicationContext(),
TrendingRepoDatabase::class.java
).allowMainThreadQueries().build()

sut = DefaultLastRequestProvider(db.lastRequestDao())
}

@Test
fun `should be able to save last request`() = runTest {
sut.update()

Truth.assertThat(db.lastRequestDao().get()).isNotNull()
}
}
7 changes: 5 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ picasso = "2.8"
lottie = "6.0.0"
coroutines-android = "1.3.9"
dagger-hilt = "2.46.1"
room = "2.5.1"

edwnmrtnz-testing-local = "1.1"
edwnmrtnz-scopey = "1.1"
Expand All @@ -37,6 +38,8 @@ edwnmrtnz-scopey = { module = "net.github.amaterasu:scopey", version.ref = "edwn
coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "coroutines-android" }
dagger-hilt-compiler = { module = "com.google.dagger:hilt-compiler", version.ref = "dagger-hilt" }
dagger-hilt = { module = "com.google.dagger:hilt-android", version.ref = "dagger-hilt" }
room = {module = "androidx.room:room-ktx", version.ref = "room"}
room-compiler = {module = "androidx.room:room-compiler", version.ref = "room"}


edwnmrtnz-testing-local = { module = "net.github.amaterasu:localtest", version.ref = "edwnmrtnz-testing-local" }
Expand All @@ -50,15 +53,15 @@ kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", v
app = [
"core-ktx", "appcompat", "material", "constraintlayout", "gson", "retrofit",
"retrofit-converter-gson", "shimmer", "picasso", "activity-ktx", "lottie",
"dagger-hilt"
"dagger-hilt", "room"
]
testing-local = [
"junit", "edwnmrtnz-testing-local"
]
testing-instrumented = [
"android-ext-junit"
]
kapt = ["dagger-hilt-compiler"]
kapt = ["dagger-hilt-compiler", "room-compiler"]


[plugins]
Expand Down

0 comments on commit 500d898

Please sign in to comment.