require 'test_helper' class OneEmbeddedProxyTest < Test::Unit::TestCase def setup @post_class = Doc('Post') do key :title, String end @author_class = EDoc('Author') do key :name, String embedded_in :post end end should "default to nil" do @post_class.one :author, :class => @author_class @post_class.new.author.should be_nil end should "be able to build" do @post_class.one :author, :class => @author_class post = @post_class.create author = post.author.build(:name => "John") post.author.should be_instance_of(@author_class) post.author.should be_new post.author.name.should == 'John' post.author.should == author post.author.post.should == post end should "be able to replace the association" do @post_class.one :author, :class => @author_class post = @post_class.new author = @author_class.new(:name => 'Frank') post.author = author post.save post.reload post.author.should == author post.author.nil?.should be_false new_author = @author_class.new(:name => 'Emily') post.author = new_author post.author.should == new_author end should "not have problem loading root document if embedded one is nil" do @post_class.one :author, :class => @author_class post = @post_class.create lambda { @post_class.find(post.id) }.should_not raise_error end should "load the parent and root documents for nested embedded documents" do @address_class = EDoc('Address') do key :city, String key :state, String end @author_class.one :address, :class => @address_class @post_class.one :author, :class => @author_class post = @post_class.create(:title => 'Post Title', :author => { :name => 'Frank', :address => { :city => 'Boston', :state => 'MA' } }) post.author.address._parent_document.should == post.author post.author.address._root_document.should == post end should "have boolean method for testing presence" do @post_class.one :author, :class => @author_class post = @post_class.new post.author?.should be_false post.author = @author_class.new(:name => 'Frank') post.author?.should be_true end end