Skip to content

Commit

Permalink
Tweaked the tests from austin's commits.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Aug 16, 2009
1 parent 8f5d0d3 commit b4f2907
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 25 deletions.
7 changes: 3 additions & 4 deletions lib/mongomapper/embedded_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module MongoMapper
module EmbeddedDocument
def self.included(model)
model.class_eval do
#module MongoMapperKeys; end
extend ClassMethods
include InstanceMethods

Expand Down Expand Up @@ -85,10 +84,10 @@ def parent_model

private
def accessors_module
if const_defined?("MongoMapperKeys") && constants.include?( "MongoMapperKeys" )
const_get "MongoMapperKeys"
if const_defined?('MongoMapperKeys') && constants.include?( 'MongoMapperKeys' )
const_get 'MongoMapperKeys'
else
const_set "MongoMapperKeys", Module.new
const_set 'MongoMapperKeys', Module.new
end
end

Expand Down
20 changes: 10 additions & 10 deletions test/functional/associations/test_many_polymorphic_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def setup

should "correctly store type when using <<, push and concat" do
room = Room.new
room.messages << Enter.new(:body => 'John entered the room', :item_order => 1)
room.messages.push Exit.new(:body => 'John entered the room', :item_order => 2)
room.messages.concat Chat.new(:body => 'Holla!' , :item_order => 3)
room.messages << Enter.new(:body => 'John entered the room', :item_order => 1)
room.messages.push Exit.new(:body => 'John entered the room', :item_order => 2)
room.messages.concat Chat.new(:body => 'Holla!' , :item_order => 3)

from_db = Room.find(room.id)
messages = from_db.messages.all :order => "item_order ASC"
Expand Down Expand Up @@ -157,16 +157,16 @@ def setup

context "with #all" do
should "work" do
@lounge.messages.find(:all, :order => "item_order ASC").should == [@lm1, @lm2]
@lounge.messages.all(:order => "item_order ASC").should == [@lm1, @lm2]
end

should "work with conditions" do
messages = @lounge.messages.find(:all, :conditions => {:body => 'Loungin!'}, :order => "item_order ASC")
messages = @lounge.messages.all(:conditions => {:body => 'Loungin!'}, :order => "item_order ASC")
messages.should == [@lm1]
end

should "work with order" do
messages = @lounge.messages.find(:all, :order => 'item_order desc')
messages = @lounge.messages.all(:order => 'item_order desc')
messages.should == [@lm2, @lm1]
end
end
Expand All @@ -184,11 +184,11 @@ def setup

context "with #first" do
should "work" do
@lounge.messages.find(:first, :order => "item_order asc").should == @lm1
@lounge.messages.first(:order => "item_order asc").should == @lm1
end

should "work with conditions" do
message = @lounge.messages.find(:first, :conditions => {:body => 'I love loungin!'}, :order => "item_order asc")
message = @lounge.messages.first(:conditions => {:body => 'I love loungin!'}, :order => "item_order asc")
message.should == @lm2
end
end
Expand All @@ -206,11 +206,11 @@ def setup

context "with #last" do
should "work" do
@lounge.messages.find(:last, :order => "item_order asc").should == @lm2
@lounge.messages.last(:order => "item_order asc").should == @lm2
end

should "work with conditions" do
message = @lounge.messages.find(:last, :conditions => {:body => 'Loungin!'}, :order => "item_order asc")
message = @lounge.messages.last(:conditions => {:body => 'Loungin!'}, :order => "item_order asc")
message.should == @lm1
end
end
Expand Down
55 changes: 44 additions & 11 deletions test/unit/test_embedded_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,23 @@ class Child < Parent
key :child, String
end

class OtherChild < Parent
include MongoMapper::EmbeddedDocument
key :other_child, String
end

module KeyOverride
def other_child
"special result"
read_attribute(:other_child) || "special result"
end

def other_child=(value)
super(value + " modified")
end
end

class OtherChild < Parent
include MongoMapper::EmbeddedDocument
include KeyOverride

key :other_child, String
end

class EmbeddedDocumentTest < Test::Unit::TestCase
context "Including MongoMapper::EmbeddedDocument" do
setup do
Expand Down Expand Up @@ -53,11 +59,13 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
parent = Class.new grandparent do
include MongoMapper::EmbeddedDocument
end

example_module = Module.new
document = Class.new(parent) do
include MongoMapper::EmbeddedDocument
include example_module
end

document.parent_model.should == parent
end

Expand Down Expand Up @@ -154,11 +162,6 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
Child.keys.keys.sort.should == ['_id', '_type', 'child', 'grandparent', 'parent']
end

should "be overrideable" do
OtherChild.send :include, KeyOverride
OtherChild.new.other_child.should == "special result"
end

should "not add anonymous objects to the ancestor tree" do
OtherChild.ancestors.any? { |a| a.name.blank? }.should be_false
end
Expand Down Expand Up @@ -334,6 +337,21 @@ def name_and_age
doc.foo
doc.instance_variable_get("@foo").should be_nil
end

should "be overrideable by modules" do
@document = Class.new do
include MongoMapper::Document
key :other_child, String
end

child = @document.new
child.other_child.should be_nil

@document.send :include, KeyOverride

overriden_child = @document.new
overriden_child.other_child.should == 'special result'
end
end

context "reading an attribute before typcasting" do
Expand Down Expand Up @@ -394,6 +412,21 @@ def name_and_age=(new_value)
doc.name.should == 'Frank'
doc.age.should == 62
end

should "be overrideable by modules" do
@document = Class.new do
include MongoMapper::Document
key :other_child, String
end

child = @document.new(:other_child => 'foo')
child.other_child.should == 'foo'

@document.send :include, KeyOverride

overriden_child = @document.new(:other_child => 'foo')
overriden_child.other_child.should == 'foo modified'
end
end # writing an attribute

context "checking if an attributes value is present" do
Expand Down

0 comments on commit b4f2907

Please sign in to comment.