-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
114 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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 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 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
89 changes: 89 additions & 0 deletions
89
library/src/commonTest/kotlin/com/mercury/sqkon/db/MetadataTest.kt
This file contains 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,89 @@ | ||
package com.mercury.sqkon.db | ||
|
||
import app.cash.turbine.test | ||
import com.mercury.sqkon.TestObject | ||
import kotlinx.coroutines.MainScope | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.test.runTest | ||
import kotlinx.datetime.Clock | ||
import org.junit.After | ||
import org.junit.Test | ||
import java.lang.Thread.sleep | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertNull | ||
import kotlin.test.assertTrue | ||
|
||
class MetadataTest { | ||
|
||
private val mainScope = MainScope() | ||
private val driver = driverFactory().createDriver() | ||
private val entityQueries = EntityQueries(driver) | ||
private val metadataQueries = MetadataQueries(driver) | ||
private val testObjectStorage = keyValueStorage<TestObject>( | ||
"test-object", entityQueries, metadataQueries, mainScope | ||
) | ||
|
||
@After | ||
fun tearDown() { | ||
mainScope.cancel() | ||
} | ||
|
||
@Test | ||
fun updateWrite_onInsert() = runTest { | ||
val expected = (1..20).map { _ -> TestObject() }.associateBy { it.id } | ||
testObjectStorage.insertAll(expected) | ||
|
||
testObjectStorage.metadata().test { | ||
sleep(1) | ||
val now = Clock.System.now() | ||
awaitItem().also { | ||
assertEquals("test-object", it.entity_name) | ||
assertNotNull(it.lastWriteAt) | ||
assertNull(it.lastReadAt) | ||
assertTrue { it.lastWriteAt!! <= now } | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun updateWrite_onUpdate() = runTest { | ||
val expected = (1..20).map { _ -> TestObject() }.associateBy { it.id } | ||
testObjectStorage.insertAll(expected) | ||
|
||
testObjectStorage.metadata().test { | ||
val now = Clock.System.now() | ||
sleep(2) | ||
awaitItem() | ||
testObjectStorage.updateAll(expected) | ||
awaitItem().also { | ||
assertEquals("test-object", it.entity_name) | ||
assertNotNull(it.lastWriteAt) | ||
assertNull(it.lastReadAt) | ||
assertTrue { it.lastWriteAt!! > now } | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun updateRead_onSelect() = runTest { | ||
val expected = (1..20).map { _ -> TestObject() }.associateBy { it.id } | ||
testObjectStorage.insertAll(expected) | ||
sleep(1) | ||
val now = Clock.System.now() | ||
sleep(2) | ||
testObjectStorage.metadata().test { | ||
awaitItem() | ||
testObjectStorage.selectAll().first() | ||
awaitItem().also { | ||
assertEquals("test-object", it.entity_name) | ||
assertNotNull(it.lastWriteAt) | ||
assertNotNull(it.lastReadAt) | ||
assertTrue { it.lastWriteAt!! <= now } | ||
assertTrue { it.lastReadAt!! > now } | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains 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
2 changes: 1 addition & 1 deletion
2
library/src/commonTest/kotlin/com/mercury/sqkon/db/SqkonDatabaseDriverTest.kt
This file contains 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package com.mercury.sqkon.db | ||
|
||
expect fun createEntityQueries(): EntityQueries | ||
internal expect fun driverFactory(): DriverFactory |
6 changes: 3 additions & 3 deletions
6
library/src/jvmTest/kotlin/com/mercury/sqkon/db/SqkonDatabaseDriverTest.jvm.kt
This file contains 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
package com.mercury.sqkon.db | ||
|
||
|
||
actual fun createEntityQueries(): EntityQueries { | ||
return createEntityQueries(DriverFactory()) | ||
} | ||
internal actual fun driverFactory(): DriverFactory { | ||
return DriverFactory() | ||
} |