Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move JdbcType into the cake and abstract it to RelationalProfile
- Loading branch information
Showing
20 changed files
with
239 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
slick-testkit/src/main/scala/com/typesafe/slick/testkit/tests/RelationalMapperTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.typesafe.slick.testkit.tests | ||
|
||
import org.junit.Assert._ | ||
|
||
import com.typesafe.slick.testkit.util.{RelationalTestDB, TestkitTest} | ||
|
||
class RelationalMapperTest extends TestkitTest[RelationalTestDB] { | ||
import tdb.profile.simple._ | ||
|
||
override val reuseInstance = true | ||
|
||
def testMappedType { | ||
sealed trait Bool | ||
case object True extends Bool | ||
case object False extends Bool | ||
|
||
implicit val boolTypeMapper = MappedColumnType.base[Bool, Int]( | ||
{ b => | ||
assertNotNull(b) | ||
if(b == True) 1 else 0 | ||
}, { i => | ||
assertNotNull(i) | ||
if(i == 1) True else False | ||
} | ||
) | ||
|
||
class T(tag: Tag) extends Table[(Int, Bool, Option[Bool])](tag, "t2") { | ||
def id = column[Int]("id", O.PrimaryKey, O.AutoInc) | ||
def b = column[Bool]("b") | ||
def c = column[Option[Bool]]("c") | ||
def * = (id, b, c) | ||
} | ||
val ts = TableQuery[T] | ||
|
||
ts.ddl.create | ||
ts.map(t => (t.b, t.c)) ++= Seq((False, None), (True, Some(True))) | ||
assertEquals(ts.run.toSet, Set((1, False, None), (2, True, Some(True)))) | ||
assertEquals(ts.filter(_.b === (True:Bool)).run.toSet, Set((2, True, Some(True)))) | ||
assertEquals(ts.filter(_.b === (False:Bool)).run.toSet, Set((1, False, None))) | ||
} | ||
|
||
def testMappedRefType { | ||
sealed trait Bool | ||
case object True extends Bool | ||
case object False extends Bool | ||
|
||
implicit val boolTypeMapper = MappedColumnType.base[Bool, String]( | ||
{ b => | ||
assertNotNull(b) | ||
if(b == True) "y" else "n" | ||
}, { i => | ||
assertNotNull(i) | ||
if(i == "y") True else False | ||
} | ||
) | ||
|
||
class T(tag: Tag) extends Table[(Int, Bool, Option[Bool])](tag, "t3") { | ||
def id = column[Int]("id", O.PrimaryKey, O.AutoInc) | ||
def b = column[Bool]("b") | ||
def c = column[Option[Bool]]("c") | ||
def * = (id, b, c) | ||
} | ||
val ts = TableQuery[T] | ||
|
||
ts.ddl.create | ||
ts.map(t => (t.b, t.c)) ++= Seq((False, None), (True, Some(True))) | ||
assertEquals(ts.run.toSet, Set((1, False, None), (2, True, Some(True)))) | ||
assertEquals(ts.where(_.b === (True:Bool)).run.toSet, Set((2, True, Some(True)))) | ||
assertEquals(ts.where(_.b === (False:Bool)).run.toSet, Set((1, False, None))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.