Created
October 23, 2009 09:02
-
-
Save royw/216763 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mongo_mapper' | |
require 'spec' | |
require 'extlib' | |
MongoMapper.connection = Mongo::Connection.new('localhost') | |
MongoMapper.database = 'tmp' | |
class Bar | |
include MongoMapper::EmbeddedDocument | |
key :title, String | |
end | |
class Foo | |
include MongoMapper::Document | |
key :name, String | |
key :alpha, Hash | |
key :group, Array | |
many :bars | |
key :name_updated, Time | |
key :alpha_updated, Time | |
key :group_updated, Time | |
key :bars_updated, Time | |
def before_save | |
self.name_updated = Time.now if self.name_changed? | |
self.alpha_updated = Time.now if self.alpha_changed? | |
self.group_updated = Time.now if self.group_changed? | |
# bars_changed? is undefined | |
# self.bars_updated = Time.now if self.bars_changed? | |
end | |
end | |
describe "test changed? behaviour" do | |
before :each do | |
Foo.collection.clear | |
end | |
it "should indicate string changed" do | |
foo = Foo.create | |
foo.name_updated.should be_nil | |
foo.name = 'one' | |
foo.changed.include?('name').should be_true | |
foo.save | |
foo.name_updated.should_not be_nil | |
end | |
it "should indicate a hash was reassigned" do | |
foo = Foo.create | |
foo.alpha_updated.should be_nil | |
foo.alpha = {:a => 1} | |
foo.changed.include?('alpha').should be_true | |
foo.save | |
foo.alpha_updated.should_not be_nil | |
end | |
it "should indicate hash changed" do | |
foo = Foo.create | |
foo.alpha_updated.should be_nil | |
foo.alpha['a'] = 1 | |
foo.changed.include?('alpha').should be_true #fails as only the hash contents changed, not the hash object | |
foo.save | |
foo.alpha_updated.should_not be_nil #fails | |
end | |
it "should indicate an array was reassigned" do | |
foo = Foo.create | |
foo.group_updated.should be_nil | |
foo.group = %w(one two three) | |
foo.changed.include?('group').should be_true | |
foo.save | |
foo.group_updated.should_not be_nil | |
end | |
it "should indicate array changed" do | |
foo = Foo.create | |
foo.group_updated.should be_nil | |
foo.group << 'one' | |
foo.group << 'two' | |
foo.group << 'three' | |
foo.changed.include?('group').should be_true #fails as only the array contents changed, not the array object | |
foo.save | |
foo.group_updated.should_not be_nil #fails | |
end | |
it "should indicate embedded document changed" do | |
foo = Foo.create | |
foo.bars_updated.should be_nil | |
foo.bars << Bar.new(:title => 'boss') | |
foo.save | |
foo.bars_updated.should_not be_nil #fails | |
end | |
end | |
__END__ | |
royw-macbook:tmp royw$ spec changed.rb | |
..F.FF | |
1) | |
'test changed? behaviour should indicate hash changed' FAILED | |
expected true, got false | |
./changed.rb:62: | |
2) | |
'test changed? behaviour should indicate array changed' FAILED | |
expected true, got false | |
./changed.rb:82: | |
3) | |
'test changed? behaviour should indicate embedded document changed' FAILED | |
expected not nil, got nil | |
./changed.rb:92: | |
Finished in 0.027555 seconds | |
6 examples, 3 failures |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment