-
Notifications
You must be signed in to change notification settings - Fork 75
/
DslSpec.scala
239 lines (192 loc) · 7.2 KB
/
DslSpec.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
package gremlin.scala.dsl
import gremlin.scala._
import java.util.{Map => JMap}
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerFactory
import org.scalatest.wordspec.AnyWordSpec
import org.scalatest.matchers.should.Matchers
import scala.collection.mutable
import scala.collection.JavaConverters._
import shapeless._
class DslSpec extends AnyWordSpec with Matchers {
import TestDomain._
"finds all persons" in {
val personSteps = PersonSteps(TinkerFactory.createModern)
personSteps.toSet() shouldBe Set(
Person(Some(1), "marko", 29),
Person(Some(2), "vadas", 27),
Person(Some(4), "josh", 32),
Person(Some(6), "peter", 35)
)
}
"label with `as` and typesafe `select` of domain types" should {
"select all labelled steps by default" in {
implicit val graph = TinkerFactory.createModern
val personAndSoftware: List[(Person, Software)] =
PersonSteps(graph)
.as("person")
.created
.as("software")
.select()
.toList()
(personAndSoftware should have).size(4)
val softwareByCreator: Map[String, Software] = personAndSoftware.map {
case (person, software) => (person.name, software)
}.toMap
softwareByCreator("marko") shouldBe Software("lop", "java")
}
"allow to select one labelled step only" in {
implicit val graph = TinkerFactory.createModern
val labelPerson = StepLabel[Person]("p")
val labelSoftware = StepLabel[Software]("s")
val personAndSoftware: Set[Software] =
PersonSteps(graph)
.as(labelPerson)
.created
.as(labelSoftware)
.select(labelSoftware)
.toSet()
personAndSoftware shouldBe Set(Software("lop", "java"), Software("ripple", "java"))
}
"allow to select multiple labelled steps" in {
implicit val graph = TinkerFactory.createModern
val labelPerson = StepLabel[Person]("p")
val labelSoftware = StepLabel[Software]("s")
val personAndSoftware: List[(Software, Person)] =
PersonSteps(graph)
.as(labelPerson)
.created
.as(labelSoftware)
.select((labelSoftware, labelPerson))
.toList()
(personAndSoftware should have).size(4)
val softwareByCreator: Map[String, Software] = personAndSoftware.map {
case (software, person) => (person.name, software)
}.toMap
softwareByCreator("marko") shouldBe Software("lop", "java")
}
}
"finds combination of person/software in for comprehension" in {
implicit val graph = TinkerFactory.createModern
val traversal = for {
person <- PersonSteps(graph)
software <- person.created
} yield (person.name, software)
val tuples = traversal.toSet() shouldBe Set(
("marko", Software("lop", "java")),
("josh", Software("lop", "java")),
("peter", Software("lop", "java")),
("josh", Software("ripple", "java"))
)
}
"filter with traversal on domain type" when {
"domain type is a case class" in {
val ripples = PersonSteps(TinkerFactory.createModern)
.filter(_.created.isRipple)
ripples.toList() shouldBe List(
Person(Some(4), "josh", 32)
)
}
}
"filterNot with traversal on domain type" in {
val notRipple = PersonSteps(TinkerFactory.createModern)
.filterNot(_.created.isRipple)
notRipple.toList().size shouldBe 3
}
"filter on domain type" in {
val markos: List[Person] =
PersonSteps(TinkerFactory.createModern)
.filterOnEnd(_.name == "marko")
.toList()
markos.size shouldBe 1
}
"aggregate intermediary results into a collection" in {
val allPersons = mutable.ArrayBuffer.empty[Person]
val markos: List[Person] =
PersonSteps(TinkerFactory.createModern)
.aggregate(allPersons)
.filterOnEnd(_.name == "marko")
.toList()
markos.size shouldBe 1
allPersons.size should be > 1
}
"allow side effects" in {
var i = 0
PersonSteps(TinkerFactory.createModern).sideEffect(_ => i = i 1).iterate()
i shouldBe 4
}
"deduplicates" in {
val results: List[Person] =
PersonSteps(TinkerFactory.createModern).created.createdBy.dedup().toList()
results.size shouldBe 3
}
"allows to use underlying gremlin-scala steps" in {
val steps: PersonSteps[_] =
PersonSteps(TinkerFactory.createModern)
.onRaw(_.hasId(1: Integer))
steps.toList().size shouldBe 1
}
"traverses from person to software" in {
val personSteps =
PersonSteps(TinkerFactory.createModern)
.onRaw(_.hasId(1: Integer))
personSteps.created.toSet() shouldBe Set(Software("lop", "java"))
}
"supports collections in map/flatMap" when {
implicit val graph = TinkerFactory.createModern
def personSteps = PersonSteps(graph)
"using List" in {
val query = personSteps.map { person =>
(person.name, person.created.toList())
}
val results: List[(String, List[Software])] = query.toList()
results.size shouldBe 4
}
"using Set" in {
val query = personSteps.map { person =>
(person.name, person.created.toSet())
}
val results: List[(String, Set[Software])] = query.toList()
results.size shouldBe 4
}
}
"allows to be cloned" in {
val graph = TinkerFactory.createModern
def personSteps = PersonSteps(graph)
val query = personSteps.hasName("marko")
val queryCloned = query.clone()
query.toList().size shouldBe 1
queryCloned.toList().size shouldBe 1
}
}
object TestDomain {
@label("person") case class Person(@id id: Option[Integer], name: String, age: Integer)
extends DomainRoot
@label("software") case class Software(name: String, lang: String) extends DomainRoot
object PersonSteps {
def apply(graph: Graph) = new PersonSteps[HNil](graph.V().hasLabel[Person]())
}
class PersonSteps[Labels <: HList](override val raw: GremlinScala[Vertex])
extends NodeSteps[Person, Labels](raw) {
def created = new SoftwareSteps[Labels](raw.out("created"))
def name =
new Steps[String, String, Labels](raw.map(_.value[String]("name")))
def hasName(name: String) =
new PersonSteps[Labels](raw.has(Key("name") -> name))
}
class SoftwareSteps[Labels <: HList](override val raw: GremlinScala[Vertex])
extends NodeSteps[Software, Labels](raw) {
def createdBy = new PersonSteps[Labels](raw.in("created"))
def isRipple = new SoftwareSteps[Labels](raw.has(Key("name") -> "ripple"))
}
implicit def toPersonSteps[Labels <: HList](
steps: Steps[Person, Vertex, Labels]): PersonSteps[Labels] =
new PersonSteps[Labels](steps.raw)
implicit def personStepsConstructor[Labels <: HList]
: Constructor.Aux[Person, Labels, Vertex, PersonSteps[Labels]] =
Constructor.forDomainNode[Person, Labels, PersonSteps[Labels]](new PersonSteps[Labels](_))
implicit def softwareStepsConstructor[Labels <: HList]
: Constructor.Aux[Software, Labels, Vertex, SoftwareSteps[Labels]] =
Constructor.forDomainNode[Software, Labels, SoftwareSteps[Labels]](new SoftwareSteps[Labels](_))
implicit def liftPerson(person: Person)(implicit graph: Graph): PersonSteps[HNil] =
new PersonSteps[HNil](graph.asScala().V(person.id.get))
}