From 7a5c6a89eea4e88d81a8876d80bdbe819c644a00 Mon Sep 17 00:00:00 2001 From: Max De Marzi Date: Fri, 4 Nov 2011 17:43:52 -0500 Subject: [PATCH] removing fakeweb, escaping execute_script for gremlin, adding execute_query for cypher, adding tests --- Gemfile.lock | 13 +++++++------ README.rdoc | 4 ++-- lib/neography.rb | 1 + lib/neography/rest.rb | 13 +++++++++---- neography.gemspec | 1 - spec/integration/rest_plugin_spec.rb | 26 ++++++++++++++++++++++++++ spec/spec_helper.rb | 13 +------------ spec/support/fake_root_spec.rb | 5 ----- 8 files changed, 46 insertions(+), 30 deletions(-) create mode 100644 spec/integration/rest_plugin_spec.rb delete mode 100644 spec/support/fake_root_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index 7ea6ada..ea2bd62 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,21 +1,22 @@ PATH remote: . specs: - neography (0.0.13) + neography (0.0.15) httparty (~> 0.7.3) json + os + rubyzip GEM remote: http://rubygems.org/ specs: crack (0.1.8) diff-lcs (1.1.2) - fakeweb (1.3.0) - httparty (0.7.4) + httparty (0.7.8) crack (= 0.1.8) - json (1.4.6) - json (1.4.6-java) + json (1.6.1) net-http-spy (0.2.1) + os (0.9.4) rake (0.8.7) rspec (2.0.1) rspec-core (~> 2.0.1) @@ -27,13 +28,13 @@ GEM rspec-mocks (2.0.1) rspec-core (~> 2.0.1) rspec-expectations (~> 2.0.1) + rubyzip (0.9.4) PLATFORMS java ruby DEPENDENCIES - fakeweb (~> 1.3.0) neography! net-http-spy (= 0.2.1) rake (~> 0.8.7) diff --git a/README.rdoc b/README.rdoc index c219a73..eacc371 100644 --- a/README.rdoc +++ b/README.rdoc @@ -131,8 +131,8 @@ To Use: @neo.get_relationship_index(index, key, value) # exact query of the relationship index with the given key/value pair @neo.find_relationship_index(index, key, value) # advanced query of the relationship index with the given key/value pair @neo.find_relationship_index(index, query) # advanced query of the relationship index with the given query - @neo.execute_script(script) # sends an arbitrary Groovy script (through the Gremlin plugin) - + @neo.execute_script("g.v(0)") # sends a Groovy script(through the Gremlin plugin) + @neo.execute_query("start n=node(0) return n") # sends a Cypher query (through the Cypher plugin) @neo.get_path(node1, node2, relationships, depth=4, algorithm="shortestPath") # finds the shortest path between two nodes @neo.get_paths(node1, node2, relationships, depth=3, algorithm="allPaths") # finds all paths between two nodes diff --git a/lib/neography.rb b/lib/neography.rb index 0324909..8c0bd98 100644 --- a/lib/neography.rb +++ b/lib/neography.rb @@ -17,6 +17,7 @@ def find_and_require_user_defined_code DIRECTIONS = ["incoming", "in", "outgoing", "out", "all", "both"] +require 'cgi' require 'httparty' require 'json' require 'logger' diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index eb10f1e..b64bffe 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -334,17 +334,22 @@ def get_paths(from, to, relationships, depth=1, algorithm="allPaths") paths = post("/node/#{get_id(from)}/paths", options) || Array.new end - def execute_script(script) - options = { :body => { :script => script } } - post("/ext/GremlinPlugin/graphdb/execute_script", options) + def execute_query(query) + options = { :body => {:query => query}.to_json, :headers => {'Content-Type' => 'application/json'} } + result = post("/ext/CypherPlugin/graphdb/execute_query", options) end + def execute_script(script) + options = { :body => "script=" + CGI::escape(script), :headers => {'Content-Type' => 'application/x-www-form-urlencoded'} } + result = post("/ext/GremlinPlugin/graphdb/execute_script", options) + result == "null" ? nil : result + end + private def evaluate_response(response) code = response.code body = response.body - case code when 200 @logger.debug "OK" if @log_enabled diff --git a/neography.gemspec b/neography.gemspec index 080f544..96b3229 100644 --- a/neography.gemspec +++ b/neography.gemspec @@ -21,7 +21,6 @@ Gem::Specification.new do |s| s.add_development_dependency "rspec", "~> 2.0.0.beta.22" s.add_development_dependency "net-http-spy", "0.2.1" - s.add_development_dependency "fakeweb", "~> 1.3.0" s.add_development_dependency "rake", "~> 0.8.7" s.add_dependency "httparty", "~> 0.7.3" s.add_dependency "json" diff --git a/spec/integration/rest_plugin_spec.rb b/spec/integration/rest_plugin_spec.rb new file mode 100644 index 0000000..222d962 --- /dev/null +++ b/spec/integration/rest_plugin_spec.rb @@ -0,0 +1,26 @@ +require File.join(File.dirname(__FILE__), '..', 'spec_helper') + +describe Neography::Rest do + before(:each) do + @neo = Neography::Rest.new + end + + describe "execute gremlin script" do + it "can get the root node id" do + root_node = @neo.execute_script("g.v(0)") + root_node.should have_key("self") + root_node["self"].split('/').last.should == "0" + end + end + + describe "execute cypher query" do + it "can get the root node id" do + root_node = @neo.execute_query("start n=node(0) return n") + root_node.should have_key("data") + root_node.should have_key("columns") + root_node["data"][0][0].should have_key("self") + root_node["data"][0][0]["self"].split('/').last.should == "0" + end + end + +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6b16928..867f001 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,18 +1,7 @@ require 'neography' -require 'fake_web' require 'benchmark' -Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f} - -FakeWeb.allow_net_connect = false - -# To test against a real database: -# 1. Make sure empty database is running on your test neo4j server (bin/neo4j start) -# 2. Uncomment the next two lines -FakeWeb.clean_registry -FakeWeb.allow_net_connect = true - -# 3. If you want to see more, uncomment the next few lines +# If you want to see more, uncomment the next few lines # require 'net-http-spy' # Net::HTTP.http_logger_options = {:body => true} # just the body # Net::HTTP.http_logger_options = {:verbose => true} # see everything diff --git a/spec/support/fake_root_spec.rb b/spec/support/fake_root_spec.rb deleted file mode 100644 index becb6b2..0000000 --- a/spec/support/fake_root_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -FakeWeb.register_uri(:get, "http://localhost:7474/", :body => '{ - "index" : "http://localhost:7474/index", - "node" : "http://localhost:7474/node", - "reference_node" : "http://localhost:7474/node/0" -}') \ No newline at end of file