Add implementations in the test module

pull/1323/head
lihenggui 2 years ago
parent fdf29ab32e
commit 92392ba13d

@ -30,36 +30,30 @@ android {
kotlin {
sourceSets {
val commonMain by getting {
dependencies {
api(projects.core.model)
implementation(libs.kotlinx.datetime)
implementation(libs.kotlinInject.runtime)
implementation(libs.sqldelight.coroutines.extensions)
implementation(libs.sqldelight.primitive.adapters)
}
commonMain.dependencies {
api(projects.core.model)
implementation(libs.kotlinx.datetime)
implementation(libs.kotlinInject.runtime)
implementation(libs.sqldelight.coroutines.extensions)
implementation(libs.sqldelight.primitive.adapters)
}
val androidMain by getting {
dependencies {
implementation(libs.sqldelight.android.driver)
}
androidMain.dependencies {
implementation(libs.sqldelight.android.driver)
}
val nativeMain by getting {
dependencies {
implementation(libs.sqldelight.native.driver)
}
androidUnitTest.dependencies {
implementation(libs.androidx.test.core)
}
val jvmMain by getting {
dependencies {
implementation(libs.sqldelight.sqlite.driver)
}
nativeMain.dependencies {
implementation(libs.sqldelight.native.driver)
}
val jsMain by getting {
dependencies {
implementation(libs.sqldelight.webworker.driver)
implementation(npm("sql.js", "1.6.2"))
implementation(devNpm("copy-webpack-plugin", "9.1.0"))
}
jvmMain.dependencies {
implementation(libs.sqldelight.sqlite.driver)
}
jsMain.dependencies {
implementation(libs.sqldelight.webworker.driver)
implementation(npm("sql.js", "1.6.2"))
implementation(devNpm("copy-webpack-plugin", "9.1.0"))
}
commonTest.dependencies {
implementation(libs.kotlin.test)

@ -22,7 +22,6 @@ import app.cash.sqldelight.db.QueryResult
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.db.SqlSchema
import app.cash.sqldelight.driver.android.AndroidSqliteDriver
import me.tatarka.inject.annotations.Component
import me.tatarka.inject.annotations.Inject
import me.tatarka.inject.annotations.Provides

@ -0,0 +1,28 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.database
import android.content.Context
import androidx.test.core.app.ApplicationProvider
import app.cash.sqldelight.async.coroutines.synchronous
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.android.AndroidSqliteDriver
actual suspend fun createDriver(): SqlDriver {
val context: Context = ApplicationProvider.getApplicationContext()
return AndroidSqliteDriver(NiaDatabase.Schema.synchronous(), context, "nia-database-test.db")
}

@ -23,7 +23,6 @@ import com.google.samples.apps.nowinandroid.core.database.dao.RecentSearchQueryD
import com.google.samples.apps.nowinandroid.core.database.dao.TopicDao
import com.google.samples.apps.nowinandroid.core.database.dao.TopicFtsDao
import kotlinx.coroutines.Dispatchers
import me.tatarka.inject.annotations.Component
import me.tatarka.inject.annotations.Provides
internal object DatabaseModule {

@ -19,7 +19,6 @@ package com.google.samples.apps.nowinandroid.core.database
import app.cash.sqldelight.db.QueryResult
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.db.SqlSchema
import me.tatarka.inject.annotations.Component
import me.tatarka.inject.annotations.Provides
expect class DriverModule {

@ -0,0 +1,31 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.database
import app.cash.sqldelight.db.SqlDriver
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.test.runTest
/**
* Init driver for each platform. Should *always* be called to setup test
*/
expect suspend fun createDriver(): SqlDriver
fun testing(block: suspend CoroutineScope.(NiaDatabase) -> Unit) = runTest {
val driver = createDriver()
block(NiaDatabase(driver))
driver.close()
}

@ -1,5 +1,5 @@
/*
* Copyright 2022 The Android Open Source Project
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,36 +16,30 @@
package com.google.samples.apps.nowinandroid.core.database.dao
import android.content.Context
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import com.google.samples.apps.nowinandroid.core.database.NiaDatabase
import com.google.samples.apps.nowinandroid.core.database.createDriver
import com.google.samples.apps.nowinandroid.core.database.model.NewsResourceEntity
import com.google.samples.apps.nowinandroid.core.database.model.NewsResourceTopicCrossRef
import com.google.samples.apps.nowinandroid.core.database.model.TopicEntity
import com.google.samples.apps.nowinandroid.core.database.model.asExternalModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import kotlinx.datetime.Instant
import org.junit.Before
import org.junit.Test
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
class NewsResourceDaoTest {
private lateinit var newsResourceDao: NewsResourceDao
private lateinit var topicDao: TopicDao
private lateinit var db: NiaDatabase
@Before
fun createDb() {
val context = ApplicationProvider.getApplicationContext<Context>()
db = Room.inMemoryDatabaseBuilder(
context,
NiaDatabase::class.java,
).build()
newsResourceDao = db.newsResourceDao()
topicDao = db.topicDao()
@BeforeTest
fun setup() = runTest {
val db = NiaDatabase(createDriver())
newsResourceDao = NewsResourceDao(db, Dispatchers.Unconfined)
topicDao = TopicDao(db, Dispatchers.Unconfined)
}
@Test

@ -20,7 +20,6 @@ import app.cash.sqldelight.db.QueryResult
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.db.SqlSchema
import app.cash.sqldelight.driver.worker.WebWorkerDriver
import me.tatarka.inject.annotations.Component
import me.tatarka.inject.annotations.Provides
import org.w3c.dom.Worker

@ -0,0 +1,29 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.database
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.worker.WebWorkerDriver
import org.w3c.dom.Worker
actual suspend fun createDriver(): SqlDriver {
return WebWorkerDriver(
Worker(
js("""new URL("@cashapp/sqldelight-sqljs-worker/sqljs.worker.js", import.meta.url)"""),
),
).also { NiaDatabase.Schema.create(it).await() }
}

@ -20,7 +20,6 @@ import app.cash.sqldelight.db.QueryResult
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.db.SqlSchema
import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver
import me.tatarka.inject.annotations.Component
import me.tatarka.inject.annotations.Provides
actual class DriverModule {

@ -0,0 +1,26 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.database
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver
import com.google.samples.apps.nowinandroid.core.database.NiaDatabase.Companion.Schema
actual suspend fun createDriver(): SqlDriver {
return JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY)
.also { Schema.create(it).await() }
}

@ -21,7 +21,6 @@ import app.cash.sqldelight.db.QueryResult
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.db.SqlSchema
import app.cash.sqldelight.driver.native.NativeSqliteDriver
import me.tatarka.inject.annotations.Component
import me.tatarka.inject.annotations.Provides
actual class DriverModule {

@ -0,0 +1,26 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.database
import app.cash.sqldelight.async.coroutines.synchronous
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.native.NativeSqliteDriver
import com.google.samples.apps.nowinandroid.core.database.NiaDatabase.Companion.Schema
actual suspend fun createDriver(): SqlDriver {
return NativeSqliteDriver(Schema.synchronous(), "nia-database-test.db")
}
Loading…
Cancel
Save