/
tx_tracker_spec.rb
234 lines (170 loc) · 5.49 KB
/
tx_tracker_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
$LOAD_PATH << File.expand_path(File.dirname(__FILE__) + "/../../lib")
$LOAD_PATH << File.expand_path(File.dirname(__FILE__) + "/..")
require 'neo4j'
require 'neo4j/spec_helper'
require 'neo4j/extensions/tx_tracker'
class TxTestNode
include Neo4j::NodeMixin
property :myid
def to_s
"TxTestNode " + props.inspect
end
end
describe "TxTracker (TxNodeList)" do
before(:all) do
Neo4j.start
Neo4j.load_tx_tracker
@tx_node_list = Neo4j::TxNodeList.instance
end
after(:all) do
stop
end
it "should have a reference to the TxNodeList" do
@tx_node_list.should_not be_nil
end
it "should be empty TxNode when first started" do
@tx_node_list.tx_nodes.empty?.should be_true
end
it "should not be empty TxNode when a node has been created" do
@tx_node_list.tx_nodes.empty?.should be_true
Neo4j::Transaction.run { TxTestNode.new }
@tx_node_list.tx_nodes.empty?.should be_false
end
it "should set a UUID on the node and the TxNode and property created=true" do
a = Neo4j::Transaction.run { TxTestNode.new }
tx_node = @tx_node_list.tx_nodes.first
tx_node[:uuid].should == a[:uuid]
tx_node[:created].should == true
end
it "should set property 'property_changed' when a node property is changed" do
a = Neo4j::Transaction.run { TxTestNode.new }
Neo4j::Transaction.run { a.myid = "hej" }
first = @tx_node_list.tx_nodes.first
first[:property_changed].should == true
first[:old_value].should == nil
first[:new_value].should == "hej"
end
it "should set property 'old_value' and 'new_value' when a node property is changed" do
Neo4j::Transaction.run do
a = TxTestNode.new
a.myid = "hej1"
a.myid = "hej2"
end
first = @tx_node_list.tx_nodes.first
first[:property_changed].should == true
first[:old_value].should == "hej1"
first[:new_value].should == "hej2"
end
it "should be possible to undo a transaction on property changed" do
a = Neo4j::Transaction.run { TxTestNode.new }
Neo4j::Transaction.run { a.myid = "hej1" }
Neo4j::Transaction.run do
a.myid = "hej2"
a.myid.should == "hej2"
end
id = a.neo_node_id
# when
Neo4j::Transaction.run { Neo4j.undo_tx }
# then
node = Neo4j.load(id)
node.myid.should == "hej1"
end
it "should be possible to undo a transaction on node created" do
Neo4j::Transaction.new
a = TxTestNode.new
id = a.neo_node_id
Neo4j::Transaction.finish
Neo4j::Transaction.new
# when
Neo4j.load(id.to_i).should be_nil
Neo4j.undo_tx
# then
Neo4j::Transaction.finish
Neo4j::Transaction.run { Neo4j.load(id).should == nil }
end
it "should be possible to undo a transaction on node deleted" do
Neo4j::Transaction.new
a = TxTestNode.new
id = a.neo_node_id
Neo4j.load(id).should_not be_nil
a.delete
Neo4j::Transaction.finish
Neo4j::Transaction.new
Neo4j.load(id).should be_nil
# when
Neo4j.undo_tx
Neo4j::Transaction.finish
Neo4j::Transaction.new
# then
Neo4j.load(id).should be_nil
Neo4j::Transaction.finish
end
it "should set property 'tx_finished' on the last TxNode that was commited" do
@tx_node_list = Neo4j::TxNodeList.instance
Neo4j::Transaction.run do
TxTestNode.new.myid = '1'
TxTestNode.new.myid = '2'
TxTestNode.new.myid = '3'
end
Neo4j::Transaction.run do
first = @tx_node_list.tx_nodes.first
first[:tx_finished].should == true
second = @tx_node_list.tx_nodes.to_a[1]
second[:tx_finished].should be_nil
third = @tx_node_list.tx_nodes.to_a[2]
third[:tx_finished].should be_nil
end
end
it "should undo a complete transaction" do
pending "Fails if the RSpecs are running in reverse order - fix it"
@tx_node_list = Neo4j::TxNodeList.instance
node1 = node2 = node3 = nil
# Transaction 1
Neo4j::Transaction.run do
node1 = TxTestNode.new
node1.myid = '1'
node2 = TxTestNode.new
node2.myid = '2'
end
# Transaction 2
Neo4j::Transaction.run do
node2.myid = 'x'
node3 = TxTestNode.new
node3.myid = '3'
end
# when undo transaction 2
Neo4j::Transaction.run { Neo4j.undo_tx }
# then
Neo4j::Transaction.run do
Neo4j.load(node3.neo_node_id).should be_nil
node2.myid.should == '2'
end
end
it "should undo a new relationship" do
a = Neo4j::Transaction.run { Neo4j::Node.new}
b = Neo4j::Transaction.run { Neo4j::Node.new}
rel = Neo4j::Transaction.run { a.relationships.outgoing(:foobar) << b}
rel_id = rel.neo_relationship_id
Neo4j::Transaction.run { Neo4j.load_relationship(rel_id).should == rel }
# when
Neo4j::Transaction.run { Neo4j.undo_tx }
# then
Neo4j::Transaction.run { Neo4j.load_relationship(rel_id).should == nil }
end
it "should undo delation of relationship" do
a = Neo4j::Transaction.run { Neo4j::Node.new}
b = Neo4j::Transaction.run { Neo4j::Node.new}
rel = Neo4j::Transaction.run { a.relationships.outgoing(:foobar) << b}
# make sure relationship exists
Neo4j::Transaction.run { a.relationship?(:foobar).should be_true }
# delete relationship
Neo4j::Transaction.run { rel.delete }
# make sure it is deleted
Neo4j::Transaction.run { a.relationship?(:foobar).should be_false }
# when
Neo4j::Transaction.run { Neo4j.undo_tx }
# then
# make sure relationship exists
Neo4j::Transaction.run { a.relationship?(:foobar).should be_true }
end
end