/
InsertTest.scala
201 lines (163 loc) · 5.89 KB
/
InsertTest.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
/*
* Copyright 2013 - 2019 Outworkers Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.outworkers.phantom.builder.query.db.crud
import com.datastax.driver.core.utils.UUIDs
import com.outworkers.phantom.PhantomSuite
import com.outworkers.phantom.dsl._
import com.outworkers.phantom.tables._
import com.outworkers.util.samplers._
import org.json4s.Extraction
import org.json4s.native._
class InsertTest extends PhantomSuite {
override def beforeAll(): Unit = {
super.beforeAll()
database.listCollectionTable.createSchema()
database.primitives.createSchema()
database.oldPrimitives.createSchema()
database.optDerivedTable.createSchema()
if (session.v4orNewer) {
database.primitivesCassandra22.createSchema()
}
database.testTable.createSchema()
database.recipes.createSchema()
}
"Insert" should "work fine for primitives columns defined with the old DSL" in {
val row = gen[OldPrimitiveRecord].copy(timeuuid = UUIDs.timeBased())
val chain = for {
_ <- database.oldPrimitives.store(row).future()
one <- database.oldPrimitives.select.where(_.pkey eqs row.pkey).one
} yield one
whenReady(chain) { res =>
res shouldBe defined
}
}
"Insert" should "work fine for primitives columns" in {
val row = gen[PrimitiveRecord]
val chain = for {
_ <- database.primitives.store(row).future()
one <- database.primitives.select.where(_.pkey eqs row.pkey).one
} yield one
whenReady(chain) { res =>
res shouldBe defined
}
}
if (session.v4orNewer) {
"Insert" should "work fine for primitives cassandra 2.2 columns" in {
val row = gen[PrimitiveCassandra22]
val chain = for {
_ <- database.primitivesCassandra22.store(row).future()
one <- database.primitivesCassandra22.select.where(_.pkey eqs row.pkey).one
} yield one
whenReady(chain) { res =>
res shouldBe defined
}
}
}
it should "insert strings with single quotes inside them and automatically escape them" in {
val row = gen[TestRow].copy(key = "test'", mapIntToInt = Map.empty[Int, Int])
val chain = for {
_ <- database.testTable.store(row).future()
one <- database.testTable.select.where(_.key eqs row.key).one
} yield one
whenReady(chain) { res =>
res.value shouldEqual row
}
}
it should "work fine with List, Set, Map" in {
val row = gen[TestRow].copy(mapIntToInt = Map.empty)
val chain = for {
_ <- database.testTable.store(row).future()
one <- database.testTable.select.where(_.key eqs row.key).one
} yield one
whenReady(chain) { res =>
res.value shouldEqual row
}
}
it should "work fine with a mix of collection types in the table definition" in {
val recipe = gen[Recipe]
val chain = for {
_ <- database.recipes.store(recipe).future()
one <- database.recipes.select.where(_.url eqs recipe.url).one
} yield one
whenReady(chain) { res =>
res shouldBe defined
res.value.url shouldEqual recipe.url
res.value.description shouldEqual recipe.description
res.value.props shouldEqual recipe.props
res.value.lastCheckedAt shouldEqual recipe.lastCheckedAt
res.value.ingredients shouldEqual recipe.ingredients
res.value.servings shouldEqual recipe.servings
}
}
it should "support serializing/de-serializing empty lists " in {
val row = gen[MyTestRow].copy(stringlist = List.empty)
val chain = for {
_ <- database.listCollectionTable.store(row).future()
one <- database.listCollectionTable.select.where(_.key eqs row.key).one
} yield one
whenReady(chain) { res =>
res.value shouldEqual row
res.value.stringlist.isEmpty shouldEqual true
}
}
it should "support serializing/de-serializing to List " in {
val row = gen[MyTestRow]
val chain = for {
_ <- database.listCollectionTable.store(row).future()
get <- database.listCollectionTable.select.where(_.key eqs row.key).one
} yield get
whenReady(chain) { res =>
res.value shouldEqual row
}
}
it should "serialize a JSON clause as the insert part" in {
val sample = gen[Recipe]
val chain = for {
_ <- database.recipes.insert.json(compactJson(renderJValue(Extraction.decompose(sample)))).future()
get <- database.recipes.select.where(_.url eqs sample.url).one()
} yield get
if (cassandraVersion.value >= Version.`2.2.0`) {
whenReady(chain) { res =>
res shouldBe defined
res.value shouldEqual sample
}
} else {
chain.failed.futureValue
}
}
it should "correctly insert a record with an Option wrapped primitive type with a None" in {
val sample = gen[OptTypesRecord].copy(wrapped = None)
val chain = for {
_ <- database.optDerivedTable.store(sample).future()
res <- database.optDerivedTable.select.where(_.pkey eqs sample.pkey).one()
} yield res
whenReady(chain) { res =>
res shouldBe defined
res.value shouldEqual sample
}
}
it should "correctly insert a record with an Option wrapped primitive type" in {
val sample = gen[OptTypesRecord]
val chain = for {
_ <- database.optDerivedTable.store(sample).future()
res <- database.optDerivedTable.select.where(_.pkey eqs sample.pkey).one()
} yield res
whenReady(chain) { res =>
res shouldBe defined
res.value shouldEqual sample
}
}
}