Permalink
Browse files

Fixes #1557, #1000

* Merge and cleanup @trevorsibanda's work on #1557 and #1000.
  Consolidate MySql tests, resolve merge conflicts with master
* Trim the BigDecimal default value string to remove trailing spaces from Oracle
  default value.
* Remove the OracleProfile specific default value handling - this is now done in the
  JdbcModelBuilder and is unnecessary.
* Add test for Oracle BigDecimal default value.

Tests fail for MySql and Postgres.

MySql:
```scala

[error] Test com.typesafe.slick.testkit.tests.JoinTest.testDiscriminatorCheck[mysql] failed: java.lang.AssertionError: expected:<Set(3)> but was:<Set()>, took 0.024 sec
[error]     at com.typesafe.slick.testkit.tests.JoinTest$$anonfun$testDiscriminatorCheck$2.apply(JoinTest.scala:347)
[error]     at com.typesafe.slick.testkit.tests.JoinTest$$anonfun$testDiscriminatorCheck$2.apply(JoinTest.scala:347)
[error]     at slick.dbio.DBIOAction$$anonfun$map$1.apply(DBIOAction.scala:44)
[error]     at slick.dbio.DBIOAction$$anonfun$map$1.apply(DBIOAction.scala:44)
[error]     at slick.basic.BasicBackend$DatabaseDef$$anonfun$runInContext$1.apply(BasicBackend.scala:149)
[error]     at slick.basic.BasicBackend$DatabaseDef$$anonfun$runInContext$1.apply(BasicBackend.scala:149)
[error]     at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:253)
[error]     at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:251)
[error]     at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
[error]     at scala.concurrent.impl.ExecutionContextImpl$AdaptedForkJoinTask.exec(ExecutionContextImpl.scala:121)
[error]     at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
[error]     at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
[error]     at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
[error]     at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
```

Postgres:
```scala
[error] Test com.typesafe.slick.testkit.tests.ModelBuilderTest.test[postgres] failed: java.lang.NumberFormatException: For input string: "'-5'::integer", took 0.413 sec
[error]     at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
[error]     at java.lang.Integer.parseInt(Integer.java:569)
[error]     at java.lang.Integer.parseInt(Integer.java:615)
[error]     at scala.collection.immutable.StringLike$class.toInt(StringLike.scala:272)
[error]     at scala.collection.immutable.StringOps.toInt(StringOps.scala:29)
[error]     at slick.jdbc.JdbcModelBuilder$ColumnBuilder$$anonfun$default$1.apply(JdbcModelBuilder.scala:220)
[error]     at slick.jdbc.JdbcModelBuilder$ColumnBuilder$$anonfun$default$1.apply(JdbcModelBuilder.scala:214)
[error]     at scala.Option.map(Option.scala:146)
[error]     at slick.jdbc.JdbcModelBuilder$ColumnBuilder.default(JdbcModelBuilder.scala:214)
[error]     at slick.jdbc.PostgresProfile$ModelBuilder$$anon$2.slick$jdbc$PostgresProfile$ModelBuilder$$anon$$super$default(PostgresProfile.scala:86)
[error]     at slick.jdbc.PostgresProfile$ModelBuilder$$anon$2$$anonfun$default$3.apply(PostgresProfile.scala:86)
[error]     at slick.jdbc.PostgresProfile$ModelBuilder$$anon$2$$anonfun$default$3.apply(PostgresProfile.scala:85)
[error]     at scala.Option.getOrElse(Option.scala:121)
[error]     at slick.jdbc.PostgresProfile$ModelBuilder$$anon$2.default(PostgresProfile.scala:85)
[error]     at slick.jdbc.JdbcModelBuilder$ColumnBuilder$$anonfun$defaultColumnOption$3.apply(JdbcModelBuilder.scala:254)
[error]     at slick.jdbc.JdbcModelBuilder$ColumnBuilder$$anonfun$defaultColumnOption$3.apply(JdbcModelBuilder.scala:254)
[error]     at scala.Option.getOrElse(Option.scala:121)
[error]     at slick.jdbc.JdbcModelBuilder$ColumnBuilder.defaultColumnOption(JdbcModelBuilder.scala:253)
[error]     at slick.jdbc.JdbcModelBuilder$ColumnBuilder.convenientDefault(JdbcModelBuilder.scala:263)
[error]     at slick.jdbc.JdbcModelBuilder$ColumnBuilder.model(JdbcModelBuilder.scala:281)
[error]     at slick.jdbc.JdbcModelBuilder$TableBuilder$$anonfun$columns$1.apply(JdbcModelBuilder.scala:163)
[error]     at slick.jdbc.JdbcModelBuilder$TableBuilder$$anonfun$columns$1.apply(JdbcModelBuilder.scala:163)
[error]     at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:234)
[error]     at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:234)
[error]     at scala.collection.Iterator$class.foreach(Iterator.scala:893)
[error]     at scala.collection.AbstractIterator.foreach(Iterator.scala:1336)
[error]     at scala.collection.IterableLike$class.foreach(IterableLike.scala:72)
[error]     at scala.collection.AbstractIterable.foreach(Iterable.scala:54)
[error]     at scala.collection.TraversableLike$class.map(TraversableLike.scala:234)
[error]     at scala.collection.AbstractTraversable.map(Traversable.scala:104)
[error]     at slick.jdbc.JdbcModelBuilder$TableBuilder.columns$lzycompute(JdbcModelBuilder.scala:163)
[error]     at slick.jdbc.JdbcModelBuilder$TableBuilder.columns(JdbcModelBuilder.scala:163)
[error]     at slick.jdbc.JdbcModelBuilder$TableBuilder.buildModel(JdbcModelBuilder.scala:161)
[error]     at slick.jdbc.JdbcModelBuilder$$anonfun$buildModel$3$$anonfun$apply$17.apply(JdbcModelBuilder.scala:96)
[error]     at slick.jdbc.JdbcModelBuilder$$anonfun$buildModel$3$$anonfun$apply$17.apply(JdbcModelBuilder.scala:96)
[error]     at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:234)
[error]     at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:234)
[error]     at scala.collection.immutable.List.foreach(List.scala:381)
[error]     at scala.collection.TraversableLike$class.map(TraversableLike.scala:234)
[error]     at scala.collection.immutable.List.map(List.scala:285)
[error]     at slick.jdbc.JdbcModelBuilder$$anonfun$buildModel$3.apply(JdbcModelBuilder.scala:96)
[error]     at slick.jdbc.JdbcModelBuilder$$anonfun$buildModel$3.apply(JdbcModelBuilder.scala:93)
[error]     at slick.dbio.DBIOAction$$anonfun$map$1.apply(DBIOAction.scala:44)
[error]     at slick.dbio.DBIOAction$$anonfun$map$1.apply(DBIOAction.scala:44)
[error]     at slick.basic.BasicBackend$DatabaseDef$$anonfun$runInContext$1.apply(BasicBackend.scala:149)
[error]     at slick.basic.BasicBackend$DatabaseDef$$anonfun$runInContext$1.apply(BasicBackend.scala:149)
[error]     at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:253)
[error]     at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:251)
[error]     at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
[error]     at scala.concurrent.impl.ExecutionContextImpl$AdaptedForkJoinTask.exec(ExecutionContextImpl.scala:121)
[error]     at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
[error]     at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.pollAndExecAll(ForkJoinPool.java:1253)
[error]     at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1346)
[error]     at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
[error]     at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
```
  • Loading branch information...
1 parent 24909b8 commit 07b9c987b241497a790637a4b19e837bfd25f1c2 @rbellamy rbellamy committed Sep 11, 2016
@@ -180,7 +180,7 @@ class $name(_tableTag: Tag) extends profile.api.Table[$elementType](_tableTag, $
case v:Boolean => s"$v"
case v:Short => s"$v"
case v:Char => s"'$v'"
- case v:BigDecimal => s"new scala.math.BigDecimal(new java.math.BigDecimal($v))"
+ case v:BigDecimal => s"""scala.math.BigDecimal(\"$v\")"""
case v => throw new SlickException( s"Dont' know how to generate code for default value $v of ${v.getClass}. Override def defaultCode to render the value." )
}
// Explicit type to allow overloading existing Slick method names.
@@ -83,25 +83,14 @@ val SimpleA = CustomTyping.SimpleA
})
},
new UUIDConfig("CG10", StandardTestDBs.H2Mem, "H2Mem", Seq("/dbs/uuid-h2.sql")),
+ new Config("CG11", StandardTestDBs.H2Mem, "H2Mem", Seq("/dbs/h2-simple.sql")) {
override def generator = tdb.profile.createModel(ignoreInvalidDefaults=false).map(new MyGen(_) {
override def Table = new Table(_){
override def Column = new Column(_){
- | DBIO.seq(
- | schema.create,
- | DefaultNumeric += entry,
- | DefaultNumeric.result.head.map{ r => assertEquals(r , entry) }
- """.stripMargin
- },
- new Config("MySQL", StandardTestDBs.MySQL, "MySQL", Seq("/dbs/mysql.sql")){
- override def testCode =
- """
- | val entry = DefaultNumericRow(d0 = scala.math.BigDecimal(123.45), d1 = scala.math.BigDecimal(90), d3 = 0)
- | DBIO.seq(
- | schema.create,
- | DefaultNumeric += entry,
- | DefaultNumeric.result.head.map{ r => assertEquals(r , entry) }
- | )
- """.stripMargin
+ override def asOption = true
+ }
+ }
+ })
},
new Config("Postgres1", StandardTestDBs.Postgres, "Postgres", Nil) {
import tdb.profile.api._
@@ -113,7 +102,7 @@ val SimpleA = CustomTyping.SimpleA
}
override def generator =
TableQuery[A].schema.create >>
- tdb.profile.createModel(ignoreInvalidDefaults=false).map(new MyGen(_))
+ tdb.profile.createModel(ignoreInvalidDefaults=false).map(new MyGen(_))
override def testCode =
"""
| import java.sql.Blob
@@ -126,66 +115,68 @@ val SimpleA = CustomTyping.SimpleA
| ).transactionally
""".stripMargin
},
- new Config("MySQL", StandardTestDBs.MySQL, "MySQL", Seq("/dbs/mysql.sql")){
- override def testCode =
- """
- | val createStmt = schema.create.statements.mkString
- | assertTrue(createStmt contains "`entry1` LONGTEXT ")
- | assertTrue(createStmt contains "`entry2` MEDIUMTEXT")
- | assertTrue(createStmt contains "`entry3` TEXT" )
- | assertTrue(createStmt contains "`entry4` VARCHAR(255)")
- | DBIO.seq( schema.create )
- """.stripMargin
- },
new UUIDConfig("Postgres2", StandardTestDBs.Postgres, "Postgres", Seq("/dbs/uuid-postgres.sql")),
new Config("MySQL", StandardTestDBs.MySQL, "MySQL", Seq("/dbs/mysql.sql") ){
override def generator: DBIO[SourceCodeGenerator] =
tdb.profile.createModel(ignoreInvalidDefaults=false).map(new SourceCodeGenerator(_){
override def parentType = Some("com.typesafe.slick.testkit.util.TestCodeRunner.TestCase")
override def code = {
- val testcode =
- """
- | val createStmt = schema.create.statements.mkString
- | assertTrue(createStmt contains "`entry1` LONGTEXT ")
- | assertTrue(createStmt contains "`entry2` MEDIUMTEXT")
- | assertTrue(createStmt contains "`entry3` TEXT" )
- | assertTrue(createStmt contains "`entry4` VARCHAR(255)")
- | DBIO.seq( schema.create ,
- | TableName += TableNameRow(0),
- | TableName.result.map{ case Seq(TableNameRow(id) ) => assertTrue("Schema name should be `slick_test`" , TableName.baseTableRow.schemaName.get eq "slick_test" ) }
- | )
- """.stripMargin
- s"""
- |lazy val tdb = $fullTdbName
- |def test = {
- | import org.junit.Assert._
- | import scala.concurrent.ExecutionContext.Implicits.global
- | $testcode
- |}
+ val testcode =
+ """
+ | val entry = DefaultNumericRow(d0 = scala.math.BigDecimal(123.45), d1 = scala.math.BigDecimal(90), d3 = 0)
+ | val createStmt = schema.create.statements.mkString
+ | assertTrue(createStmt contains "`entry1` LONGTEXT")
+ | assertTrue(createStmt contains "`entry2` MEDIUMTEXT")
+ | assertTrue(createStmt contains "`entry3` TEXT")
+ | assertTrue(createStmt contains "`entry4` VARCHAR(255)")
+ | DBIO.seq(
+ | schema.create,
+ | TableName += TableNameRow(0),
+ | TableName.result.map{ case Seq(TableNameRow(id) ) => assertTrue("Schema name should be `slick_test`" , TableName.baseTableRow.schemaName.get eq "slick_test" ) },
+ | DefaultNumeric += entry,
+ | DefaultNumeric.result.head.map{ r => assertEquals(r , entry) }
+ | )
+ """.stripMargin
+ s"""
+ |lazy val tdb = $fullTdbName
+ |def test = {
+ | import org.junit.Assert._
+ | import scala.concurrent.ExecutionContext.Implicits.global
+ | $testcode
+ |}
""".stripMargin + super.code
}
})
},
new Config("EmptyDB", StandardTestDBs.H2Mem, "H2Mem", Nil),
new Config("Oracle1", StandardTestDBs.Oracle, "Oracle", Seq("/dbs/oracle1.sql")) {
override def useSingleLineStatements = true
- override def testCode = "DBIO.successful(())"
+ override def testCode =
+ """
+ | val entry = PersonRow(1)
+ | assertEquals(scala.math.BigDecimal(0), entry.age)
+ | DBIO.seq (
+ | schema.create,
+ | Person += entry,
+ | Person.result.head.map{ r => assertEquals(r , entry) }
+ | )
+ """.stripMargin
}
)
//Unified UUID config
class UUIDConfig(objectName: String, tdb: JdbcTestDB, tdbName: String, initScripts: Seq[String])
extends Config(objectName, tdb, tdbName, initScripts) {
- override def generator = tdb.profile.createModel(ignoreInvalidDefaults=false).map(new MyGen(_) {
- override def Table = new Table(_) {
- override def Column = new Column(_){
- override def defaultCode: (Any) => String = {
- case v: java.util.UUID => s"""java.util.UUID.fromString("${v.toString}")"""
- case v => super.defaultCode(v)
- }
+ override def generator = tdb.profile.createModel(ignoreInvalidDefaults=false).map(new MyGen(_) {
+ override def Table = new Table(_) {
+ override def Column = new Column(_){
+ override def defaultCode: (Any) => String = {
+ case v: java.util.UUID => s"""java.util.UUID.fromString("${v.toString}")"""
+ case v => super.defaultCode(v)
}
- override def code = {
- Seq("""
+ }
+ override def code = {
+ Seq("""
| /* default UUID, which is the same as for 'uuid.sql' */
| val defaultUUID = java.util.UUID.fromString("2f3f866c-d8e6-11e2-bb56-50e549c9b654")
| /* convert UUID */
@@ -196,35 +187,35 @@ val SimpleA = CustomTyping.SimpleA
| implicit object GetOptionUUID extends slick.jdbc.GetResult[Option[java.util.UUID]] {
| def apply(rs: slick.jdbc.PositionedResult) = Option(rs.nextObject().asInstanceOf[java.util.UUID])
| }
- """.stripMargin) ++ super.code
- }
+ """.stripMargin) ++ super.code
}
- })
- override def testCode =
- """
- | import java.util.UUID
- | val u1 = UUID.randomUUID()
- | val u2 = UUID.randomUUID()
- | val u3 = UUID.randomUUID()
- | val u4 = UUID.randomUUID()
- | val p1 = PersonRow(1, u1, uuidFunc = Some(u3))
- | val p2 = PersonRow(2, u2, uuidFunc = Some(u4))
- |
- | def assertAll(all: Seq[PersonRow]) = {
- | assertEquals( 2, all.size )
- | assertEquals( Set(1,2), all.map(_.id).toSet )
- | assertEquals( Set(u1, u2), all.map(_.uuid).toSet )
- | assertEquals( Set(Some(u3), Some(u4)), all.map(_.uuidFunc).toSet )
- | //it should contain sample UUID
- | assert(all.forall(_.uuidDef == Some(defaultUUID)))
- | }
- |
- | DBIO.seq(
- | schema.create,
- | Person += p1,
- | Person += p2,
- | Person.result.map(assertAll)
- | ).transactionally
- """.stripMargin
- }
+ }
+ })
+ override def testCode =
+ """
+ | import java.util.UUID
+ | val u1 = UUID.randomUUID()
+ | val u2 = UUID.randomUUID()
+ | val u3 = UUID.randomUUID()
+ | val u4 = UUID.randomUUID()
+ | val p1 = PersonRow(1, u1, uuidFunc = Some(u3))
+ | val p2 = PersonRow(2, u2, uuidFunc = Some(u4))
+ |
+ | def assertAll(all: Seq[PersonRow]) = {
+ | assertEquals( 2, all.size )
+ | assertEquals( Set(1,2), all.map(_.id).toSet )
+ | assertEquals( Set(u1, u2), all.map(_.uuid).toSet )
+ | assertEquals( Set(Some(u3), Some(u4)), all.map(_.uuidFunc).toSet )
+ | //it should contain sample UUID
+ | assert(all.forall(_.uuidDef == Some(defaultUUID)))
+ | }
+ |
+ | DBIO.seq(
+ | schema.create,
+ | Person += p1,
+ | Person += p2,
+ | Person.result.map(assertAll)
+ | ).transactionally
+ """.stripMargin
+ }
}
@@ -227,7 +227,7 @@ class JdbcModelBuilder(mTables: Seq[MTable], ignoreInvalidDefaults: Boolean)(imp
case 3 => v(1) // quoted character
}
case (v,"String") if meta.typeName == "CHAR" => v.head // FIXME: check length
- case (v,"scala.math.BigDecimal") => v // FIXME: probably we shouldn't use a string here
+ case (v,"scala.math.BigDecimal") => BigDecimal(s"${v.trim}") // need the trim for Oracle trailing space
case (StringPattern(str),"String") => str
case ("TRUE","Boolean") => true
case ("FALSE","Boolean") => false
@@ -83,14 +83,6 @@ trait OracleProfile extends JdbcProfile {
case 101 => "Double"
case _ => super.tpe
}
- override def default = meta.columnDef.map((_,tpe)).collect{
- case (v, "scala.math.BigDecimal") => Some(Some(scala.math.BigDecimal(v.stripSuffix(" "))))
- }.getOrElse{
- val d = super.default
- if(meta.nullable == Some(true) && d == None){
- Some(None)
- } else d
- }
override def rawDefault = super.rawDefault.map(_.stripSuffix(" ")).map{
case "null" => "NULL"
case v => v

0 comments on commit 07b9c98

Please sign in to comment.