@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2021 aTox contributors
// SPDX-FileCopyrightText: 2021-2022 aTox contributors
//
// SPDX-License-Identifier: GPL-3.0-only
@@ -8,11 +8,8 @@ import androidx.room.Room
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.asExecutor
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.TestCoroutineScope
import kotlinx.coroutines.test.runBlockingTest
import kotlinx.coroutines.test.runTest
import ltd.evilcorp.core.vo.ConnectionStatus
import ltd.evilcorp.core.vo.User
import ltd.evilcorp.core.vo.UserStatus
@@ -28,12 +25,8 @@ import org.junit.runner.RunWith
@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
class UserDaoTest {
private val dispatcher = TestCoroutineDispatcher()
private val scope = TestCoroutineScope(dispatcher)
private val db =
Room.inMemoryDatabaseBuilder(InstrumentationRegistry.getInstrumentation().targetContext, Database::class.java)
.setTransactionExecutor(dispatcher.asExecutor())
.setQueryExecutor(dispatcher.asExecutor())
.allowMainThreadQueries()
.build()
private val dao = db.userDao()
@@ -51,13 +44,13 @@ class UserDaoTest {
fun clearDb() = db.clearAllTables()
@Test
fun save_and_load() = scope.runBlockingTest {
fun save_and_load() = runTest {
dao.save(first)
assertEquals(first, dao.load(first.publicKey).first())
}
@Test
fun update() = scope.runBlockingTest {
fun update() = runTest {
dao.save(first)
dao.update(first.copy(name = "new name"))
assertNotEquals(first, dao.load(first.publicKey).first())
@@ -65,14 +58,14 @@ class UserDaoTest {
}
@Test
fun exists() = scope.runBlockingTest {
fun exists() = runTest {
assertFalse(dao.exists(first.publicKey))
dao.save(first)
assertTrue(dao.exists(first.publicKey))
}
@Test
fun cant_replace_user_with_save() = scope.runBlockingTest {
fun cant_replace_user_with_save() = runTest {
dao.save(first)
try {
dao.save(first.copy(name = "new name"))
@@ -82,7 +75,7 @@ class UserDaoTest {
}
@Test
fun update_name() = scope.runBlockingTest {
fun update_name() = runTest {
dao.save(first)
dao.updateName(first.publicKey, "new name")
assertNotEquals(first, dao.load(first.publicKey).first())
@@ -90,7 +83,7 @@ class UserDaoTest {
}
@Test
fun update_status_message() = scope.runBlockingTest {
fun update_status_message() = runTest {
dao.save(first)
dao.updateStatusMessage(first.publicKey, "new status")
assertNotEquals(first, dao.load(first.publicKey).first())
@@ -98,7 +91,7 @@ class UserDaoTest {
}
@Test
fun update_connection() = scope.runBlockingTest {
fun update_connection() = runTest {
dao.save(first)
dao.updateConnection(first.publicKey, ConnectionStatus.TCP)
assertNotEquals(first, dao.load(first.publicKey).first())
@@ -106,7 +99,7 @@ class UserDaoTest {
}
@Test
fun update_status() = scope.runBlockingTest {
fun update_status() = runTest {
dao.save(first)
dao.updateStatus(first.publicKey, UserStatus.Busy)
assertNotEquals(first, dao.load(first.publicKey).first())