From cd0e7549d4a9876923649b83bbc82dab9caa0232 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Tue, 5 Mar 2019 19:21:17 -0500 Subject: Split db tests per dao --- .../kotlin/com/pitchedapps/frost/db/BaseDbTest.kt | 32 +++++++++ .../com/pitchedapps/frost/db/CookieDbTest.kt | 67 +++++++++++++++++ .../com/pitchedapps/frost/db/FrostDatabaseTest.kt | 83 ---------------------- 3 files changed, 99 insertions(+), 83 deletions(-) create mode 100644 app/src/androidTest/kotlin/com/pitchedapps/frost/db/BaseDbTest.kt create mode 100644 app/src/androidTest/kotlin/com/pitchedapps/frost/db/CookieDbTest.kt delete mode 100644 app/src/androidTest/kotlin/com/pitchedapps/frost/db/FrostDatabaseTest.kt diff --git a/app/src/androidTest/kotlin/com/pitchedapps/frost/db/BaseDbTest.kt b/app/src/androidTest/kotlin/com/pitchedapps/frost/db/BaseDbTest.kt new file mode 100644 index 00000000..6621ea95 --- /dev/null +++ b/app/src/androidTest/kotlin/com/pitchedapps/frost/db/BaseDbTest.kt @@ -0,0 +1,32 @@ +package com.pitchedapps.frost.db + +import android.content.Context +import androidx.room.Room +import androidx.test.core.app.ApplicationProvider +import androidx.test.ext.junit.runners.AndroidJUnit4 +import org.junit.runner.RunWith +import kotlin.test.AfterTest +import kotlin.test.BeforeTest + +@RunWith(AndroidJUnit4::class) +abstract class BaseDbTest { + + protected lateinit var db: FrostDatabase + + @BeforeTest + fun before() { + val context = ApplicationProvider.getApplicationContext() + val privateDb = Room.inMemoryDatabaseBuilder( + context, FrostPrivateDatabase::class.java + ).build() + val publicDb = Room.inMemoryDatabaseBuilder( + context, FrostPublicDatabase::class.java + ).build() + db = FrostDatabase(privateDb, publicDb) + } + + @AfterTest + fun after() { + db.close() + } +} \ No newline at end of file diff --git a/app/src/androidTest/kotlin/com/pitchedapps/frost/db/CookieDbTest.kt b/app/src/androidTest/kotlin/com/pitchedapps/frost/db/CookieDbTest.kt new file mode 100644 index 00000000..351490e2 --- /dev/null +++ b/app/src/androidTest/kotlin/com/pitchedapps/frost/db/CookieDbTest.kt @@ -0,0 +1,67 @@ +package com.pitchedapps.frost.db + +import kotlinx.coroutines.runBlocking +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNull + +class CookieDbTest : BaseDbTest() { + + @Test + fun basicCookie() { + val cookie = CookieEntity(id = 1234L, name = "testName", cookie = "testCookie") + runBlocking { + db.cookieDao().insertCookie(cookie) + val cookies = db.cookieDao().selectAll() + assertEquals(listOf(cookie), cookies, "Cookie mismatch") + } + } + + @Test + fun deleteCookie() { + val cookie = CookieEntity(id = 1234L, name = "testName", cookie = "testCookie") + + runBlocking { + db.cookieDao().insertCookie(cookie) + db.cookieDao().deleteById(cookie.id + 1) + assertEquals( + listOf(cookie), + db.cookieDao().selectAll(), + "Cookie list should be the same after inexistent deletion" + ) + db.cookieDao().deleteById(cookie.id) + assertEquals(emptyList(), db.cookieDao().selectAll(), "Cookie list should be empty after deletion") + } + } + + @Test + fun insertReplaceCookie() { + val cookie = CookieEntity(id = 1234L, name = "testName", cookie = "testCookie") + runBlocking { + db.cookieDao().insertCookie(cookie) + assertEquals(listOf(cookie), db.cookieDao().selectAll(), "Cookie insertion failed") + db.cookieDao().insertCookie(cookie.copy(name = "testName2")) + assertEquals( + listOf(cookie.copy(name = "testName2")), + db.cookieDao().selectAll(), + "Cookie replacement failed" + ) + db.cookieDao().insertCookie(cookie.copy(id = 123L)) + assertEquals( + setOf(cookie.copy(id = 123L), cookie.copy(name = "testName2")), + db.cookieDao().selectAll().toSet(), + "New cookie insertion failed" + ) + } + } + + @Test + fun selectCookie() { + val cookie = CookieEntity(id = 1234L, name = "testName", cookie = "testCookie") + runBlocking { + db.cookieDao().insertCookie(cookie) + assertEquals(cookie, db.cookieDao().selectById(cookie.id), "Cookie selection failed") + assertNull(db.cookieDao().selectById(cookie.id + 1), "Inexistent cookie selection failed") + } + } +} \ No newline at end of file diff --git a/app/src/androidTest/kotlin/com/pitchedapps/frost/db/FrostDatabaseTest.kt b/app/src/androidTest/kotlin/com/pitchedapps/frost/db/FrostDatabaseTest.kt deleted file mode 100644 index dcc96c2d..00000000 --- a/app/src/androidTest/kotlin/com/pitchedapps/frost/db/FrostDatabaseTest.kt +++ /dev/null @@ -1,83 +0,0 @@ -package com.pitchedapps.frost.db - -import android.content.Context -import androidx.room.Room -import androidx.test.core.app.ApplicationProvider -import androidx.test.ext.junit.runners.AndroidJUnit4 -import kotlinx.coroutines.runBlocking -import org.junit.runner.RunWith -import kotlin.test.AfterTest -import kotlin.test.BeforeTest -import kotlin.test.Test -import kotlin.test.assertEquals - -@RunWith(AndroidJUnit4::class) -class FrostDatabaseTest { - - private lateinit var db: FrostDatabase - - @BeforeTest - fun before() { - val context = ApplicationProvider.getApplicationContext() - val privateDb = Room.inMemoryDatabaseBuilder( - context, FrostPrivateDatabase::class.java - ).build() - val publicDb = Room.inMemoryDatabaseBuilder( - context, FrostPublicDatabase::class.java - ).build() - db = FrostDatabase(privateDb, publicDb) - } - - @AfterTest - fun after() { - db.close() - } - - @Test - fun basicCookie() { - val cookie = CookieEntity(id = 1234L, name = "testName", cookie = "testCookie") - runBlocking { - db.cookieDao().insertCookie(cookie) - val cookies = db.cookieDao().selectAll() - assertEquals(listOf(cookie), cookies, "Cookie mismatch") - } - } - - @Test - fun deleteCookie() { - val cookie = CookieEntity(id = 1234L, name = "testName", cookie = "testCookie") - - runBlocking { - db.cookieDao().insertCookie(cookie) - db.cookieDao().deleteById(cookie.id + 1) - assertEquals( - listOf(cookie), - db.cookieDao().selectAll(), - "Cookie list should be the same after inexistent deletion" - ) - db.cookieDao().deleteById(cookie.id) - assertEquals(emptyList(), db.cookieDao().selectAll(), "Cookie list should be empty after deletion") - } - } - - @Test - fun insertCookie() { - val cookie = CookieEntity(id = 1234L, name = "testName", cookie = "testCookie") - runBlocking { - db.cookieDao().insertCookie(cookie) - assertEquals(listOf(cookie), db.cookieDao().selectAll(), "Cookie insertion failed") - db.cookieDao().insertCookie(cookie.copy(name = "testName2")) - assertEquals( - listOf(cookie.copy(name = "testName2")), - db.cookieDao().selectAll(), - "Cookie replacement failed" - ) - db.cookieDao().insertCookie(cookie.copy(id = 123L)) - assertEquals( - setOf(cookie.copy(id = 123L), cookie.copy(name = "testName2")), - db.cookieDao().selectAll().toSet(), - "New cookie insertion failed" - ) - } - } -} \ No newline at end of file -- cgit v1.2.3