Skip to content

Instantly share code, notes, and snippets.

@ProArun
Created August 19, 2023 16:32

Revisions

  1. ProArun created this gist Aug 19, 2023.
    33 changes: 33 additions & 0 deletions LiveDataUtils.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    import androidx.lifecycle.LiveData
    import androidx.lifecycle.Observer
    import java.util.concurrent.CountDownLatch
    import java.util.concurrent.TimeUnit
    import java.util.concurrent.TimeoutException

    fun <T> LiveData<T>.getOrAwaitValue(
    time: Long = 2,
    timeUnit: TimeUnit = TimeUnit.SECONDS,
    afterObserve: () -> Unit = {}
    ): T {
    var data: T? = null
    val latch = CountDownLatch(1)
    val observer = object : Observer<T> {
    override fun onChanged(value: T) {
    data = value
    latch.countDown()
    this@getOrAwaitValue.removeObserver(this)
    }
    }
    this.observeForever(observer)

    afterObserve.invoke()

    // Don't wait indefinitely if the LiveData is not set.
    if (!latch.await(time, timeUnit)) {
    this.removeObserver(observer)
    throw TimeoutException("LiveData value was never set.")
    }

    @Suppress("UNCHECKED_CAST")
    return data as T
    }
    7 changes: 7 additions & 0 deletions Quote.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    @Entity
    data class Quote(
    @PrimaryKey(autoGenerate = true)
    val id: Int,
    val text: String,
    val author: String
    )
    4 changes: 4 additions & 0 deletions QuoteDatabase.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    @Database(entities = [Quote::class],version = 1)
    abstract class QuoteDatabase : RoomDatabase(){
    abstract fun quoteDao(): QuotesDao
    }
    16 changes: 16 additions & 0 deletions QuotesDao.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    interface QuotesDao {
    @Insert
    suspend fun insertQuote(quote: Quote)

    @Update
    suspend fun updateQuote(quote: Quote)

    @Query("DELETE FROM quote")
    suspend fun delete()

    @Query("SELECT * FROM quote")
    fun getQuotes():LiveData<List<Quote>>

    @Query("SELECT * FROM quote where id = :quoteId")
    suspend fun gerQuoteById()
    }
    47 changes: 47 additions & 0 deletions QuotesDaoTest.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    class QuotesDaoTest{

    @get:Rule
    val instantExecutorRule = InstantTaskExecutorRule()//this will execute architecture component code syncronasly

    lateinit var quoteDatabase: QuoteDatabase
    lateinit var quotesDao: QuotesDao

    @Before
    fun setUp(){

    quoteDatabse = Room.inMemoryDatabseBuilder(
    ApplicationProvider.getApplicationContext(),
    QuoteDatabase::Class.java
    ).allowMainThreadQueries().build()//allowMainThreadQueries():- This method will makesure all query will run on main thread.

    quotesDao = quoteDatabase.quoteDao()
    }

    @After
    fun tearDown(){
    quoteDatabase.close()
    }

    @Test
    fun `insertQuote expected single quote`() = runBlocking{//this runBlocking will block the thread until all the code of this methord is not executed
    val quote = Quote(id:0,text:"This is a test quote",author:"CheezyCode")
    quotesDao.insertQuote(quote)

    val result = quotesDao.getQuotes().getOrAwaitValue()

    Assert.assertEquals(1,result.size)
    Assert.assertEquals("This is a test quote",reqult[0].text)
    }

    @Test
    fun `deleteQuote expected no quote`() = runBlocking{//this runBlocking will block the thread until all the code of this methord is not executed
    val quote = Quote(id:0,text:"This is a test quote",author:"CheezyCode")
    quotesDao.insertQuote(quote)

    quotesDao.delete()

    val result = quotesDao.getQuotes().getOrAwaitValue()

    Assert.assertEquals(0,result.size)
    }
    }
    15 changes: 15 additions & 0 deletions build.gradle
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    plugins{
    id 'kotlin-kapt'
    }

    dependencies {
    def room_version = "2.4.3"
    implementation "androidx.room:room-runtime: $room_version"
    implementation "androidx.room:room-ktx: $room_version"
    kapt "androidx.room:room-compiler: $room_version"

    testImplementation "androidx.room:room-testing: $room_version"
    androidTestImplementation "androidx.room:room-testing: $room_version"
    testImplementation "androidx.arch.core:core-testing:2.1.0"
    androidTestImplementation "androidx.arch.core:core-testing:2.1.0"
    }