spec/mongoid/persistable/pushable_spec.rb in mongoid-8.0.8 vs spec/mongoid/persistable/pushable_spec.rb in mongoid-8.1.0

- old
+ new

@@ -202,10 +202,46 @@ person.add_to_set test_array: [ 1, 4 ] expect(person.changes).to eq({"test_array" => [[ 1, 2, 3 ], [ 1, 2, 3, 4 ]]}) end end end + + context "when executing on a readonly document" do + + let(:person) do + Person.create!(test_array: [ 1, 2, 3 ]) + end + + context "when legacy_readonly is true" do + config_override :legacy_readonly, true + + before do + person.__selected_fields = { "test_array" => 1 } + end + + it "persists the changes" do + expect(person).to be_readonly + person.add_to_set(test_array: [ 1, 4 ]) + expect(person.test_array).to eq([ 1, 2, 3, 4 ]) + end + end + + context "when legacy_readonly is false" do + config_override :legacy_readonly, false + + before do + person.readonly! + end + + it "raises a ReadonlyDocument error" do + expect(person).to be_readonly + expect do + person.add_to_set(test_array: [ 1, 4 ]) + end.to raise_error(Mongoid::Errors::ReadonlyDocument) + end + end + end end describe "#push" do context "when the document is a root document" do @@ -344,9 +380,45 @@ it "marks a dirty change for the pushed fields" do person.atomically do person.push test_array: [ 1, 4 ] expect(person.changes).to eq({"test_array" => [[ 1, 2, 3 ], [ 1, 2, 3, 1, 4 ]]}) + end + end + end + + context "when executing on a readonly document" do + + let(:person) do + Person.create!(test_array: [ 1, 2, 3 ]) + end + + context "when legacy_readonly is true" do + config_override :legacy_readonly, true + + before do + person.__selected_fields = { "test_array" => 1 } + end + + it "persists the changes" do + expect(person).to be_readonly + person.push(test_array: [ 1, 4 ]) + expect(person.test_array).to eq([ 1, 2, 3, 1, 4 ]) + end + end + + context "when legacy_readonly is false" do + config_override :legacy_readonly, false + + before do + person.readonly! + end + + it "raises a ReadonlyDocument error" do + expect(person).to be_readonly + expect do + person.push(test_array: [ 1, 4 ]) + end.to raise_error(Mongoid::Errors::ReadonlyDocument) end end end end end