-
Notifications
You must be signed in to change notification settings - Fork 273
Expand file tree
/
Copy pathtransactions_spec.rb
More file actions
143 lines (122 loc) · 4.07 KB
/
Copy pathtransactions_spec.rb
File metadata and controls
143 lines (122 loc) · 4.07 KB
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
describe ActiveGraph::Transactions do
let(:read_query) { 'RETURN 1' }
let(:write_query) { 'CREATE (n:Student{uuid: randomUuid()}) RETURN count(n)' }
before do
clear_model_memory_caches
stub_node_class('Student')
end
describe '.transaction' do
context 'driver' do
it 'executes read' do
expect(ActiveGraph::Base.transaction { |tx| tx.run(read_query).single.first }).to eq 1
end
it 'executes write' do
expect(ActiveGraph::Base.transaction { |tx| tx.run(write_query).single.first }).to eq 1
end
end
context 'DSL' do
it 'executes read' do
expect(ActiveGraph::Base.transaction { Student.count }).to eq 0
end
it 'executes write' do
expect(ActiveGraph::Base.transaction { Student.create }).to be_a Student
end
end
end
describe '.write_transaction' do
context 'driver' do
it 'executes read' do
expect(ActiveGraph::Base.write_transaction { |tx| tx.run(read_query).single.first }).to eq 1
end
it 'executes write' do
expect(ActiveGraph::Base.write_transaction { |tx| tx.run(write_query).single.first }).to eq 1
end
end
context 'DSL' do
it 'executes read' do
expect(ActiveGraph::Base.write_transaction { Student.count }).to eq 0
end
it 'executes write' do
expect(ActiveGraph::Base.write_transaction { Student.create }).to be_a Student
end
end
end
describe '.read_transaction' do
context 'driver' do
it 'executes read' do
expect(ActiveGraph::Base.read_transaction { |tx| tx.run(read_query).single.first }).to eq 1
end
end
context 'DSL' do
it 'executes read' do
expect(ActiveGraph::Base.read_transaction { Student.count }).to eq 0
end
end
end
describe '.session' do
it 'allows write on implicitly writable' do
expect { ActiveGraph::Base.session { ActiveGraph::Base.transaction { Student.create } } }.not_to raise_error
end
it 'allows write on explicitely writable' do
expect do
ActiveGraph::Base.session(default_access_mode: Neo4j::Driver::AccessMode::WRITE) { ActiveGraph::Base.transaction { Student.create } }
end.not_to raise_error
end
it 'returns and accepts bookmarks' do
expect do
ActiveGraph::Base.session do
ActiveGraph::Base.write_transaction { Student.create }
ActiveGraph::Base.write_transaction { Student.create }
end
ActiveGraph::Base.session(bookmarks: ActiveGraph::Base.last_bookmarks) do
ActiveGraph::Base.read_transaction { Student.count }
end
end.not_to raise_error
end
it 'can mix DSL with pure driver ' do
expect do
ActiveGraph::Base.session do |session|
ActiveGraph::Base.write_transaction do |tx|
tx.run(read_query)
Student.create
end
session.execute_write { |tx| tx.run(write_query) }
session.run(read_query, {}, timeout: 1.minute)
end
end.not_to raise_error
end
it 'can nest transactions' do
expect do
ActiveGraph::Base.session do
ActiveGraph::Base.write_transaction do |outer_tx|
ActiveGraph::Transaction.transaction do |inner_tx|
expect(inner_tx).to eq outer_tx # regardless by which module accessed
end
end
end
end
end
it 'can call transaction on Node' do
Student.session do
Student.write_transaction do |tx|
Student.create
expect(Student.count).to eq 1
end
end
end
end
describe 'rollback in nested transaction' do
it 'allows reading model errors after failed update inside an outer transaction' do
stub_node_class('ValidatedStudent') do
property :name
validates :name, presence: true
end
student = ValidatedStudent.create!(name: 'Alice')
errors = ActiveGraph::Base.transaction do
student.update(name: nil)
student.errors.full_messages
end
expect(errors).to include("Name can't be blank")
end
end
end