<commit_before>require 'test_helper'

class OneProxyTest < Test::Unit::TestCase
  def setup
    @post_class = Doc('Post')
    @author_class = Doc do
      key :post_id, ObjectId
    end
  end
  
  should "default to nil" do
    @post_class.one :author, :class => @author_class
    @post_class.new.author.nil?.should be_true
  end
  
  should "send object id to target" do
    @post_class.one :author, :class => @author_class
    
    post = @post_class.new
    author = @author_class.new(:name => 'Frank')
    post.author = author
    author.save.should be_true
    post.save.should be_true
    
    post.author.object_id.should == post.author.target.object_id
  end

  should "allow assignment of associated document using a hash" do
    @post_class.one :author, :class => @author_class

    post = @post_class.new('author' => { 'name' => 'Frank' })
    post.author.name.should == 'Frank'

    post.save.should be_true
    post.reload

    post.author.name.should == 'Frank'
  end
  
  context "replacing the association" do
    context "with an object of the class" do
      should "work" do
        @post_class.one :author, :class => @author_class

        post = @post_class.new
        author = @author_class.new(:name => 'Frank')
        post.author = author
        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
    end

    context "with a Hash" do
      should "convert to an object of the class and work" do
        @post_class.one :author, :class => @author_class

        post = @post_class.new
        author = { 'name' => 'Frank' }
        post.author = author
        post.reload

        post.author.name.should == 'Frank'
        post.author.nil?.should be_false

        new_author = { 'name' => 'Emily' }
        post.author = new_author
        post.author.name.should == 'Emily'
      end
    end
  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
  
  should "work with criteria" do
    @post_class.one :primary_author, :class => @author_class, :primary => true
    @post_class.one :author, :class => @author_class
    
    post = @post_class.create
    author = @author_class.create(:name => 'Frank', :primary => false, :post_id => post.id)
    primary = @author_class.create(:name => 'Bill', :primary => true, :post_id => post.id)
    post.author.should == author
    post.primary_author.should == primary
  end
  
  should "unset the association" do
    @post_class.one :author, :class => @author_class
    post = @post_class.new
    author = @author_class.new
    post.author = author
    post.reload
    
    post.author = nil
    post.author.nil?.should be_false
  end
  <commit_msg>Small tweak to hopefully nab the random failure in this test. Only happens like 1 in 30 runs or so.<commit_after>require 'test_helper'

class OneProxyTest < Test::Unit::TestCase
  def setup
    @post_class = Doc('Post')
    @author_class = Doc do
      key :post_id, ObjectId
    end
  end
  
  should "default to nil" do
    @post_class.one :author, :class => @author_class
    @post_class.new.author.nil?.should be_true
  end
  
  should "send object id to target" do
    @post_class.one :author, :class => @author_class
    
    post = @post_class.new
    author = @author_class.new(:name => 'Frank')
    post.author = author
    author.save.should be_true
    post.save.should be_true
    
    post.author.object_id.should == post.author.target.object_id
  end

  should "allow assignment of associated document using a hash" do
    @post_class.one :author, :class => @author_class

    post = @post_class.new('author' => { 'name' => 'Frank' })
    post.author.name.should == 'Frank'

    post.save.should be_true
    post.reload

    post.author.name.should == 'Frank'
  end
  
  context "replacing the association" do
    context "with an object of the class" do
      should "work" do
        @post_class.one :author, :class => @author_class

        post = @post_class.new
        author = @author_class.new(:name => 'Frank')
        post.author = author
        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
    end

    context "with a Hash" do
      should "convert to an object of the class and work" do
        @post_class.one :author, :class => @author_class

        post = @post_class.new
        author = { 'name' => 'Frank' }
        post.author = author
        post.reload

        post.author.name.should == 'Frank'
        post.author.nil?.should be_false

        new_author = { 'name' => 'Emily' }
        post.author = new_author
        post.author.name.should == 'Emily'
      end
    end
  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
  
  should "work with criteria" do
    @post_class.one :primary_author, :class => @author_class, :primary => true
    @post_class.one :author, :class => @author_class
    
    post = @post_class.create
    author = @author_class.create(:name => 'Frank', :primary => false, :post_id => post.id)
    primary = @author_class.create(:name => 'Bill', :primary => true, :post_id => post.id)
    post.reload
    post.author.should == author
    post.primary_author.should == primary
  end
  
  should "unset the association" do
    @post_class.one :author, :class => @author_class
    post = @post_class.new
    author = @author_class.new
    post.author = author
    post.reload
    
    post.author = nil
    post.author.nil?.should be_false
  end
  
{'fraction_non_alphanumeric': 0.11528455284552845, 'fraction_numerical': 0.0004878048780487805, 'mean_word_length': 2.6055099648300115, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '406350', 'n_tokens_mistral': 1977, 'n_tokens_neox': 1928, 'n_words': 504}