Sha256: 23c5bed7b8e6713c4be189043573a4d435b55f9cb5dbe23625b98f651308fd6a

Contents?: true

Size: 1.46 KB

Versions: 7

Compression:

Stored size: 1.46 KB

Contents

require 'test_helper'
require 'models'

class BelongsToPolymorphicProxyTest < Test::Unit::TestCase
  def setup
    Status.collection.remove
    Project.collection.remove
  end
  
  should "default to nil" do
    status = Status.new
    status.target.nil?.should be_true
    status.target.inspect.should == "nil"
  end

  should "be able to replace the association" do
    status = Status.new(:name => 'Foo!')
    project = Project.new(:name => "mongomapper")
    status.target = project
    status.save.should be_true

    from_db = Status.find(status.id)
    from_db.target.nil?.should be_false
    from_db.target_id.should == project.id
    from_db.target_type.should == "Project"
    from_db.target.name.should == "mongomapper"
  end

  should "unset the association" do
    status = Status.new(:name => 'Foo!')
    project = Project.new(:name => "mongomapper")
    status.target = project
    status.save.should be_true

    from_db = Status.find(status.id)
    from_db.target = nil
    from_db.target_type.nil?.should be_true
    from_db.target_id.nil?.should be_true
    from_db.target.nil?.should be_true
  end
  
  context "association id set but document not found" do
    setup do
      @status = Status.new(:name => 'Foo!')
      project = Project.new(:name => "mongomapper")
      @status.target = project
      @status.save.should be_true
      project.destroy
    end

    should "return nil instead of raising error" do
      @status.target.nil?.should be_true
    end
  end
end

Version data entries

7 entries across 7 versions & 3 rubygems

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