From 3e4206ddd04c53c4f5d390f81f86cd7b8eb03ff3 Mon Sep 17 00:00:00 2001 From: maxdemarzi Date: Sun, 14 Nov 2010 22:08:26 -0800 Subject: [PATCH] added build_node so we return neo_id on node creates and a few more tests --- lib/neography.rb | 2 +- lib/neography/node.rb | 47 ++++++++++++++++++++++++++---------- spec/neography_spec.rb | 55 ++++++++++++++++++++++++++++++++++++------ 3 files changed, 82 insertions(+), 22 deletions(-) diff --git a/lib/neography.rb b/lib/neography.rb index 8db7e79..60b355d 100644 --- a/lib/neography.rb +++ b/lib/neography.rb @@ -39,7 +39,7 @@ def evaluate_response(response) require 'httparty' require 'json' require 'logger' -#require 'net-http-spy' +require 'net-http-spy' #Net::HTTP.http_logger_options = {:verbose => true} #Net::HTTP.http_logger_options = {:body => true} diff --git a/lib/neography/node.rb b/lib/neography/node.rb index 2dfb52e..58232e5 100644 --- a/lib/neography/node.rb +++ b/lib/neography/node.rb @@ -8,22 +8,22 @@ class << self def new(*args) if args[0].respond_to?(:each_pair) && args[0] - headers 'Content-Type' => 'application/json' - response = post("/node", :body => args[0].to_json) + options = { :body => args[0].to_json, :headers => {'Content-Type' => 'application/json'} } + response = post("/node", options) evaluate_response(response) - response.parsed_response + build_node(response) else response = post("/node") evaluate_response(response) - response.parsed_response + build_node(response) end end - def get_node(id) + def load(id) begin response = get("/node/#{id}") evaluate_response(response) - response.parsed_response + build_node(response) rescue nil end @@ -34,15 +34,36 @@ def properties(id) end def set_properties(id, properties) - begin - response = put("/node/#{id}/properties", :body => properties.to_json) - evaluate_response(response) - response.parsed_response - rescue - nil - end + options = { :body => properties.to_json, :headers => {'Content-Type' => 'application/json'} } + response = put("/node/#{id}/properties", options) + evaluate_response(response) + response.parsed_response + end + + def remove_property(id, property) + response = delete("/node/#{id}/properties/#{property}") + evaluate_response(response) + response.parsed_response + end + + def del(id) + response = delete("/node/#{id}") + evaluate_response(response) + response.parsed_response end + private + + def build_node(response) + begin + node = response.parsed_response["data"] + rescue + node = Array.new + end + node[:neo_id] = response.parsed_response["self"].split('/').last + node + end + end end end diff --git a/spec/neography_spec.rb b/spec/neography_spec.rb index 966dd3c..4bb1091 100644 --- a/spec/neography_spec.rb +++ b/spec/neography_spec.rb @@ -1,30 +1,30 @@ require 'neography' describe Neography::Neo do -# it "has a root node" do -# Neography::Neo.root_node.should include("reference_node") -# end + it "has a root node" do + Neography::Neo.root_node.should include("reference_node") + end end describe Neography::Node do it "can create an empty node" do - Neography::Node.new.should include("data"=>{}) + Neography::Node.new.should include(:neo_id) end it "can create a node with one property" do - Neography::Node.new(:name => "Max").should include("data"=>{"name"=>"Max"}) + Neography::Node.new(:name => "Max").should include("name"=>"Max") end it "can create a node with more than one property" do - Neography::Node.new(:age => 31, :name => "Max").should include("data"=>{"age"=>31, "name"=>"Max"}) + Neography::Node.new(:age => 31, :name => "Max").should include("age"=>31, "name"=>"Max") end it "can find a node by its id" do - Neography::Node.get_node(2).should include("self"=>"http://localhost:9999/node/2") + Neography::Node.load(2).should include(:neo_id=>"2") end it "fails to find a node that does not exist" do - Neography::Node.get_node(999).should be_nil + Neography::Node.load(999).should be_nil end it "can get a node's properties" do @@ -45,6 +45,45 @@ Neography::Node.set_properties(999,{:age => 33, :name => "Harry"}).should be_nil end + it "can delete a node's property" do + Neography::Node.set_properties(2, {:age => 32, :name => "Tom", :weight => 200} ).should be_nil + Neography::Node.remove_property(2, :weight).should be_nil + Neography::Node.properties(2).should_not include("weight"=>200) + end + + it "returns nil if it tries to delete a property that does not exist" do + Neography::Node.set_properties(2, {:age => 32, :name => "Tom", :weight => 200} ).should be_nil + Neography::Node.remove_property(2, :height).should be_nil + end + + it "returns nil if it tries to delete a property on a node that does not exist" do + Neography::Node.remove_property(9999, :height).should be_nil + end + + it "can delete an unrelated node" do + newnode = Neography::Node.new + Neography::Node.del(newnode[:neo_id]).should be_nil + end + + it "returns nil if it tries to delete a node that does not exist" do + Neography::Node.del(9999).should be_nil + end + + it "returns nil if it tries to delete a node that has already been deleted" do + newnode = Neography::Node.new + Neography::Node.del(newnode[:neo_id]).should be_nil + Neography::Node.del(newnode[:neo_id]).should be_nil + end + + it "returns nil if it tries to delete a node that has existing relationships" do + node1 = Neography::Node.new + node2 = Neography::Node.new + pending "create relationship from node1 to node2" + Neography::Node.del(node1[:neo_id]).should be_nil + Neography::Node.del(node2[:neo_id]).should be_nil + end + + end \ No newline at end of file