From 6c3d33ccef7b7f69a59e19868186732c4d01d2a4 Mon Sep 17 00:00:00 2001 From: maxdemarzi Date: Thu, 9 Dec 2010 00:20:13 -0800 Subject: [PATCH] node relationship specs --- lib/neography/node_traverser.rb | 1 - spec/integration/node_relationship_spec.rb | 167 +++++++++++++++++++++ 2 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 spec/integration/node_relationship_spec.rb diff --git a/lib/neography/node_traverser.rb b/lib/neography/node_traverser.rb index 0897e9d..3a620a1 100644 --- a/lib/neography/node_traverser.rb +++ b/lib/neography/node_traverser.rb @@ -110,7 +110,6 @@ def iterator 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 diff --git a/spec/integration/node_relationship_spec.rb b/spec/integration/node_relationship_spec.rb new file mode 100644 index 0000000..6cee78d --- /dev/null +++ b/spec/integration/node_relationship_spec.rb @@ -0,0 +1,167 @@ +require File.join(File.dirname(__FILE__), '..', 'spec_helper') + +describe Neography::NodeRelationship do + + def create_nodes + # + # f + # ^ + # friends + # | + # a --friends-> b --friends--> c + # | ^ + # | | + # +--- work -----+ + # | + # +--- work ---> d --- work --> e + a = Neography::Node.create :name => 'a' + b = Neography::Node.create :name => 'b' + c = Neography::Node.create :name => 'c' + d = Neography::Node.create :name => 'd' + e = Neography::Node.create :name => 'e' + f = Neography::Node.create :name => 'f' + a.outgoing(:friends) << b + b.outgoing(:friends) << c + b.outgoing(:work) << c + b.outgoing(:work) << d + d.outgoing(:work) << e + b.outgoing(:friends) << f + [a,b,c,d,e,f] + end + + describe "outgoing" do + it "#outgoing(:friends) << other_node creates an outgoing relationship of type :friends" do + a = Neography::Node.create + other_node = Neography::Node.create + + # when + a.outgoing(:friends) << other_node + + # then + a.outgoing(:friends).first.should == other_node + end + + it "#outgoing(:friends) << b << c creates an outgoing relationship of type :friends" do + a = Neography::Node.create + b = Neography::Node.create + c = Neography::Node.create + + # when + a.outgoing(:friends) << b << c + + # then + a.outgoing(:friends).should include(b,c) + end + + it "#outgoing returns all incoming nodes of any type" do + a,b,c,d,e,f = create_nodes + + b.outgoing.should include(c,f,d) + [*b.outgoing].size.should == 3 + end + end + + describe "incoming" do + it "#incoming(:friends) << other_node should add an incoming relationship" do + a = Neography::Node.create + other_node = Neography::Node.create + + # when + a.incoming(:friends) << other_node + + # then + a.incoming(:friends).first.should == other_node + end + + it "#incoming(:friends) << b << c creates an incoming relationship of type :friends" do + a = Neography::Node.create + b = Neography::Node.create + c = Neography::Node.create + + # when + a.incoming(:friends) << b << c + + # then + a.incoming(:friends).should include(b,c) + end + + it "#incoming returns all incoming nodes of any type" do + a,b,c,d,e,f = create_nodes + + b.incoming.should include(a) + [*b.incoming].size.should == 1 + end + end + + describe "both" do + it "#both(:friends) << other_node should raise an exception" do + a = Neography::Node.create + other_node = Neography::Node.create + + # when + a.both(:friends) << other_node + a.incoming(:friends).first.should == other_node + a.outgoing(:friends).first.should == other_node + end + + it "#both returns all incoming and outgoing nodes of any type" do + a,b,c,d,e,f = create_nodes + + b.both.should include(a,c,d,f) + + [*b.both].size.should == 4 + b.incoming.should include(a) + b.outgoing.should include(c) + end + + end + +# b.both.each { |n| puts n.inspect } + + + describe "rels" do + it "" do + pending + end + end + + describe "rel" do + it "" do + pending + end + end + + + describe "rel?" do + it "#rel? returns true if there are any relationships" do + n1 = Neography::Node.create + n1.rel?.should be_false + n1.outgoing(:foo) << Neography::Node.create + + 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 + 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 \ No newline at end of file