mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-23 02:48:26 +00:00
Temporary table support for holding fts results
This commit is contained in:
@ -11,6 +11,7 @@ import docspell.common._
|
||||
import docspell.logging.TestLoggingConfig
|
||||
import munit.CatsEffectSuite
|
||||
import org.testcontainers.utility.DockerImageName
|
||||
import doobie._
|
||||
|
||||
import java.util.UUID
|
||||
|
||||
@ -19,6 +20,8 @@ trait DatabaseTest
|
||||
with TestContainersFixtures
|
||||
with TestLoggingConfig {
|
||||
|
||||
val cio: Sync[ConnectionIO] = Sync[ConnectionIO]
|
||||
|
||||
lazy val mariadbCnt = ForAllContainerFixture(
|
||||
MariaDBContainer.Def(DockerImageName.parse("mariadb:10.5")).createContainer()
|
||||
)
|
||||
@ -29,12 +32,12 @@ trait DatabaseTest
|
||||
|
||||
lazy val pgDataSource = ResourceSuiteLocalFixture(
|
||||
"pgDataSource",
|
||||
DatabaseTest.makeDataSourceFixture(postgresCnt())
|
||||
DatabaseTest.makeDataSourceFixture(IO(postgresCnt()))
|
||||
)
|
||||
|
||||
lazy val mariaDataSource = ResourceSuiteLocalFixture(
|
||||
"mariaDataSource",
|
||||
DatabaseTest.makeDataSourceFixture(mariadbCnt())
|
||||
DatabaseTest.makeDataSourceFixture(IO(mariadbCnt()))
|
||||
)
|
||||
|
||||
lazy val h2DataSource = ResourceSuiteLocalFixture(
|
||||
@ -50,34 +53,42 @@ trait DatabaseTest
|
||||
} yield (jdbc, ds))
|
||||
|
||||
lazy val pgStore = ResourceSuiteLocalFixture(
|
||||
"pgStore", {
|
||||
val (jdbc, ds) = pgDataSource()
|
||||
StoreFixture.store(ds, jdbc)
|
||||
}
|
||||
"pgStore",
|
||||
for {
|
||||
t <- Resource.eval(IO(pgDataSource()))
|
||||
store <- StoreFixture.store(t._2, t._1)
|
||||
} yield store
|
||||
)
|
||||
|
||||
lazy val mariaStore = ResourceSuiteLocalFixture(
|
||||
"mariaStore", {
|
||||
val (jdbc, ds) = mariaDataSource()
|
||||
StoreFixture.store(ds, jdbc)
|
||||
}
|
||||
"mariaStore",
|
||||
for {
|
||||
t <- Resource.eval(IO(mariaDataSource()))
|
||||
store <- StoreFixture.store(t._2, t._1)
|
||||
} yield store
|
||||
)
|
||||
|
||||
lazy val h2Store = ResourceSuiteLocalFixture(
|
||||
"h2Store", {
|
||||
val (jdbc, ds) = h2DataSource()
|
||||
StoreFixture.store(ds, jdbc)
|
||||
}
|
||||
"h2Store",
|
||||
for {
|
||||
t <- Resource.eval(IO(h2DataSource()))
|
||||
store <- StoreFixture.store(t._2, t._1)
|
||||
} yield store
|
||||
)
|
||||
|
||||
def postgresAll = List(postgresCnt, pgDataSource, pgStore)
|
||||
def mariaDbAll = List(mariadbCnt, mariaDataSource, mariaStore)
|
||||
def h2All = List(h2DataSource, h2Store)
|
||||
}
|
||||
|
||||
object DatabaseTest {
|
||||
private def jdbcConfig(cnt: JdbcDatabaseContainer) =
|
||||
JdbcConfig(LenientUri.unsafe(cnt.jdbcUrl), cnt.username, cnt.password)
|
||||
|
||||
private def makeDataSourceFixture(cnt: JdbcDatabaseContainer) =
|
||||
private def makeDataSourceFixture(cnt: IO[JdbcDatabaseContainer]) =
|
||||
for {
|
||||
jdbc <- Resource.eval(IO(jdbcConfig(cnt)))
|
||||
c <- Resource.eval(cnt)
|
||||
jdbc <- Resource.pure(jdbcConfig(c))
|
||||
ds <- StoreFixture.dataSource(jdbc)
|
||||
} yield (jdbc, ds)
|
||||
}
|
||||
|
@ -0,0 +1,55 @@
|
||||
package docspell.store.impl
|
||||
|
||||
import cats.effect.IO
|
||||
import docspell.common.Ident
|
||||
import docspell.store._
|
||||
import docspell.store.impl.TempIdTable.Row
|
||||
import docspell.store.qb._
|
||||
import docspell.store.qb.DSL._
|
||||
|
||||
class TempIdTableTest extends DatabaseTest {
|
||||
|
||||
override def munitFixtures = postgresAll ++ mariaDbAll ++ h2All
|
||||
|
||||
def id(str: String): Ident = Ident.unsafe(str)
|
||||
|
||||
test("create temporary table postgres") {
|
||||
val store = pgStore()
|
||||
assertCreateTempTable(store)
|
||||
}
|
||||
|
||||
test("create temporary table mariadb") {
|
||||
val store = mariaStore()
|
||||
assertCreateTempTable(store)
|
||||
}
|
||||
|
||||
test("create temporary table h2") {
|
||||
val store = h2Store()
|
||||
assertCreateTempTable(store)
|
||||
}
|
||||
|
||||
def assertCreateTempTable(store: Store[IO]) = {
|
||||
val insertRows = List(Row(id("abc-def")), Row(id("abc-123")), Row(id("zyx-321")))
|
||||
val create =
|
||||
for {
|
||||
table <- TempIdTable.createTable(store.dbms, "tt")
|
||||
n <- table.insertAll(insertRows)
|
||||
_ <- table.createIndex
|
||||
rows <- Select(select(table.all), from(table))
|
||||
.orderBy(table.id)
|
||||
.build
|
||||
.query[Row]
|
||||
.to[List]
|
||||
} yield (n, rows)
|
||||
|
||||
val verify =
|
||||
store.transact(create).map { case (inserted, rows) =>
|
||||
if (store.dbms != Db.MariaDB) {
|
||||
assertEquals(inserted, 3)
|
||||
}
|
||||
assertEquals(rows, insertRows.sortBy(_.id))
|
||||
}
|
||||
|
||||
verify *> verify
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user