Skip to content

Commit

Permalink
Add some basic unit tests for REST classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
rdvdijk committed Sep 8, 2012
1 parent bbe019c commit d1b50e8
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 5 deletions.
1 change: 0 additions & 1 deletion lib/neography/rest/clean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ def initialize(connection)
@connection = connection
end


def execute
@connection.delete(clean_path)
end
Expand Down
8 changes: 4 additions & 4 deletions lib/neography/rest/nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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
17 changes: 17 additions & 0 deletions spec/unit/rest/clean_spec.rb
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions spec/unit/rest/cypher_spec.rb
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions spec/unit/rest/gremlin_spec.rb
Original file line number Diff line number Diff line change
@@ -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
194 changes: 194 additions & 0 deletions spec/unit/rest/nodes_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d1b50e8

Please sign in to comment.