Sha256: 7fd7f81e19f18436f92385f4866dad2a645320f47a275cb92f6d61d2db981014

Contents?: true

Size: 1.24 KB

Versions: 6

Compression:

Stored size: 1.24 KB

Contents

require 'test_helper'
require 'models'

class BelongsToProxyTest < Test::Unit::TestCase
  def setup    
    @post_class = Class.new do
      include MongoMapper::Document
    end
    
    @comment_class = Class.new do
      include MongoMapper::Document
      key :post_id, String
    end
    @comment_class.belongs_to :post, :class => @post_class
    
    @post_class.collection.remove
    @comment_class.collection.remove
  end
  
  should "default to nil" do
    @comment_class.new.post.nil?.should be_true
  end
  
  should "be able to replace the association" do
    post = @post_class.new(:name => 'mongomapper')
    comment = @comment_class.new(:name => 'Foo!', :post => post)
    comment.save.should be_true
    
    comment = comment.reload
    comment.post.should == post
    comment.post.nil?.should be_false
  end
  
  should "unset the association" do
    post = @post_class.new(:name => 'mongomapper')
    comment = @comment_class.new(:name => 'Foo!', :post => post)
    comment.save.should be_true
    
    comment = comment.reload
    comment.post = nil
    comment.post.nil?.should be_true
  end
  
  should "return nil if id set but document not found" do
    @comment_class.new(:name => 'Foo', :post_id => '1234').post.nil?.should be_true
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
mongo_mapper-unstable-2009.11.8 test/functional/associations/test_belongs_to_proxy.rb
mongo_mapper-unstable-2009.11.6 test/functional/associations/test_belongs_to_proxy.rb
djsun-mongo_mapper-0.5.8.2 test/functional/associations/test_belongs_to_proxy.rb
djsun-mongo_mapper-0.5.8.1 test/functional/associations/test_belongs_to_proxy.rb
mongo_mapper-unstable-2009.11.2 test/functional/associations/test_belongs_to_proxy.rb
mongo_mapper-unstable-2009.10.31 test/functional/associations/test_belongs_to_proxy.rb