mirror of
https://github.com/hmalik144/Farmr.git
synced 2026-03-18 15:35:55 +00:00
- RepositoryImplTest.kt added
This commit is contained in:
@@ -1,17 +0,0 @@
|
||||
package com.appttude.h_mal.farmr;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Example local unit test, which will execute on the development machine (host).
|
||||
*
|
||||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
||||
*/
|
||||
public class ExampleUnitTest {
|
||||
@Test
|
||||
public void addition_isCorrect() throws Exception {
|
||||
assertEquals(4, 2 + 2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.appttude.h_mal.farmr.data
|
||||
|
||||
import com.appttude.h_mal.farmr.data.legacydb.LegacyDatabase
|
||||
import com.appttude.h_mal.farmr.data.legacydb.ShiftObject
|
||||
import com.appttude.h_mal.farmr.data.prefs.PreferenceProvider
|
||||
import io.mockk.MockKAnnotations
|
||||
import io.mockk.every
|
||||
import io.mockk.impl.annotations.MockK
|
||||
import io.mockk.mockk
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.mockito.ArgumentMatchers.anyLong
|
||||
import java.util.UUID
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertIs
|
||||
|
||||
class RepositoryImplTest {
|
||||
|
||||
private lateinit var repository: RepositoryImpl
|
||||
|
||||
@MockK
|
||||
lateinit var db: LegacyDatabase
|
||||
|
||||
@MockK
|
||||
lateinit var prefs: PreferenceProvider
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
MockKAnnotations.init(this)
|
||||
repository = RepositoryImpl(db, prefs)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun readDatabase_validResponse() {
|
||||
// Arrange
|
||||
val elements = listOf<ShiftObject>(
|
||||
mockk { every { id } returns anyLong() },
|
||||
mockk { every { id } returns anyLong() },
|
||||
mockk { every { id } returns anyLong() },
|
||||
mockk { every { id } returns anyLong() }
|
||||
)
|
||||
|
||||
//Act
|
||||
every { db.readShiftsFromDatabase() } returns elements
|
||||
|
||||
// Assert
|
||||
val result = repository.readShiftsFromDatabase()
|
||||
assertIs<List<ShiftObject>>(result)
|
||||
assertEquals(result.first().id, anyLong())
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.appttude.h_mal.farmr.utils
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.Observer
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.runBlocking
|
||||
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
|
||||
): T {
|
||||
var data: T? = null
|
||||
val latch = CountDownLatch(1)
|
||||
val observer = object : Observer<T> {
|
||||
override fun onChanged(o: T?) {
|
||||
data = o
|
||||
latch.countDown()
|
||||
this@getOrAwaitValue.removeObserver(this)
|
||||
}
|
||||
}
|
||||
|
||||
this.observeForever(observer)
|
||||
|
||||
// Don't wait indefinitely if the LiveData is not set.
|
||||
if (!latch.await(time, timeUnit)) {
|
||||
throw TimeoutException("LiveData value was never set.")
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return data as T
|
||||
}
|
||||
|
||||
fun sleep(millis: Long = 1000) {
|
||||
runBlocking(Dispatchers.Default) { delay(millis) }
|
||||
}
|
||||
Reference in New Issue
Block a user