test/unit/test_key.rb in mongo_mapper-0.8.6 vs test/unit/test_key.rb in mongo_mapper-0.9.0

- old
+ new

@@ -10,11 +10,11 @@ 'from_mongo' end end class KeyTest < Test::Unit::TestCase - include MongoMapper::Plugins::Keys + Key = MongoMapper::Plugins::Keys::Key context "Initializing a new key" do should "allow setting the name" do Key.new(:foo, String).name.should == 'foo' end @@ -99,10 +99,25 @@ ids = [BSON::ObjectId.new, BSON::ObjectId.new, BSON::ObjectId.new.to_s, BSON::ObjectId.new.to_s] subject.set(ids).should == ids.map { |id| ObjectId.to_mongo(id) } end end + context "for an array with :typecast option of Date" do + setup { @key = Key.new(:dates, Array, :typecast => 'Date') } + subject { @key } + + should "cast each element correctly when get" do + dates = [Date.yesterday, Date.today, Date.tomorrow.to_s] + subject.get(dates).should == dates.map { |date| Date.from_mongo(date) } + end + + should "cast each element correctly when set" do + dates = [Date.yesterday, Date.today, Date.tomorrow.to_s] + subject.set(dates).should == dates.map { |date| Date.to_mongo(date) } + end + end + context "for a set with :typecast option" do setup { @key = Key.new(:user_ids, Set, :typecast => 'ObjectId') } subject { @key } should "cast each element correctly" do @@ -161,9 +176,14 @@ setup do @key = Key.new(:foo, String, :default => 'baz') end should "return default value if value nil" do + @key.get(nil).should == 'baz' + end + + should "return a dup of the default value" do + @key.get(nil).replace('bar') @key.get(nil).should == 'baz' end should "return value if not blank" do @key.get('foobar').should == 'foobar'