Skip to content

Commit

Permalink
Added tests and fixed some existing ones.
Browse files Browse the repository at this point in the history
  • Loading branch information
amangale authored and bkeepers committed Sep 2, 2011
1 parent 5957d7e commit aef6659
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/mongo_mapper/plugins/keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def attributes=(attrs)

def attributes
HashWithIndifferentAccess.new.tap do |attrs|
keys.select { |name,key| !self[key.name].nil? }.each_pair do |name, key|
keys.select { |name,key| !self[key.name].nil? || key.type == ObjectId }.each_pair do |name, key|
value = key.set(self[key.name])
attrs[name] = value
end
Expand Down
41 changes: 41 additions & 0 deletions test/unit/test_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,5 +204,46 @@ class Post
(@document.new('_id' => @oid) == another_document.new('_id' => @oid)).should be(false)
end
end

context "nil attributes" do

should "list all the keys and default non nil attributes" do
doc = @document.new
doc.keys.keys.sort.should == ['_id', 'age', 'name']
doc.attributes.keys.sort.should == ['_id']
end

should "list all the keys and non nil attributes" do
doc = @document.new(:name => "John")
doc.keys.keys.sort.should == ['_id', 'age', 'name']
doc.attributes.keys.sort.should == ['_id','name']
end

should "list all the keys and pickup changed nil attributes" do
doc = @document.new(:name => "John")
doc.keys.keys.sort.should == ['_id', 'age', 'name']
doc.attributes.keys.sort.should == ['_id','name']

doc.name = nil

doc.keys.keys.sort.should == ['_id', 'age', 'name']
doc.attributes.keys.sort.should == ['_id']
end

should "list all the keys and pickup changed nil and non-nil attributes" do
doc = @document.new(:name => "John")
doc.keys.keys.sort.should == ['_id', 'age', 'name']
doc.attributes.keys.sort.should == ['_id','name']

doc.name = nil
doc.age = 12

doc.keys.keys.sort.should == ['_id', 'age', 'name']
doc.attributes.keys.sort.should == ['_id','age']
end

end

end # instance of a document
end # DocumentTest

17 changes: 11 additions & 6 deletions test/unit/test_embedded_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,16 @@ def passwd
context "attributes" do
should "default to hash with all keys" do
doc = @document.new
doc.attributes.keys.sort.should == ['_id', 'age', 'name']
doc.keys.keys.sort.should == ['_id', 'age', 'name']
doc.attributes.keys.sort.should == ['_id']
end

should "return all keys with values" do
doc = @document.new(:name => 'string', :age => nil)
doc.attributes.keys.sort.should == ['_id', 'age', 'name']
doc.attributes.keys.sort.should == ['_id', 'name']
doc.keys.keys.sort.should == ['_id', 'age', 'name']
doc.attributes.values.should include('string')
doc.attributes.values.should include(nil)
#doc.attributes.values.should include(nil)
end

should "have indifferent access" do
Expand All @@ -384,14 +386,16 @@ def passwd
context "to_mongo" do
should "default to hash with _id key" do
doc = @document.new
doc.to_mongo.keys.sort.should == ['_id', 'age', 'name']
doc.to_mongo.keys.sort.should == ['_id']
doc.keys.keys.sort.should == ['_id', 'age', 'name']
end

should "return all keys" do
doc = @document.new(:name => 'string', :age => nil)
doc.to_mongo.keys.sort.should == ['_id', 'age', 'name']
doc.keys.keys.sort.should == ['_id', 'age', 'name']
doc.to_mongo.keys.sort.should == ['_id','name']
doc.to_mongo.values.should include('string')
doc.to_mongo.values.should include(nil)
#doc.to_mongo.values.should include(nil)
end
end

Expand Down Expand Up @@ -675,3 +679,4 @@ def name_and_age=(new_value)
end # instance of a embedded document
end
end

0 comments on commit aef6659

Please sign in to comment.