Permalink
Browse files

Add tests for the `removeTakeDrop` phase:

- The new “h2rownum” TestDB is a copy of “h2mem” with `removeTakeDrop`
  enabled. This phase is not needed by any driver in Slick core but it
  is used by Slick Extensions drivers and also proves useful for testing
  the query compiler because it can create rather complex ASTs.

- Fix a typing bug in the code generator. The folded `schema` val can
  have a wrong inferred type, so we have to generate a type annotation.
  • Loading branch information...
szeiger committed Aug 10, 2015
1 parent 9e28dec commit 5ca0e5cbd9b91740ed9554d5078f8cfa8f0c89e3
@@ -28,9 +28,9 @@ abstract class AbstractSourceCodeGenerator(model: m.Model)
"\n/** DDL for all tables. Call .create to execute. */" +
(
if(tables.length > 5)
"\nlazy val schema = Array(" + tables.map(_.TableValue.name + ".schema").mkString(", ") + ").reduceLeft(_ ++ _)"
"\nlazy val schema: profile.SchemaDescription = Array(" + tables.map(_.TableValue.name + ".schema").mkString(", ") + ").reduceLeft(_ ++ _)"
else
"\nlazy val schema = " + tables.map(_.TableValue.name + ".schema").mkString(" ++ ")
"\nlazy val schema: profile.SchemaDescription = " + tables.map(_.TableValue.name + ".schema").mkString(" ++ ")
) +
"\n@deprecated(\"Use .schema instead of .ddl\", \"3.0\")"+
"\ndef ddl = schema" +
@@ -4,7 +4,7 @@
testkit {
# Use only the following DBs (or use all if not set)
#testDBs = [h2mem, h2disk, hsqldbmem, hsqldbdisk, sqlitemem, sqlitedisk, derbymem, derbydisk, postgres, mysql, access, heap]
#testDBs = [h2mem, h2rownum, h2disk, hsqldbmem, hsqldbdisk, sqlitemem, sqlitedisk, derbymem, derbydisk, postgres, mysql, access, heap]
testDBs = null
# Store database files in this path (ignored by MySQL and in-memory databases)
@@ -3,6 +3,7 @@ package com.typesafe.slick.testkit.util
import java.io.File
import java.util.logging.{Level, Logger}
import java.sql.SQLException
import slick.compiler.Phase
import slick.dbio._
import slick.driver._
import slick.memory.MemoryDriver
@@ -20,6 +21,16 @@ object StandardTestDBs {
override def isPersistent = false
}
/** A modified H2Mem that tests the `removeTakeDrop` phase (which is not used by any of the
* standard drivers. */
lazy val H2Rownum = new H2TestDB("h2rownum", false) {
val url = "jdbc:h2:mem:test_rownum"
override def isPersistent = false
override val driver = new H2Driver {
override protected def computeQueryCompiler = super.computeQueryCompiler.addAfter(Phase.removeTakeDrop, Phase.expandSums)
}
}
lazy val H2MemKeepAlive = new H2TestDB("h2mem", true) {
val url = "jdbc:h2:mem:test1"
}
@@ -132,7 +143,7 @@ object StandardTestDBs {
}
abstract class H2TestDB(confName: String, keepAlive: Boolean) extends InternalJdbcTestDB(confName) {
val driver = H2Driver
val driver: Driver = H2Driver
val jdbcDriver = "org.h2.Driver"
override def capabilities = super.capabilities - TestDB.capabilities.jdbcMetaGetFunctions - TestDB.capabilities.jdbcMetaGetClientInfoProperties
override def createDB(): profile.Backend#Database = database.forURL(url, driver = jdbcDriver, keepAliveConnection = keepAlive)
@@ -1,5 +1,7 @@
package slick.test.codegen
import slick.driver.JdbcProfile
import scala.concurrent.ExecutionContext.Implicits.global
import slick.jdbc.meta.MTable
import slick.ast.{Node, Select}
@@ -13,7 +15,7 @@ class GeneratedCodeTest extends TestCodeRunner(AllTests)
object GeneratedCodeTest {
def testCG1 = {
import CG1._
import tdb.driver.api._
import profile.api._
DBIO.seq(
schema.create,
Suppliers += Supplier(1, "1", "2", "3", "4", "5"),
@@ -70,7 +72,7 @@ object GeneratedCodeTest {
}
val Db1 = new Db1
import Db1._
import tdb.driver.api._
import profile.api._
val s = Supplier(49, "Superior Coffee", "1 Party Place", "Mendocino", "CA", "95460")
DBIO.seq(
schema.create,
@@ -82,7 +84,7 @@ object GeneratedCodeTest {
def testCG3 = {
import CG3._
import tdb.driver.api._
import profile.api._
val s = Supplier(49, "Superior Coffee", "1 Party Place", "Mendocino", "CA", "95460")
DBIO.seq(
schema.create,
@@ -93,7 +95,7 @@ object GeneratedCodeTest {
def testCG7 = {
import CG7._
import tdb.driver.api._
import profile.api._
DBIO.seq(
schema.create,
Supps.length.result.map(assertEquals(0, _)),
@@ -105,7 +107,7 @@ object GeneratedCodeTest {
def testCG8 = {
import CG8._
import tdb.driver.api._
import profile.api._
DBIO.seq(
schema.create,
SimpleAs.length.result.map(assertEquals(0, _)),
@@ -117,7 +119,7 @@ object GeneratedCodeTest {
def testCG9 = {
import CG9._
import tdb.driver.api._
import profile.api._
def assertAll(all: Seq[ERow]) = {
assertEquals( 3, all.size )
assertEquals( Set(1,2,3), all.map(_.k1.get).toSet )
@@ -6,6 +6,9 @@ import com.typesafe.slick.testkit.util.{StandardTestDBs, DriverTest, Testkit}
@RunWith(classOf[Testkit])
class H2MemTest extends DriverTest(StandardTestDBs.H2Mem)
@RunWith(classOf[Testkit])
class H2RownumTest extends DriverTest(StandardTestDBs.H2Rownum)
@RunWith(classOf[Testkit])
class H2DiskTest extends DriverTest(StandardTestDBs.H2Disk)
@@ -168,7 +168,6 @@ object Phase {
val createAggregates = new CreateAggregates
val rewriteJoins = new RewriteJoins
val verifySymbols = new VerifySymbols
val removeTakeDrop = new RemoveTakeDrop
val resolveZipJoins = new ResolveZipJoins
val createResultSetMapping = new CreateResultSetMapping
val hoistClientOps = new HoistClientOps
@@ -180,6 +179,7 @@ object Phase {
val removeFieldNames = new RemoveFieldNames
/* Extra phases that are not enabled by default */
val removeTakeDrop = new RemoveTakeDrop
val resolveZipJoinsRownumStyle = new ResolveZipJoins(rownumStyle = true)
val rewriteBooleans = new RewriteBooleans
val specializeParameters = new SpecializeParameters

0 comments on commit 5ca0e5c

Please sign in to comment.