/
RelationalMapperTest.scala
166 lines (140 loc) · 5.24 KB
/
RelationalMapperTest.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.typesafe.slick.testkit.tests
import scala.language.existentials
import com.typesafe.slick.testkit.util.{RelationalTestDB, AsyncTest}
class RelationalMapperTest extends AsyncTest[RelationalTestDB] {
import tdb.profile.api._
def testMappedType = {
sealed trait Bool
case object True extends Bool
case object False extends Bool
implicit val boolTypeMapper = MappedColumnType.base[Bool, Int](
{ b =>
b shouldNotBe null
if(b == True) 1 else 0
}, { i =>
i shouldNotBe null
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]
seq(
ts.schema.create,
ts.map(t => (t.b, t.c)) ++= Seq((False, None), (True, Some(True))),
ts.to[Set].result.map(_ shouldBe Set((1, False, None), (2, True, Some(True)))),
ts.filter(_.b === (True:Bool)).to[Set].result.map(_ shouldBe Set((2, True, Some(True)))),
ts.filter(_.b === (False:Bool)).to[Set].result.map(_ shouldBe Set((1, False, None)))
)
}
def testMappedType2 = {
sealed trait EnumType
case object EnumValue1 extends EnumType
case object EnumValue2 extends EnumType
case object EnumValue3 extends EnumType
implicit val enumTypeMapper = MappedColumnType.base[EnumType, Char](
{ t =>
t shouldNotBe null
t match {
case EnumValue1 => 'A'
case EnumValue2 => 'B'
case _ => 'C'
}
}, { c =>
c shouldNotBe null
c match {
case 'A' => EnumValue1
case 'B' => EnumValue2
case _ => EnumValue3
}
}
)
class T(tag: Tag) extends Table[(Int, EnumType, Option[EnumType])](tag, "t32") {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def b = column[EnumType]("b")
def c = column[Option[EnumType]]("c")
def * = (id, b, c)
}
val ts = TableQuery[T]
seq(
ts.schema.create,
ts.map(t => (t.b, t.c)) ++= Seq((EnumValue1, None), (EnumValue1, Some(EnumValue2)), (EnumValue2, Some(EnumValue3))),
ts.to[Set].result.map(_ shouldBe Set((1, EnumValue1, None), (2, EnumValue1, Some(EnumValue2)), (3, EnumValue2, Some(EnumValue3)))),
ts.filter(_.b === (EnumValue1:EnumType)).to[Set].result.map(_ shouldBe Set((1, EnumValue1, None), (2, EnumValue1, Some(EnumValue2)))),
ts.filter(_.b === (EnumValue2:EnumType)).to[Set].result.map(_ shouldBe Set((3, EnumValue2, Some(EnumValue3))))
)
}
def testMappedRefType = {
sealed trait Bool
case object True extends Bool
case object False extends Bool
implicit val boolTypeMapper = MappedColumnType.base[Bool, String](
{ b =>
b shouldNotBe null
if(b == True) "y" else "n"
}, { i =>
i shouldNotBe null
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]
seq(
ts.schema.create,
ts.map(t => (t.b, t.c)) ++= Seq((False, None), (True, Some(True))),
ts.to[Set].result.map(_ shouldBe Set((1, False, None), (2, True, Some(True)))),
ts.filter(_.b === (True:Bool)).to[Set].result.map(_ shouldBe Set((2, True, Some(True)))),
ts.filter(_.b === (False:Bool)).to[Set].result.map(_ shouldBe Set((1, False, None)))
)
}
def testAutoMapped = {
class T(tag: Tag) extends Table[(MyMappedID, Int)](tag, "t_automapped") {
def id = column[MyMappedID]("id", O.PrimaryKey)
def v = column[Int]("v")
def * = (id, v)
}
val ts = TableQuery[T]
seq(
ts.schema.create,
ts ++= Seq((MyMappedID(1), 2), (MyMappedID(3), 4)),
ts.to[Set].result.map(_ shouldBe Set((MyMappedID(1), 2), (MyMappedID(3), 4))),
ts.filter(_.id === MyMappedID(1)).to[Set].result.map(_ shouldBe Set((MyMappedID(1), 2)))
)
}
def mappedToMacroCompilerBug = {
case class MyId(val value: Int) extends MappedTo[Int]
class MyTable(tag: Tag) extends Table[MyId](tag, "table") {
def * = ???
}
implicitly[Shape[_ <: FlatShapeLevel, MyTable, _, _]]
TableQuery(new MyTable(_)).map(identity)
}
def testMappedProjectionShape = {
def toLower(s: Option[String]) = s.map(_.toLowerCase)
def toUpper(s: Option[String]): Option[Option[String]] = Some(s.map(_.toUpperCase))
case class Row(id: String, escaped: Option[String])
class T(tag: Tag) extends Table[Row](tag, "t".withUniquePostFix) {
val id = column[String]("id", O.PrimaryKey)
val name = column[Option[String]]("name")
val upperName = name.shaped <> (toLower, toUpper)
def * = (id, upperName) <> (Row.tupled, Row.unapply)
}
val ts = TableQuery[T]
seq(
ts.schema.create,
ts += Row("a", Some("foo")),
ts.map(_.name).result.map(_ shouldBe Seq(Some("FOO"))),
ts.result.map(_ shouldBe Seq(Row("a", Some("foo"))))
)
}
}
case class MyMappedID(value: Int) extends AnyVal with slick.lifted.MappedTo[Int]