-
-
Notifications
You must be signed in to change notification settings - Fork 609
/
UnionTest.scala
108 lines (96 loc) · 3.48 KB
/
UnionTest.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
package com.typesafe.slick.testkit.tests
import com.typesafe.slick.testkit.util.{RelationalTestDB, AsyncTest}
class UnionTest extends AsyncTest[RelationalTestDB] {
import tdb.profile.api._
class Managers(tag: Tag) extends Table[(Int, String, String)](tag, "managers") {
def id = column[Int]("id")
def name = column[String]("name")
def department = column[String]("department")
def * = (id, name, department)
}
lazy val managers = TableQuery[Managers]
class Employees(tag: Tag) extends Table[(Int, String, Int)](tag, "employees") {
def id = column[Int]("id")
def name = column[String]("name2")
def manager = column[Int]("manager")
def * = (id, name, manager)
// A convenience method for selecting employees by department
def departmentIs(dept: String) = manager in managers.filter(_.department === dept).map(_.id)
}
lazy val employees = TableQuery[Employees]
def testBasic = {
val q1 = for(m <- managers filter { _.department === "IT" }) yield (m.id, m.name)
val q2 = for(e <- employees filter { _.departmentIs("IT") }) yield (e.id, e.name)
val q3 = (q1 union q2).sortBy(_._2.asc)
val q4 = managers.map(_.id)
val q4b = q4 union q4
val q4c = q4 union q4 union q4
seq(
(managers.schema ++ employees.schema).create,
managers ++= Seq(
(1, "Peter", "HR"),
(2, "Amy", "IT"),
(3, "Steve", "IT")
),
employees ++= Seq(
(4, "Jennifer", 1),
(5, "Tom", 1),
(6, "Leonard", 2),
(7, "Ben", 2),
(8, "Greg", 3)
),
q1.result.map(r => r.toSet shouldBe Set((2,"Amy"), (3,"Steve"))),
q2.result.map(r => r.toSet shouldBe Set((7,"Ben"), (8,"Greg"), (6,"Leonard"))),
q3.result.map(_ shouldBe List((2,"Amy"), (7,"Ben"), (8,"Greg"), (6,"Leonard"), (3,"Steve"))),
q4b.result.map(r => r.toSet shouldBe Set(1, 2, 3)),
q4c.result.map(r => r.toSet shouldBe Set(1, 2, 3))
) andFinally (managers.schema ++ employees.schema).drop
}
def testUnionWithoutProjection = {
def f (s: String) = managers filter { _.name === s}
val q = f("Peter") union f("Amy")
seq(
managers.schema.create,
managers ++= Seq(
(1, "Peter", "HR"),
(2, "Amy", "IT"),
(3, "Steve", "IT")
),
q.result.map(r => r.toSet shouldBe Set((1, "Peter", "HR"), (2, "Amy", "IT")))
) andFinally managers.schema.drop
}
def testUnionOfJoins = {
class Drinks(tag: Tag, tableName: String) extends Table[(Long, Long)](tag, tableName) {
def pk = column[Long]("pk")
def pkCup = column[Long]("pkCup")
def * = (pk, pkCup)
}
val coffees = TableQuery(new Drinks(_, "Coffee"))
val teas = TableQuery(new Drinks(_, "Tea"))
val q1 = for {
coffee <- coffees
tea <- teas if coffee.pkCup === tea.pkCup
} yield (coffee.pk, coffee.pkCup)
val q2 = for {
coffee <- coffees
tea <- teas if coffee.pkCup === tea.pkCup
} yield (tea.pk, tea.pkCup)
val q3 = q1 union q2
seq(
(coffees.schema ++ teas.schema).create,
coffees ++= Seq(
(10L, 1L),
(20L, 2L),
(30L, 3L)
),
teas ++= Seq(
(100L, 1L),
(200L, 2L),
(300L, 3L)
),
q1.result.map(r => r.toSet shouldBe Set((10L, 1L), (20L, 2L), (30L, 3L))),
q2.result.map(r => r.toSet shouldBe Set((100L, 1L), (200L, 2L), (300L, 3L))),
q3.result.map(r => r.toSet shouldBe Set((10L, 1L), (20L, 2L), (30L, 3L), (100L, 1L), (200L, 2L), (300L, 3L)))
)
}
}