Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/dao test #50

Merged
merged 6 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions core/database/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,32 @@ android {
buildFeatures {
buildConfig = true
}

testOptions {
unitTests {
isIncludeAndroidResources = true
}
}

defaultConfig {
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

packaging {
resources {
excludes.add("META-INF/LICENSE.md")
excludes.add("META-INF/LICENSE-notice.md")
}
}
}

dependencies {
implementation(projects.core.common)

Local()

testImplementation(libs.robolectric)
testImplementation(projects.core.testing)
testImplementation(libs.room.test)
testImplementation("androidx.arch.core:core-testing:2.1.0")
}
34 changes: 34 additions & 0 deletions core/database/src/test/kotlin/kr/co/database/RoomTestRule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package kr.co.database

import androidx.room.Room
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement

internal class RoomTestRule : TestRule {

val db: SeeDocsDatabase by lazy { initialiseRoomDb() }

override fun apply(base: Statement?, description: Description?): Statement {
return RoomStatement(statement = base)
}

inner class RoomStatement(private val statement: Statement?) : Statement() {

override fun evaluate() {
try {
statement?.evaluate()
} finally {
db.close()
}
}

}

private fun initialiseRoomDb(): SeeDocsDatabase = Room.inMemoryDatabaseBuilder(
InstrumentationRegistry.getInstrumentation().targetContext,
SeeDocsDatabase::class.java
).allowMainThreadQueries().build()

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package kr.co.database.dao

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import junit.framework.TestCase.assertEquals
import kotlinx.coroutines.test.runTest
import kr.co.database.RoomTestRule
import kr.co.database.SeeDocsDatabase
import kr.co.database.dummy.DataBaseDummy
import kr.co.database.model.BookmarkFile
import kr.co.testing.util.testWithItem
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import java.io.IOException

@RunWith(RobolectricTestRunner::class)
internal class BookmarkFileDaoTest {

private lateinit var bookmarkFileDao: BookmarkFileDao

@get: Rule
val roomTestRule = RoomTestRule()

private val db: SeeDocsDatabase = roomTestRule.db

@get: Rule
val instantTaskExecutorRule = InstantTaskExecutorRule()

@Before
fun setup() {
bookmarkFileDao = db.bookmarkFileDao()
}

@After
@Throws(IOException::class)
fun close() {
db.close()
}

@Test
@Throws(Exception::class)
fun `Given bookmarkFile When insert Then get bookmarkFile`() = runTest {
val bookmarkFile = DataBaseDummy.BOOKMARK_DUMMY

bookmarkFileDao.insert(bookmarkFile)

bookmarkFileDao.get().testWithItem {
assertEquals(listOf(bookmarkFile), this)
}
}

@Test
@Throws(Exception::class)
fun `Given bookmarkFile When delete Then get emptyList`() = runTest {
val bookmarkFile = DataBaseDummy.BOOKMARK_DUMMY

bookmarkFileDao.insert(bookmarkFile)
bookmarkFileDao.delete(bookmarkFile)

bookmarkFileDao.get().testWithItem {
assertEquals(emptyList<BookmarkFile>(), this)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package kr.co.database.dummy

import kr.co.database.model.BookmarkFile
import kr.co.database.model.FileType
import kr.co.database.model.RecentFile
import java.time.LocalDateTime

internal object DataBaseDummy {

val BOOKMARK_DUMMY = BookmarkFile(
name = "DUMMY.pdf",
path = "",
type = FileType.PDF,
size = 0,
createdAt = LocalDateTime.of(2024,6,2,2,0),
lastModified = LocalDateTime.of(2024,6,2,2,0)
)

val RECENT_DUMMY = RecentFile(
name = "DUMMY.pdf",
path = "",
type = FileType.PDF,
size = 0,
createdAt = LocalDateTime.of(2024,6,2,2,0),
lastModified = LocalDateTime.of(2024,6,2,2,0)
)
}
5 changes: 5 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ firebaseCrashlyticsPlugin = "3.0.2"
ksp = "2.0.20-1.0.25"
lifecycleLivedataKtx = "2.8.7"
navigationFragmentKtx = "2.8.4"
robolectric = "4.14.1"
room = "2.6.1"
appcompat = "1.7.0"
material = "1.12.0"
Expand All @@ -32,6 +33,7 @@ composeActivity = "1.9.3"
composeNavigation = "2.8.4"
compose-plugin = "1.6.10"

testRunner = "1.6.2"
coreKtx = "1.15.0"
junit = "4.13.2"
junitVersion = "1.2.1"
Expand All @@ -48,6 +50,7 @@ turbine = "1.2.0"
android-desugarJdkLibs = { group = "com.android.tools", name = "desugar_jdk_libs", version.ref = "androidDesugarJdkLibs" }

junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-runner = { group = "androidx.test", name = "runner", version.ref = "testRunner" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
androidx-core-splashscreen = { group = "androidx.core", name = "core-splashscreen", version.ref = "coreSplashscreen" }
Expand All @@ -68,9 +71,11 @@ koin-logger = { group = "io.insert-koin", name = "koin-logger-slf4j", version.re
koin-test = { group = "io.insert-koin", name = "koin-test", version.ref = "koin" }
koin-junit = { group = "io.insert-koin", name = "koin-test-junit4", version.ref = "koin" }

robolectric = { module = "org.robolectric:robolectric", version.ref = "robolectric" }
room-ktx = { group = "androidx.room", name = "room-ktx", version.ref = "room" }
room-compiler = { group = "androidx.room", name = "room-compiler", version.ref = "room" }
room-runtime = { group = "androidx.room", name = "room-runtime", version.ref = "room" }
room-test = { group = "androidx.room", name = "room-testing", version.ref = "room" }

datastore-preferences = { group = "androidx.datastore", name = "datastore-preferences", version.ref = "dataStore" }

Expand Down
Loading