Skip to content

Commit

Permalink
Bug fix: Using custom accessors for NodeMixin#update [#113 state:reso…
Browse files Browse the repository at this point in the history
…lved]
  • Loading branch information
andreas committed Mar 7, 2010
1 parent 5c5cf7c commit 513ce30
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 82 deletions.
7 changes: 6 additions & 1 deletion lib/neo4j/mixins/java_property_mixin.rb
Expand Up @@ -111,7 +111,12 @@ def update(struct_or_hash, options={})
struct_or_hash.each_pair do |key, value|
next if %w(_neo_id _classname).include? key.to_s # do not allow special properties to be mass assigned
keys_to_delete.delete(key) if strict
self[key] = value
setter_meth = "#{key}=".to_sym
if @_wrapper && @_wrapper.respond_to?(setter_meth)
@_wrapper.send(setter_meth, value)
else
self[key] = value
end
end
keys_to_delete.each{|key| delete_property(key) } if strict
self
Expand Down
81 changes: 0 additions & 81 deletions test/neo4j/node_mixin_spec.rb
Expand Up @@ -215,87 +215,6 @@ class DelegateTest
# end
#end
#
## ----------------------------------------------------------------------------
## update
##
#
#describe 'NodeMixin#update' do
#
# before(:all) do
# class TestNode
# include Neo4j::NodeMixin
# property :name, :age
# end
#
# start
# end
#
# before(:each) do
# Neo4j::Transaction.new
# end
#
# after(:each) do
# Neo4j::Transaction.finish
# end
#
# it "should be able to update a node from a value obejct" do
# # given
# t = TestNode.new
# t[:name]='kalle'
# t[:age]=2
# vo = t.value_object
# t2 = TestNode.new
# t2[:name] = 'foo'
#
# # when
# t2.update(vo)
#
# # then
# t2[:name].should == 'kalle'
# t2[:age].should == 2
# end
#
# it "should be able to update a node by using a hash even if the keys in the hash is not a declarared property" do
# t = TestNode.new
# t.update({:name=>'123', :oj=>'hoj'})
# t.name.should == '123'
# t.age.should == nil
# t['oj'].should == 'hoj'
# end
#
# it "should be able to update a node by using a hash" do
# t = TestNode.new
# t.update({:name=>'andreas', :age=>3})
# t.name.should == 'andreas'
# t.age.should == 3
# end
#
# it "should not allow the classname to be changed" do
# t = TestNode.new
# t.update({:classname => 'wrong'})
# t.classname.should == 'TestNode'
# end
#
# it "should not allow the id to be changed" do
# t = TestNode.new
# t.update({:id => 987654321})
# t.props['id'].should == t.neo_id
# end
#
# it "should remove attributes that are not mentioned if the strict option is set" do
# t = TestNode.new
# t.update({:name=>'andreas', :age=>3})
# t.update({:age=>4}, :strict => true)
# t.name.should be_nil
# end
#
# it "should not remove attributes that are not mentioned if the strict option is not set" do
# t = TestNode.new
# t.update({:name=>'andreas', :age=>3})
# t.update({:age=>4})
# t.name.should == 'andreas'
# end
#end
#
#
## ----------------------------------------------------------------------------
Expand Down
97 changes: 97 additions & 0 deletions test/neo4j/property_spec.rb
Expand Up @@ -327,6 +327,103 @@ class SubNode < TestNode
p.methods.should include("salary", "salary=")
end

end


# ----------------------------------------------------------------------------
# update
#

describe Neo4j::JavaPropertyMixin, "#update" do

before(:all) do
class TestNode
include Neo4j::NodeMixin
property :name, :age

def my_accessor
@my_accessor
end

def my_accessor=(val)
@my_accessor = val + 1
end
end

start
end

before(:each) do
Neo4j::Transaction.new
end

after(:each) do
Neo4j::Transaction.finish
end

it "should be able to update a node from a value obejct" do
# given
t = TestNode.new
t[:name]='kalle'
t[:age]=2
vo = t.value_object
t2 = TestNode.new
t2[:name] = 'foo'

# when
t2.update(vo)

# then
t2[:name].should == 'kalle'
t2[:age].should == 2
end

it "should use your own setters method if it exists" do
t = TestNode.new
# when
t.update({ :my_accessor => 1})
# then
t.my_accessor.should == 2
end

it "should be able to update a node by using a hash even if the keys in the hash is not a declarared property" do
t = TestNode.new
t.update({:name=>'123', :oj=>'hoj'})
t.name.should == '123'
t.age.should == nil
t['oj'].should == 'hoj'
end

it "should be able to update a node by using a hash" do
t = TestNode.new
t.update({:name=>'andreas', :age=>3})
t.name.should == 'andreas'
t.age.should == 3
end

it "should not allow the classname to be changed" do
t = TestNode.new
t.update({:_classname => 'wrong'})
t[:_classname].should == 'TestNode'
end

it "should not allow the id to be changed" do
t = TestNode.new
t.update({:_neo_id => 987654321})
t.props['_neo_id'].should == t.neo_id
end

it "should remove attributes that are not mentioned if the strict option is set" do
t = TestNode.new
t.update({:name=>'andreas', :age=>3})
t.update({:age=>4}, :strict => true)
t.name.should be_nil
end

it "should not remove attributes that are not mentioned if the strict option is not set" do
t = TestNode.new
t.update({:name=>'andreas', :age=>3})
t.update({:age=>4})
t.name.should == 'andreas'
end
end

0 comments on commit 513ce30

Please sign in to comment.