-
Notifications
You must be signed in to change notification settings - Fork 5
/
cypher_create_spec.rb
279 lines (232 loc) · 10.1 KB
/
cypher_create_spec.rb
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
require 'spec_helper'
describe "Neo4j::Cypher" do
describe 'DELETE' do
describe "node(4).del" do
it do
Proc.new do
node(4).del
end.should be_cypher(%Q[START v1=node(4) DELETE v1])
end
end
describe "node(3) > rel('r').del > node.del" do
it do
Proc.new do
node(3) > rel('r').del > node.del
end.should be_cypher("START v1=node(3) MATCH (v1)-[r]->(v2) DELETE r,v2")
end
end
end
describe 'CREATE' do
describe 'node.new' do
describe "node.new" do
it do
Proc.new do
node.new
end.should be_cypher(%Q[CREATE (v1) RETURN v1])
end
end
describe "node.new(:name => 'Andres', :age => 42)" do
it do
Proc.new do
node.new(:name => 'Andres', :age => 42)
end.should be_cypher("CREATE (v1 {name : 'Andres', age : 42}) RETURN v1", "CREATE (v1 {age : 42, name : 'Andres'}) RETURN v1")
end
end
describe "node.new(:name => 'Andres').as(:a); :a" do
it do
Proc.new do
node.new(:name => 'Andres').as(:a)
:a
end.should be_cypher(%Q[CREATE (a {name : 'Andres'}) RETURN a])
end
end
describe "node.new(:_name => 'Andres').as(:a); :a" do
it do
Proc.new do
node.new(:_name => 'Andres').as(:a) # Notice, no "" around the string !
:a
end.should be_cypher(%Q[CREATE (a {name : Andres}) RETURN a])
end
end
# Label support
describe "node.new({}, :person).as(:a); :a" do
it do
Proc.new do
node.new({}, :person).as(:a) # Notice, no "" around the string !
:a
end.should be_cypher(%Q[CREATE (a:`person`) RETURN a])
end
end
describe "node.new({}, :person, :friend).as(:a); :a" do
it do
Proc.new do
node.new({}, :person, :friend).as(:a) # Notice, no "" around the string !
:a
end.should be_cypher(%Q[CREATE (a:`person`:`friend`) RETURN a])
end
end
describe "node.new({name: 'kalle'}, :person, :friend).as(:a); :a" do
it do
Proc.new do
node.new({name: 'kalle'}, :person, :friend).as(:a) # Notice, no "" around the string !
:a
end.should be_cypher(%Q[CREATE (a:`person`:`friend` {name : 'kalle'}) RETURN a])
end
end
end
describe 'create_path' do
describe "create_path{node(1) > rel(:friends) > node(2)}" do
it do
Proc.new do
create_path { node(1) > rel(:friends) > node(2) }.as(:new_node)
end.should be_cypher(%Q[START v1=node(1),v2=node(2) CREATE new_node = (v1)-[:`friends`]->(v2) RETURN new_node])
end
end
describe "n2 = node(2); node(1).create_path{|n| n > rel(:friends) > node(2) }" do
it do
Proc.new do
n2 = node(2)
node(1).create_path { |n| n > rel(:friends) > n2 }
end.should be_cypher(%Q[START v2=node(2),v1=node(1) WITH v1 CREATE (v1)-[:`friends`]->(v2)])
end
end
describe "node(1).create_path{|n| n > rel(:friends) > node(2) }" do
it do
Proc.new do
node(1).create_path { |n| n > rel(:friends) > node(2) }
end.should be_cypher(%Q[START v1=node(1),v2=node(2) WITH v1 CREATE (v1)-[:`friends`]->(v2)])
end
end
describe "node(1).create_path{|n| n > rel(:friends) > node(2) }" do
it do
Proc.new do
node(1).create_path { |n| n > "f:friends" > node(2) }; :f
end.should be_cypher(%Q[START v1=node(1),v2=node(2) WITH v1 CREATE (v1)-[f:friends]->(v2) RETURN f])
end
end
describe "node(1).create_path{|n| n > rel(:friends) > node.new(:name => 'Andreas') }" do
it do
Proc.new do
node(1).create_path { |n| n > rel(:friends) > node.new(:name => 'Andreas') }
end.should be_cypher(%Q[START v1=node(1) WITH v1 CREATE (v1)-[:`friends`]->(v2 {name : 'Andreas'})])
end
end
describe %Q[a = node(1).as(:a); b = node(2).as(:b); create_path { a > rel(:friends, :_name => "a.name + '<->' + b.name") > b }] do
it do
Proc.new do
a = node(1).as(:a)
b = node(2).as(:b)
create_path { a > rel(:friends, :_name => "a.name + '<->' + b.name") > b }
end.should be_cypher(%Q[START a=node(1),b=node(2) CREATE v1 = (a)-[:`friends` {name : a.name + '<->' + b.name}]->(b) RETURN v1])
end
end
describe "create_path{node.new(:name => 'Andres') > rel(:WORKS_AT) > node < rel(:WORKS_AT) < node.new(:name => 'Micahel')}" do
it do
Proc.new do
create_path { node.new(:name => 'Andres') > rel(:WORKS_AT) > node < rel(:WORKS_AT) < node.new(:name => 'Micahel') }
end.should be_cypher(%Q[CREATE v4 = (v1 {name : 'Andres'})-[:`WORKS_AT`]->(v2)<-[:`WORKS_AT`]-(v3 {name : 'Micahel'}) RETURN v4])
end
end
describe "node(2) > :knows > node(:other).create_path { |other| other > :like > :new_person }" do
it do
Proc.new do
node(2) > :knows > node(:other).create_path { |other| other > :like > :new_person }
end.should be_cypher(%Q[START v1=node(2) MATCH (v1)-[:`knows`]->(other) WITH other CREATE (other)-[:`like`]->(new_person)])
end
end
describe "node(2).as(:morpheus) > :knows > node(:other).create_path(:morpheus) { |other, morpheus| other > :like > morpheus }" do
it do
Proc.new do
node(2).as(:morpheus) > :knows > node(:other).create_path(:morpheus) { |other, morpheus| other > :like > morpheus }
end.should be_cypher("START morpheus=node(2) MATCH (morpheus)-[:`knows`]->(other) WITH other,morpheus CREATE (other)-[:`like`]->(morpheus)")
end
end
describe "node(1).outgoing(:knows).create_path { |other| other > rel(:works) > node(:newfoo) }.outgoing(:loves)" do
it do
Proc.new do
node(1).outgoing(:knows).create_path { |other| other > rel(:works) > node(:newfoo) }.outgoing(:loves)
end.should be_cypher(%Q[START v2=node(1) MATCH (v2)-[:`knows`]->(v1),(v1)-[:`loves`]->(v3) WITH v1 CREATE (v1)-[:`works`]->(newfoo)])
end
end
describe "node(1) > rel(:knows) > node(:other).create_path{ |other| other > rel(:works) > node } > rel(:loves) > node(2)" do
it do
Proc.new do
node(1) > rel(:KNOWS) > node(:other).create_path { |other| other > rel(:works) > node } > rel(:KNOWS) > node(2).create_path(node(:other)) { |_, t| node > rel(:friends) > t }; :other
end.should be_cypher(%Q[START v4=node(1),v2=node(2) MATCH (v4)-[:`KNOWS`]->(other)-[:`KNOWS`]->(v2) WITH other CREATE (other)-[:`works`]->(v1),v2,other CREATE (v3)-[:`friends`]->(other) RETURN other])
end
end
end
describe 'create unique' do
describe "node(1).create_unique_path { |other| other > rel(' r :KNOWS ').ret > node(3, 4) }" do
it do
Proc.new do
node(1).create_unique_path { |other| other > rel('r:KNOWS').ret > node(3, 4) }
end.should be_cypher(%Q[START v1=node(1),v2=node(3,4) WITH v1 CREATE UNIQUE (v1)-[r:KNOWS]->(v2) RETURN r])
end
end
describe "create_unique_path(node(1)) { |other| other > rel('r:KNOWS').ret > node(3, 4) }" do
it do
Proc.new do
create_unique_path(node(1)) { |other| other > rel('r:KNOWS').ret > node(3, 4) }
end.should be_cypher(%Q[START v1=node(1),v2=node(3,4) WITH v1 CREATE UNIQUE (v1)-[r:KNOWS]->(v2) RETURN r])
end
end
end
describe 'set' do
describe "node(2).tap { |n| n.set_label(:person) }" do
it do
Proc.new { node(2).tap { |n| n.set_label(:person) } }.should be_cypher('START v1=node(2) SET v1 :person')
end
end
describe "node(2).tap { |n| n.set_label('Person', 'Friend') }" do
it do
Proc.new { node(2).tap { |n| n.set_label('Person', 'Friend') } }.should be_cypher('START v1=node(2) SET v1 :Person:Friend')
end
end
describe "node(2).tap { |n| n.del_label(:person) }" do
it do
Proc.new { node(2).tap { |n| n.del_label(:person) } }.should be_cypher('START v1=node(2) REMOVE v1 :person')
end
end
describe "node(2).tap { |n| n.del_label(:person, :friend) }" do
it do
Proc.new { node(2).tap { |n| n.del_label(:person, :friend) } }.should be_cypher('START v1=node(2) REMOVE v1 :person:friend')
end
end
describe "node(2).tap{|n| n[:surname] = 'Taylor'}" do
it do
Proc.new { node(2).tap { |n| n[:surname] = 'Taylor' } }.should be_cypher('START v1=node(2) SET v1.surname = "Taylor"')
end
end
describe "n=node(2); n[:surname] = 'Taylor'; ret(n)" do
it do
Proc.new { n=node(2); n[:surname] = 'Taylor'; ret(n) }.should be_cypher('START v1=node(2) SET v1.surname = "Taylor" RETURN v1')
end
end
# Remove property
describe "node(2).tap{|n| n[:surname] = nil}" do
it do
Proc.new { node(2).tap { |n| n[:surname] = nil } }.should be_cypher('START v1=node(2) SET v1.surname = NULL')
end
end
describe "node(2).ret[:surname] = 'Taylor'" do
it do
Proc.new { node(2).ret[:surname] = 'Taylor' }.should be_cypher('START v1=node(2) SET v1.surname = "Taylor" RETURN v1')
end
end
describe "node(2) >> node.ret.tap{|n| n[:surname] = 'Taylor'}" do
it do
Proc.new { node(2) >> node.ret.tap { |n| n[:surname] = 'Taylor' } }.should be_cypher('START v1=node(2) MATCH (v1)-->(v2) SET v2.surname = "Taylor" RETURN v2')
end
end
end
describe 'foreach' do
describe "(node(2) > rel > node(1)).nodes.foreach {|n| n[:marked] = true}" do
it do
Proc.new { (node(2) > rel > node(1)).nodes.foreach {|n| n[:marked] = true}}.should \
be_cypher "START v2=node(2),v3=node(1) MATCH v1 = (v2)-[?]->(v3) FOREACH (x in nodes(v1) : SET x.marked = true) RETURN nodes(v1)"
end
end
end
end
end