-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSampleNeo4jSpec.groovy
145 lines (109 loc) · 4.25 KB
/
SampleNeo4jSpec.groovy
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
package org.neo4j.extension.spock
import org.junit.Rule
import org.neo4j.graphdb.GraphDatabaseService
import org.neo4j.graphdb.Label
import org.neo4j.graphdb.NotInTransactionException
import org.neo4j.helpers.collection.Iterables
import org.neo4j.kernel.impl.proc.Procedures
import org.neo4j.kernel.internal.GraphDatabaseAPI
import org.neo4j.graphdb.Node
import spock.lang.Specification
class SampleNeo4jSpec extends Specification {
/**
* provide Neo4j stuff: graphDatabaseService and executionEngine
*/
@Rule
@Delegate(interfaces=false)
Neo4jResource neo4jResource = new Neo4jResource( config: [execution_guard_enabled: "true"])
GraphDatabaseService dummy
def setup() {
dummy = graphDatabaseService
}
def "graphDatabaseService is available"() {
expect:
graphDatabaseService != null // N.B. due to @Delegate, we're accessing neo4jResource.graphDatabaseService
}
def "by default a feature method has no transactional context"() {
when: "trigger a write operation"
graphDatabaseService.createNode()
then:
thrown(NotInTransactionException)
}
@WithNeo4jTransaction
def "withNeo4jTransaction provides transactional"() {
expect: "empty database"
Iterables.count(graphDatabaseService.allNodes) == 0
when:
graphDatabaseService.createNode()
then:
notThrown NotInTransactionException
Iterables.count(graphDatabaseService.allNodes) == 1
}
@WithNeo4jTransaction(field = "dummy")
def "withNeo4jTransaction works with a field parameter"() {
expect: "empty database"
Iterables.count(graphDatabaseService.allNodes) == 0
when:
graphDatabaseService.createNode()
then:
notThrown NotInTransactionException
Iterables.count(graphDatabaseService.allNodes) == 1
}
def "cypher method applied to String class"() {
when:
def executionResult = "create (n) return n".cypher()
then:
executionResult[0].n instanceof Node
}
def "parameterized cypher works on String class"() {
setup:
"create (n:Person {props})".cypher(props: [name: 'Stefan'])
when:
def executionResult = "match (n:Person) where n.name={name} return n.name as name".cypher(name:'Stefan')
then:
executionResult[0].name == 'Stefan'
}
def "check long and int"() {
setup:
"create (n:Person{value:1357016400000})".cypher()
"create (n:Person{value:toInt(1)})".cypher()
when:
Neo4jUtils.withSuccessTransaction(graphDatabaseService) {
def n = graphDatabaseService.createNode(Label.label("Person"))
n.setProperty("value", 1357016400000 as int)
}
Neo4jUtils.withSuccessTransaction(graphDatabaseService) {
for (def n: graphDatabaseService.findNodes(Label.label("Person"))) {
println "id: ${n.id}, val: ${n.getProperty("value")}, type: ${n.getProperty("value").class}"
}
}
then:
true
when:
def result = "match(n:Person) where n.value>1357016300000 return n".cypher()
then:
result.size() == 1
when:
result = "match(n:Person) where n.value>1357016400001 return n".cypher()
then:
result.size() == 0
when:
result = "match(n:Person) where n.value>={v} return n".cypher(v:1357016300000)
then:
result.size() == 1
when:
result = "match(n:Person) where n.value>={v} return n".cypher(v:1357016300000L)
then:
result.size() == 1
}
def "procedures defined in non-jar parts of classpath are loaded automatically"() {
when:
def procedures = ((GraphDatabaseAPI)graphDatabaseService).dependencyResolver.resolveDependency(Procedures)
def nonSystemProcedures = procedures.allProcedures.grep { !(it.name().namespace()[0] in ["db" ,"sys"]) }
def nonSystemFunctions = procedures.allFunctions.grep { !(it.name().namespace()[0] in ["db" ,"sys"]) }
then: "some procedures from this project have been loaded"
!nonSystemProcedures.empty
and: "some functions from this project have been loaded"
!nonSystemFunctions.empty
}
}