spec/mongoid/association/depending_spec.rb in mongoid-7.1.11 vs spec/mongoid/association/depending_spec.rb in mongoid-7.2.0.rc1

- old
+ new

@@ -487,11 +487,11 @@ expect(from_db.person_ids).to_not include(person.id) end end end - context "when dependent is restrict_with_error" do + context "when dependent is restrict_with_exception" do context "when restricting a references many" do let!(:association) do Person.has_many :drugs, dependent: :restrict_with_exception @@ -823,47 +823,99 @@ end end context 'when the strategy is :restrict_with_error' do - let(:person) do - Person.new - end + context "when restricting a one-to-many" do + + let(:person) do + Person.new + end + + let(:post) do + Post.new + end - let(:post) do - Post.new - end + let!(:association) do + Person.has_many :restrictable_posts, class_name: "Post", dependent: :restrict_with_error + end - let!(:association) do - Person.has_many :restrictable_posts, class_name: "Post", dependent: :restrict_with_error - end + after do + Person.dependents.delete(association) + end - after do - Person.dependents.delete(association) - end + context 'when there are related objects' do - context 'when there are related objects' do + before do + person.restrictable_posts << post + end - before do - person.restrictable_posts << post + it 'adds an error to the parent object' do + expect(person.delete).to be(false) + + person.errors[:restrictable_posts].first.should == + "is not empty and prevents the document from being destroyed" + end end - it 'adds an error to the parent object' do - expect(person.delete).to be(false) - expect(person.errors[:restrictable_posts].first).to be( - Mongoid::Association::Depending::RESTRICT_ERROR_MSG) + context 'when there are no related objects' do + + before do + expect(post).to receive(:delete).never + expect(post).to receive(:destroy).never + end + + it 'deletes the object and leaves the other one intact' do + expect(person.delete).to be(true) + end end + + context 'when deleted inside a transaction' do + require_transaction_support + + before do + person.restrictable_posts << post + end + + it 'doesn\'t raise an exception' do + person.with_session do |session| + session.with_transaction do + expect { person.destroy }.to_not raise_error + end + end + end + end end - context 'when there are no related objects' do + context "when restricting a many to many" do - before do - expect(post).to receive(:delete).never - expect(post).to receive(:destroy).never + let!(:association) do + Person.has_and_belongs_to_many :houses, dependent: :restrict_with_error end - it 'deletes the object and leaves the other one intact' do - expect(person.delete).to be(true) + after do + Person.dependents.delete(association) + Person.has_and_belongs_to_many :houses, validate: false + end + + let(:person) do + Person.new houses: [House.new] + end + + it "returns false" do + expect(person.destroy).to be false + end + + context "when inside a transaction" do + require_transaction_support + + it 'doesn\'t raise an exception inside a transaction' do + person.with_session do |session| + session.with_transaction do + expect { person.destroy }.to_not raise_error + end + end + end end end end end