Skip to content

Commit

Permalink
Add unit tests for node/relationship properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
rdvdijk committed Oct 1, 2012
1 parent 491c319 commit 121fabc
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 25 deletions.
47 changes: 22 additions & 25 deletions lib/neography/property.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Neography
module Property


def [](key)
return unless respond_to?(key)
@table[key]
Expand All @@ -11,9 +10,9 @@ def []=(key, value)
k = key.to_s
if value.nil?
if self.is_a? Neography::Node
neo_server.remove_node_properties(self.neo_id, [key])
neo_server.remove_node_properties(self.neo_id, [k])
else
neo_server.remove_relationship_properties(self.neo_id, [key])
neo_server.remove_relationship_properties(self.neo_id, [k])
end
else
if self.is_a? Neography::Node
Expand All @@ -26,32 +25,30 @@ def []=(key, value)
@table[key] = value
end


def new_ostruct_member(name)
name = name.to_sym
unless self.respond_to?(name)
meta = class << self; self; end
meta.send(:define_method, name) { @table[name] }
meta.send(:define_method, "#{name}=") do |x|
@table[name] = x
self[name.to_sym] = x
def new_ostruct_member(name)
name = name.to_sym
unless self.respond_to?(name)
meta = class << self; self; end
meta.send(:define_method, name) { @table[name] }
meta.send(:define_method, "#{name}=") do |value|
@table[name] = value
self[name.to_sym] = value
end
end
name
end
name
end

def method_missing(method_sym, *arguments, &block)
if (method_sym.to_s =~ /=$/) != nil
new_ostruct_member(method_sym.to_s.chomp("="))

def method_missing(method_sym, *arguments, &block)
if (method_sym.to_s =~ /=$/) != nil
new_ostruct_member(method_sym.to_s.chomp("="))

# We just defined the getter/setter above, but we haven't actually
# applied them yet.
self.send(method_sym, *arguments)
else
super
# We just defined the getter/setter above, but we haven't actually
# applied them yet.
self.send(method_sym, *arguments)
else
super
end
end
end

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

module Neography
describe "Properties" do

before do
@db = mock(Neography::Rest, :is_a? => true).as_null_object
Rest.stub(:new) { @db }
end

context "Node" do

subject(:node) do
node = Node.create
node.stub(:neo_id => 42)
node
end

it "sets properties as accessor" do
@db.should_receive(:"set_node_properties").with(42, { "key" => "value" })
node.key = "value"
end

it "sets properties as array entry" do
@db.should_receive(:"set_node_properties").with(42, { "key" => "value" })
node["key"] = "value"
end

it "gets properties as accessor" do
@db.stub(:"set_node_properties").with(42, { "key" => "value" })
node.key = "value"
node.key.should == "value"
end

it "gets properties as array entry" do
@db.stub(:"set_node_properties").with(42, { "key" => "value" })
node["key"] = "value"
node["key"].should == "value"
end

it "resets properties as accessor" do
@db.should_receive(:"remove_node_properties").with(42, ["key"])
node.key = nil
end

it "resets properties as array entry" do
@db.should_receive(:"remove_node_properties").with(42, ["key"])
node["key"] = nil
end

it "gets unknown properties as nil" do
node.unknown.should == nil
end

it "overwrites existing properties" do
@db.should_receive(:"set_node_properties").with(42, { "key" => "value1" })
node.key = "value1"

@db.should_receive(:"set_node_properties").with(42, { "key" => "value2" })
node.key = "value2"
end

end

context "Relationship" do

subject(:relationship) do
from = Node.create
to = Node.create

rel = Relationship.create(:type, from, to)
rel.stub(:neo_id => 42)
rel
end

it "sets properties as accessor" do
@db.should_receive(:"set_relationship_properties").with(42, { "key" => "value" })
relationship.key = "value"
end

it "sets properties as array entry" do
@db.should_receive(:"set_relationship_properties").with(42, { "key" => "value" })
relationship["key"] = "value"
end

it "gets properties as accessor" do
@db.stub(:"set_relationship_properties").with(42, { "key" => "value" })
relationship.key = "value"
relationship.key.should == "value"
end

it "gets properties as array entry" do
@db.stub(:"set_relationship_properties").with(42, { "key" => "value" })
relationship["key"] = "value"
relationship["key"].should == "value"
end

it "resets properties as accessor" do
@db.should_receive(:"remove_relationship_properties").with(42, ["key"])
relationship.key = nil
end

it "resets properties as array entry" do
@db.should_receive(:"remove_relationship_properties").with(42, ["key"])
relationship["key"] = nil
end

it "gets unknown properties as nil" do
relationship.unknown.should == nil
end

it "overwrites existing properties" do
@db.should_receive(:"set_relationship_properties").with(42, { "key" => "value1" })
relationship.key = "value1"

@db.should_receive(:"set_relationship_properties").with(42, { "key" => "value2" })
relationship.key = "value2"
end

end
end
end

0 comments on commit 121fabc

Please sign in to comment.