From 0c98ca98be6177db07b8b48d134af5e052428d97 Mon Sep 17 00:00:00 2001 From: jules Ivanic Date: Sat, 1 Jun 2024 01:15:30 +0800 Subject: [PATCH] improve tests --- .../io/getquill/BatchActionMultiTest.scala | 101 +++++++++++++----- 1 file changed, 72 insertions(+), 29 deletions(-) diff --git a/quill-sql/src/test/scala/io/getquill/BatchActionMultiTest.scala b/quill-sql/src/test/scala/io/getquill/BatchActionMultiTest.scala index b8832b32..1d244d05 100644 --- a/quill-sql/src/test/scala/io/getquill/BatchActionMultiTest.scala +++ b/quill-sql/src/test/scala/io/getquill/BatchActionMultiTest.scala @@ -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( ( @@ -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) @@ -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( ( @@ -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( ( @@ -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( ( @@ -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( ( @@ -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( ( @@ -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( ( @@ -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( ( @@ -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) @@ -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)