Skip to content

Commit

Permalink
Add NodeRelationships specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
rdvdijk committed Sep 8, 2012
1 parent 9cd051b commit cf6f52c
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/neography/rest/node_relationships.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,32 @@ def initialize(connection)
@connection = connection
end

def create(type, from, to, props)
def create(type, from, to, properties)
options = {
:body => {
:to => @connection.configuration + "/node/#{get_id(to)}",
:data => props,
:data => properties,
:type => type
}.to_json,
:headers => json_content_type }

@connection.post(base_path(:id => get_id(from)), options)
end

def get(id, direction, types)
direction = get_direction(direction)
def get(id, direction = nil, types = nil)
direction = parse_direction(direction)

if types.nil?
node_relationships = @connection.get(direction_path(:id => get_id(id), :direction => direction)) || []
else
node_relationships = @connection.get(type_path(:id => get_id(id), :direction => direction, :types => Array(types).join('&'))) || []
end

return nil if node_relationships.empty?
node_relationships
end

def get_direction(direction)
def parse_direction(direction)
case direction
when :incoming, "incoming", :in, "in"
"in"
Expand Down
16 changes: 16 additions & 0 deletions spec/matchers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Convenience matcher for matching JSON fields with a hash
RSpec::Matchers.define :json_match do |field, expected|

match do |actual|
expected == JSON.parse(actual[field])
end

failure_message_for_should do
"expected JSON in field '#{@field}' to not match '#{@expected}'"
end

description do
"JSON in field '#{field}' should match '#{expected.inspect}'"
end

end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'neography'
require 'benchmark'
require 'matchers'

# If you want to see more, uncomment the next few lines
# require 'net-http-spy'
Expand All @@ -17,6 +18,7 @@ def generate_text(length=8)
c.filter_run_excluding :slow => true, :break_gremlin => true
end


def json_content_type
{"Content-Type"=>"application/json"}
end
Expand Down
62 changes: 62 additions & 0 deletions spec/unit/rest/node_relationships_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require 'spec_helper'

module Neography
class Rest
describe NodeRelationships do

let(:connection) { stub(:configuration => "http://configuration") }
subject { NodeRelationships.new(connection) }

it "creates a relationship" do
body_hash = { "type" => "some_type",
"to" => "http://configuration/node/43",
"data" => {"foo"=>"bar","baz"=>"qux"}
}
connection.should_receive(:post).with("/node/42/relationships", json_match(:body, body_hash))

subject.create("some_type", "42", "43", {:foo => "bar", :baz => "qux"})
end

it "gets relationships" do
connection.should_receive(:get).with("/node/42/relationships/all")
subject.get("42")
end

it "gets relationships with direction" do
connection.should_receive(:get).with("/node/42/relationships/in")
subject.get("42", :in)
end

it "gets relationships with direction and type" do
connection.should_receive(:get).with("/node/42/relationships/in/foo")
subject.get("42", :in, "foo")
end

it "gets relationships with direction and types" do
connection.should_receive(:get).with("/node/42/relationships/in/foo&bar")
subject.get("42", :in, ["foo", "bar"])
end

context "directions" do

[ :incoming, "incoming", :in, "in" ].each do |direction|
it "parses 'in' direction" do
NodeRelationships.new(nil).parse_direction(direction).should == "in"
end
end

[ :outgoing, "outgoing", :out, "out" ].each do |direction|
it "parses 'out' direction" do
NodeRelationships.new(nil).parse_direction(direction).should == "out"
end
end

it "parses 'all' direction by default" do
NodeRelationships.new(nil).parse_direction("foo").should == "all"
end

end

end
end
end

0 comments on commit cf6f52c

Please sign in to comment.