Skip to content

Commit 7faa2dc

Browse files
Merge pull request #213 from neo4jrb/collections_within_transactions
fixes handling of collections within transactions
2 parents 0f7fa28 + 6c33421 commit 7faa2dc

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

lib/neo4j-server/cypher_response.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ def id_from_url(url)
263263
attr_reader :result_index
264264

265265
def mapped_rest_data
266-
response.body[:results][0][:data][result_index][:rest][row_index]
266+
data = response.body[:results][0][:data][result_index][:rest][row_index]
267+
data.is_a?(Array) ? data.first : data
267268
end
268269
end
269270
end

spec/shared_examples/node_with_tx.rb

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,46 @@
7676
end
7777
end
7878

79-
describe 'returning a hash from cypher' do
80-
before { Neo4j::Node.create({name: 'Foo'}, :person) }
79+
describe 'complex structures' do
80+
let!(:node) { Neo4j::Node.create({name: 'Foo'}, :Person) }
81+
82+
describe 'returning a collection' do
83+
it 'does not raise an error' do
84+
begin
85+
tx = Neo4j::Transaction.new
86+
expect { Neo4j::Session.current.query.match(p: :Person).pluck('collect(p)') }.not_to raise_error
87+
ensure
88+
tx.close
89+
end
90+
end
8191

82-
it 'is not mistaken for a malformed object' do
83-
begin
84-
tx = Neo4j::Transaction.new
85-
Neo4j::Session.current.query.match(person: 'person').pluck('person limit 1')
86-
response = Neo4j::Session.current.query.match(person: 'person').where(person: {name: 'Foo'})
87-
.pluck('person, { name: person.name, labels: LABELS(person)[0]} as ph').first
88-
expect(response[1]).to be_a(Hash)
89-
# Behavior differs slightly in JRuby/Embedded.
90-
key = response[1].key?('name') ? 'name' : :name
91-
expect(response[1][key]).to eq 'Foo'
92-
ensure
93-
tx.close if Neo4j::Transaction.current
92+
it 'returns a node within an array' do
93+
begin
94+
tx = Neo4j::Transaction.new
95+
result = Neo4j::Session.current.query.match(p: :Person).pluck('collect(p)').first
96+
expect(result).to be_a(Array)
97+
expect(result.first).to respond_to(:neo_id)
98+
expect(result.first.labels).to include(:Person)
99+
ensure
100+
tx.close
101+
end
102+
end
103+
end
104+
105+
describe 'returning a hash from cypher' do
106+
it 'is not mistaken for a malformed object' do
107+
begin
108+
tx = Neo4j::Transaction.new
109+
Neo4j::Session.current.query.match(person: 'Person').pluck('person limit 1')
110+
response = Neo4j::Session.current.query.match(person: 'Person').where(person: {name: 'Foo'})
111+
.pluck('person, { name: person.name, labels: LABELS(person)[0]} as ph').first
112+
expect(response[1]).to be_a(Hash)
113+
# Behavior differs slightly in JRuby/Embedded.
114+
key = response[1].key?('name') ? 'name' : :name
115+
expect(response[1][key]).to eq 'Foo'
116+
ensure
117+
tx.close if Neo4j::Transaction.current
118+
end
94119
end
95120
end
96121
end

0 commit comments

Comments
 (0)