-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CallProcedureAcceptanceTest.scala
309 lines (242 loc) · 8.39 KB
/
CallProcedureAcceptanceTest.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
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.internal.cypher.acceptance
import org.neo4j.collection.RawIterator
import org.neo4j.cypher.{CypherExecutionException, CypherTypeException, ExecutionEngineFunSuite, InvalidArgumentException}
import org.neo4j.kernel.api.KernelAPI
import org.neo4j.kernel.api.exceptions.ProcedureException
import org.neo4j.kernel.api.proc.Procedure.Context
import org.neo4j.kernel.api.proc.ProcedureSignature.procedureSignature
import org.neo4j.kernel.api.proc.{Neo4jTypes}
import org.neo4j.kernel.api.proc.Procedure.BasicProcedure
class CallProcedureAcceptanceTest extends ExecutionEngineFunSuite {
test("should be able to find labels from built-in-procedure") {
// Given
createLabeledNode("A")
createLabeledNode("B")
createLabeledNode("C")
//When
val result = execute("CALL sys.db.labels")
// Then
result.toList should equal(
List(
Map("label" -> "A"),
Map("label" -> "B"),
Map("label" -> "C")))
}
test("sys.db.labels work on an empty database") {
// Given an empty database
//When
val result = execute("CALL sys.db.labels")
// Then
result.toList shouldBe empty
}
test("sys.db.labels should be empty when all labels are removed") {
// Given
createLabeledNode("A")
execute("MATCH (a:A) REMOVE a:A")
//When
val result = execute("CALL sys.db.labels")
// Then
result shouldBe empty
}
test("sys.db.labels should be empty when all nodes are removed") {
// Given
createLabeledNode("A")
execute("MATCH (a) DETACH DELETE a")
//When
val result = execute("CALL sys.db.labels")
// Then
result shouldBe empty
}
test("should be able to find types from built-in-procedure") {
// Given
relate(createNode(), createNode(), "A")
relate(createNode(), createNode(), "B")
relate(createNode(), createNode(), "C")
// When
val result = execute("CALL sys.db.relationshipTypes")
// Then
result.toList should equal(
List(
Map("relationshipType" -> "A"),
Map("relationshipType" -> "B"),
Map("relationshipType" -> "C")))
}
test("sys.db.relationshipType work on an empty database") {
// Given an empty database
//When
val result = execute("CALL sys.db.relationshipTypes")
// Then
result shouldBe empty
}
test("sys.db.relationshipTypes should be empty when all relationships are removed") {
// Given
// Given
relate(createNode(), createNode(), "A")
relate(createNode(), createNode(), "B")
relate(createNode(), createNode(), "C")
execute("MATCH (a) DETACH DELETE a")
//When
val result = execute("CALL sys.db.relationshipTypes")
// Then
result shouldBe empty
}
test("should be able to find propertyKeys from built-in-procedure") {
// Given
createNode("A" -> 1, "B" -> 2, "C" -> 3)
// When
val result = execute("CALL sys.db.propertyKeys")
// Then
result.toList should equal(
List(
Map("propertyKey" -> "A"),
Map("propertyKey" -> "B"),
Map("propertyKey" -> "C")))
}
test("sys.db.propertyKeys works on an empty database") {
// Given an empty database
// When
val result = execute("CALL sys.db.propertyKeys")
// Then
result shouldBe empty
}
test("removing properties from nodes and relationships does not remove them from the store") {
// Given
relate(createNode("A" -> 1), createNode("B" -> 1), "R" ->1)
execute("MATCH (a)-[r]-(b) REMOVE a.A, r.R, b.B")
// When
val result = execute("CALL sys.db.propertyKeys")
// Then
result.toList should equal(
List(
Map("propertyKey" -> "A"),
Map("propertyKey" -> "B"),
Map("propertyKey" -> "R")))
}
test("removing all nodes and relationship does not remove properties from the store") {
// Given
relate(createNode("A" -> 1), createNode("B" -> 1), "R" ->1)
execute("MATCH (a) DETACH DELETE a")
println(execute("MATCH (a) RETURN count(a)").toList)
// When
val result = execute("CALL sys.db.propertyKeys")
// Then
result.toList should equal(
List(
Map("propertyKey" -> "A"),
Map("propertyKey" -> "B"),
Map("propertyKey" -> "R")))
}
test("should be able to call procedure with explicit arguments") {
// Given
register(Neo4jTypes.NTString, Neo4jTypes.NTNumber)
// When
val result = execute("CALL my.first.proc('42', 42)")
// Then
result.toList should equal(List(Map("out0" -> "42", "out1" -> 42)))
}
test("should be able to call procedure with implicit arguments") {
// Given
register(Neo4jTypes.NTString, Neo4jTypes.NTNumber)
// When
val result = execute("CALL my.first.proc", "in0" -> "42", "in1" -> 42)
// Then
result.toList should equal(List(Map("out0" -> "42", "out1" -> 42)))
}
test("should fail if input type is wrong") {
// Given
register(Neo4jTypes.NTNumber)
// Then
a [CypherTypeException] shouldBe thrownBy(execute("CALL my.first.proc('ten')"))
}
test("if signature declares number all number types are valid") {
// Given
register(Neo4jTypes.NTNumber)
// Then
execute("CALL my.first.proc(42)").toList should equal(List(Map("out0" -> 42)))
execute("CALL my.first.proc(42.3)").toList should equal(List(Map("out0" -> 42.3)))
}
test("arguments are nullable") {
// Given
register(Neo4jTypes.NTNumber)
// Then
execute("CALL my.first.proc(NULL)").toList should equal(List(Map("out0" -> null)))
}
test("should fail a procedure declares an integer but gets a float ") {
// Given
register(Neo4jTypes.NTInteger)
// Then
a [CypherTypeException] shouldBe thrownBy(execute("CALL my.first.proc(42.0)"))
}
test("should fail a procedure declares a float but gets an integer") {
// Given
register(Neo4jTypes.NTFloat)
// Then
a [CypherTypeException] shouldBe thrownBy(execute("CALL my.first.proc(42)"))
}
test("should fail if explicit argument is missing") {
// Given
register(Neo4jTypes.NTString, Neo4jTypes.NTNumber)
// Then
an [InvalidArgumentException] shouldBe thrownBy(execute("CALL my.first.proc('ten')"))
}
test("should fail if too many arguments") {
// Given
register(Neo4jTypes.NTString, Neo4jTypes.NTNumber)
// Then
an [InvalidArgumentException] shouldBe thrownBy(execute("CALL my.first.proc('ten', 10, 42)"))
}
test("should fail if implicit argument is missing") {
// Given
register(Neo4jTypes.NTString, Neo4jTypes.NTNumber)
// Then
an [InvalidArgumentException] shouldBe thrownBy(execute("CALL my.first.proc", "in0" -> "42", "in42" -> 42))
}
test("should be able to call a procedure with explain") {
// Given
register(Neo4jTypes.NTNumber)
// When
val result = execute("EXPLAIN CALL my.first.proc(42)")
// Then
result shouldBe empty
}
test("should fail if calling non-existent procedure") {
a [CypherExecutionException] shouldBe thrownBy(execute("CALL no.such.thing.exists(42)"))
}
private def register(types: Neo4jTypes.AnyType*) = {
val builder = procedureSignature(Array("my", "first"), "proc")
for (i <- types.indices) {
builder
.in(s"in$i", types(i))
.out(s"out$i", types(i))
}
val proc = new BasicProcedure(builder.build) {
override def apply(ctx: Context, input: Array[AnyRef]): RawIterator[Array[AnyRef], ProcedureException] =
RawIterator.of[Array[AnyRef], ProcedureException](input)
}
kernel.registerProcedure(proc)
}
override protected def initTest() {
super.initTest()
kernel = graph.getDependencyResolver.resolveDependency(classOf[KernelAPI])
}
private var kernel: KernelAPI = null
}