Skip to content

Commit

Permalink
Cover all mongo array update operators with tests. Include $pull and …
Browse files Browse the repository at this point in the history
…$pop in special handling conditions for Mongoize
  • Loading branch information
JohnMaguir committed May 20, 2024
1 parent 2835640 commit 3fb7254
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/mongoid/atomic_update_preparer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def prepare_operation(klass, key, value)

# Get the value for the provided operator, klass, key and value.
#
# This is necessary for special cases like $rename, $addToSet and $push.
# This is necessary for special cases like $rename, $addToSet, $push, $pull and $pop.
#
# @param [ String ] operator The operator.
# @param [ Class ] klass The model class.
Expand All @@ -60,7 +60,7 @@ def prepare_operation(klass, key, value)
def value_for(operator, klass, key, value)
case operator
when '$rename' then value.to_s
when '$addToSet', '$push' then value.mongoize
when '$addToSet', '$push', '$pull', '$pop' then value.mongoize
else mongoize_for(operator, klass, key, value)
end
end
Expand Down
69 changes: 69 additions & 0 deletions spec/mongoid/contextual/mongo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3640,6 +3640,75 @@
expect(new_order.reload.genres).to eq(["electronic"])
end
end

context "when operation is $pull" do
context "when pulling single element" do

before do
depeche_mode.update_attribute(:genres, ["electronic", "pop"])
new_order.update_attribute(:genres, ["electronic", "pop"])
context.update_all("$pull" => { genres: "electronic" })
end

it "updates the first matching document" do
expect(depeche_mode.reload.genres).to eq(["pop"])
end

it "updates the last matching document" do
expect(new_order.reload.genres).to eq(["pop"])
end
end

context "when pulling based on condition" do
before do
depeche_mode.update_attribute(:genres, ["electronic", "pop", "dance"])
new_order.update_attribute(:genres, ["electronic", "pop", "dance"])
context.update_all("$pull" => { genres: {'$in' => ["electronic", "pop"] }})
end

it "updates the first matching document" do
expect(depeche_mode.reload.genres).to eq(["dance"])
end

it "updates the last matching document" do
expect(new_order.reload.genres).to eq(["dance"])
end
end
end

context "when operation is $pop" do

before do
depeche_mode.update_attribute(:genres, ["pop", "electronic" ])
end

it "removes first element in array" do
context.update_all("$pop" => { genres: -1 })
expect(depeche_mode.reload.genres).to eq(["electronic"])
end

it "removes last element in array" do
context.update_all("$pop" => { genres: 1 })
expect(depeche_mode.reload.genres).to eq(["pop"])
end
end

context "when operation is $pullAll" do

before do
depeche_mode.update_attribute(:genres, ["pop", "electronic", "dance" ])
new_order.update_attribute(:genres, ["electronic", "pop", "dance"])
context.update_all("$pullAll" => { genres: ["pop", "electronic"] })
end

it "updates the first matching document" do
expect(depeche_mode.reload.genres).to eq(["dance"])
end

it "updates the last matching document" do
expect(new_order.reload.genres).to eq(["dance"])
end
end
end

context 'when using aliased field names' do
Expand Down

0 comments on commit 3fb7254

Please sign in to comment.