/
RelationalMiscTest.scala
180 lines (145 loc) · 5.59 KB
/
RelationalMiscTest.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package com.typesafe.slick.testkit.tests
import scala.language.higherKinds
import org.junit.Assert._
import com.typesafe.slick.testkit.util.{RelationalTestDB, TestkitTest}
class RelationalMiscTest extends TestkitTest[RelationalTestDB] {
import tdb.profile.simple._
override val reuseInstance = true
def isNotAndOrTest {
class T(tag: Tag) extends Table[(String, String)](tag, "users") {
def a = column[String]("a")
def b = column[String]("b")
def * = (a, b)
}
val ts = TableQuery[T]
ts.ddl.create
ts ++= Seq(("1", "a"), ("2", "a"), ("3", "b"))
val q1 = for(t <- ts if t.a === "1" || t.a === "2") yield t
assertEquals(Set(("1", "a"), ("2", "a")), q1.run.toSet)
val q2 = for(t <- ts if (t.a =!= "1") || (t.b =!= "a")) yield t
assertEquals(Set(("2", "a"), ("3", "b")), q2.run.toSet)
// No need to test that the unexpected result is actually unexpected
// now that the compiler prints a warning about it
val q4 = for(t <- ts if t.a =!= "1" || t.b =!= "a") yield t
assertEquals(Set(("2", "a"), ("3", "b")), q4.run.toSet)
}
def testLike {
class T1(tag: Tag) extends Table[String](tag, "t1_2") {
def a = column[String]("a")
def * = a
}
val t1s = TableQuery[T1]
t1s.ddl.create
t1s ++= Seq("foo", "bar", "foobar", "foo%")
val q1 = for { t1 <- t1s if t1.a like "foo" } yield t1.a
assertEquals(List("foo"), q1.run)
val q2 = for { t1 <- t1s if t1.a like "foo%" } yield t1.a
assertEquals(Set("foo", "foobar", "foo%"), q2.run.toSet)
ifCap(rcap.likeEscape) {
val q3 = for { t1 <- t1s if t1.a.like("foo^%", '^') } yield t1.a
assertEquals(List("foo%"), q3.run)
}
}
def testSorting {
import scala.slick.lifted.{Shape, ShapeLevel, Ordered}
class T1(tag: Tag) extends Table[(String, String, String)](tag, "t1_3") {
def a = column[String]("a")
def b = column[String]("b")
def c = column[String]("c")
def * = (a, b, c)
}
val t1s = TableQuery[T1]
t1s.ddl.create
t1s ++= Seq(("a2", "b2", "c2"), ("a1", "b1", "c1"))
implicit class TupledQueryExtensionMethods[E1, E2, U1, U2, C[_]](q: Query[(E1, E2), (U1, U2), C]) {
def sortedValues(implicit ordered: (E1 => Ordered),
shape: Shape[FlatShapeLevel, E2, U2, E2]): Query[E2, U2, C] =
q.sortBy(_._1).map(_._2)
}
val q1 = (for {
t1 <- t1s
} yield t1.c -> (t1.a, t1.b)).sortedValues
assertEquals(List(("a1", "b1"), ("a2", "b2")), q1.run)
}
def testConditional {
class T1(tag: Tag) extends Table[Int](tag, "t1_conditional") {
def a = column[Int]("a")
def * = a
}
val t1s = TableQuery[T1]
t1s.ddl.create
t1s ++= Seq(1, 2, 3, 4)
val q1 = t1s.map { t1 => (t1.a, Case.If(t1.a < 3) Then 1 Else 0) }
assertEquals(Set((1, 1), (2, 1), (3, 0), (4, 0)), q1.run.toSet)
val q2 = t1s.map { t1 => (t1.a, Case.If(t1.a < 3) Then 1) }
assertEquals(Set((1, Some(1)), (2, Some(1)), (3, None), (4, None)), q2.run.toSet)
val q3 = t1s.map { t1 => (t1.a, Case.If(t1.a < 3) Then 1 If(t1.a < 4) Then 2 Else 0) }
assertEquals(Set((1, 1), (2, 1), (3, 2), (4, 0)), q3.run.toSet)
}
def testCast {
class T1(tag: Tag) extends Table[(String, Int)](tag, "t1_4") {
def a = column[String]("a")
def b = column[Int]("b")
def * = (a, b)
}
val t1s = TableQuery[T1]
t1s.ddl.create
t1s ++= Seq(("foo", 1), ("bar", 2))
val q1 = t1s.map(t1 => t1.a ++ t1.b.asColumnOf[String])
val r1 = q1.run.toSet
assertEquals(Set("foo1", "bar2"), r1)
}
def testOptionConversions {
class T1(tag: Tag) extends Table[(Int, Option[Int])](tag, "t1_optconv") {
def a = column[Int]("a")
def b = column[Option[Int]]("b")
def * = (a, b)
}
val t1s = TableQuery[T1]
t1s.ddl.create
t1s ++= Seq((1, Some(10)), (2, None))
// GetOrElse in ResultSetMapping on client side
val q1 = for { t <- t1s } yield (t.a, t.b.getOrElse(0))
assertEquals(Set((1, 10), (2, 0)), q1.run.toSet)
// GetOrElse in query on the DB side
val q2 = for { t <- t1s } yield (t.a, t.b.getOrElse(0) + 1)
assertEquals(Set((1, 11), (2, 1)), q2.run.toSet)
}
def testInitErrors {
case class Id(toInt: Int)
case class Customer(id: Id)
// Before making `shaped` and `toNode` in `TableQuery` lazy,
// putting `Tables before `A` caused a StackOverflowException
object Tables {
val as = TableQuery[A]
implicit val idMapper = MappedColumnType.base[Id, Int](_.toInt, Id)
}
class A(tag: Tag) extends Table[Customer](tag, "INIT_A") {
def id = column[Id]("ID", O.PrimaryKey, O.AutoInc)(Tables.idMapper)
def * = id <> (Customer.apply, Customer.unapply)
}
Tables.as.ddl
case class Id2(toInt: Int)
implicit val id2Mapper = null.asInstanceOf[BaseColumnType[Id2]]
class B(tag: Tag) extends Table[Id2](tag, "INIT_A") {
def id = column[Id2]("ID", O.PrimaryKey, O.AutoInc)
def * = id
}
val bs = TableQuery[B]
try {
bs.map(_.id)
bs.ddl
???
} catch {
case t: NullPointerException if (t.getMessage ne null) && (t.getMessage contains "initialization order") =>
// This is the expected error message from RelationalTableComponent.Table.column
}
try {
MappedColumnType.base[Id, Int](_.toInt, Id)(implicitly, null.asInstanceOf[BaseColumnType[Int]])
???
} catch {
case t: NullPointerException if (t.getMessage ne null) && (t.getMessage contains "initialization order") =>
// This is the expected error message from RelationalTypesComponent.MappedColumnTypeFactory.assertNonNullType
}
}
}