From c9ede85960e9e09050f8cbb57c22c52691d768d8 Mon Sep 17 00:00:00 2001 From: Chris Grigg Date: Tue, 23 Jun 2015 20:42:42 -0400 Subject: [PATCH 1/3] fixes handling of collections within transactions --- lib/neo4j-server/cypher_response.rb | 3 +- spec/shared_examples/node_with_tx.rb | 51 +++++++++++++++++++++------- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/lib/neo4j-server/cypher_response.rb b/lib/neo4j-server/cypher_response.rb index e2d82906..b166206a 100644 --- a/lib/neo4j-server/cypher_response.rb +++ b/lib/neo4j-server/cypher_response.rb @@ -263,7 +263,8 @@ def id_from_url(url) attr_reader :result_index def mapped_rest_data - response.body[:results][0][:data][result_index][:rest][row_index] + data = response.body[:results][0][:data][result_index][:rest][row_index] + data.is_a?(Array) ? data.first : data end end end diff --git a/spec/shared_examples/node_with_tx.rb b/spec/shared_examples/node_with_tx.rb index 58bc21e9..a93a212b 100644 --- a/spec/shared_examples/node_with_tx.rb +++ b/spec/shared_examples/node_with_tx.rb @@ -76,21 +76,46 @@ end end - describe 'returning a hash from cypher' do - before { Neo4j::Node.create({name: 'Foo'}, :person) } + describe 'complex structures' do + let!(:node) { Neo4j::Node.create({name: 'Foo'}, :Person)} - it 'is not mistaken for a malformed object' do - begin + describe 'returning a collection' do + it 'does not raise an error' do + begin tx = Neo4j::Transaction.new - Neo4j::Session.current.query.match(person: 'person').pluck('person limit 1') - response = Neo4j::Session.current.query.match(person: 'person').where(person: {name: 'Foo'}) - .pluck('person, { name: person.name, labels: LABELS(person)[0]} as ph').first - expect(response[1]).to be_a(Hash) - # Behavior differs slightly in JRuby/Embedded. - key = response[1].key?('name') ? 'name' : :name - expect(response[1][key]).to eq 'Foo' - ensure - tx.close if Neo4j::Transaction.current + expect { Neo4j::Session.current.query.match(p: :Person).pluck('collect(p)') }.not_to raise_error + ensure + tx.close + end + end + + it 'returns a node within an array' do + begin + tx = Neo4j::Transaction.new + result = Neo4j::Session.current.query.match(p: :Person).pluck('collect(p)').first + expect(result).to be_a(Array) + expect(result.first).to be_a(Neo4j::Node) + expect(result.first.labels).to include(:Person) + ensure + tx.close + end + end + end + + describe 'returning a hash from cypher' do + it 'is not mistaken for a malformed object' do + begin + tx = Neo4j::Transaction.new + Neo4j::Session.current.query.match(person: 'Person').pluck('person limit 1') + response = Neo4j::Session.current.query.match(person: 'Person').where(person: {name: 'Foo'}) + .pluck('person, { name: person.name, labels: LABELS(person)[0]} as ph').first + expect(response[1]).to be_a(Hash) + # Behavior differs slightly in JRuby/Embedded. + key = response[1].key?('name') ? 'name' : :name + expect(response[1][key]).to eq 'Foo' + ensure + tx.close if Neo4j::Transaction.current + end end end end From 56fb3a2a8f93fc54755c83575c325bba4433aac2 Mon Sep 17 00:00:00 2001 From: Chris Grigg Date: Tue, 23 Jun 2015 20:43:43 -0400 Subject: [PATCH 2/3] rubocop --- spec/shared_examples/node_with_tx.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/shared_examples/node_with_tx.rb b/spec/shared_examples/node_with_tx.rb index a93a212b..29cf966d 100644 --- a/spec/shared_examples/node_with_tx.rb +++ b/spec/shared_examples/node_with_tx.rb @@ -77,15 +77,15 @@ end describe 'complex structures' do - let!(:node) { Neo4j::Node.create({name: 'Foo'}, :Person)} + let!(:node) { Neo4j::Node.create({name: 'Foo'}, :Person) } describe 'returning a collection' do it 'does not raise an error' do begin - tx = Neo4j::Transaction.new - expect { Neo4j::Session.current.query.match(p: :Person).pluck('collect(p)') }.not_to raise_error - ensure - tx.close + tx = Neo4j::Transaction.new + expect { Neo4j::Session.current.query.match(p: :Person).pluck('collect(p)') }.not_to raise_error + ensure + tx.close end end @@ -108,7 +108,7 @@ tx = Neo4j::Transaction.new Neo4j::Session.current.query.match(person: 'Person').pluck('person limit 1') response = Neo4j::Session.current.query.match(person: 'Person').where(person: {name: 'Foo'}) - .pluck('person, { name: person.name, labels: LABELS(person)[0]} as ph').first + .pluck('person, { name: person.name, labels: LABELS(person)[0]} as ph').first expect(response[1]).to be_a(Hash) # Behavior differs slightly in JRuby/Embedded. key = response[1].key?('name') ? 'name' : :name From 6c334216b3c04fd15168e9b8535a0059db2f8c7d Mon Sep 17 00:00:00 2001 From: Chris Grigg Date: Tue, 23 Jun 2015 21:04:27 -0400 Subject: [PATCH 3/3] respond_to? > is_a? for Embedded in tests --- spec/shared_examples/node_with_tx.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/shared_examples/node_with_tx.rb b/spec/shared_examples/node_with_tx.rb index 29cf966d..6c6529d7 100644 --- a/spec/shared_examples/node_with_tx.rb +++ b/spec/shared_examples/node_with_tx.rb @@ -94,7 +94,7 @@ tx = Neo4j::Transaction.new result = Neo4j::Session.current.query.match(p: :Person).pluck('collect(p)').first expect(result).to be_a(Array) - expect(result.first).to be_a(Neo4j::Node) + expect(result.first).to respond_to(:neo_id) expect(result.first.labels).to include(:Person) ensure tx.close