Skip to content

Commit

Permalink
Abstract inMemoryTasksAppDatabaseBuilder()
Browse files Browse the repository at this point in the history
  • Loading branch information
opatry committed Oct 28, 2024
1 parent bdc1c36 commit fa09139
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 1 deletion.
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ play-services-auth = { module = "com.google.android.gms:play-services-auth", ver

about-libraries-core = { module = "com.mikepenz:aboutlibraries-core", version.ref = "about-libraries" }

androidx-test-core = { module = "androidx.test:core", version = "1.6.1" }

[bundles]
ktor-server = ["ktor-server-core", "ktor-server-cio"]
ktor-client = [
Expand Down
4 changes: 4 additions & 0 deletions tasks-app-shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ kotlin {
jvmTest.dependencies {
implementation(compose.desktop.currentOs)
}

androidInstrumentedTest.dependencies {
implementation(libs.androidx.test.core)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2024 Olivier Patry
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package net.opatry.tasks.data.util

import android.content.Context
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.test.core.app.ApplicationProvider
import net.opatry.tasks.data.TasksAppDatabase

actual fun inMemoryTasksAppDatabaseBuilder(): RoomDatabase.Builder<TasksAppDatabase> {
val context = ApplicationProvider.getApplicationContext<Context>()
return Room.inMemoryDatabaseBuilder(context, TasksAppDatabase::class.java)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2024 Olivier Patry
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package net.opatry.tasks.data.util

import androidx.room.RoomDatabase
import net.opatry.tasks.data.TasksAppDatabase

actual fun inMemoryTasksAppDatabaseBuilder(): RoomDatabase.Builder<TasksAppDatabase> {
// not expected to run as Android unit test any time soon
// can only rely on Android context in either Robolectric or Instrumentation tests
TODO()
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package net.opatry.tasks.data.util

import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.sqlite.driver.bundled.BundledSQLiteDriver
import io.ktor.client.HttpClient
import io.ktor.client.engine.mock.MockEngine
Expand All @@ -36,11 +37,14 @@ import net.opatry.tasks.data.TaskRepository
import net.opatry.tasks.data.TasksAppDatabase


expect fun inMemoryTasksAppDatabaseBuilder(): RoomDatabase.Builder<TasksAppDatabase>

internal fun runTaskRepositoryTest(
mockEngine: MockEngine = MockEngine { respondNoNetwork() },
test: suspend TestScope.(TaskRepository) -> Unit
) = runTest {
val db = Room.inMemoryDatabaseBuilder<TasksAppDatabase>()
Room.inMemoryDatabaseBuilder<TasksAppDatabase>()
val db = inMemoryTasksAppDatabaseBuilder()
.setDriver(BundledSQLiteDriver())
.setQueryCoroutineContext(backgroundScope.coroutineContext)
.build()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2024 Olivier Patry
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package net.opatry.tasks.data.util

import androidx.room.Room
import androidx.room.RoomDatabase
import net.opatry.tasks.data.TasksAppDatabase

actual fun inMemoryTasksAppDatabaseBuilder(): RoomDatabase.Builder<TasksAppDatabase> {
return Room.inMemoryDatabaseBuilder<TasksAppDatabase>()
}

0 comments on commit fa09139

Please sign in to comment.