Created
August 19, 2023 16:32
Revisions
-
ProArun created this gist
Aug 19, 2023 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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 } This file contains hidden or 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 charactersOriginal 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 ) This file contains hidden or 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 charactersOriginal 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 } This file contains hidden or 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 charactersOriginal 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() } This file contains hidden or 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 charactersOriginal 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) } } This file contains hidden or 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 charactersOriginal 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" }