Skip to content

Commit

Permalink
simplifies transaction response parse, fixes path/nodes bug
Browse files Browse the repository at this point in the history
  • Loading branch information
subvertallchris committed Jan 29, 2016
1 parent 0c4b752 commit 2c17e5d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 46 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ All notable changes to this project will be documented in this file.
This file should follow the standards specified on [http://keepachangelog.com/]
This project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

### Fixed

- Returning paths within transactions in Server mode could result in incorrectly wrapped nodes.

## [6.1.0] - 2016-01-01

### Fixed
Expand Down
63 changes: 17 additions & 46 deletions lib/neo4j-server/cypher_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,8 @@ def to_struct_enumeration(cypher = EMPTY_STRING)

def to_node_enumeration(cypher = EMPTY_STRING, session = Neo4j::Session.current)
Enumerator.new do |yielder|
@result_index = 0
to_struct_enumeration(cypher).each do |row|
@row_index = 0
yielder << row_pair_in_struct(row, session)
@result_index += 1
end
end
end
Expand All @@ -68,7 +65,6 @@ def row_pair_in_struct(row, session)
@struct.new.tap do |result|
row.each_pair do |column, value|
result[column] = map_row_value(value, session)
@row_index += 1
end
end
end
Expand All @@ -84,17 +80,10 @@ def map_row_value(value, session)
end

def hash_value_as_object(value, session)
if transaction_response?
add_transaction_entity_id
data = mapped_rest_data
elsif [:node, :relationship].include?(identify_entity(value))
add_entity_id(value)
data = value
else
return value
end
return value unless [:node, :relationship].include?(identify_entity(value))
add_entity_id(value)

basic_obj = (node?(value) ? CypherNode : CypherRelationship).new(session, data)
basic_obj = (node?(value) ? CypherNode : CypherRelationship).new(session, value)
unwrapped? ? basic_obj : basic_obj.wrapper
end

Expand All @@ -112,11 +101,7 @@ def identify_entity(data)
end

def looks_like_an_object?(value)
if transaction_response?
mapped_rest_data[:outgoing_relationships] || (mapped_rest_data[:start] && mapped_rest_data[:properties])
else
value[:labels] || value[:type]
end
value[:labels] || value[:type]
end

def unwrapped!
Expand All @@ -128,7 +113,7 @@ def unwrapped?
end

def node?(value)
transaction_response? ? !mapped_rest_data[:start] : value[:labels]
value[:labels]
end

attr_reader :struct
Expand All @@ -151,7 +136,6 @@ def entity_data(id = nil)
def first_data
if @uncommited
@data.first[:row].first
# data.is_a?(Hash) ? {'data' => data, 'id' => id} : data
else
data = @data[0][0]
data.is_a?(Hash) ? add_entity_id(data) : data
Expand All @@ -167,11 +151,6 @@ def add_entity_id(data)
data
end

def add_transaction_entity_id
mapped_rest_data[:id] = mapped_rest_data[:self].split('/').last.to_i
mapped_rest_data
end

def error?
!!@error
end
Expand All @@ -191,9 +170,18 @@ def raise_unless_response_code(code)
end

def each_data_row
data.each { |r| yield (@uncommited ? r[:row] : r) }
data.each do |r|
yieldable = if @uncommitted
r[:row]
else
transaction_row?(r) ? r[:rest] : r
end
yield yieldable
end
end



def set_data(response)
@data = response[:data]
@columns = response[:columns]
Expand Down Expand Up @@ -244,27 +232,10 @@ def self.create_with_tx(response)
end
end

def transaction_response?
response.respond_to?(:body) && !response.body[:commit].nil?
end

def rest_data
@result_index = @row_index = 0
mapped_rest_data
end

def rest_data_with_id
rest_data[:id] = mapped_rest_data[:self].split('/').last.to_i
rest_data
end

private

attr_reader :row_index, :result_index

def mapped_rest_data
data = response.body[:results][0][:data][result_index][:rest][row_index]
data.is_a?(Array) ? data.first : data
def transaction_row?(row)
row.is_a?(Hash) && row[:rest] && row[:row]
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions spec/shared_examples/node_with_tx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,23 @@
end
end
end

describe 'returning a path from cypher' do
before do
Neo4j::Session.current
.query("CREATE (:Person {name: 'son'})<-[:PARENT_OF]-(:Person {name: 'father'})<-[:PARENT_OF]-(:Person {name: 'grandfather'})")
end
let(:names) do
Neo4j::Session.current
.query
.match("p=(:Person {name: 'son'})<-[PARENT_OF*]-(:Person {name: 'grandfather'})")
.pluck('nodes(p)')[0].map { |n| n.props[:name] }
end

it 'correctly wraps nodes' do
expect(Neo4j::Transaction.run { names }).to eq %w(son father grandfather)
end
end
end
end

Expand Down

0 comments on commit 2c17e5d

Please sign in to comment.