Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 27 additions & 52 deletions test/ecto/changeset_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@ defmodule Ecto.ChangesetTest do
import Ecto.Changeset
require Ecto.Query

defmacrop assert_eq_macro_to_string(ast, post_1_13, pre_1_13) do
# AST is represented as string differently on versions pre 1.13
if Version.match?(System.version(), ">= 1.13.0-dev") do
quote do
assert Macro.to_string(unquote(ast)) == unquote(post_1_13)
end
else
quote do
assert Macro.to_string(unquote(ast)) == unquote(pre_1_13)
end
defmacrop assert_eq_macro_to_string(ast, string) do
quote do
assert Macro.to_string(unquote(ast)) == unquote(string)
end
end

Expand Down Expand Up @@ -2372,8 +2365,8 @@ defmodule Ecto.ChangesetTest do
assert_receive [MockRepo, function: :exists?, query: %Ecto.Query{wheres: wheres}, opts: []]
assert [%{expr: pk_expr}, %{expr: check_expr}] = wheres

assert_eq_macro_to_string(pk_expr, "not (&0.id() == ^0)", "not(&0.id() == ^0)")
assert_eq_macro_to_string(check_expr, "&0.body() == ^0", "&0.body() == ^0")
assert_eq_macro_to_string(pk_expr, "not (&0.id() == ^0)")
assert_eq_macro_to_string(check_expr, "&0.body() == ^0")
end

test "generates correct where clause for composite primary keys without query option for loaded schema" do
Expand All @@ -2386,13 +2379,9 @@ defmodule Ecto.ChangesetTest do
assert_receive [MockRepo, function: :exists?, query: %Ecto.Query{wheres: wheres}, opts: []]
assert [%{expr: pk_expr}, %{expr: check_expr}] = wheres

assert_eq_macro_to_string(
pk_expr,
"not (&0.id() == ^0 and &0.token() == ^1)",
"not(&0.id() == ^0 and &0.token() == ^1)"
)
assert_eq_macro_to_string(pk_expr, "not (&0.id() == ^0 and &0.token() == ^1)")

assert_eq_macro_to_string(check_expr, "&0.body() == ^0", "&0.body() == ^0")
assert_eq_macro_to_string(check_expr, "&0.body() == ^0")
end

test "generates correct where clause for single primary key with query option for loaded schema" do
Expand All @@ -2408,14 +2397,10 @@ defmodule Ecto.ChangesetTest do
assert_receive [MockRepo, function: :exists?, query: %Ecto.Query{wheres: wheres}, opts: []]
assert [%{expr: query_expr}, %{expr: pk_expr}, %{expr: check_expr}] = wheres

assert_eq_macro_to_string(
query_expr,
"is_nil(&0.published_at())",
"is_nil(&0.published_at())"
)
assert_eq_macro_to_string(query_expr, "is_nil(&0.published_at())")

assert_eq_macro_to_string(pk_expr, "not (&0.id() == ^0)", "not(&0.id() == ^0)")
assert_eq_macro_to_string(check_expr, "&0.body() == ^0", "&0.body() == ^0")
assert_eq_macro_to_string(pk_expr, "not (&0.id() == ^0)")
assert_eq_macro_to_string(check_expr, "&0.body() == ^0")
end

test "generates correct where clause for composite primary keys with query option for loaded schema" do
Expand All @@ -2431,19 +2416,11 @@ defmodule Ecto.ChangesetTest do
assert_receive [MockRepo, function: :exists?, query: %Ecto.Query{wheres: wheres}, opts: []]
assert [%{expr: query_expr}, %{expr: pk_expr}, %{expr: check_expr}] = wheres

assert_eq_macro_to_string(
query_expr,
"is_nil(&0.published_at())",
"is_nil(&0.published_at())"
)
assert_eq_macro_to_string(query_expr, "is_nil(&0.published_at())")

assert_eq_macro_to_string(
pk_expr,
"not (&0.id() == ^0 and &0.token() == ^1)",
"not(&0.id() == ^0 and &0.token() == ^1)"
)
assert_eq_macro_to_string(pk_expr, "not (&0.id() == ^0 and &0.token() == ^1)")

assert_eq_macro_to_string(check_expr, "&0.body() == ^0", "&0.body() == ^0")
assert_eq_macro_to_string(check_expr, "&0.body() == ^0")
end

test "generates correct where clause for single primary key without query option when schema wasn't loaded" do
Expand All @@ -2453,7 +2430,7 @@ defmodule Ecto.ChangesetTest do
assert_receive [MockRepo, function: :exists?, query: %Ecto.Query{wheres: wheres}, opts: []]
assert [%{expr: check_expr}] = wheres

assert_eq_macro_to_string(check_expr, "&0.body() == ^0", "&0.body() == ^0")
assert_eq_macro_to_string(check_expr, "&0.body() == ^0")
end

test "generates correct where clause for composite primary keys without query option when schema wasn't loaded" do
Expand All @@ -2463,7 +2440,7 @@ defmodule Ecto.ChangesetTest do
assert_receive [MockRepo, function: :exists?, query: %Ecto.Query{wheres: wheres}, opts: []]
assert [%{expr: check_expr}] = wheres

assert_eq_macro_to_string(check_expr, "&0.body() == ^0", "&0.body() == ^0")
assert_eq_macro_to_string(check_expr, "&0.body() == ^0")
end

test "generates correct where clause for single primary key with query option when schema wasn't loaded" do
Expand All @@ -2476,13 +2453,9 @@ defmodule Ecto.ChangesetTest do
assert_receive [MockRepo, function: :exists?, query: %Ecto.Query{wheres: wheres}, opts: []]
assert [%{expr: query_expr}, %{expr: check_expr}] = wheres

assert_eq_macro_to_string(
query_expr,
"is_nil(&0.published_at())",
"is_nil(&0.published_at())"
)
assert_eq_macro_to_string(query_expr, "is_nil(&0.published_at())")

assert_eq_macro_to_string(check_expr, "&0.body() == ^0", "&0.body() == ^0")
assert_eq_macro_to_string(check_expr, "&0.body() == ^0")
end

test "generates correct where clause for composite primary keys with query option when schema wasn't loaded" do
Expand All @@ -2495,13 +2468,9 @@ defmodule Ecto.ChangesetTest do
assert_receive [MockRepo, function: :exists?, query: %Ecto.Query{wheres: wheres}, opts: []]
assert [%{expr: query_expr}, %{expr: check_expr}] = wheres

assert_eq_macro_to_string(
query_expr,
"is_nil(&0.published_at())",
"is_nil(&0.published_at())"
)
assert_eq_macro_to_string(query_expr, "is_nil(&0.published_at())")

assert_eq_macro_to_string(check_expr, "&0.body() == ^0", "&0.body() == ^0")
assert_eq_macro_to_string(check_expr, "&0.body() == ^0")
end

test "only queries the db when necessary" do
Expand Down Expand Up @@ -2555,7 +2524,7 @@ defmodule Ecto.ChangesetTest do
assert_receive [MockRepo, function: :exists?, query: %Ecto.Query{wheres: wheres}, opts: []]
assert [%{expr: check_expr}] = wheres

assert_eq_macro_to_string(check_expr, "&0.origin() == ^0", "&0.origin() == ^0")
assert_eq_macro_to_string(check_expr, "&0.origin() == ^0")
end
end

Expand Down Expand Up @@ -3696,7 +3665,13 @@ defmodule Ecto.ChangesetTest do
end

test "redacts all non-primary-key fields when schema sets @schema_redact :all_except_primary_keys" do
changeset = Ecto.Changeset.cast(%RedactAllExceptPrimaryKeysSchema{}, %{username: "Hunter", password: "hunter2"}, [:username, :password])
changeset =
Ecto.Changeset.cast(
%RedactAllExceptPrimaryKeysSchema{},
%{username: "Hunter", password: "hunter2"},
[:username, :password]
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one seemingly was just caused by my mix format, sorry I can remove it if it's too annoying :)


assert inspect(changeset) =~ "id"
refute inspect(changeset) =~ "hunter2"
assert inspect(changeset) =~ "**redacted**"
Expand Down
61 changes: 17 additions & 44 deletions test/ecto/query/builder/dynamic_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,52 +27,25 @@ defmodule Ecto.Query.Builder.DynamicTest do
assert params == [{1, {0, :foo}}]
end

# TODO: AST is represented as string differently on versions pre 1.13
if Version.match?(System.version(), ">= 1.13.0-dev") do
test "with dynamic interpolation" do
dynamic = dynamic([p], p.bar == ^2)
dynamic = dynamic([p], p.foo == ^1 and ^dynamic or p.baz == ^3)
assert {expr, _, params, [], _, _} = fully_expand(query(), dynamic)
assert Macro.to_string(expr) ==
"(&0.foo() == ^0 and &0.bar() == ^1) or &0.baz() == ^2"
assert params == [{1, {0, :foo}}, {2, {0, :bar}}, {3, {0, :baz}}]
end
else
test "with dynamic interpolation" do
dynamic = dynamic([p], p.bar == ^2)
dynamic = dynamic([p], p.foo == ^1 and ^dynamic or p.baz == ^3)
assert {expr, _, params, [], _, _} = fully_expand(query(), dynamic)
assert Macro.to_string(expr) ==
"&0.foo() == ^0 and &0.bar() == ^1 or &0.baz() == ^2"
assert params == [{1, {0, :foo}}, {2, {0, :bar}}, {3, {0, :baz}}]
end
test "with dynamic interpolation" do
dynamic = dynamic([p], p.bar == ^2)
dynamic = dynamic([p], p.foo == ^1 and ^dynamic or p.baz == ^3)
assert {expr, _, params, [], _, _} = fully_expand(query(), dynamic)
assert Macro.to_string(expr) ==
"(&0.foo() == ^0 and &0.bar() == ^1) or &0.baz() == ^2"
assert params == [{1, {0, :foo}}, {2, {0, :bar}}, {3, {0, :baz}}]
end

# TODO: AST is represented as string differently on versions pre 1.13
if Version.match?(System.version(), ">= 1.13.0-dev") do
test "with subquery and dynamic interpolation" do
dynamic = dynamic([p], p.sq in subquery(query()))
dynamic = dynamic([p], p.bar1 == ^"bar1" or ^dynamic or p.bar3 == ^"bar3")
dynamic = dynamic([p], p.foo == ^"foo" and ^dynamic and p.baz == ^"baz")
assert {expr, binding, params, [_subquery], _, _} = fully_expand(query(), dynamic)
assert Macro.to_string(binding) == "[p]"
assert Macro.to_string(expr) ==
"&0.foo() == ^0 and (&0.bar1() == ^1 or &0.sq() in {:subquery, 0} or &0.bar3() == ^3) and\n &0.baz() == ^4"
assert params == [{"foo", {0, :foo}}, {"bar1", {0, :bar1}}, {:subquery, 0},
{"bar3", {0, :bar3}}, {"baz", {0, :baz}}]
end
else
test "with subquery and dynamic interpolation" do
dynamic = dynamic([p], p.sq in subquery(query()))
dynamic = dynamic([p], p.bar1 == ^"bar1" or ^dynamic or p.bar3 == ^"bar3")
dynamic = dynamic([p], p.foo == ^"foo" and ^dynamic and p.baz == ^"baz")
assert {expr, binding, params, [_subquery], _, _} = fully_expand(query(), dynamic)
assert Macro.to_string(binding) == "[p]"
assert Macro.to_string(expr) ==
"&0.foo() == ^0 and (&0.bar1() == ^1 or &0.sq() in {:subquery, 0} or &0.bar3() == ^3) and &0.baz() == ^4"
assert params == [{"foo", {0, :foo}}, {"bar1", {0, :bar1}}, {:subquery, 0},
{"bar3", {0, :bar3}}, {"baz", {0, :baz}}]
end
test "with subquery and dynamic interpolation" do
dynamic = dynamic([p], p.sq in subquery(query()))
dynamic = dynamic([p], p.bar1 == ^"bar1" or ^dynamic or p.bar3 == ^"bar3")
dynamic = dynamic([p], p.foo == ^"foo" and ^dynamic and p.baz == ^"baz")
assert {expr, binding, params, [_subquery], _, _} = fully_expand(query(), dynamic)
assert Macro.to_string(binding) == "[p]"
assert Macro.to_string(expr) ==
"&0.foo() == ^0 and (&0.bar1() == ^1 or &0.sq() in {:subquery, 0} or &0.bar3() == ^3) and\n &0.baz() == ^4"
assert params == [{"foo", {0, :foo}}, {"bar1", {0, :bar1}}, {:subquery, 0},
{"bar3", {0, :bar3}}, {"baz", {0, :baz}}]
end

test "with nested dynamic interpolation" do
Expand Down
23 changes: 6 additions & 17 deletions test/ecto/query/builder/join_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,12 @@ defmodule Ecto.Query.Builder.JoinTest do
end
end

# TODO: AST is represented as string differently on versions pre 1.13
if Version.match?(System.version(), ">= 1.13.0-dev") do
test "accepts keywords on :on" do
assert %{joins: [join]} =
join("posts", :inner, [p], c in "comments", on: [post_id: p.id, public: true])
assert Macro.to_string(join.on.expr) ==
"&1.post_id() == &0.id() and\n &1.public() == %Ecto.Query.Tagged{tag: nil, type: {1, :public}, value: true}"
assert join.on.params == []
end
else
test "accepts keywords on :on" do
assert %{joins: [join]} =
join("posts", :inner, [p], c in "comments", on: [post_id: p.id, public: true])
assert Macro.to_string(join.on.expr) ==
"&1.post_id() == &0.id() and &1.public() == %Ecto.Query.Tagged{tag: nil, type: {1, :public}, value: true}"
assert join.on.params == []
end
test "accepts keywords on :on" do
assert %{joins: [join]} =
join("posts", :inner, [p], c in "comments", on: [post_id: p.id, public: true])
assert Macro.to_string(join.on.expr) ==
"&1.post_id() == &0.id() and\n &1.public() == %Ecto.Query.Tagged{tag: nil, type: {1, :public}, value: true}"
assert join.on.params == []
end

test "accepts queries on interpolation" do
Expand Down
Loading
Loading