From e42beda7dcf2380a9781e4cb6f291e85062e67e1 Mon Sep 17 00:00:00 2001 From: maxdemarzi Date: Sat, 20 Nov 2010 02:18:09 -0800 Subject: [PATCH] more cleanup --- README.rdoc | 109 +++++---- lib/neography.rb | 3 +- lib/neography/rest.rb | 143 +++++++----- spec/integration/rest_index_spec.rb | 31 +-- spec/integration/rest_node_spec.rb | 151 +++++++------ spec/integration/rest_path_spec.rb | 11 +- spec/integration/rest_relationship_spec.rb | 249 +++++++++++---------- spec/spec_helper.rb | 3 - 8 files changed, 376 insertions(+), 324 deletions(-) diff --git a/README.rdoc b/README.rdoc index c9becdb..449cc11 100644 --- a/README.rdoc +++ b/README.rdoc @@ -10,72 +10,71 @@ Neography is a thin ruby wrapper to the Neo4j Rest API, for more information: gem install 'neography' require 'neography' +=== Dependencies -==== Configuration + for use: + json + httparty + logger - Neography::Config.use do |config| - config[:protocol] = 'http://' - config[:server] = 'localhost' - config[:port] = '7474' - end + for development: + rspec + net-http-spy + fakeweb ==== Rails Just add gem 'neography' to your Gemfile and run bundle install -Use the defaults (shown above) or create neography.rb in your config/initializers directory. - === Documentation -There are two ways to use Neography: - A thin ruby wrapper Neography::Rest which tries to mirror the Neo4j Rest API and returns JSON or Nil: - Neography::Rest.get_root # Get the root node - Neography::Rest.create_node # Create an empty node - Neography::Rest.create_node("age" => 31, "name" => "Max") # Create a node with some properties - Neography::Rest.get_node(id) # Get a node and its properties - Neography::Rest.delete_node(id) # Delete an unrelated node - Neography::Rest.delete_node!(id) # Delete a node and all its relationships - - Neography::Rest.reset_node_properties(id, {"age" => 31}) # Reset a node's properties - Neography::Rest.set_node_properties(id, {"weight" => 200}) # Set a node's properties - Neography::Rest.get_node_properties(id) # Get just the node properties - Neography::Rest.get_node_properties(id, ["weight","age"]) # Get some of the node properties - Neography::Rest.remove_node_properties(id) # Remove all properties of a node - Neography::Rest.remove_node_properties(id, "weight") # Remove one property of a node - Neography::Rest.remove_node_properties(id, ["weight","age"]) # Remove multiple properties of a node - - Neography::Rest.create_relationship("friends", node1, node2) # Create a relationship between node1 and node2 - Neography::Rest.get_node_relationships(id) # Get all relationships - Neography::Rest.get_node_relationships(id, "in") # Get only incoming relationships - Neography::Rest.get_node_relationships(id, "all", "enemies") # Get all relationships of type enemies - Neography::Rest.get_node_relationships(id, "in", "enemies") # Get only incoming relationships of type enemies - Neography::Rest.delete_relationship(id) # Delete a relationship - - Neography::Rest.reset_relationship_properties(id, {"age" => 31}) # Reset a relationship's properties - Neography::Rest.set_relationship_properties(id, {"weight" => 200}) # Set a relationship's properties - Neography::Rest.get_relationship_properties(id) # Get just the relationship properties - Neography::Rest.get_relationship_properties(id, ["since","met"]) # Get some of the relationship properties - Neography::Rest.remove_relationship_properties(id) # Remove all properties of a relationship - Neography::Rest.remove_relationship_properties(id, "since") # Remove one property of a relationship - Neography::Rest.remove_relationship_properties(id, ["since","met"]) # Remove multiple properties of a relationship - - Neography::Rest.list_indexes # doesn't really seam to do what the api says it does - Neography::Rest.add_to_index(key, value, id) # adds a node to an index with the given key/value pair - Neography::Rest.remove_from_index(key, value, id) # removes a node to an index with the given key/value pair - Neography::Rest.get_index(key, value) # gets an index with the given key/value pair - -... and a work in progress more rubyish layer is in the works. - - Neography::Node.new # Create an empty node - Neography::Node.new(:age => 31, :name => "Max") # Create a node with some properties - Neography::Node.load(id) # Get a node and its properties - Neography::Node.set_properties(3, {:age => 31, :name => "Max"} ) # Deletes any existing properties with the passed hash - Neography::Node.properties(3) # Returns a hash of a node's properties or nil - Neography::Node.remove_property(3, :age) # Deletes the existing property - Neography::Node.del(3) # Deletes a node without any relationships - Neography::Node.del!(3) # Deletes a node and all its relationships + # protocol, server, port, log_file, log_enabled + @neo = Neography::Rest.new ('http://', '192.168.10.1', 7479, 'log/neography.log', true) + +Default Parameters are: + + @neo = Neography::Rest.new ('http://', 'localhost', 7474, '/neography.log', false) + +To Use: + + @neo = Neography::Rest.new + + @neo.get_root # Get the root node + @neo.create_node # Create an empty node + @neo.create_node("age" => 31, "name" => "Max") # Create a node with some properties + @neo.get_node(id) # Get a node and its properties + @neo.delete_node(id) # Delete an unrelated node + @neo.delete_node!(id) # Delete a node and all its relationships + + @neo.reset_node_properties(id, {"age" => 31}) # Reset a node's properties + @neo.set_node_properties(id, {"weight" => 200}) # Set a node's properties + @neo.get_node_properties(id) # Get just the node properties + @neo.get_node_properties(id, ["weight","age"]) # Get some of the node properties + @neo.remove_node_properties(id) # Remove all properties of a node + @neo.remove_node_properties(id, "weight") # Remove one property of a node + @neo.remove_node_properties(id, ["weight","age"]) # Remove multiple properties of a node + + @neo.create_relationship("friends", node1, node2) # Create a relationship between node1 and node2 + @neo.get_node_relationships(id) # Get all relationships + @neo.get_node_relationships(id, "in") # Get only incoming relationships + @neo.get_node_relationships(id, "all", "enemies") # Get all relationships of type enemies + @neo.get_node_relationships(id, "in", "enemies") # Get only incoming relationships of type enemies + @neo.delete_relationship(id) # Delete a relationship + + @neo.reset_relationship_properties(id, {"age" => 31}) # Reset a relationship's properties + @neo.set_relationship_properties(id, {"weight" => 200}) # Set a relationship's properties + @neo.get_relationship_properties(id) # Get just the relationship properties + @neo.get_relationship_properties(id, ["since","met"]) # Get some of the relationship properties + @neo.remove_relationship_properties(id) # Remove all properties of a relationship + @neo.remove_relationship_properties(id, "since") # Remove one property of a relationship + @neo.remove_relationship_properties(id, ["since","met"]) # Remove multiple properties of a relationship + + @neo.list_indexes # doesn't really seam to do what the api says it does + @neo.add_to_index(key, value, id) # adds a node to an index with the given key/value pair + @neo.remove_from_index(key, value, id) # removes a node to an index with the given key/value pair + @neo.get_index(key, value) # gets an index with the given key/value pair === License diff --git a/lib/neography.rb b/lib/neography.rb index bddadf4..b1480b9 100644 --- a/lib/neography.rb +++ b/lib/neography.rb @@ -18,8 +18,9 @@ def find_and_require_user_defined_code require 'httparty' require 'json' +require 'logger' -require 'neography/config' require 'neography/rest' find_and_require_user_defined_code + diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index bc5fc75..e0f4c33 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -1,40 +1,56 @@ module Neography class Rest include HTTParty - base_uri Neography::Config.to_s - format :json + attr_accessor :protocol, :server, :port, :log_file, :log_enabled, :logger - class << self + def initialize(protocol='http://', server='localhost', port=7474, log_file='neography.log', log_enabled=true) + @protocol = protocol + @server = server + @port = port + @log_file = log_file + @log_enabled = log_enabled + @logger = Logger.new(@log_file) if @log_enabled + end + + def configure(protocol, server, port) + @protocol = protocol + @server = server + @port = port + end + + def configuration + @protocol + @server + ':' + @port.to_s + "/db/data" + end def get_root - rescue_ij { get('/db/data/') } + get('/') end def create_node(*args) if args[0].respond_to?(:each_pair) && args[0] - options = { :body => args[0].to_json, :headers => {'Content-Type' => 'application/json'} } - rescue_ij { post("/db/data/node", options) } + options = { :body => args[0].to_json, :headers => {'Content-Type' => 'application/json'} } + post("/node", options) else - rescue_ij { post("/db/data/node") } + post("/node") end end def get_node(id) - rescue_ij { get("/db/data/node/#{id}") } + get("/node/#{id}") end def reset_node_properties(id, properties) options = { :body => properties.to_json, :headers => {'Content-Type' => 'application/json'} } - rescue_ij { put("/db/data/node/#{id}/properties", options) } + put("/node/#{id}/properties", options) end def get_node_properties(id, properties = nil) if properties.nil? - rescue_ij { get("/db/data/node/#{id}/properties") } + get("/node/#{id}/properties") else node_properties = Hash.new properties.to_a.each do |property| - value = rescue_ij { get("/db/data/node/#{id}/properties/#{property}") } + value = get("/node/#{id}/properties/#{property}") node_properties[property] = value unless value.nil? end return nil if node_properties.empty? @@ -44,10 +60,10 @@ def get_node_properties(id, properties = nil) def remove_node_properties(id, properties = nil) if properties.nil? - rescue_ij { delete("/db/data/node/#{id}/properties") } + delete("/node/#{id}/properties") else properties.to_a.each do |property| - rescue_ij { delete("/db/data/node/#{id}/properties/#{property}") } + delete("/node/#{id}/properties/#{property}") end end end @@ -55,31 +71,31 @@ def remove_node_properties(id, properties = nil) def set_node_properties(id, properties) properties.each do |key, value| options = { :body => value.to_json, :headers => {'Content-Type' => 'application/json'} } - rescue_ij { put("/db/data/node/#{id}/properties/#{key}", options) } + put("/node/#{id}/properties/#{key}", options) end end def delete_node(id) - rescue_ij { delete("/db/data/node/#{id}") } + delete("/node/#{id}") end def create_relationship(type, from, to, props = nil) - options = { :body => {:to => Neography::Config.to_s + "/node/#{to}", :data => props, :type => type }.to_json, :headers => {'Content-Type' => 'application/json'} } - rescue_ij { post("/db/data/node/#{from}/relationships", options) } + options = { :body => {:to => self.configuration + "/node/#{to}", :data => props, :type => type }.to_json, :headers => {'Content-Type' => 'application/json'} } + post("/node/#{from}/relationships", options) end def reset_relationship_properties(id, properties) options = { :body => properties.to_json, :headers => {'Content-Type' => 'application/json'} } - rescue_ij { put("/db/data/relationship/#{id}/properties", options) } + put("/relationship/#{id}/properties", options) end def get_relationship_properties(id, properties = nil) if properties.nil? - rescue_ij { get("/db/data/relationship/#{id}/properties") } + get("/relationship/#{id}/properties") else relationship_properties = Hash.new properties.to_a.each do |property| - value = rescue_ij { get("/db/data/relationship/#{id}/properties/#{property}") } + value = get("/relationship/#{id}/properties/#{property}") relationship_properties[property] = value unless value.nil? end return nil if relationship_properties.empty? @@ -89,10 +105,10 @@ def get_relationship_properties(id, properties = nil) def remove_relationship_properties(id, properties = nil) if properties.nil? - rescue_ij { delete("/db/data/relationship/#{id}/properties") } + delete("/relationship/#{id}/properties") else properties.to_a.each do |property| - rescue_ij { delete("/db/data/relationship/#{id}/properties/#{property}") } + delete("/relationship/#{id}/properties/#{property}") end end end @@ -100,21 +116,21 @@ def remove_relationship_properties(id, properties = nil) def set_relationship_properties(id, properties) properties.each do |key, value| options = { :body => value.to_json, :headers => {'Content-Type' => 'application/json'} } - rescue_ij { put("/db/data/relationship/#{id}/properties/#{key}", options) } + put("/relationship/#{id}/properties/#{key}", options) end end def delete_relationship(id) - rescue_ij { delete("/db/data/relationship/#{id}") } + delete("/relationship/#{id}") end def get_node_relationships(id, dir=nil, types=nil) dir = get_dir(dir) if types.nil? - node_relationships = rescue_ij { get("/db/data/node/#{id}/relationships/#{dir}") } || Array.new + node_relationships = get("/node/#{id}/relationships/#{dir}") || Array.new else - node_relationships = rescue_ij { get("/db/data/node/#{id}/relationships/#{dir}/#{types.to_a.join('&')}") } || Array.new + node_relationships = get("/node/#{id}/relationships/#{dir}/#{types.to_a.join('&')}") || Array.new end return nil if node_relationships.empty? node_relationships @@ -123,51 +139,81 @@ def get_node_relationships(id, dir=nil, types=nil) def delete_node!(id) relationships = get_node_relationships(id) relationships.each { |r| delete_relationship(r["self"].split('/').last) } unless relationships.nil? - rescue_ij { delete("/db/data/node/#{id}") } + delete("/node/#{id}") end def list_indexes - rescue_ij { get("/db/data/index") } + get("/index") end def add_to_index(key, value, id) - options = { :body => (Neography::Config.to_s + "/node/#{id}").to_json, :headers => {'Content-Type' => 'application/json'} } - rescue_ij { post("/db/data/index/node/#{key}/#{value}", options) } + options = { :body => (self.configuration + "/node/#{id}").to_json, :headers => {'Content-Type' => 'application/json'} } + post("/index/node/#{key}/#{value}", options) end def remove_from_index(key, value, id) - rescue_ij { delete("/db/data/index/node/#{key}/#{value}/#{id}") } + delete("/index/node/#{key}/#{value}/#{id}") end def get_index(key, value) - index = rescue_ij { get("/db/data/index/node/#{key}/#{value}") } || Array.new + index = get("/index/node/#{key}/#{value}") || Array.new return nil if index.empty? index end def get_path(from, to, relationships, depth=1, algorithm="allPaths") - options = { :body => {"to" => Neography::Config.to_s + "/node/#{to}", "relationships" => relationships, "max depth" => depth, "algorithm" => get_algorithm(algorithm) }.to_json, :headers => {'Content-Type' => 'application/json'} } - path = rescue_ij { post("/db/data/node/#{from}/path", options) } || Hash.new + options = { :body => {"to" => self.configuration + "/node/#{to}", "relationships" => relationships, "max depth" => depth, "algorithm" => get_algorithm(algorithm) }.to_json, :headers => {'Content-Type' => 'application/json'} } + path = post("/node/#{from}/path", options) || Hash.new end def get_paths(from, to, relationships, depth=1, algorithm="allPaths") - options = { :body => {"to" => Neography::Config.to_s + "/node/#{to}", "relationships" => relationships, "max depth" => depth, "algorithm" => get_algorithm(algorithm) }.to_json, :headers => {'Content-Type' => 'application/json'} } - paths = rescue_ij { post("/db/data/node/#{from}/paths", options) } || Array.new + options = { :body => {"to" => self.configuration + "/node/#{to}", "relationships" => relationships, "max depth" => depth, "algorithm" => get_algorithm(algorithm) }.to_json, :headers => {'Content-Type' => 'application/json'} } + paths = post("/node/#{from}/paths", options) || Array.new + end + + private + + def evaluate_response(response) + code = response.code + body = response.body + + case code + when 200 + @logger.debug "OK" if @log_enabled + response + when 201 + @logger.debug "OK, created #{body}" if @log_enabled + response + when 204 + @logger.debug "OK, no content returned" if @log_enabled + nil + when 400 + @logger.error "Invalid data sent #{body}" if @log_enabled + nil + when 404 + @logger.error "#{body}" if @log_enabled + nil + when 409 + @logger.error "Node could not be deleted (still has relationships?)" if @log_enabled + nil + end end - private + def get(path,options={}) + evaluate_response(HTTParty.get(configuration + path, options)) + end - # Rescue from Invalid JSON error thrown by Crack Gem + def post(path,options={}) + evaluate_response(HTTParty.post(configuration + path, options)) + end - def rescue_ij(&block) - begin - response = yield - response = response.parsed_response - rescue - response = nil - end - response - end + def put(path,options={}) + evaluate_response(HTTParty.put(configuration + path, options)) + end + + def delete(path,options={}) + evaluate_response(HTTParty.delete(configuration + path, options)) + end def get_dir(dir) case dir @@ -191,8 +237,5 @@ def get_algorithm(algorithm) end end - - end - end end \ No newline at end of file diff --git a/spec/integration/rest_index_spec.rb b/spec/integration/rest_index_spec.rb index b19f444..939376f 100644 --- a/spec/integration/rest_index_spec.rb +++ b/spec/integration/rest_index_spec.rb @@ -1,51 +1,54 @@ require File.join(File.dirname(__FILE__), '..', 'spec_helper') describe Neography::Rest do + before(:each) do + @neo = Neography::Rest.new + end describe "list indexes" do it "can get a listing of indexes" do - Neography::Rest.list_indexes.should_not be_nil + @neo.list_indexes.should_not be_nil end end describe "add to index" do it "can add a node to an index" do - new_node = Neography::Rest.create_node + new_node = @neo.create_node new_node[:id] = new_node["self"].split('/').last key = generate_text(6) value = generate_text - Neography::Rest.add_to_index(key, value, new_node[:id]) - new_index = Neography::Rest.get_index(key, value) + @neo.add_to_index(key, value, new_node[:id]) + new_index = @neo.get_index(key, value) new_index.should_not be_nil - Neography::Rest.remove_from_index(key, value, new_node[:id]) + @neo.remove_from_index(key, value, new_node[:id]) end end describe "remove from index" do it "can remove a node from an index" do - new_node = Neography::Rest.create_node + new_node = @neo.create_node new_node[:id] = new_node["self"].split('/').last key = generate_text(6) value = generate_text - Neography::Rest.add_to_index(key, value, new_node[:id]) - new_index = Neography::Rest.get_index(key, value) + @neo.add_to_index(key, value, new_node[:id]) + new_index = @neo.get_index(key, value) new_index.should_not be_nil - Neography::Rest.remove_from_index(key, value, new_node[:id]) - new_index = Neography::Rest.get_index(key, value) + @neo.remove_from_index(key, value, new_node[:id]) + new_index = @neo.get_index(key, value) new_index.should be_nil end end describe "get index" do it "can get an index" do - new_node = Neography::Rest.create_node + new_node = @neo.create_node new_node[:id] = new_node["self"].split('/').last key = generate_text(6) value = generate_text - Neography::Rest.add_to_index(key, value, new_node[:id]) - new_index = Neography::Rest.get_index(key, value) + @neo.add_to_index(key, value, new_node[:id]) + new_index = @neo.get_index(key, value) new_index.should_not be_nil - Neography::Rest.remove_from_index(key, value, new_node[:id]) + @neo.remove_from_index(key, value, new_node[:id]) end end diff --git a/spec/integration/rest_node_spec.rb b/spec/integration/rest_node_spec.rb index b44a967..b282429 100644 --- a/spec/integration/rest_node_spec.rb +++ b/spec/integration/rest_node_spec.rb @@ -1,27 +1,30 @@ require File.join(File.dirname(__FILE__), '..', 'spec_helper') describe Neography::Rest do + before(:each) do + @neo = Neography::Rest.new + end describe "get_root" do it "can get the root node" do - root_node = Neography::Rest.get_root + root_node = @neo.get_root root_node.should have_key("reference_node") end end describe "create_node" do it "can create an empty node" do - new_node = Neography::Rest.create_node + new_node = @neo.create_node new_node.should_not be_nil end it "can create a node with one property" do - new_node = Neography::Rest.create_node("name" => "Max") + new_node = @neo.create_node("name" => "Max") new_node["data"]["name"].should == "Max" end it "can create a node with more than one property" do - new_node = Neography::Rest.create_node("age" => 31, "name" => "Max") + new_node = @neo.create_node("age" => 31, "name" => "Max") new_node["data"]["name"].should == "Max" new_node["data"]["age"].should == 31 end @@ -29,127 +32,127 @@ describe "get_node" do it "can get a node that exists" do - new_node = Neography::Rest.create_node + new_node = @neo.create_node new_node[:id] = new_node["self"].split('/').last - existing_node = Neography::Rest.get_node(new_node[:id]) + existing_node = @neo.get_node(new_node[:id]) existing_node.should_not be_nil existing_node.should have_key("self") existing_node["self"].split('/').last.should == new_node[:id] end it "returns nil if it tries to get a node that does not exist" do - new_node = Neography::Rest.create_node + new_node = @neo.create_node new_node[:id] = new_node["self"].split('/').last - existing_node = Neography::Rest.get_node(new_node[:id].to_i + 1000) + existing_node = @neo.get_node(new_node[:id].to_i + 1000) existing_node.should be_nil end end describe "set_node_properties" do it "can set a node's properties" do - new_node = Neography::Rest.create_node + new_node = @neo.create_node new_node[:id] = new_node["self"].split('/').last - Neography::Rest.set_node_properties(new_node[:id], {"weight" => 200, "eyes" => "brown"}) - existing_node = Neography::Rest.get_node(new_node[:id]) + @neo.set_node_properties(new_node[:id], {"weight" => 200, "eyes" => "brown"}) + existing_node = @neo.get_node(new_node[:id]) existing_node["data"]["weight"].should == 200 existing_node["data"]["eyes"].should == "brown" end it "it fails to set properties on a node that does not exist" do - new_node = Neography::Rest.create_node + new_node = @neo.create_node new_node[:id] = new_node["self"].split('/').last - Neography::Rest.set_node_properties(new_node[:id].to_i + 1000, {"weight" => 150, "hair" => "blonde"}) - node_properties = Neography::Rest.get_node_properties(new_node[:id].to_i + 1000) + @neo.set_node_properties(new_node[:id].to_i + 1000, {"weight" => 150, "hair" => "blonde"}) + node_properties = @neo.get_node_properties(new_node[:id].to_i + 1000) node_properties.should be_nil end end describe "reset_node_properties" do it "can reset a node's properties" do - new_node = Neography::Rest.create_node + new_node = @neo.create_node new_node[:id] = new_node["self"].split('/').last - Neography::Rest.set_node_properties(new_node[:id], {"weight" => 200, "eyes" => "brown", "hair" => "black"}) - Neography::Rest.reset_node_properties(new_node[:id], {"weight" => 190, "eyes" => "blue"}) - existing_node = Neography::Rest.get_node(new_node[:id]) + @neo.set_node_properties(new_node[:id], {"weight" => 200, "eyes" => "brown", "hair" => "black"}) + @neo.reset_node_properties(new_node[:id], {"weight" => 190, "eyes" => "blue"}) + existing_node = @neo.get_node(new_node[:id]) existing_node["data"]["weight"].should == 190 existing_node["data"]["eyes"].should == "blue" existing_node["data"]["hair"].should be_nil end it "it fails to reset properties on a node that does not exist" do - new_node = Neography::Rest.create_node + new_node = @neo.create_node new_node[:id] = new_node["self"].split('/').last - Neography::Rest.reset_node_properties(new_node[:id].to_i + 1000, {"weight" => 170, "eyes" => "green"}) - node_properties = Neography::Rest.get_node_properties(new_node[:id].to_i + 1000) + @neo.reset_node_properties(new_node[:id].to_i + 1000, {"weight" => 170, "eyes" => "green"}) + node_properties = @neo.get_node_properties(new_node[:id].to_i + 1000) node_properties.should be_nil end end describe "get_node_properties" do it "can get all of a node's properties" do - new_node = Neography::Rest.create_node("weight" => 200, "eyes" => "brown") + new_node = @neo.create_node("weight" => 200, "eyes" => "brown") new_node[:id] = new_node["self"].split('/').last - node_properties = Neography::Rest.get_node_properties(new_node[:id]) + node_properties = @neo.get_node_properties(new_node[:id]) node_properties["weight"].should == 200 node_properties["eyes"].should == "brown" end it "can get some of a node's properties" do - new_node = Neography::Rest.create_node("weight" => 200, "eyes" => "brown", "height" => "2m") + new_node = @neo.create_node("weight" => 200, "eyes" => "brown", "height" => "2m") new_node[:id] = new_node["self"].split('/').last - node_properties = Neography::Rest.get_node_properties(new_node[:id], ["weight", "height"]) + node_properties = @neo.get_node_properties(new_node[:id], ["weight", "height"]) node_properties["weight"].should == 200 node_properties["height"].should == "2m" node_properties["eyes"].should be_nil end it "returns nil if it gets the properties on a node that does not have any" do - new_node = Neography::Rest.create_node + new_node = @neo.create_node new_node[:id] = new_node["self"].split('/').last - Neography::Rest.get_node_properties(new_node[:id]).should be_nil + @neo.get_node_properties(new_node[:id]).should be_nil end it "returns nil if it tries to get some of the properties on a node that does not have any" do - new_node = Neography::Rest.create_node + new_node = @neo.create_node new_node[:id] = new_node["self"].split('/').last - Neography::Rest.get_node_properties(new_node[:id], ["weight", "height"]).should be_nil + @neo.get_node_properties(new_node[:id], ["weight", "height"]).should be_nil end it "returns nil if it fails to get properties on a node that does not exist" do - new_node = Neography::Rest.create_node + new_node = @neo.create_node new_node[:id] = new_node["self"].split('/').last - Neography::Rest.get_node_properties(new_node[:id].to_i + 10000).should be_nil + @neo.get_node_properties(new_node[:id].to_i + 10000).should be_nil end end describe "remove_node_properties" do it "can remove a node's properties" do - new_node = Neography::Rest.create_node("weight" => 200, "eyes" => "brown") + new_node = @neo.create_node("weight" => 200, "eyes" => "brown") new_node[:id] = new_node["self"].split('/').last - Neography::Rest.remove_node_properties(new_node[:id]) - Neography::Rest.get_node_properties(new_node[:id]).should be_nil + @neo.remove_node_properties(new_node[:id]) + @neo.get_node_properties(new_node[:id]).should be_nil end it "returns nil if it fails to remove the properties of a node that does not exist" do - new_node = Neography::Rest.create_node + new_node = @neo.create_node new_node[:id] = new_node["self"].split('/').last - Neography::Rest.remove_node_properties(new_node[:id].to_i + 10000).should be_nil + @neo.remove_node_properties(new_node[:id].to_i + 10000).should be_nil end it "can remove a specific node property" do - new_node = Neography::Rest.create_node("weight" => 200, "eyes" => "brown") + new_node = @neo.create_node("weight" => 200, "eyes" => "brown") new_node[:id] = new_node["self"].split('/').last - Neography::Rest.remove_node_properties(new_node[:id], "weight") - node_properties = Neography::Rest.get_node_properties(new_node[:id]) + @neo.remove_node_properties(new_node[:id], "weight") + node_properties = @neo.get_node_properties(new_node[:id]) node_properties["weight"].should be_nil node_properties["eyes"].should == "brown" end it "can remove more than one property" do - new_node = Neography::Rest.create_node("weight" => 200, "eyes" => "brown", "height" => "2m") + new_node = @neo.create_node("weight" => 200, "eyes" => "brown", "height" => "2m") new_node[:id] = new_node["self"].split('/').last - Neography::Rest.remove_node_properties(new_node[:id], ["weight", "eyes"]) - node_properties = Neography::Rest.get_node_properties(new_node[:id]) + @neo.remove_node_properties(new_node[:id], ["weight", "eyes"]) + node_properties = @neo.get_node_properties(new_node[:id]) node_properties["weight"].should be_nil node_properties["eyes"].should be_nil end @@ -157,80 +160,80 @@ describe "delete_node" do it "can delete an unrelated node" do - new_node = Neography::Rest.create_node + new_node = @neo.create_node new_node[:id] = new_node["self"].split('/').last - Neography::Rest.delete_node(new_node[:id]).should be_nil - existing_node = Neography::Rest.get_node(new_node[:id]) + @neo.delete_node(new_node[:id]).should be_nil + existing_node = @neo.get_node(new_node[:id]) existing_node.should be_nil end it "cannot delete a node that has relationships" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id]) - Neography::Rest.delete_node(new_node1[:id]).should be_nil - existing_node = Neography::Rest.get_node(new_node1[:id]) + @neo.create_relationship("friends", new_node1[:id], new_node2[:id]) + @neo.delete_node(new_node1[:id]).should be_nil + existing_node = @neo.get_node(new_node1[:id]) existing_node.should_not be_nil end it "returns nil if it tries to delete a node that does not exist" do - new_node = Neography::Rest.create_node + new_node = @neo.create_node new_node[:id] = new_node["self"].split('/').last - Neography::Rest.delete_node(new_node[:id].to_i + 1000).should be_nil - existing_node = Neography::Rest.get_node(new_node[:id].to_i + 1000) + @neo.delete_node(new_node[:id].to_i + 1000).should be_nil + existing_node = @neo.get_node(new_node[:id].to_i + 1000) existing_node.should be_nil end it "returns nil if it tries to delete a node that has already been deleted" do - new_node = Neography::Rest.create_node + new_node = @neo.create_node new_node[:id] = new_node["self"].split('/').last - Neography::Rest.delete_node(new_node[:id]).should be_nil - existing_node = Neography::Rest.get_node(new_node[:id]) + @neo.delete_node(new_node[:id]).should be_nil + existing_node = @neo.get_node(new_node[:id]) existing_node.should be_nil - Neography::Rest.delete_node(new_node[:id]).should be_nil - existing_node = Neography::Rest.get_node(new_node[:id]) + @neo.delete_node(new_node[:id]).should be_nil + existing_node = @neo.get_node(new_node[:id]) existing_node.should be_nil end end describe "delete_node!" do it "can delete an unrelated node" do - new_node = Neography::Rest.create_node + new_node = @neo.create_node new_node[:id] = new_node["self"].split('/').last - Neography::Rest.delete_node!(new_node[:id]).should be_nil - existing_node = Neography::Rest.get_node(new_node[:id]) + @neo.delete_node!(new_node[:id]).should be_nil + existing_node = @neo.get_node(new_node[:id]) existing_node.should be_nil end it "can delete a node that has relationships" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id]) - Neography::Rest.delete_node!(new_node1[:id]).should be_nil - existing_node = Neography::Rest.get_node(new_node1[:id]) + @neo.create_relationship("friends", new_node1[:id], new_node2[:id]) + @neo.delete_node!(new_node1[:id]).should be_nil + existing_node = @neo.get_node(new_node1[:id]) existing_node.should be_nil end it "returns nil if it tries to delete a node that does not exist" do - new_node = Neography::Rest.create_node + new_node = @neo.create_node new_node[:id] = new_node["self"].split('/').last - Neography::Rest.delete_node!(new_node[:id].to_i + 1000).should be_nil - existing_node = Neography::Rest.get_node(new_node[:id].to_i + 1000) + @neo.delete_node!(new_node[:id].to_i + 1000).should be_nil + existing_node = @neo.get_node(new_node[:id].to_i + 1000) existing_node.should be_nil end it "returns nil if it tries to delete a node that has already been deleted" do - new_node = Neography::Rest.create_node + new_node = @neo.create_node new_node[:id] = new_node["self"].split('/').last - Neography::Rest.delete_node!(new_node[:id]).should be_nil - existing_node = Neography::Rest.get_node(new_node[:id]) + @neo.delete_node!(new_node[:id]).should be_nil + existing_node = @neo.get_node(new_node[:id]) existing_node.should be_nil - Neography::Rest.delete_node!(new_node[:id]).should be_nil - existing_node = Neography::Rest.get_node(new_node[:id]) + @neo.delete_node!(new_node[:id]).should be_nil + existing_node = @neo.get_node(new_node[:id]) existing_node.should be_nil end end diff --git a/spec/integration/rest_path_spec.rb b/spec/integration/rest_path_spec.rb index 439eb8e..3957282 100644 --- a/spec/integration/rest_path_spec.rb +++ b/spec/integration/rest_path_spec.rb @@ -1,15 +1,18 @@ require File.join(File.dirname(__FILE__), '..', 'spec_helper') describe Neography::Rest do + before(:each) do + @neo = Neography::Rest.new + end describe "get path" do it "can get a path between two nodes" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2005', "met" => "college"}) - path = Neography::Rest.get_path(new_node1[:id], new_node2[:id], {"type"=> "friends", "direction" => "out"}) + new_relationship = @neo.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2005', "met" => "college"}) + path = @neo.get_path(new_node1[:id], new_node2[:id], {"type"=> "friends", "direction" => "out"}) path["start"].should == new_node1["self"] path["end"].should == new_node2["self"] path["nodes"].should == [new_node1["self"], new_node2["self"]] diff --git a/spec/integration/rest_relationship_spec.rb b/spec/integration/rest_relationship_spec.rb index 78a5bfc..08bbf63 100644 --- a/spec/integration/rest_relationship_spec.rb +++ b/spec/integration/rest_relationship_spec.rb @@ -1,33 +1,36 @@ require File.join(File.dirname(__FILE__), '..', 'spec_helper') describe Neography::Rest do + before(:each) do + @neo = Neography::Rest.new + end describe "create_relationship" do it "can create an empty relationship" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id]) + new_relationship = @neo.create_relationship("friends", new_node1[:id], new_node2[:id]) new_relationship["start"].should_not be_nil new_relationship["end"].should_not be_nil end it "can create a relationship with one property" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2010'}) + new_relationship = @neo.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2010'}) new_relationship["data"]["since"].should == '10-1-2010' end it "can create a relationship with more than one property" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2010', "met" => "college"}) + new_relationship = @neo.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2010', "met" => "college"}) new_relationship["data"]["since"].should == '10-1-2010' new_relationship["data"]["met"].should == "college" end @@ -35,167 +38,167 @@ describe "set_relationship_properties" do it "can set a relationship's properties" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id]) + new_relationship = @neo.create_relationship("friends", new_node1[:id], new_node2[:id]) new_relationship[:id] = new_relationship["self"].split('/').last - Neography::Rest.set_relationship_properties(new_relationship[:id], {"since" => '10-1-2010', "met" => "college"}) - Neography::Rest.set_relationship_properties(new_relationship[:id], {"roommates" => "no"}) - relationship_properties = Neography::Rest.get_relationship_properties(new_relationship[:id]) + @neo.set_relationship_properties(new_relationship[:id], {"since" => '10-1-2010', "met" => "college"}) + @neo.set_relationship_properties(new_relationship[:id], {"roommates" => "no"}) + relationship_properties = @neo.get_relationship_properties(new_relationship[:id]) relationship_properties["since"].should == '10-1-2010' relationship_properties["met"].should == "college" relationship_properties["roommates"].should == "no" end it "it fails to set properties on a relationship that does not exist" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id]) + new_relationship = @neo.create_relationship("friends", new_node1[:id], new_node2[:id]) new_relationship[:id] = new_relationship["self"].split('/').last - Neography::Rest.set_relationship_properties(new_relationship[:id].to_i + 10000, {"since" => '10-1-2010', "met" => "college"}) - relationship_properties = Neography::Rest.get_relationship_properties(new_relationship[:id].to_i + 10000) + @neo.set_relationship_properties(new_relationship[:id].to_i + 10000, {"since" => '10-1-2010', "met" => "college"}) + relationship_properties = @neo.get_relationship_properties(new_relationship[:id].to_i + 10000) relationship_properties.should be_nil end end describe "reset_relationship_properties" do it "can reset a relationship's properties" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id]) + new_relationship = @neo.create_relationship("friends", new_node1[:id], new_node2[:id]) new_relationship[:id] = new_relationship["self"].split('/').last - Neography::Rest.set_relationship_properties(new_relationship[:id], {"since" => '10-1-2010', "met" => "college"}) - Neography::Rest.reset_relationship_properties(new_relationship[:id], {"roommates" => "no"}) - relationship_properties = Neography::Rest.get_relationship_properties(new_relationship[:id]) + @neo.set_relationship_properties(new_relationship[:id], {"since" => '10-1-2010', "met" => "college"}) + @neo.reset_relationship_properties(new_relationship[:id], {"roommates" => "no"}) + relationship_properties = @neo.get_relationship_properties(new_relationship[:id]) relationship_properties["since"].should be_nil relationship_properties["met"].should be_nil relationship_properties["roommates"].should == "no" end it "it fails to reset properties on a relationship that does not exist" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id]) + new_relationship = @neo.create_relationship("friends", new_node1[:id], new_node2[:id]) new_relationship[:id] = new_relationship["self"].split('/').last - Neography::Rest.reset_relationship_properties(new_relationship[:id].to_i + 10000, {"since" => '10-1-2010', "met" => "college"}) - relationship_properties = Neography::Rest.get_relationship_properties(new_relationship[:id].to_i + 10000) + @neo.reset_relationship_properties(new_relationship[:id].to_i + 10000, {"since" => '10-1-2010', "met" => "college"}) + relationship_properties = @neo.get_relationship_properties(new_relationship[:id].to_i + 10000) relationship_properties.should be_nil end end describe "get_relationship_properties" do it "can get all of a relationship's properties" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2010', "met" => "college"}) + new_relationship = @neo.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2010', "met" => "college"}) new_relationship[:id] = new_relationship["self"].split('/').last - relationship_properties = Neography::Rest.get_relationship_properties(new_relationship[:id]) + relationship_properties = @neo.get_relationship_properties(new_relationship[:id]) relationship_properties["since"].should == '10-1-2010' relationship_properties["met"].should == "college" end it "can get some of a relationship's properties" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2010', "met" => "college", "roommates" => "no"}) + new_relationship = @neo.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2010', "met" => "college", "roommates" => "no"}) new_relationship[:id] = new_relationship["self"].split('/').last - relationship_properties = Neography::Rest.get_relationship_properties(new_relationship[:id], ["since", "roommates"]) + relationship_properties = @neo.get_relationship_properties(new_relationship[:id], ["since", "roommates"]) relationship_properties["since"].should == '10-1-2010' relationship_properties["met"].should be_nil relationship_properties["roommates"].should == "no" end it "returns nil if it gets the properties on a relationship that does not have any" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id]) + new_relationship = @neo.create_relationship("friends", new_node1[:id], new_node2[:id]) new_relationship[:id] = new_relationship["self"].split('/').last - relationship_properties = Neography::Rest.get_relationship_properties(new_relationship[:id]) + relationship_properties = @neo.get_relationship_properties(new_relationship[:id]) relationship_properties.should be_nil end it "returns nil if it tries to get some of the properties on a relationship that does not have any" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id]) + new_relationship = @neo.create_relationship("friends", new_node1[:id], new_node2[:id]) new_relationship[:id] = new_relationship["self"].split('/').last - relationship_properties = Neography::Rest.get_relationship_properties(new_relationship[:id], ["since", "roommates"]) + relationship_properties = @neo.get_relationship_properties(new_relationship[:id], ["since", "roommates"]) relationship_properties.should be_nil end it "returns nil if it fails to get properties on a relationship that does not exist" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id]) + new_relationship = @neo.create_relationship("friends", new_node1[:id], new_node2[:id]) new_relationship[:id] = new_relationship["self"].split('/').last - relationship_properties = Neography::Rest.get_relationship_properties(new_relationship[:id].to_i + 10000) + relationship_properties = @neo.get_relationship_properties(new_relationship[:id].to_i + 10000) relationship_properties.should be_nil end end describe "remove_relationship_properties" do it "can remove a relationship's properties" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2010', "met" => "college"}) + new_relationship = @neo.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2010', "met" => "college"}) new_relationship[:id] = new_relationship["self"].split('/').last - Neography::Rest.remove_relationship_properties(new_relationship[:id]) - Neography::Rest.get_relationship_properties(new_relationship[:id]).should be_nil + @neo.remove_relationship_properties(new_relationship[:id]) + @neo.get_relationship_properties(new_relationship[:id]).should be_nil end it "returns nil if it fails to remove the properties of a relationship that does not exist" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2010', "met" => "college"}) + new_relationship = @neo.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2010', "met" => "college"}) new_relationship[:id] = new_relationship["self"].split('/').last - Neography::Rest.remove_relationship_properties(new_relationship[:id]) - Neography::Rest.get_relationship_properties(new_relationship[:id].to_i + 10000).should be_nil + @neo.remove_relationship_properties(new_relationship[:id]) + @neo.get_relationship_properties(new_relationship[:id].to_i + 10000).should be_nil end it "can remove a specific relationship property" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2010', "met" => "college"}) + new_relationship = @neo.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2010', "met" => "college"}) new_relationship[:id] = new_relationship["self"].split('/').last - Neography::Rest.remove_relationship_properties(new_relationship[:id], "met") - relationship_properties = Neography::Rest.get_relationship_properties(new_relationship[:id], ["met", "since"]) + @neo.remove_relationship_properties(new_relationship[:id], "met") + relationship_properties = @neo.get_relationship_properties(new_relationship[:id], ["met", "since"]) relationship_properties["met"].should be_nil relationship_properties["since"].should == '10-1-2010' end it "can remove more than one relationship property" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2010', "met" => "college", "roommates" => "no"}) + new_relationship = @neo.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2010', "met" => "college", "roommates" => "no"}) new_relationship[:id] = new_relationship["self"].split('/').last - Neography::Rest.remove_relationship_properties(new_relationship[:id], ["met", "since"]) - relationship_properties = Neography::Rest.get_relationship_properties(new_relationship[:id], ["since", "met", "roommates"]) + @neo.remove_relationship_properties(new_relationship[:id], ["met", "since"]) + relationship_properties = @neo.get_relationship_properties(new_relationship[:id], ["since", "met", "roommates"]) relationship_properties["met"].should be_nil relationship_properties["since"].should be_nil relationship_properties["roommates"].should == "no" @@ -204,50 +207,50 @@ describe "delete_relationship" do it "can delete an existing relationship" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2010', "met" => "college"}) + new_relationship = @neo.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2010', "met" => "college"}) new_relationship[:id] = new_relationship["self"].split('/').last - Neography::Rest.delete_relationship(new_relationship[:id]) - relationships = Neography::Rest.get_node_relationships(new_node1[:id]) + @neo.delete_relationship(new_relationship[:id]) + relationships = @neo.get_node_relationships(new_node1[:id]) relationships.should be_nil end it "returns nil if it tries to delete a relationship that does not exist" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2010', "met" => "college"}) + new_relationship = @neo.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2010', "met" => "college"}) new_relationship[:id] = new_relationship["self"].split('/').last - existing_relationship = Neography::Rest.delete_relationship(new_relationship[:id].to_i + 1000) + existing_relationship = @neo.delete_relationship(new_relationship[:id].to_i + 1000) existing_relationship.should be_nil end it "returns nil if it tries to delete a relationship that has already been deleted" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2010', "met" => "college"}) + new_relationship = @neo.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2010', "met" => "college"}) new_relationship[:id] = new_relationship["self"].split('/').last - existing_relationship = Neography::Rest.delete_relationship(new_relationship[:id]) + existing_relationship = @neo.delete_relationship(new_relationship[:id]) existing_relationship.should be_nil - existing_relationship = Neography::Rest.delete_relationship(new_relationship[:id]) + existing_relationship = @neo.delete_relationship(new_relationship[:id]) existing_relationship.should be_nil end end describe "get_node_relationships" do it "can get a node's relationship" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2005', "met" => "college"}) - relationships = Neography::Rest.get_node_relationships(new_node1[:id]) + new_relationship = @neo.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2005', "met" => "college"}) + relationships = @neo.get_node_relationships(new_node1[:id]) relationships.should_not be_nil relationships[0]["start"].split('/').last.should == new_node1[:id] relationships[0]["end"].split('/').last.should == new_node2[:id] @@ -257,15 +260,15 @@ end it "can get a node's multiple relationships" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - new_node3 = Neography::Rest.create_node + new_node3 = @neo.create_node new_node3[:id] = new_node3["self"].split('/').last - new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2005', "met" => "college"}) - new_relationship = Neography::Rest.create_relationship("enemies", new_node1[:id], new_node3[:id], {"since" => '10-2-2010', "met" => "work"}) - relationships = Neography::Rest.get_node_relationships(new_node1[:id]) + new_relationship = @neo.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2005', "met" => "college"}) + new_relationship = @neo.create_relationship("enemies", new_node1[:id], new_node3[:id], {"since" => '10-2-2010', "met" => "work"}) + relationships = @neo.get_node_relationships(new_node1[:id]) relationships.should_not be_nil relationships[0]["start"].split('/').last.should == new_node1[:id] relationships[0]["end"].split('/').last.should == new_node2[:id] @@ -280,15 +283,15 @@ end it "can get a node's outgoing relationship" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - new_node3 = Neography::Rest.create_node + new_node3 = @neo.create_node new_node3[:id] = new_node3["self"].split('/').last - new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2005', "met" => "college"}) - new_relationship = Neography::Rest.create_relationship("enemies", new_node3[:id], new_node1[:id], {"since" => '10-2-2010', "met" => "work"}) - relationships = Neography::Rest.get_node_relationships(new_node1[:id], "out") + new_relationship = @neo.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2005', "met" => "college"}) + new_relationship = @neo.create_relationship("enemies", new_node3[:id], new_node1[:id], {"since" => '10-2-2010', "met" => "work"}) + relationships = @neo.get_node_relationships(new_node1[:id], "out") relationships.should_not be_nil relationships[0]["start"].split('/').last.should == new_node1[:id] relationships[0]["end"].split('/').last.should == new_node2[:id] @@ -299,15 +302,15 @@ end it "can get a node's incoming relationship" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - new_node3 = Neography::Rest.create_node + new_node3 = @neo.create_node new_node3[:id] = new_node3["self"].split('/').last - new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2005', "met" => "college"}) - new_relationship = Neography::Rest.create_relationship("enemies", new_node3[:id], new_node1[:id], {"since" => '10-2-2010', "met" => "work"}) - relationships = Neography::Rest.get_node_relationships(new_node1[:id], "in") + new_relationship = @neo.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2005', "met" => "college"}) + new_relationship = @neo.create_relationship("enemies", new_node3[:id], new_node1[:id], {"since" => '10-2-2010', "met" => "work"}) + relationships = @neo.get_node_relationships(new_node1[:id], "in") relationships.should_not be_nil relationships[0]["start"].split('/').last.should == new_node3[:id] relationships[0]["end"].split('/').last.should == new_node1[:id] @@ -318,15 +321,15 @@ end it "can get a specific type of node relationships" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - new_node3 = Neography::Rest.create_node + new_node3 = @neo.create_node new_node3[:id] = new_node3["self"].split('/').last - new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2005', "met" => "college"}) - new_relationship = Neography::Rest.create_relationship("enemies", new_node1[:id], new_node3[:id], {"since" => '10-2-2010', "met" => "work"}) - relationships = Neography::Rest.get_node_relationships(new_node1[:id], "all", "friends") + new_relationship = @neo.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2005', "met" => "college"}) + new_relationship = @neo.create_relationship("enemies", new_node1[:id], new_node3[:id], {"since" => '10-2-2010', "met" => "work"}) + relationships = @neo.get_node_relationships(new_node1[:id], "all", "friends") relationships.should_not be_nil relationships[0]["start"].split('/').last.should == new_node1[:id] relationships[0]["end"].split('/').last.should == new_node2[:id] @@ -337,18 +340,18 @@ end it "can get a specific type and direction of a node relationships" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - new_node2 = Neography::Rest.create_node + new_node2 = @neo.create_node new_node2[:id] = new_node2["self"].split('/').last - new_node3 = Neography::Rest.create_node + new_node3 = @neo.create_node new_node3[:id] = new_node3["self"].split('/').last - new_node4 = Neography::Rest.create_node + new_node4 = @neo.create_node new_node4[:id] = new_node4["self"].split('/').last - new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2005', "met" => "college"}) - new_relationship = Neography::Rest.create_relationship("enemies", new_node1[:id], new_node3[:id], {"since" => '10-2-2010', "met" => "work"}) - new_relationship = Neography::Rest.create_relationship("enemies", new_node4[:id], new_node1[:id], {"since" => '10-3-2010', "met" => "gym"}) - relationships = Neography::Rest.get_node_relationships(new_node1[:id], "in", "enemies") + new_relationship = @neo.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2005', "met" => "college"}) + new_relationship = @neo.create_relationship("enemies", new_node1[:id], new_node3[:id], {"since" => '10-2-2010', "met" => "work"}) + new_relationship = @neo.create_relationship("enemies", new_node4[:id], new_node1[:id], {"since" => '10-3-2010', "met" => "gym"}) + relationships = @neo.get_node_relationships(new_node1[:id], "in", "enemies") relationships.should_not be_nil relationships[0]["start"].split('/').last.should == new_node4[:id] relationships[0]["end"].split('/').last.should == new_node1[:id] @@ -359,9 +362,9 @@ end it "returns nil if there are no relationships" do - new_node1 = Neography::Rest.create_node + new_node1 = @neo.create_node new_node1[:id] = new_node1["self"].split('/').last - relationships = Neography::Rest.get_node_relationships(new_node1[:id]) + relationships = @neo.get_node_relationships(new_node1[:id]) relationships.should be_nil end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2a76399..ef23d43 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -16,9 +16,6 @@ # Net::HTTP.http_logger_options = {:body => true} # just the body # Net::HTTP.http_logger_options = {:verbose => true} # see everything - - - def generate_text(length=8) chars = 'abcdefghjkmnpqrstuvwxyz' key = ''