-
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.
Add TrendingRepoDatabase and test last request provider with robolect…
…ric help
- Loading branch information
Showing
7 changed files
with
154 additions
and
8 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
26 changes: 26 additions & 0 deletions
26
app/src/main/java/com/edwnmrtnz/trendingrepo/core/data/LastRequestDao.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,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) | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
app/src/main/java/com/edwnmrtnz/trendingrepo/core/data/LastRequestRow.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,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) | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
app/src/main/java/com/edwnmrtnz/trendingrepo/core/data/TrendingRepoDatabase.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,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 | ||
} | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
app/src/test/java/com/edwnmrtnz/trendingrepo/core/data/DefaultLastRequestProviderTest.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,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() | ||
} | ||
} |
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