From d1b50e839b151878350852ae2f7ad637f340e923 Mon Sep 17 00:00:00 2001 From: Roel van Dijk Date: Sat, 8 Sep 2012 17:07:45 +0200 Subject: [PATCH] Add some basic unit tests for REST classes. --- lib/neography/rest/clean.rb | 1 - lib/neography/rest/nodes.rb | 8 +- spec/unit/rest/clean_spec.rb | 17 +++ spec/unit/rest/cypher_spec.rb | 21 ++++ spec/unit/rest/gremlin_spec.rb | 26 +++++ spec/unit/rest/nodes_spec.rb | 194 +++++++++++++++++++++++++++++++++ 6 files changed, 262 insertions(+), 5 deletions(-) create mode 100644 spec/unit/rest/clean_spec.rb create mode 100644 spec/unit/rest/cypher_spec.rb create mode 100644 spec/unit/rest/gremlin_spec.rb create mode 100644 spec/unit/rest/nodes_spec.rb diff --git a/lib/neography/rest/clean.rb b/lib/neography/rest/clean.rb index 5dfd686..d0ea00c 100644 --- a/lib/neography/rest/clean.rb +++ b/lib/neography/rest/clean.rb @@ -10,7 +10,6 @@ def initialize(connection) @connection = connection end - def execute @connection.delete(clean_path) end diff --git a/lib/neography/rest/nodes.rb b/lib/neography/rest/nodes.rb index 3d95307..9f2cacb 100644 --- a/lib/neography/rest/nodes.rb +++ b/lib/neography/rest/nodes.rb @@ -48,6 +48,10 @@ def create_empty @connection.post(index_path) end + def delete(id) + @connection.delete(base_path(:id => get_id(id))) + end + def create_multiple(nodes) nodes = Array.new(nodes) if nodes.kind_of? Fixnum created_nodes = [] @@ -93,10 +97,6 @@ def create_multiple_threaded(nodes) created_nodes end - def delete(id) - @connection.delete(base_path(:id => get_id(id))) - end - end end end diff --git a/spec/unit/rest/clean_spec.rb b/spec/unit/rest/clean_spec.rb new file mode 100644 index 0000000..f10a1f6 --- /dev/null +++ b/spec/unit/rest/clean_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper' + +module Neography + class Rest + describe Clean do + + let(:connection) { stub } + subject { Clean.new(connection) } + + it "cleans the database" do + connection.should_receive(:delete).with("/cleandb/secret-key") + subject.execute + end + + end + end +end diff --git a/spec/unit/rest/cypher_spec.rb b/spec/unit/rest/cypher_spec.rb new file mode 100644 index 0000000..8b34b57 --- /dev/null +++ b/spec/unit/rest/cypher_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +module Neography + class Rest + describe Cypher do + + let(:connection) { stub(:cypher_path => "/cypher") } + subject { Cypher.new(connection) } + + it "executes a cypher query" do + options = { + :body=>"{\"query\":\"SOME QUERY\",\"params\":{\"foo\":\"bar\",\"baz\":\"qux\"}}", + :headers=>{"Content-Type"=>"application/json", "Accept"=>"application/json;stream=true"} + } + connection.should_receive(:post).with("/cypher", options) + subject.query("SOME QUERY", { :foo => "bar", :baz => "qux" }) + end + + end + end +end diff --git a/spec/unit/rest/gremlin_spec.rb b/spec/unit/rest/gremlin_spec.rb new file mode 100644 index 0000000..44b78dd --- /dev/null +++ b/spec/unit/rest/gremlin_spec.rb @@ -0,0 +1,26 @@ +require 'spec_helper' + +module Neography + class Rest + describe Gremlin do + + let(:connection) { stub(:gremlin_path => "/gremlin") } + subject { Gremlin.new(connection) } + + it "executes a gremlin script" do + options = { + :body=>"{\"script\":\"SOME SCRIPT\",\"params\":{\"foo\":\"bar\",\"baz\":\"qux\"}}", + :headers=>{"Content-Type"=>"application/json"} + } + connection.should_receive(:post).with("/gremlin", options) + subject.execute("SOME SCRIPT", { :foo => "bar", :baz => "qux" }) + end + + it "returns nil if script result is null" do + connection.stub(:post).and_return("null") + subject.execute("", {}).should be_nil + end + + end + end +end diff --git a/spec/unit/rest/nodes_spec.rb b/spec/unit/rest/nodes_spec.rb new file mode 100644 index 0000000..916b281 --- /dev/null +++ b/spec/unit/rest/nodes_spec.rb @@ -0,0 +1,194 @@ +require 'spec_helper' + +module Neography + class Rest + describe Nodes do + + let(:connection) { stub } + subject { Nodes.new(connection) } + + context "get nodes" do + it "gets single nodes" do + connection.should_receive(:get).with("/node/42") + subject.get("42") + end + + it "gets multiple nodes" do + connection.should_receive(:get).with("/node/42") + connection.should_receive(:get).with("/node/43") + subject.get_each("42", "43") + end + + it "returns multiple nodes in an array" do + connection.stub(:get).and_return("foo", "bar") + subject.get_each("42", "43").should == [ "foo", "bar" ] + end + + it "gets the root node" do + connection.stub(:get).with("/").and_return({ "reference_node" => "42" }) + connection.should_receive(:get).with("/node/42") + subject.root + end + + it "returns the root node" do + connection.stub(:get).and_return({ "reference_node" => "42" }, "foo") + subject.root.should == "foo" + end + end + + context "create nodes" do + + it "creates with attributes" do + options = { + :body => '{"foo":"bar","baz":"qux"}', + :headers => json_content_type + } + connection.should_receive(:post).with("/node", options) + subject.create_with_attributes({:foo => "bar", :baz => "qux"}) + end + + it "returns the created node" do + connection.stub(:post).and_return("foo") + subject.create_with_attributes({}).should == "foo" + end + + it "creates with attributes using #create method" do + options = { + :body => '{"foo":"bar","baz":"qux"}', + :headers => json_content_type + } + connection.should_receive(:post).with("/node", options) + subject.create({:foo => "bar", :baz => "qux"}) + end + + it "creates empty nodes" do + connection.should_receive(:post).with("/node") + subject.create_empty + end + + it "returns an empty node" do + connection.stub(:post).and_return("foo") + subject.create_empty.should == "foo" + end + + it "creates empty nodes using #create method" do + connection.should_receive(:post).with("/node") + subject.create + end + + end + + context "delete nodes" do + + it "deletes a node" do + connection.should_receive(:delete).with("/node/42") + subject.delete("42") + end + + end + + context "#create_multiple" do + + it "creates multiple with attributes" do + options1 = { + :body => '{"foo1":"bar1","baz1":"qux1"}', + :headers => json_content_type + } + options2 = { + :body => '{"foo2":"bar2","baz2":"qux2"}', + :headers => json_content_type + } + connection.should_receive(:post).with("/node", options1) + connection.should_receive(:post).with("/node", options2) + + subject.create_multiple([ + {:foo1 => "bar1", :baz1 => "qux1"}, + {:foo2 => "bar2", :baz2 => "qux2"} + ]) + end + + it "returns multiple nodes with attributes in an array" do + connection.stub(:post).and_return("foo", "bar") + subject.create_multiple([{},{}]).should == ["foo", "bar"] + end + + # exotic? + it "creates multiple with and without attributes" do + options1 = { + :body => '{"foo1":"bar1","baz1":"qux1"}', + :headers => json_content_type + } + connection.should_receive(:post).with("/node", options1) + connection.should_receive(:post).with("/node") + + subject.create_multiple([ + {:foo1 => "bar1", :baz1 => "qux1"}, + "not a hash" # ? + ]) + end + + it "creates multiple empty nodes" do + connection.should_receive(:post).with("/node").twice + subject.create_multiple(2) + end + + it "returns multiple empty nodes in an array" do + connection.stub(:post).and_return("foo", "bar") + subject.create_multiple(2).should == ["foo", "bar"] + end + + end + + context "#create_multiple_threaded" do + + let(:connection) { stub(:max_threads => 2) } + + it "creates multiple with attributes" do + options1 = { + :body => '{"foo1":"bar1","baz1":"qux1"}', + :headers => json_content_type + } + options2 = { + :body => '{"foo2":"bar2","baz2":"qux2"}', + :headers => json_content_type + } + connection.should_receive(:post).with("/node", options1) + connection.should_receive(:post).with("/node", options2) + + subject.create_multiple_threaded([ + {:foo1 => "bar1", :baz1 => "qux1"}, + {:foo2 => "bar2", :baz2 => "qux2"} + ]) + end + + # exotic? + it "creates multiple with and without attributes" do + options1 = { + :body => '{"foo1":"bar1","baz1":"qux1"}', + :headers => json_content_type + } + connection.should_receive(:post).with("/node", options1) + connection.should_receive(:post).with("/node") + + subject.create_multiple_threaded([ + {:foo1 => "bar1", :baz1 => "qux1"}, + "not a hash" # ? + ]) + end + + it "creates multiple empty nodes" do + connection.should_receive(:post).with("/node").twice + subject.create_multiple_threaded(2) + end + + end + + private + + def json_content_type + {"Content-Type"=>"application/json"} + end + + end + end +end