例子
package com.viki.vikilitics_kmm
import com.squareup.sqldelight.sqlite.driver.JdbcDriver
import com.google.common.truth.Truth.assertThat
import com.squareup.sqldelight.db.SqlDriver
import com.squareup.sqldelight.sqlite.driver.JdbcSqliteDriver
import com.viki.vikiliticskmm.Event
import com.viki.vikiliticskmm.EventQueries
import org.junit.After
import org.junit.Before
import org.junit.Test
import java.sql.DriverManager
import java.sql.Connection
class AndroidEventDatabaseTest {
private lateinit var queries: EventQueries
// When your test needs a driver
@Before
fun before() {
val driver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY)
val database = EventDatabase(driver)
EventDatabase.Schema.create(driver)
queries = database.eventQueries
}
@Test
fun `select all events`() {
queries.insertEvent("1", "2", "{Click,Open}")
queries.insertEvent("2", "2", "{Click,Close}")
assertThat(queries.selectAllEvents().executeAsList())
.containsExactly(
Event(
as_counter = "1",
t_ms = "2",
event_map = "{Click,Open}"
),
Event(
as_counter = "2",
t_ms = "2",
event_map = "{Click,Close}"
)
)
}
@Test
fun `delete multiple events`() {
queries.insertEvent("1", "1", "{Click,Open}")
queries.insertEvent("1", "2", "{Click,Close},{Read,Open}")
queries.insertEvent("1", "3", "{Click,Open}")
queries.insertEvent("2", "3", "{Click,Open}")
val event1 = listOf("1","3")
val event2 = listOf("1","2")
val event3 = listOf("1","4")
val eventList = listOf(event1,event2,event3)
for (event in eventList){
queries.deleteEventListByKey(event.elementAt(0), event.elementAt(1))
}
assertThat(queries.selectAllEvents().executeAsList())
.containsExactly(
Event(
as_counter = "1",
t_ms = "1",
event_map = "{Click,Open}"
), Event(
as_counter = "2",
t_ms = "3",
event_map = "{Click,Open}"
),
)
}
@Test
fun `delete single event`() {
queries.insertEvent("1", "1", "{Click,Open}")
queries.insertEvent("1", "2", "{Click,Close},{Read,Open}")
queries.insertEvent("1", "3", "{Click,Open}")
queries.insertEvent("2", "3", "{Click,Open}")
queries.deleteEventListByKey("1", "3")
assertThat(queries.selectAllEvents().executeAsList())
.containsExactly(
Event(
as_counter = "1",
t_ms = "1",
event_map = "{Click,Open}"
), Event(
as_counter = "2",
t_ms = "3",
event_map = "{Click,Open}"
),
Event(
as_counter = "1",
t_ms = "2",
event_map = "{Click,Close},{Read,Open}"
)
)
}
@Test
fun `update events`() {
queries.insertEvent("1", "2", "{Click,Open}")
queries.insertEvent("1", "2", "{Click,Close}")
assertThat(queries.selectAllEvents().executeAsList())
.containsExactly(
Event(
as_counter = "1",
t_ms = "2",
event_map = "{Click,Close}"
)
)
}
}
参考
https://github.com/touchlab/KaMPKit/blob/main/shared/src/commonTest/kotlin/co/touchlab/kampkit/SqlDelightTest.kt