Skip to content

Commit

Permalink
Clean up the Node and Relationship classes, add some basic specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roel van Dijk committed Sep 23, 2012
1 parent b4834e5 commit 6536a7e
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 16 deletions.
18 changes: 8 additions & 10 deletions lib/neography/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,21 @@ class Node < PropertyContainer

class << self
def create(*args)
# the arguments can be an hash of properties to set or a rest instance
props = (args[0].respond_to?(:each_pair) && args[0]) || args[1]
db = (args[0].is_a?(Neography::Rest) && args[0]) || args[1] || Neography::Rest.new
db, props = split_args(*args)

node = self.new(db.create_node(props))
node.neo_server = db
node
end

def load(*args)
# the first argument can be an hash of properties to set
node = !args[0].is_a?(Neography::Rest) && args[0] || args[1]
db, node = split_args(*args)

# a db instance can be given, it is the first argument or the second
db = (args[0].is_a?(Neography::Rest) && args[0]) || args[1] || Neography::Rest.new
node = db.get_node(node)
node = self.new(node) unless node.nil?
node.neo_server = db unless node.nil?
if node
node = self.new(node)
node.neo_server = db
end
node
end

Expand All @@ -42,4 +40,4 @@ def exist?
end

end
end
end
19 changes: 18 additions & 1 deletion lib/neography/property_container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,22 @@ def initialize(hash=nil)
end
end

# the arguments are either a Rest instance, or something else
def self.split_args(*args)
db = other = nil

args.each do |arg|
case arg
when Rest
db = arg
else
other = arg
end
end
db ||= Neography::Rest.new

[ db, other ]
end

end
end
end
7 changes: 2 additions & 5 deletions lib/neography/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ def create(type, from_node, to_node, props=nil)
end

def load(*args)
# the first argument can be an hash of properties to set
rel = !args[0].is_a?(Neography::Rest) && args[0] || args[1]
db, rel = split_args(*args)

# a db instance can be given, it is the first argument or the second
db = (args[0].is_a?(Neography::Rest) && args[0]) || args[1] || Neography::Rest.new
rel = db.get_relationship(rel)
unless rel.nil?
if rel
rel = Neography::Relationship.new(rel)
rel.start_node = Neography::Node.load(rel.start_node, db)
rel.end_node = Neography::Node.load(rel.end_node, db)
Expand Down
97 changes: 97 additions & 0 deletions spec/unit/node_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
require 'spec_helper'

module Neography
describe Node do

describe "::create" do
context "no explicit server" do

before do
# stub out actual connections
@db = stub(Rest).as_null_object
Rest.stub(:new) { @db }
end

it "assigns a new Rest db by default" do
node = Node.create
node.neo_server.should == @db
end

it "creates without arguments" do
@db.should_receive(:create_node).with(nil)
Node.create
end

it "creates with only a hash argument" do
properties = { :foo => "bar" }
@db.should_receive(:create_node).with(properties)
Node.create(properties)
end

end

context "explicit server" do

it "can pass a server as the first arugment, properties as the second" do
@other_server = Neography::Rest.new
properties = { :foo => "bar" }
@other_server.should_receive(:create_node).with(properties)
Node.create(@other_server, properties)
end

it "can pass properties as the first argument, a server as the second" do
@other_server = Neography::Rest.new
properties = { :foo => "bar" }
@other_server.should_receive(:create_node).with(properties)
Node.create(properties, @other_server)
end

end
end

describe "::load" do
context "no explicit server" do

before do
# stub out actual connections
@db = stub(Rest).as_null_object
Rest.stub(:new) { @db }
end

it "load by id" do
@db.should_receive(:get_node).with(5)
Node.load(5)
end

it "loads by node" do
node = Node.new
@db.should_receive(:get_node).with(node)
Node.load(node)
end

it "loads by full server string" do
@db.should_receive(:get_node).with("http://localhost:7474/db/data/node/2")
Node.load("http://localhost:7474/db/data/node/2")
end

end

context "explicit server" do

it "can pass a server as the first argument, node as the second" do
@other_server = Neography::Rest.new
@other_server.should_receive(:get_node).with(42)
node = Node.load(@other_server, 42)
end

it "can pass a node as the first argument, server as the second" do
@other_server = Neography::Rest.new
@other_server.should_receive(:get_node).with(42)
node = Node.load(42, @other_server)
end

end
end

end
end
83 changes: 83 additions & 0 deletions spec/unit/relationship_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
require 'spec_helper'

module Neography
describe Relationship do

let(:db) { stub(Rest).as_null_object }
let(:relationship_hash) do
{
"self" => "0",
"start" => "1",
"end" => "2",
"data" => {}
}
end

describe "::create" do
let(:from) { stub(:neo_server => db) }
let(:to) { stub(:neo_server => db) }
let(:props) { { :foo => "bar" } }

it "creates a new node through Rest" do
db.should_receive(:create_relationship).with("type", from, to, props)

Relationship.create("type", from, to, props)
end

it "assigns fields" do
db.stub(:create_relationship).and_return(relationship_hash)

rel = Relationship.create("type", from, to, props)

rel.start_node.should == from
rel.end_node.should == to
rel.rel_type.should == "type"
end
end

describe "::load" do
context "no explicit server" do

before do
# stub out actual connections
@db = stub(Rest).as_null_object
Rest.stub(:new) { @db }
end

it "load by id" do
@db.should_receive(:get_relationship).with(5)
Relationship.load(5)
end

it "loads by relationship" do
relationship = Relationship.new(relationship_hash)
@db.should_receive(:get_relationship).with(relationship)
Relationship.load(relationship)
end

it "loads by full server string" do
@db.should_receive(:get_relationship).with("http://localhost:7474/db/data/relationship/2")
Relationship.load("http://localhost:7474/db/data/relationship/2")
end

end

context "explicit server" do

it "can pass a server as the first argument, relationship as the second" do
@other_server = Neography::Rest.new
@other_server.should_receive(:get_relationship).with(42)
relationship = Relationship.load(@other_server, 42)
end

it "can pass a relationship as the first argument, server as the second" do
@other_server = Neography::Rest.new
@other_server.should_receive(:get_relationship).with(42)
relationship = Relationship.load(42, @other_server)
end

end
end

end
end

0 comments on commit 6536a7e

Please sign in to comment.