-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.scala
256 lines (199 loc) · 7.91 KB
/
test.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import org.scalatest._
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.time.{Seconds, Span}
import slick.mongodb.lifted.MongoDriver.api._
import slick.lifted.{ProvenShape, Tag}
import slick.mongodb.types.doc
import scala.concurrent.ExecutionContext.Implicits.global
/**
* Created by adam on 10.07.15.
*/
case class top1(x: Int, secondLevel: second, arr: IndexedSeq[Double])
case class top2(secondLevel: second)
case class second(c: Int, thirdLevel1: third)
case class third(c: Double, s: IndexedSeq[String])
class topLevel1(tags: Tag) extends Document[top1](tags, "topLevel1") {
def x1 = field[Int]("primitiveFieldFirstLevel")
def secondDoc = doc[secoundLevelDocument](tags)
def arrOfDouble = array(field[Double]("doubleArray"))
def * = (x1, secondDoc, arrOfDouble) <> (top1.tupled, top1.unapply) // tutaj chodzi o to że dokument jest nested
}
class topLevel2(tags: Tag) extends Document[top2](tags, "topLevel2") {
def secondDoc = doc[secoundLevelDocument](tags)
def * = (secondDoc) <> (top2, top2.unapply) // tutaj chodzi o to że dokument jest nested
}
class secoundLevelDocument(tag: Tag) extends SubDocument[second](tag, "secoundLevelDocument") {
def x2 = field[Int]("primitiveFieldSecundLevel")
def arrOfInt = array(field[Int]("PrimitiveFieldFourthLevelForArray"))
def thirdDoc = doc[thirdLevelDocument](tag)
def * = (x2, thirdDoc) <> (second.tupled, second.unapply)
}
class thirdLevelDocument(tag: Tag) extends SubDocument[third](tag, "thirdLevelDocument") {
def x3 = field[Double]("primitiveFieldThirdLevel11")
def x4 = array(field[String]("array of primitives"))
def * = (x3, x4) <> (third.tupled, third.unapply)
}
//class additional1(tag:Tag) extends SubDocument[fourth](tag,"add1") {
// def x4 = field[Int]("firstPrimitiveFieldFourthLevel")
// def arrOfInt = array(field[Int]("PrimitiveFieldFourthLevelForArray"))
// def * = (x4, arrOfInt) <> (fourth.tupled, fourth.unapply)
//}
//
//
//class additional2(tag:Tag) extends SubDocument[fourth](tag,"add2") {
// def x4 = field[Int]("firstPrimitiveFieldFourthLevel")
// def arrOfInt = array(field[Int]("PrimitiveFieldFourthLevelForArray"))
// def * = (x4, arrOfInt) <> (fourth.tupled, fourth.unapply)
//}
//
//case class noProj(c: Int, s: Int)
//
//
//class noProjection(tag:Tag) extends SubDocument[noProj](tag,"noProjectionDocument") {
//
// def x4 = field[Int]("noProjectionField1")
// def next =doc[fourthLevelDocument](tag)
// def arrOfInt =field[Int]("noProjectionField2")
//
// def * = (x4, arrOfInt) <> (noProj.tupled, noProj.unapply)
//}
class test extends FunSuite with BeforeAndAfter with ScalaFutures {
implicit override val patienceConfig = PatienceConfig(timeout = Span(50, Seconds))
val documentQuery1 = TableQuery[topLevel1]
val documentQuery2 = TableQuery[topLevel2]
before {
db = Database.forURL("mongodb://localhost:27017/test") // MongoDB binds to 127.0.0.1 in travis
}
var db: Database = _
test("single value insert test 1") {
val singleValueInsert = DBIO.seq(documentQuery1 +=
top1(1, second(1, third(10, IndexedSeq("John", "Smith"))), IndexedSeq(2.3, 7.8, 9.1))
)
lazy val result = (db.run(singleValueInsert)).futureValue
result
}
test("single value insert test 2") {
val singleValueInsert = DBIO.seq(documentQuery2 +=
top2(second(1, third(111, IndexedSeq("Adam", "Kozuch"))))
)
lazy val result = (db.run(singleValueInsert)).futureValue
result
}
test("multiple value insert test 1 - document top1") {
val multipleValueInsert = DBIO.seq(documentQuery1 ++=
List(
top1(100, second(5, third(10, IndexedSeq("ABC", "DEF"))), IndexedSeq(4.5, 3.0, 8.8, 1.6)),
top1(145, second(7, third(19, IndexedSeq("IJK", "LMN"))), IndexedSeq(4.5, 9.0, 3.8, 7.6))
))
lazy val result = (db.run(multipleValueInsert)).futureValue
result
}
test("multiple value insert test 2 - document top2") {
val multipleValueInsert = DBIO.seq(documentQuery2 ++=
List(
top2(second(5, third(10, IndexedSeq("AAAA", "BBB")))),
top2(second(7, third(19, IndexedSeq("CCC", "DDD"))))
))
lazy val result = (db.run(multipleValueInsert)).futureValue
result
}
test("topLevel1 whole document no conditions") {
val expected = Vector(
top1(1, second(1, third(10, IndexedSeq("John", "Smith"))), IndexedSeq(2.3, 7.8, 9.1)),
top1(100, second(5, third(10, IndexedSeq("ABC", "DEF"))), IndexedSeq(4.5, 3.0, 8.8, 1.6)),
top1(145, second(7, third(19, IndexedSeq("IJK", "LMN"))), IndexedSeq(4.5, 9.0, 3.8, 7.6)))
lazy val result = (db.run(documentQuery1.result)).futureValue
assert(result == expected)
}
test("topLevel1 query array from top level") {
val expected = Vector(
IndexedSeq(2.3, 7.8, 9.1)
, IndexedSeq(4.5, 3.0, 8.8, 1.6)
, IndexedSeq(4.5, 9.0, 3.8, 7.6))
lazy val result = (db.run(documentQuery1.map(x => x.arrOfDouble).result)).futureValue
print(result)
assert(result == expected)
}
test("topLevel1 query primitive value") {
val expected = Vector(1, 100, 145)
lazy val result = (db.run(documentQuery1.map(x => x.x1).result)).futureValue
print(result)
assert(result == expected)
}
test("topLevel1 query nested document") {
val expected = Vector(
second(1, third(10, IndexedSeq("John", "Smith"))),
second(5, third(10, IndexedSeq("ABC", "DEF"))),
second(7, third(19, IndexedSeq("IJK", "LMN"))))
lazy val result = (db.run(documentQuery1.map(x => x.secondDoc).result)).futureValue
print(result)
assert(result == expected)
}
test("topLevel1 query nested primitive array")
{
val expected = Vector(
IndexedSeq("John", "Smith"),
IndexedSeq("ABC", "DEF"),
IndexedSeq("IJK", "LMN"))
lazy val result = (db.run(documentQuery1.map(x => x.secondDoc.thirdDoc.x4).result)).futureValue
assert(result == expected)
}
test("topLevel1 checking number of documnents after filter") {
lazy val result = (db.run(documentQuery1.filter(x => x.secondDoc.x2 <= 5).map(x => x.arrOfDouble).result)).futureValue
print(result)
assert(result.length == 2)
}
test("topLevel1 query nested primitive field")
{
val expected = Vector(
10,
10,
19)
lazy val result = (db.run(documentQuery1.map(x => x.secondDoc.thirdDoc.x3).result)).futureValue
assert(result == expected)
}
test("topLevel2 whole document no conditions (chaninig case)")
{
val expected = Vector(
top2(second(1,third(111,IndexedSeq("Adam", "Kozuch")))),
top2(second(5,third(10,IndexedSeq("AAAA", "BBB")))),
top2(second(7,third(19,IndexedSeq("CCC", "DDD")))))
lazy val result = (db.run(documentQuery2.result)).futureValue
assert(result == expected)
}
test("topLevel2 query nested document")
{
val expected = Vector(
second(1,third(111,IndexedSeq("Adam", "Kozuch"))),
second(5,third(10,IndexedSeq("AAAA", "BBB"))),
second(7,third(19,IndexedSeq("CCC", "DDD"))))
lazy val result = (db.run(documentQuery2.map(x => x.secondDoc).result)).futureValue
print(result)
assert(result == expected)
}
test("topLevel2 query nested primitive field")
{
val expected = Vector(
1,
5,
7)
lazy val result = (db.run(documentQuery2.map(x => x.secondDoc.x2).result)).futureValue
assert(result == expected)
}
test("topLevel2 query nested primitive field - 2")
{
val expected = Vector(
111, 10, 19)
lazy val result = (db.run(documentQuery2.map(x => x.secondDoc.thirdDoc.x3).result)).futureValue
assert(result == expected)
}
test("topLevel2 query nested primitive array")
{
val expected = Vector(
IndexedSeq("Adam", "Kozuch"),
IndexedSeq("AAAA", "BBB"),
IndexedSeq("CCC", "DDD"))
lazy val result = (db.run(documentQuery2.map(x => x.secondDoc.thirdDoc.x4).result)).futureValue
assert(result == expected)
}
}