Skip to content

Commit

Permalink
adding tests, wacky way to solve untyped incoming,outgoing and both c…
Browse files Browse the repository at this point in the history
…alls
  • Loading branch information
maxdemarzi committed Dec 9, 2010
1 parent 8b20bea commit f894aa9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 48 deletions.
6 changes: 3 additions & 3 deletions lib/neography/node_relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ def outgoing(types=nil)
if types
NodeTraverser.new(self).outgoing(types)
else
NodeTraverser.new(self).outgoing
NodeTraverser.new(self).outgoing(types).collect {|n| n}
end
end

def incoming(types=nil)
if types
NodeTraverser.new(self).incoming(types)
else
NodeTraverser.new(self).incoming
NodeTraverser.new(self).incoming(types).collect {|n| n}
end
end

def both(types=nil)
if types
NodeTraverser.new(self).both(types)
else
NodeTraverser.new(self) # default is both
NodeTraverser.new(self).both(types).collect {|n| n}
end
end

Expand Down
27 changes: 24 additions & 3 deletions lib/neography/node_traverser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ def create(other_node)
when "incoming", "in"
rel = Neography::Relationship.new(@from.neo_server.create_relationship(@relationships.first["type"], other_node, @from))
else
rel << @from.neo_server.create_relationship(@relationships.first["type"], @from, other_node)
rel << @from.neo_server.create_relationship(@relationships.first["type"], other_node, @from)
rel = Array.new
rel << Neography::Relationship.new(@from.neo_server.create_relationship(@relationships.first["type"], @from, other_node))
rel << Neography::Relationship.new(@from.neo_server.create_relationship(@relationships.first["type"], other_node, @from))
end
rel
end
Expand Down Expand Up @@ -104,7 +105,27 @@ def iterator
options["prune evaluator"] = @prune unless @prune.nil?
options["return filter"] = @filter unless @filter.nil?
options["depth"] = @depth unless @depth.nil?
@from.neo_server.traverse(@from, "nodes", options)

if @relationships[0]["type"].blank?
rels = @from.neo_server.get_node_relationships(@from, @relationships[0]["direction"])
case @relationships[0]["direction"]
when "in"
# rels.collect { |r| r["start"]}.uniq.collect{ |r| @from.neo_server.get_node(r) }
rels.collect { |r| @from.neo_server.get_node(r["start"]) }.uniq
when "out"
rels.collect { |r| @from.neo_server.get_node(r["end"]) }.uniq
else
rels.collect { |r|
if @from.neo_id == r["start"].split('/').last
@from.neo_server.get_node(r["end"])
else
@from.neo_server.get_node(r["start"])
end
}.uniq
end
else
@from.neo_server.traverse(@from, "nodes", options)
end
end

end
Expand Down
50 changes: 8 additions & 42 deletions spec/integration/relationship_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,16 @@
p1 = Neography::Node.create
p2 = Neography::Node.create

new_rel = Neography::Relationship.create(:family, p1, p2)
puts new_rel.inspect
new_rel.start_node.should == p1
new_rel.end_node.should == p2


# p1.outgoing(:family).should include(p2)
# p2.incoming(:family).should include(p1)
Neography::Relationship.create(:family, p1, p2)
p1.outgoing(:family).should include(p2)
p2.incoming(:family).should include(p1)
end

it "#new(:family, p1, p2, :since => '1998', :colour => 'blue') creates relationship and sets its properties" do
p1 = Neography::Node.create
p2 = Neography::Node.create

rel = Neography::Relationship.create(:family, p1, p2, :since => 1998, :colour => 'blue')

rel[:since].should == 1998
rel[:colour].should == 'blue'
rel.since.should == 1998
Expand All @@ -30,42 +25,13 @@
it "#outgoing(:friends).create(other) creates a new relationship between self and other node" do
p1 = Neography::Node.create
p2 = Neography::Node.create

rel = p1.outgoing(:foo).create(p2)
p1.outgoing(:foo).first.should == p2
rel.should be_kind_of(Neography::Relationship)
end
end

describe "rel?" do
it "#rel? returns true if there are any relationships" do
n1 = Neography::Node.create
n2 = Neography::Node.create
new_rel = Neography::Relationship.create(:foo, n1, n2)
n1.rel?.should be_true
n1.rel?(:bar).should be_false
n1.rel?(:foo).should be_true
n1.rel?(:incoming, :foo).should be_false
n1.rel?(:outgoing, :foo).should be_true
n1.rel?(:foo, :incoming).should be_false
n1.rel?(:foo, :outgoing).should be_true

n1.rel?(:incoming).should be_false
n1.rel?(:outgoing).should be_true
n1.rel?(:both).should be_true
n1.rel?(:all).should be_true
n1.rel?.should be_true
rel.should be_kind_of(Neography::Relationship)
p1.outgoing(:foo).first.should == p2
p1.outgoing(:foo).should include(p2)
p2.incoming(:foo).should include(p1)
end
end


describe "delete relationship" do
it "can delete an existing relationship" do
p1 = Neography::Node.create
p2 = Neography::Node.create
new_rel = Neography::Relationship.create(:family, p1, p2)
new_rel.del
Neography::Relationship.load(new_rel).should be_nil
end
end
end

0 comments on commit f894aa9

Please sign in to comment.