-
Notifications
You must be signed in to change notification settings - Fork 19
Add in-memory database for tests #273
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
core/src/commonIntegrationTest/kotlin/com/powersync/db/InMemoryTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.powersync.db | ||
|
||
import app.cash.turbine.turbineScope | ||
import co.touchlab.kermit.ExperimentalKermitApi | ||
import co.touchlab.kermit.Logger | ||
import co.touchlab.kermit.Severity | ||
import co.touchlab.kermit.TestConfig | ||
import co.touchlab.kermit.TestLogWriter | ||
import com.powersync.PowerSyncDatabase | ||
import com.powersync.db.schema.Column | ||
import com.powersync.db.schema.Schema | ||
import com.powersync.db.schema.Table | ||
import io.kotest.matchers.collections.shouldHaveSize | ||
import io.kotest.matchers.shouldBe | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.Test | ||
|
||
@OptIn(ExperimentalKermitApi::class) | ||
class InMemoryTest { | ||
private val logWriter = | ||
TestLogWriter( | ||
loggable = Severity.Debug, | ||
) | ||
|
||
private val logger = | ||
Logger( | ||
TestConfig( | ||
minSeverity = Severity.Debug, | ||
logWriterList = listOf(logWriter), | ||
), | ||
) | ||
|
||
@Test | ||
fun createsSchema() = | ||
runTest { | ||
val db = PowerSyncDatabase.Companion.inMemory(schema, this, logger) | ||
try { | ||
db.getAll("SELECT * FROM users") { } shouldHaveSize 0 | ||
} finally { | ||
db.close() | ||
} | ||
} | ||
|
||
@Test | ||
fun watch() = | ||
runTest { | ||
val db = PowerSyncDatabase.Companion.inMemory(schema, this, logger) | ||
try { | ||
turbineScope { | ||
val turbine = | ||
db.watch("SELECT name FROM users", mapper = { it.getString(0)!! }).testIn(this) | ||
|
||
turbine.awaitItem() shouldBe listOf() | ||
|
||
db.execute("INSERT INTO users (id, name) VALUES (uuid(), ?)", listOf("test user")) | ||
turbine.awaitItem() shouldBe listOf("test user") | ||
turbine.cancelAndIgnoreRemainingEvents() | ||
} | ||
} finally { | ||
db.close() | ||
} | ||
} | ||
|
||
companion object { | ||
private val schema = | ||
Schema( | ||
Table( | ||
name = "users", | ||
columns = | ||
listOf( | ||
Column.Companion.text("name"), | ||
), | ||
), | ||
) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
core/src/commonMain/kotlin/com/powersync/db/driver/SingleConnectionPool.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.powersync.db.driver | ||
|
||
import androidx.sqlite.SQLiteConnection | ||
import com.powersync.ExperimentalPowerSyncAPI | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.SharedFlow | ||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
|
||
/** | ||
* A [SQLiteConnectionPool] backed by a single database connection. | ||
* | ||
* This does not provide any concurrency, but is still a reasonable implementation to use for e.g. tests. | ||
*/ | ||
@OptIn(ExperimentalPowerSyncAPI::class) | ||
internal class SingleConnectionPool( | ||
private val conn: SQLiteConnection, | ||
) : SQLiteConnectionPool { | ||
private val mutex: Mutex = Mutex() | ||
private var closed = false | ||
private val tableUpdatesFlow = MutableSharedFlow<Set<String>>(replay = 0) | ||
|
||
init { | ||
conn.setupDefaultPragmas(false) | ||
} | ||
|
||
override suspend fun <T> read(callback: suspend (SQLiteConnectionLease) -> T): T = write(callback) | ||
|
||
override suspend fun <T> write(callback: suspend (SQLiteConnectionLease) -> T): T = | ||
mutex.withLock { | ||
check(!closed) { "Connection closed" } | ||
|
||
try { | ||
callback(RawConnectionLease(conn)) | ||
} finally { | ||
val updates = conn.readPendingUpdates() | ||
if (updates.isNotEmpty()) { | ||
tableUpdatesFlow.emit(updates) | ||
} | ||
} | ||
} | ||
|
||
override suspend fun <R> withAllConnections( | ||
action: suspend (writer: SQLiteConnectionLease, readers: List<SQLiteConnectionLease>) -> R, | ||
) = write { writer -> | ||
action(writer, emptyList()) | ||
Unit | ||
} | ||
|
||
override val updates: SharedFlow<Set<String>> | ||
get() = tableUpdatesFlow | ||
|
||
override suspend fun close() { | ||
mutex.withLock { | ||
conn.close() | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 0 additions & 5 deletions
5
...elight/src/appleTest/kotlin/com/powersync/integrations/sqldelight/SqlDelightTest.apple.kt
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 0 additions & 5 deletions
5
...sqldelight/src/jvmTest/kotlin/com/powersync/integrations/sqldelight/SqlDelightTest.jvm.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.