/
MarshallableSpec.scala
416 lines (337 loc) · 13.2 KB
/
MarshallableSpec.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package gremlin.scala.marshallable
// specifically moved this to a separate package to verify `gremlin.scala.` imports
import gremlin.scala.{
asScalaEdge,
asScalaVertex,
id,
label,
nullable,
underlying,
Edge,
Element,
GraphAsScala,
Marshallable,
Vertex
}
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph
import org.scalatest.wordspec.AnyWordSpec
import org.scalatest.matchers.should.Matchers
import scala.collection.JavaConverters._
import shapeless.test.illTyped
case class CCSimple(s: String, i: Int)
case class MyValueClass(value: Int) extends AnyVal
case class CCWithValueClass(s: String, i: MyValueClass)
case class CCWithOption(i: Int, s: Option[String])
case class CCWithOptionValueClass(s: String, i: Option[MyValueClass])
case class CCWithOptionAnyVal(x: Option[Int], y: Option[Long])
case class CCWithList(s: String, ss: List[String], is: List[Int], ds: List[Double])
case class CCWithSet(s: String, ss: Set[String])
case class CCWithNullable(i: Int, @nullable maybeNull: String)
case class CCWithOptionId(s: String, @id id: Option[Int])
case class CCWithOptionIdNested(s: String, @id id: Option[Int], i: MyValueClass)
case class CCWithNonOptionalIdShouldFail(@id id: Int)
case class MyIdValueClass(value: Long) extends AnyVal
case class CCWithValueClassId(s: String, @id id: Option[MyIdValueClass])
case class CCWithUnderlyingVertex(@underlying underlying: Option[Vertex], s: String)
case class CCWithNonOptionalUnderlyingShouldFail(@underlying underlying: Vertex)
@label("label_a")
case class CCWithLabel(s: String)
@label("the_label")
case class ComplexCC(
s: String,
l: Long,
o: Option[String],
seq: Seq[String],
map: Map[String, String],
nested: NestedClass
) { def randomDef = ??? }
case class NestedClass(s: String)
class NoneCaseClass(s: String)
class MarshallableSpec extends AnyWordSpec with Matchers {
"marshals / unmarshals case classes".which {
"only have simple members" in new Fixture {
val cc = CCSimple("text", 12)
val v = graph + cc
val vl = graph.V(v.id).head()
vl.label shouldBe cc.getClass.getSimpleName
vl.valueMap should contain("s" -> cc.s)
vl.valueMap should contain("i" -> cc.i)
}
"contain options" should {
"map `Some[A]` to `A`" in new Fixture {
val ccWithOptionSome =
CCWithOption(Int.MaxValue, Some("optional value"))
val v = graph + ccWithOptionSome
v.toCC[CCWithOption] shouldBe ccWithOptionSome
val vl = graph.V(v.id).head()
vl.value[String]("s") shouldBe ccWithOptionSome.s.get
}
"map `None` to `null`" in new Fixture {
val ccWithOptionNone = CCWithOption(Int.MaxValue, None)
val v = graph + ccWithOptionNone
v.toCC[CCWithOption] shouldBe ccWithOptionNone
val vl = graph.V(v.id).head()
vl.keys should not contain "s" //None should be mapped to `null`
}
"handle an optional AnyVal" in new Fixture {
val ccWithOptionAnyVal = CCWithOptionAnyVal(Some(1), None)
val v = graph + ccWithOptionAnyVal
v.toCC[CCWithOptionAnyVal]
val vl = graph.V(v.id).head()
vl.value[Int]("x") shouldBe ccWithOptionAnyVal.x.get
vl.keys should not contain "y"
}
// Background: if we marshal Option types, the graph db needs to understand scala.Option,
// which wouldn't make any sense. So we rather translate it to `null` if it's `None`.
// https://github.com/mpollmeier/gremlin-scala/issues/98
}
"contain value classes" should {
"unwrap a plain value class" in new Fixture {
val cc = CCWithValueClass("some text", MyValueClass(42))
val v = graph + cc
val vl = graph.V(v.id).head()
vl.label shouldBe cc.getClass.getSimpleName
vl.valueMap should contain("s" -> cc.s)
vl.valueMap should contain("i" -> cc.i.value)
vl.toCC[CCWithValueClass] shouldBe cc
}
"unwrap an optional value class" in new Fixture {
val cc = CCWithOptionValueClass("some text", Some(MyValueClass(42)))
val v = graph + cc
val vl = graph.V(v.id).head()
vl.label shouldBe cc.getClass.getSimpleName
vl.valueMap should contain("s" -> cc.s)
vl.valueMap should contain("i" -> cc.i.get.value)
vl.toCC[CCWithOptionValueClass] shouldBe cc
}
"handle None value class" in new Fixture {
val cc = CCWithOptionValueClass("some text", None)
val v = graph + cc
val vl = graph.V(v.id).head()
vl.label shouldBe cc.getClass.getSimpleName
vl.valueMap should contain("s" -> cc.s)
vl.valueMap.keySet should not contain ("i")
vl.toCC[CCWithOptionValueClass] shouldBe cc
}
"handle value class ID" in new Fixture {
val cc = CCWithValueClassId(s = "some text", id = None)
val v = graph + cc
val underlyingId = v.id().asInstanceOf[Long]
v.toCC[CCWithValueClassId].id shouldBe Some(MyIdValueClass(underlyingId))
}
}
"handle List members" in new Fixture {
val cc = CCWithList(s = "foo", ss = List("one", "two"), is = List(1, 2), ds = Nil)
val v = graph + cc
v.toCC[CCWithList] shouldBe cc
val vl = graph.V(v.id).head()
val properties = vl.properties[String]("ss").asScala.toList
properties.size shouldBe 2
properties.map(_.value) shouldBe List("one", "two")
}
"handle Set members" in new Fixture {
val cc = CCWithSet(s = "foo", ss = Set("bar", "baz"))
val v = graph + cc
v.toCC[CCWithSet] shouldBe cc
val vl = graph.V(v.id).head()
val properties = vl.properties[String]("ss").asScala.toList
properties.size shouldBe 2
properties.map(_.value) shouldBe List("bar", "baz")
}
"allows members to be `null` if annotated with `@nullable`" in new Fixture {
val cc = CCWithNullable(i = 42, maybeNull = null)
val v = graph + cc
v.toCC[CCWithNullable] shouldBe cc
}
"define their custom marshaller" in new Fixture {
val ccWithOptionNone = CCWithOption(Int.MaxValue, None)
implicit val marshaller = new Marshallable[CCWithOption] {
import gremlin.scala.PropertyOps
def fromCC(cc: CCWithOption) =
FromCC(None, "CCWithOption", List("i" -> cc.i, "s" -> cc.s.getOrElse("undefined")))
def toCC(element: Element): CCWithOption =
CCWithOption(i = element.value[Int]("i"), s = element.property[String]("s").toOption)
}
val v = graph + ccWithOptionNone
v.toCC[CCWithOption](marshaller) shouldBe CCWithOption(ccWithOptionNone.i, Some("undefined"))
}
"combination of many things" in new Fixture {
val cc = ComplexCC(
"some string",
Long.MaxValue,
Some("option type"),
Seq("test1", "test2"),
Map("key1" -> "value1", "key2" -> "value2"),
NestedClass("nested")
)
val v = graph + cc
val vl = graph.V(v.id).head()
vl.label shouldBe "the_label"
vl.valueMap should contain("s" -> cc.s)
vl.valueMap should contain("l" -> cc.l)
vl.valueMap should contain("o" -> cc.o.get)
vl.valueMap should contain("seq" -> cc.seq)
vl.valueMap should contain("map" -> cc.map)
vl.valueMap should contain("nested" -> cc.nested)
}
"have an Option @id annotation" in new Fixture {
val cc = CCWithOptionId(s = "text", id = Some(12))
val v = graph + cc
v.toCC[CCWithOptionId].s shouldBe cc.s
val vl = graph.V(v.id).head()
vl.label shouldBe cc.getClass.getSimpleName
vl.valueMap should contain("s" -> cc.s)
}
"fails compilation for non-option @id annotation" in new Fixture {
// id must be assigned by graph (in the context of Marshallable)
illTyped {
"""
val cc = CCWithNonOptionalIdShouldFail(12)
graph + cc
"""
}
}
"have @underlying vertex" in new Fixture {
val cc = CCWithUnderlyingVertex(
underlying = None, //not known yet, not part of graph yet
"some string"
)
val vertex = graph + cc
val ccFromVertex = vertex.toCC[CCWithUnderlyingVertex]
ccFromVertex.s shouldBe cc.s
ccFromVertex.underlying shouldBe Symbol("defined")
graph.V(ccFromVertex.underlying.get.id).value[String]("s").head() shouldBe cc.s
}
"fails compilation for non-option @underlying annotation" in new Fixture {
illTyped {
"""
val cc = CCWithNonOptionalUnderlyingShouldFail(null)
graph + cc
"""
}
}
}
"find vertices by label" in new Fixture {
val ccSimple = CCSimple("a string", 42)
val ccWithOption = CCWithOption(52, Some("other string"))
val ccWithLabel = CCWithLabel("s")
graph + ccSimple
graph + ccWithOption
graph + ccWithLabel
graph.V().count().head() shouldBe 3
val ccSimpleVertices = graph.V().hasLabel[CCSimple]().toList()
(ccSimpleVertices should have).size(1)
ccSimpleVertices.head.toCC[CCSimple] shouldBe ccSimple
val ccWithLabelVertices = graph.V().hasLabel[CCWithLabel]().toList()
(ccWithLabelVertices should have).size(1)
ccWithLabelVertices.head.toCC[CCWithLabel] shouldBe ccWithLabel
}
"add edges using case-class".which {
"have no id-annotation" in new CCEdgeAddFixture {
val ccEdgeWithLabelInitial = CCWithLabel("edge-property")
ccVertexFrom.addEdge(ccVertexTo, ccEdgeWithLabelInitial).toCC[CCWithLabel]
val ccEdgesWithLabel = graph.E().hasLabel[CCWithLabel]().toList()
(ccEdgesWithLabel should have).size(1)
ccEdgesWithLabel.head.toCC[CCWithLabel] shouldBe ccEdgeWithLabelInitial
}
"have id-annotation None" in new CCEdgeAddFixture {
val ccEdgeWithOptionIdNoneInitial = CCWithOptionId("edge-property", None)
val ccEdgeWithOptionIdNone = ccVertexFrom
.addEdge(ccVertexTo, ccEdgeWithOptionIdNoneInitial)
.toCC[CCWithOptionId]
ccEdgeWithOptionIdNone.id should not be empty
val ccEdgesWithOptionIdNone = graph.E().hasLabel[CCWithOptionId]().toList()
(ccEdgesWithOptionIdNone should have).size(1)
ccEdgesWithOptionIdNone.head
.toCC[CCWithOptionId] shouldBe ccEdgeWithOptionIdNone
}
"have id-annotation Some(123)" in new CCEdgeAddFixture {
val ccEdgeWithOptionIdSomeInitial =
CCWithOptionId("edge-property", Some(123))
val ccEdgeWithOptionIdSome = ccVertexFrom
.addEdge(ccVertexTo, ccEdgeWithOptionIdSomeInitial)
.toCC[CCWithOptionId]
ccEdgeWithOptionIdSome.id shouldBe ccEdgeWithOptionIdSomeInitial.id
val ccEdgesWithOptionIdSome = graph.E().hasLabel[CCWithOptionId]().toList()
(ccEdgesWithOptionIdSome should have).size(1)
ccEdgesWithOptionIdSome.head
.toCC[CCWithOptionId] shouldBe ccEdgeWithOptionIdSome
}
}
"edge" should {
"update using a case-class template" in new CCEdgeUpdateFixture {
graph
.E(ccWithIdSet.id.get)
.head()
.updateWith(ccUpdate)
.toCC[CC] shouldBe ccUpdate
graph.E(ccWithIdSet.id.get).head().toCC[CC] shouldBe ccUpdate
}
"update as a case class" in new CCEdgeUpdateFixture {
graph
.E(ccWithIdSet.id.get)
.head()
.updateAs[CC](_.copy(s = ccUpdate.s, i = ccUpdate.i))
.toCC[CC] shouldBe ccUpdate
graph.E(ccWithIdSet.id.get).head().toCC[CC] shouldBe ccUpdate
}
}
"vertex" should {
"update using a case-class template" in new CCVertexUpdateFixture {
graph
.V(ccWithIdSet.id.get)
.head()
.updateWith(ccUpdate)
.toCC[CC] shouldBe ccUpdate
graph.V(ccWithIdSet.id.get).head().toCC[CC] shouldBe ccUpdate
}
"update as a case class" in new CCVertexUpdateFixture {
graph
.V(ccWithIdSet.id.get)
.head()
.updateAs[CC](_.copy(s = ccUpdate.s, i = ccUpdate.i))
.toCC[CC] shouldBe ccUpdate
graph.V(ccWithIdSet.id.get).head().toCC[CC] shouldBe ccUpdate
}
}
"marshals a the end step of a traversal" in new Fixture {
val cc1 = CCSimple("text one", 1)
val cc2 = CCSimple("text two", 2)
graph + cc1
graph + cc2
val results: Set[CCSimple] = graph.V().toCC[CCSimple].toSet()
results shouldBe Set(cc1, cc2)
}
case class EmptyCC()
"handle case class without elements" in new Fixture {
val m = implicitly[Marshallable[EmptyCC]]
}
trait Fixture {
val graph = TinkerGraph.open.asScala()
}
trait CCUpdateFixture[E <: Element] extends Fixture {
type CC = CCWithOptionIdNested
val ccInitial = CCWithOptionIdNested("string", None, MyValueClass(42))
def ccWithIdSet: CC
lazy val ccUpdate = ccWithIdSet.copy(s = "otherString", i = MyValueClass(7))
}
trait CCVertexUpdateFixture extends CCUpdateFixture[Vertex] {
val ccWithIdSet = (graph + ccInitial).toCC[CC]
}
trait CCEdgeAddFixture extends Fixture {
val ccVertexFrom = graph + "fromLabel"
val ccVertexTo = graph + "toLabel"
}
trait CCEdgeUpdateFixture extends CCUpdateFixture[Edge] {
private val testVertex = graph + "Huh"
val ccWithIdSet = testVertex.addEdge(testVertex, ccInitial).toCC[CC]
}
"can't persist a none product type (none case class or tuple)" in {
illTyped {
"""
val graph = TinkerGraph.open.asScala()
graph + new NoneCaseClass("test")
"""
}
}
}