Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Fix native SQLDelight tests #1826

Merged
merged 1 commit into from
Apr 29, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@

package app.tivi.data

data class DatabaseConfiguration(val inMemory: Boolean = false)
data class DatabaseConfiguration(
val name: String = "tivi",
val inMemory: Boolean = false,
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@

package app.tivi.data

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 app.cash.sqldelight.driver.native.inMemoryDriver
import app.cash.sqldelight.driver.native.wrapConnection
import app.tivi.inject.ApplicationScope
import me.tatarka.inject.annotations.Provides

Expand All @@ -14,11 +17,11 @@ actual interface SqlDelightDatabasePlatformComponent {
@ApplicationScope
fun provideDriverFactory(configuration: DatabaseConfiguration): SqlDriver {
return when {
configuration.inMemory -> inMemoryDriver(Database.Schema)
configuration.inMemory -> inMemoryDriver(Database.Schema, configuration.name)
else -> {
NativeSqliteDriver(
schema = Database.Schema,
name = "tivi.db",
name = configuration.name,
maxReaderConnections = 4,
)
}
Expand All @@ -27,3 +30,24 @@ actual interface SqlDelightDatabasePlatformComponent {
}
}
}

private fun inMemoryDriver(
schema: SqlSchema<QueryResult.Value<Unit>>,
name: String,
): NativeSqliteDriver = NativeSqliteDriver(
co.touchlab.sqliter.DatabaseConfiguration(
name = name,
inMemory = true,
version = if (schema.version > Int.MAX_VALUE) {
error("Schema version is larger than Int.MAX_VALUE: ${schema.version}.")
} else {
schema.version.toInt()
},
create = { connection ->
wrapConnection(connection) { schema.create(it) }
},
upgrade = { connection, oldVersion, newVersion ->
wrapConnection(connection) { schema.migrate(it, oldVersion.toLong(), newVersion.toLong()) }
},
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,16 @@ actual interface SqlDelightDatabasePlatformComponent {
): SqlDriver = JdbcSqliteDriver(
url = when {
configuration.inMemory -> JdbcSqliteDriver.IN_MEMORY
else -> "jdbc:sqlite:${getDatabaseFile().absolutePath}"
else -> "jdbc:sqlite:${configuration.file.absolutePath}"
},
).also { db ->
Database.Schema.create(db)
db.execute(null, "PRAGMA foreign_keys=ON", 0)
}
}

private fun getDatabaseFile(): File {
return File(
appDir.also { if (!it.exists()) it.mkdirs() },
"tivi.db",
)
}
private val DatabaseConfiguration.file: File
get() = File(appDir.also { if (!it.exists()) it.mkdirs() }, "$name.db")

private val appDir: File
get() {
Expand Down
2 changes: 2 additions & 0 deletions data/test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ kotlin {
implementation(libs.kotlin.coroutines.test)

implementation(libs.turbine)

implementation(libs.uuid)
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions data/test/src/commonTest/kotlin/app/tivi/data/DatabaseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import app.tivi.trakt.TraktCommonComponent
import app.tivi.util.Logger
import app.tivi.utils.SuccessRefreshTokenAction
import app.tivi.utils.SuccessTraktLoginAction
import com.benasher44.uuid.uuid4
import kotlin.test.AfterTest
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
Expand Down Expand Up @@ -56,9 +57,7 @@ abstract class TestApplicationComponent :

@Provides
override fun provideDatabaseConfiguration(): DatabaseConfiguration {
return super.provideDatabaseConfiguration().copy(
inMemory = true,
)
return DatabaseConfiguration(name = uuid4().toString(), inMemory = true)
}

@Provides
Expand Down
Loading