/
node_mixin_spec.rb
319 lines (248 loc) · 6.11 KB
/
node_mixin_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
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
$LOAD_PATH << File.expand_path(File.dirname(__FILE__) + "/../../lib")
$LOAD_PATH << File.expand_path(File.dirname(__FILE__) + "/..")
require 'neo4j'
require 'neo4j/spec_helper'
# ----------------------------------------------------------------------------
# initialize
#
describe 'NodeMixin#initialize' do
before(:all) do
start
end
before(:each) do
Neo4j::Transaction.new
end
after(:each) do
Neo4j::Transaction.finish
end
it "should accept no arguments" do
class TestNode1
include Neo4j::NodeMixin
end
TestNode1.new
end
it "should allow to initialize itself" do
# given an initialize method
class TestNode2
include Neo4j::NodeMixin
attr_reader :foo
property :baaz
def initialize(baaz)
super
@foo = "bar"
self.baaz = baaz
end
end
# when
n = TestNode2.new('hajhaj')
# then
n.foo.should == 'bar'
n[:baaz].should == 'hajhaj'
end
it "should allow arguments for the initialize method" do
class TestNode3
include Neo4j::NodeMixin
attr_reader :foo
def initialize(value)
@foo = value
end
end
n = TestNode3.new 'hi'
n.foo.should == 'hi'
end
it "should allow to create a node from a native Neo Java object" do
class TestNode4
include Neo4j::NodeMixin
end
node1 = TestNode4.new
node2 = TestNode4.new(node1.internal_node)
node1.internal_node.should == node2.internal_node
end
end
describe 'NodeMixin properties' do
before(:all) do
start
end
before(:each) do
Neo4j::Transaction.new
end
after(:each) do
Neo4j::Transaction.finish
end
it "should behave like a hash" do
n = Neo4j::Node.new
n[:a] = 'a'
n[:a].should == 'a'
n['foo'] = 42
n['foo'].should == 42
n[34] = true
n[34].should == true
end
it "should allow to update properties" do
n = Neo4j::Node.new
n[:a] = 'a'
n[:a] = 'b'
n[:a].should == 'b'
end
end
# ----------------------------------------------------------------------------
# update
#
describe 'NodeMixin#update' do
before(:all) do
class TestNode
include Neo4j::NodeMixin
property :name, :age
end
start
end
before(:each) do
Neo4j::Transaction.new
end
after(:each) do
Neo4j::Transaction.finish
end
it "should be able to update a node from a value obejct" do
# given
t = TestNode.new
t[:name]='kalle'
t[:age]=2
vo = t.value_object
t2 = TestNode.new
t2[:name] = 'foo'
# when
t2.update(vo)
# then
t2[:name].should == 'kalle'
t2[:age].should == 2
end
it "should be able to update a node by using a hash even if the keys in the hash is not a declarared property" do
t = TestNode.new
t.update({:name=>'123', :oj=>'hoj'})
t.name.should == '123'
t.age.should == nil
t['oj'].should == 'hoj'
end
it "should be able to update a node by using a hash" do
t = TestNode.new
t.update({:name=>'andreas', :age=>3})
t.name.should == 'andreas'
t.age.should == 3
end
it "should not allow the classname to be changed" do
t = TestNode.new
t.update({:classname => 'wrong'})
t.classname.should == 'TestNode'
end
it "should not allow the id to be changed" do
t = TestNode.new
t.update({:id => 987654321})
t.props['id'].should == t.neo_node_id
end
end
# ----------------------------------------------------------------------------
# equality ==
#
describe 'NodeMixin#equality (==)' do
before(:all) do
start
NODES = 5
@nodes = []
Neo4j::Transaction.run do
NODES.times { @nodes << Neo4j::Node.new}
end
end
before(:each) do
Neo4j::Transaction.new
end
after(:each) do
Neo4j::Transaction.finish
end
it "should be == another node only if it has the same node id" do
node = Neo4j::Node.new(@nodes[0].internal_node)
node.internal_node.should be_equal(@nodes[0].internal_node)
node.should == @nodes[0]
node.hash.should == @nodes[0].hash
end
it "should not be == another node only if it has not the same node id" do
node = Neo4j::Node.new(@nodes[1].internal_node)
node.internal_node.should_not be_equal(@nodes[0].internal_node)
node.should_not == @nodes[0]
node.hash.should_not == @nodes[0].hash
end
end
# ----------------------------------------------------------------------------
# delete
#
describe "Neo4j::Node#delete" do
before(:all) do
start
end
before(:each) do
Neo4j::Transaction.new
end
after(:each) do
Neo4j::Transaction.finish
end
it "should delete all relationships as well" do
# given
t1 = Neo4j::Node.new
t2 = Neo4j::Node.new
t2.relationships.outgoing(:friends) << t1
t2.relationships.both(:friends).nodes.to_a.should include(t1)
# when
t1.delete
# then
t2.relationships.both(:friends).nodes.to_a.should_not include(t1)
end
end
# ----------------------------------------------------------------------------
# props
#
describe "Neo4j::Node#props" do
before(:all) do
start
undefine_class :TestNode
class TestNode
include Neo4j::NodeMixin
property :name
property :age
end
end
after(:all) do
stop
end
it "should only contain id and classname on a node with no properties" do
t1 = TestNode.new
p = t1.props
p.keys.should include('id')
p.keys.should include('classname')
p['id'].should == t1.neo_node_id
p['classname'].should == 'TestNode'
p.keys.size.should == 2
end
it "should be okay to call props on a loaded node with no properties" do
t1 = TestNode.new
id = t1.neo_node_id
t2 = Neo4j.load(id)
p = t2.props
p.keys.should include('id')
p.keys.should include('classname')
p.keys.size.should == 2
end
it "should return declared properties" do
t1 = TestNode.new
t1.name = 'abc'
t1.age = 3
p = t1.props
p['name'].should == 'abc'
p['age'].should == 3
end
it "should return undeclared properties" do
t1 = TestNode.new
t1.set_property('hoj', 'koj')
p = t1.props
p.keys.should include('hoj')
p['hoj'].should == 'koj'
end
end