<commit_before>require "spec_helper"

describe Mongoid::Relations::Referenced::In do

  before do
    [ Person, Game, Post ].map(&:delete_all)
  end

  describe "#=" do

    context "when the parent is a references one" do

      context "when the child is a new record" do

        let(:person) do
          Person.new
        end

        let(:game) do
          Game.new
        end

        before do
          game.person = person
        end

        it "sets the target of the relation" do
          game.person.target.should == person
        end

        it "sets the foreign key on the relation" do
          game.person_id.should == person.id
        end

        it "sets the base on the inverse relation" do
          person.game.should == game
        end

        it "does not save the target" do
          person.should_not be_persisted
        end
      end

      context "when the child is not a new record" do

        let(:person) do
          Person.new(:ssn => "437-11-1112")
        end

        let(:game) do
          Game.create
        end

        before do
          game.person = person
        end

        it "sets the target of the relation" do
          game.person.target.should == person
        end

        it "sets the foreign key of the relation" do
          game.person_id.should == person.id
        end

        it "sets the base on the inverse relation" do
          person.game.should == game
        end

        it "does not saves the target" do
          person.should_not be_persisted
        end
      end
    end

    context "when the parent is a references many" do

      context "when the child is a new record" do

        let(:person) do
          Person.new
        end

        let(:post) do
          Post.new
        end

        before do
          post.person = person
        end

        it "sets the target of the relation" do
          post.person.target.should == person
        end

        it "sets the foreign key on the relation" do
          post.person_id.should == person.id
        end

        it "sets the base on the inverse relation" do
          person.posts.should == [ post ]
        end

        it "does not save the target" do
          person.should_not be_persisted
        end
      end

      context "when the child is not a new record" do

        let(:person) do
          Person.new(:ssn => "437-11-1112")
        end

        let(:post) do
          Post.create
        end

        before do
          post.person = person
        end

        it "sets the target of the relation" do
          post.person.target.should == person
        end

        it "sets the foreign key of the relation" do
          post.person_id.should == person.id
        end

        it "sets the base on the inverse relation" do
          person.posts.should == [ post ]
        end

        it "does not saves the target" do
          person.should_not be_persisted
        end
      end
    end
  end

  describe "#= nil" do

    context "when the parent is a new record" do

      let(:person) do
        Person.new
      end

      let(:game) do
        Game.new
      end

      before do
        game.person = person
        game.person = nil
      end

      it "sets the relation to nil" do
        game.person.should be_nil
      end

      it "removed the inverse relation" do
        person.game.should be_nil
      end

      it "removes the foreign key value" do
        game.person_id.should be_nil
      end
    end

    context "when the parent is not a new record" do

      let(:person) do
        Person.new(:ssn => "437-11-1112")
      end

      let(:game) do
        Game.create
      end

      before do
        game.person = person
        game.person = nil
      end

      it "sets the relation to nil" do
        game.person.should be_nil
      end

      it "removed the inverse relation" do
        person.game.should be_nil
      end

      it "removes the foreign key value" do
        game.person_id.should be_nil
      end
    end
  end
end<commit_msg>Referenced in setting to nil with a parent references many<commit_after>require "spec_helper"

describe Mongoid::Relations::Referenced::In do

  before do
    [ Person, Game, Post ].map(&:delete_all)
  end

  describe "#=" do

    context "when the parent is a references one" do

      context "when the child is a new record" do

        let(:person) do
          Person.new
        end

        let(:game) do
          Game.new
        end

        before do
          game.person = person
        end

        it "sets the target of the relation" do
          game.person.target.should == person
        end

        it "sets the foreign key on the relation" do
          game.person_id.should == person.id
        end

        it "sets the base on the inverse relation" do
          person.game.should == game
        end

        it "does not save the target" do
          person.should_not be_persisted
        end
      end

      context "when the child is not a new record" do

        let(:person) do
          Person.new(:ssn => "437-11-1112")
        end

        let(:game) do
          Game.create
        end

        before do
          game.person = person
        end

        it "sets the target of the relation" do
          game.person.target.should == person
        end

        it "sets the foreign key of the relation" do
          game.person_id.should == person.id
        end

        it "sets the base on the inverse relation" do
          person.game.should == game
        end

        it "does not saves the target" do
          person.should_not be_persisted
        end
      end
    end

    context "when the parent is a references many" do

      context "when the child is a new record" do

        let(:person) do
          Person.new
        end

        let(:post) do
          Post.new
        end

        before do
          post.person = person
        end

        it "sets the target of the relation" do
          post.person.target.should == person
        end

        it "sets the foreign key on the relation" do
          post.person_id.should == person.id
        end

        it "sets the base on the inverse relation" do
          person.posts.should == [ post ]
        end

        it "does not save the target" do
          person.should_not be_persisted
        end
      end

      context "when the child is not a new record" do

        let(:person) do
          Person.new(:ssn => "437-11-1112")
        end

        let(:post) do
          Post.create
        end

        before do
          post.person = person
        end

        it "sets the target of the relation" do
          post.person.target.should == person
        end

        it "sets the foreign key of the relation" do
          post.person_id.should == person.id
        end

        it "sets the base on the inverse relation" do
          person.posts.should == [ post ]
        end

        it "does not saves the target" do
          person.should_not be_persisted
        end
      end
    end
  end

  describe "#= nil" do

    context "when the parent is a references one" do

      context "when the parent is a new record" do

        let(:person) do
          Person.new
        end

        let(:game) do
          Game.new
        end

        before do
          game.person = person
          game.person = nil
        end

        it "sets the relation to nil" do
          game.person.should be_nil
        end

        it "removed the inverse relation" do
          person.game.should be_nil
        end

        it "removes the foreign key value" do
          game.person_id.should be_nil
        end
      end

      context "when the parent is not a new record" do

        let(:person) do
          Person.new(:ssn => "437-11-1112")
        end

        let(:game) do
          Game.create
        end

        before do
          game.person = person
          game.person = nil
        end

        it "sets the relation to nil" do
          game.person.should be_nil
        end

        it "removed the inverse relation" do
          person.game.should be_nil
        end

        it "removes the foreign key value" do
          game.person_id.should be_nil
        end
      end
    end

    context "when the parent is a references many" do

      context "when the parent is a new record" do

        let(:person) do
          Person.new
        end

        let(:post) do
          Post.new
        end

        before do
          post.person = person
          post.person = nil
        end

        it "sets the relation to nil" do
          post.person.should be_nil
        end

        it "removed the inverse relation" do
          person.posts.should be_empty
        end

        it "removes the foreign key value" do
          post.person_id.should be_nil
        end
      end

      context "when the parent is not a new record" do

        let(:person) do
          Person.new(:ssn => "437-11-1112")
        end

        let(:post) do
          Post.create
        end

        before do
          post.person = person
          post.person = nil
        end

        it "sets the relation to nil" do
          post.person.should be_nil
        end

        it "removed the inverse relation" do
          person.posts.should be_empty
        end

        it "removes the foreign key value" do
          post.person_id.should be_nil
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.06047538914598233, 'fraction_numerical': 0.006625999158603281, 'mean_word_length': 1.3994448650012616, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, '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': '5759127', 'n_tokens_mistral': 2678, 'n_tokens_neox': 2523, 'n_words': 990}