Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
guizmaii committed May 31, 2024
1 parent 7213fcc commit 0c98ca9
Showing 1 changed file with 72 additions and 29 deletions.
101 changes: 72 additions & 29 deletions quill-sql/src/test/scala/io/getquill/BatchActionMultiTest.scala
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
package io.getquill

import scala.language.implicitConversions
import io.getquill.Quoted
import io.getquill.ast._
import io.getquill.QuotationLot
import io.getquill.QuotationVase
import io.getquill.context.ExecutionType
import org.scalatest._
import io.getquill.ast.*
import io.getquill.context.{Context, ExecutionType}
import io.getquill.context.ExecutionType.{Dynamic, Static}
import io.getquill.quat.quatOf
import io.getquill.context.ExecutionType.Static
import io.getquill.context.ExecutionType.Dynamic
import io.getquill.context.Context
import io.getquill.quote
import io.getquill.query
import io.getquill.{QuotationLot, QuotationVase, Quoted, query, quote}
import io.getquill.util.debug.PrintMac
import org.scalatest.*

import scala.language.implicitConversions

class BatchActionMultiTest extends Spec with Inside with SuperContext[PostgresDialect, Literal] {
// Need to fully type this otherwise scala compiler thinks it's still just 'Context' from the super-class
// and the extensions (m: MirrorContext[_, _]#BatchActionMirror) etc... classes in Spec don't match their types correctly
val ctx: MirrorContext[PostgresDialect, Literal] = new MirrorContext[PostgresDialect, Literal](PostgresDialect, Literal)
import ctx._

import ctx.*

"Multi-row Batch Action Should work with" - {
"inserts > batch-size - (2rows + 2rows) + (1row)" - {
val people = List(Person(1, "A", 111, Sex.Male), Person(2, "B", 222, Sex.Male), Person(3, "C", 333, Sex.Male), Person(4, "D", 444, Sex.Female), Person(5, "E", 555, Sex.Female))
val people = List(
Person(1, "A", 111, Sex.Male),
Person(2, "B", 222, Sex.Male),
Person(3, "C", 333, Sex.Male),
Person(4, "D", 444, Sex.Female),
Person(5, "E", 555, Sex.Female)
)

def expect(executionType: ExecutionType) =
List(
(
Expand Down Expand Up @@ -60,6 +63,7 @@ class BatchActionMultiTest extends Spec with Inside with SuperContext[PostgresDi
executionType
)
)

"static - mixed" in {
val static = ctx.run(liftQuery(people).foreach(p => query[Person].insert(_.id -> p.id, _.name -> (lift("foo") + p.name + "bar"), _.age -> p.age)), 2)
static.tripleBatchMulti mustEqual expect2(ExecutionType.Static)
Expand All @@ -73,7 +77,13 @@ class BatchActionMultiTest extends Spec with Inside with SuperContext[PostgresDi
}

"batch insert - (2rows + 2rows)" - {
val people = List(Person(1, "A", 111), Person(2, "B", 222), Person(3, "C", 333), Person(4, "D", 444))
val people = List(
Person(1, "A", 111, Sex.Male),
Person(2, "B", 222, Sex.Male),
Person(3, "C", 333, Sex.Male),
Person(4, "D", 444, Sex.Female),
)

def expect(executionType: ExecutionType) =
List(
(
Expand All @@ -95,7 +105,11 @@ class BatchActionMultiTest extends Spec with Inside with SuperContext[PostgresDi
}

"inserts == batch-size" - {
val people = List(Person(1, "A", 111), Person(2, "B", 222))
val people = List(
Person(1, "A", 111, Sex.Male),
Person(2, "B", 222, Sex.Male),
)

def expect(executionType: ExecutionType) =
List(
(
Expand All @@ -117,7 +131,8 @@ class BatchActionMultiTest extends Spec with Inside with SuperContext[PostgresDi
}

"inserts < batch-size - (1row)" - {
val people = List(Person(1, "A", 111))
val people = List(Person(1, "A", 111, Sex.Male))

def expect(executionType: ExecutionType) =
List(
(
Expand All @@ -140,8 +155,15 @@ class BatchActionMultiTest extends Spec with Inside with SuperContext[PostgresDi

"fallback for non-insert query (in a context that doesn't support update)" - {
val ctx: MirrorContext[MySQLDialect, Literal] = new MirrorContext[MySQLDialect, Literal](MySQLDialect, Literal)
import ctx._
val people = List(Person(1, "A", 111), Person(2, "B", 222), Person(3, "C", 333), Person(4, "D", 444), Person(5, "E", 555))
import ctx.*
val people = List(
Person(1, "A", 111, Sex.Male),
Person(2, "B", 222, Sex.Male),
Person(3, "C", 333, Sex.Male),
Person(4, "D", 444, Sex.Female),
Person(5, "E", 555, Sex.Female)
)

def expect(executionType: ExecutionType) =
List(
(
Expand All @@ -163,7 +185,14 @@ class BatchActionMultiTest extends Spec with Inside with SuperContext[PostgresDi
}

"update query" - {
val people = List(Person(1, "A", 111), Person(2, "B", 222), Person(3, "C", 333), Person(4, "D", 444), Person(5, "E", 555))
val people = List(
Person(1, "A", 111, Sex.Male),
Person(2, "B", 222, Sex.Male),
Person(3, "C", 333, Sex.Male),
Person(4, "D", 444, Sex.Female),
Person(5, "E", 555, Sex.Female)
)

def expect(executionType: ExecutionType) =
List(
(
Expand All @@ -190,7 +219,14 @@ class BatchActionMultiTest extends Spec with Inside with SuperContext[PostgresDi
}

"supported contexts" - {
val people = List(Person(1, "A", 111), Person(2, "B", 222), Person(3, "C", 333), Person(4, "D", 444), Person(5, "E", 555))
val people = List(
Person(1, "A", 111, Sex.Male),
Person(2, "B", 222, Sex.Male),
Person(3, "C", 333, Sex.Male),
Person(4, "D", 444, Sex.Female),
Person(5, "E", 555, Sex.Female)
)

def makeRow(executionType: ExecutionType)(queryA: String, queryB: String) =
List(
(
Expand Down Expand Up @@ -231,37 +267,44 @@ class BatchActionMultiTest extends Spec with Inside with SuperContext[PostgresDi

"postgres - regular/returning" in {
val ctx: MirrorContext[PostgresDialect, Literal] = new MirrorContext(PostgresDialect, Literal)
import ctx._
import ctx.*
ctx.run(liftQuery(people).foreach(p => insertPeople(p)), 2).tripleBatchMulti mustEqual expect(ExecutionType.Static)
ctx.run(liftQuery(people).foreach(p => insertPeople(p).returning(_.id)), 2).tripleBatchMulti mustEqual expectPostgresReturning(ExecutionType.Static)
}
"sqlserver - regular/returning" in {
val ctx: MirrorContext[SQLServerDialect, Literal] = new MirrorContext(SQLServerDialect, Literal)
import ctx._
import ctx.*
ctx.run(liftQuery(people).foreach(p => insertPeople(p)), 2).tripleBatchMulti mustEqual expect(ExecutionType.Static)
ctx.run(liftQuery(people).foreach(p => insertPeople(p).returning(_.id)), 2).tripleBatchMulti mustEqual expectSqlServerReturning(ExecutionType.Static)
}
"mysql - regular/returning" in {
val ctx: MirrorContext[MySQLDialect, Literal] = new MirrorContext(MySQLDialect, Literal)
import ctx._
import ctx.*
ctx.run(liftQuery(people).foreach(p => insertPeople(p)), 2).tripleBatchMulti mustEqual expect(ExecutionType.Static)
ctx.run(liftQuery(people).foreach(p => insertPeople(p).returning(_.id)), 2).tripleBatchMulti mustEqual expect(ExecutionType.Static)
}
"h2 - regular/returning" in {
val ctx: MirrorContext[H2Dialect, Literal] = new MirrorContext(H2Dialect, Literal)
import ctx._
import ctx.*
ctx.run(liftQuery(people).foreach(p => insertPeople(p)), 2).tripleBatchMulti mustEqual expectH2(ExecutionType.Static)
ctx.run(liftQuery(people).foreach(p => insertPeople(p).returning(_.id)), 2).tripleBatchMulti mustEqual expectH2(ExecutionType.Static)
}
"sqlite - only regular" in {
val ctx: MirrorContext[SqliteDialect, Literal] = new MirrorContext(SqliteDialect, Literal)
import ctx._
import ctx.*
ctx.run(liftQuery(people).foreach(p => insertPeople(p)), 2).tripleBatchMulti mustEqual expect(ExecutionType.Static)
}
}

"fallback for non-supported context" - {
val people = List(Person(1, "A", 111), Person(2, "B", 222), Person(3, "C", 333), Person(4, "D", 444), Person(5, "E", 555))
val people = List(
Person(1, "A", 111, Sex.Male),
Person(2, "B", 222, Sex.Male),
Person(3, "C", 333, Sex.Male),
Person(4, "D", 444, Sex.Female),
Person(5, "E", 555, Sex.Female)
)

def expect(executionType: ExecutionType) =
List(
(
Expand All @@ -273,7 +316,7 @@ class BatchActionMultiTest extends Spec with Inside with SuperContext[PostgresDi

"oracle" - {
val ctx: MirrorContext[OracleDialect, Literal] = new MirrorContext[OracleDialect, Literal](OracleDialect, Literal)
import ctx._
import ctx.*
"static" in {
val static = ctx.run(liftQuery(people).foreach(p => insertPeople(p)), 2)
static.tripleBatchMulti mustEqual expect(ExecutionType.Static)
Expand All @@ -285,7 +328,7 @@ class BatchActionMultiTest extends Spec with Inside with SuperContext[PostgresDi
}
"sqlite - with returning clause" - {
val ctx: MirrorContext[OracleDialect, Literal] = new MirrorContext[OracleDialect, Literal](OracleDialect, Literal)
import ctx._
import ctx.*
"static" in {
val static = ctx.run(liftQuery(people).foreach(p => insertPeople(p).returning(_.id)), 2)
static.tripleBatchMulti mustEqual expect(ExecutionType.Static)
Expand Down

0 comments on commit 0c98ca9

Please sign in to comment.