Skip to content

Commit

Permalink
RestMixin traversal should support query parameter depth of value 'al…
Browse files Browse the repository at this point in the history
…l' [#62 state:resolved]
  • Loading branch information
andreas committed Aug 24, 2009
1 parent aebba0c commit 5b0df82
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
13 changes: 9 additions & 4 deletions lib/neo4j/extensions/rest/rest.rb
Expand Up @@ -217,12 +217,17 @@ def self.query_from_params(params)
begin
Neo4j::Transaction.run do
node = Neo4j.load(params[:id])
return 404, "Can't find node with id #{params[:id]}" if node.nil?
return 404, {'error' => "Can't find node with id #{params[:id]}"}.to_json if node.nil?

relationship = params['relationship']
depth = params['depth']
depth ||= 1
uris = node.traverse.outgoing(relationship.to_sym).depth(depth.to_i).collect{|node| node._uri}
depth = case params['depth']
when nil then 1
when 'all' then :all
else params['depth'].to_i
end
return 400, {'error' => "invalid depth parameter - must be an integer"}.to_json if depth == 0

uris = node.traverse.outgoing(relationship.to_sym).depth(depth).collect{|node| node._uri}
{'uri_list' => uris}.to_json
end
rescue RestException => exception
Expand Down
34 changes: 33 additions & 1 deletion test/rest/rest_spec.rb
Expand Up @@ -118,7 +118,7 @@ class FooRest
p._uri.should == "http://0.0.0.0:#{port}/nodes/RestPerson/#{p.neo_node_id}"
end

it "should traverse a relationship on GET nodes/RestPerson/<id>/traverse?relationship=friends&depth=1" do
it "should traverse a relationship on depth 1 - e.g. GET nodes/RestPerson/<id>/traverse?relationship=friends&depth=1" do
# the reference node has id = 0; the index node has id = 1
adam = RestPerson.new # neo_node_id = 2
adam.name = 'adam'
Expand All @@ -142,6 +142,38 @@ class FooRest
body['uri_list'].size.should == 2
end

it "should traverse a relationship depth all - e.g. GET nodes/RestPerson/<id>/traverse?relationship=friends&depth=all" do
# the reference node has id = 0; the index node has id = 1
adam = RestPerson.new # neo_node_id = 2
adam.name = 'adam'

bertil = RestPerson.new # neo_node_id = 3
bertil.name = 'bertil'

carl = RestPerson.new # neo_node_id = 4

adam.friends << bertil
bertil.friends << carl

# when
get "/nodes/RestPerson/#{adam.neo_node_id}/traverse?relationship=friends&depth=all"

# then
last_response.status.should == 200
body = JSON.parse(last_response.body)
body['uri_list'].should_not be_nil
body['uri_list'][0].should == 'http://0.0.0.0:4567/nodes/RestPerson/3' # bertil
body['uri_list'][1].should == 'http://0.0.0.0:4567/nodes/RestPerson/4' # carl
body['uri_list'].size.should == 2
end

it "should receive a 400 error code if the depth parameter is not an integer on the traverse resource" do
get "/nodes/RestPerson/#{RestPerson.new.neo_node_id}/traverse?relationship=friends&depth=oj"

# then
last_response.status.should == 400
end

it "should create declared relationship on POST /nodes/RestPerson/friends" do
adam = RestPerson.new
adam.name = 'adam'
Expand Down

0 comments on commit 5b0df82

Please sign in to comment.