-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from bunnylushington/fix/update-with-in-filter
fix(bug): allow "complex" filter with update statement
- Loading branch information
Showing
2 changed files
with
35 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,9 @@ defmodule Moebius.UpdateTest do | |
end | ||
|
||
test "a basic user update", %{cmd: cmd} do | ||
assert cmd.sql == "update users set email = $1 where id = $2 returning *;" | ||
assert cmd.sql == "update users set email = $2 where id = $1 returning *;" | ||
assert length(cmd.params) == 2 | ||
assert cmd.params == [1, "[email protected]"] | ||
end | ||
|
||
test "a basic user insert has params set", %{cmd: cmd} do | ||
|
@@ -34,11 +35,36 @@ defmodule Moebius.UpdateTest do | |
test "a bulk update with a string filter and params" do | ||
cmd = | ||
db(:users) | ||
|> filter("email LIKE %$2", "test") | ||
|> filter("email LIKE %$1", "test") | ||
|> update(email: "[email protected]") | ||
|
||
assert cmd.sql == "update users set email = $1 where email LIKE %$2 returning *;" | ||
assert cmd.sql == "update users set email = $2 where email LIKE %$1 returning *;" | ||
assert length(cmd.params) == 2 | ||
assert cmd.params == ["test", "[email protected]"] | ||
end | ||
|
||
test "basic update with 'in' filter" do | ||
names = ["Super", "Mike"] | ||
|
||
cmd = | ||
db(:users) | ||
|> filter(:first, in: names) | ||
|> update(roles: ["newrole"]) | ||
|
||
assert cmd.sql == "update users set roles = $3 where first IN($1, $2) returning *;" | ||
assert length(cmd.params) == 3 | ||
assert cmd.params == names ++ [["newrole"]] | ||
end | ||
|
||
test "basic update with '>' filter" do | ||
cmd = | ||
db(:users) | ||
|> filter(:order_count, gt: 5) | ||
|> update(roles: ["newrole"]) | ||
|
||
assert cmd.sql == "update users set roles = $2 where order_count > $1 returning *;" | ||
assert length(cmd.params) == 2 | ||
assert cmd.params == [5, ["newrole"]] | ||
end | ||
|
||
# TODO: Move this to date tests | ||
|