diff --git a/test/ecto/changeset_test.exs b/test/ecto/changeset_test.exs index b70216f04f..a7b966fa86 100644 --- a/test/ecto/changeset_test.exs +++ b/test/ecto/changeset_test.exs @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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] + ) + assert inspect(changeset) =~ "id" refute inspect(changeset) =~ "hunter2" assert inspect(changeset) =~ "**redacted**" diff --git a/test/ecto/query/builder/dynamic_test.exs b/test/ecto/query/builder/dynamic_test.exs index 0d3328fc7f..d11e11be4b 100644 --- a/test/ecto/query/builder/dynamic_test.exs +++ b/test/ecto/query/builder/dynamic_test.exs @@ -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 diff --git a/test/ecto/query/builder/join_test.exs b/test/ecto/query/builder/join_test.exs index fc95f61ba7..96c6f2518c 100644 --- a/test/ecto/query/builder/join_test.exs +++ b/test/ecto/query/builder/join_test.exs @@ -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 diff --git a/test/ecto/query/builder/select_test.exs b/test/ecto/query/builder/select_test.exs index eab58b0cde..e27adbbcf7 100644 --- a/test/ecto/query/builder/select_test.exs +++ b/test/ecto/query/builder/select_test.exs @@ -296,123 +296,64 @@ defmodule Ecto.Query.Builder.SelectTest do assert length(query.select.params) == 1 end - # TODO: AST is represented as string differently on versions pre 1.13 - if Version.match?(System.version(), ">= 1.13.0-dev") do - test "supports multiple nested partly dynamic subqueries" do - created_by_id = 8 - ignore_template_id = 9 - - subquery0 = - from(t in "tasks", - where: t.list_id == parent_as(:list).id and t.created_by_id == ^created_by_id, - select: max(t.due_on) - ) + test "supports multiple nested partly dynamic subqueries" do + created_by_id = 8 + ignore_template_id = 9 + + subquery0 = + from(t in "tasks", + where: t.list_id == parent_as(:list).id and t.created_by_id == ^created_by_id, + select: max(t.due_on) + ) - subquery1 = - from(t in "templates", where: parent_as(^:list).from_template_id == t.id, select: t.title) - - subquery2 = - from(u in "users", where: parent_as(^:list).created_by_id == u.id, select: u.email) - - ref = - dynamic( - [l], - fragment( - "CASE WHEN ? THEN ? ELSE ? END", - l.from_template_id == ^ignore_template_id, - "", - subquery(subquery1) - ) - ) + subquery1 = + from(t in "templates", where: parent_as(^:list).from_template_id == t.id, select: t.title) - query = - from(l in "lists", - as: :list, - select: %{ - title: l.archived_at, - maxdue: subquery(subquery0), - user_email: subquery(subquery2) - }, - select_merge: ^%{template_name: ref} - ) - - assert Macro.to_string(query.select.expr) == """ - %{\n\ - title: &0.archived_at(),\n\ - maxdue: {:subquery, 0},\n\ - user_email: {:subquery, 1},\n\ - template_name:\n\ - fragment(\n\ - {:raw, "CASE WHEN "},\n\ - {:expr, &0.from_template_id() == ^2},\n\ - {:raw, " THEN "},\n\ - {:expr, ""},\n\ - {:raw, " ELSE "},\n\ - {:expr, {:subquery, 2}},\n\ - {:raw, " END"}\n\ - )\n\ - }\ - """ - - assert length(query.select.subqueries) == 3 - assert query.select.params == [{:subquery, 0}, {:subquery, 1}, {ignore_template_id, {0, :from_template_id}}, {:subquery, 2}] - end - else - test "supports multiple nested partly dynamic subqueries" do - created_by_id = 8 - ignore_template_id = 9 - - subquery0 = - from(t in "tasks", - where: t.list_id == parent_as(:list).id and t.created_by_id == ^created_by_id, - select: max(t.due_on) - ) + subquery2 = + from(u in "users", where: parent_as(^:list).created_by_id == u.id, select: u.email) - subquery1 = - from(t in "templates", where: parent_as(^:list).from_template_id == t.id, select: t.title) - - subquery2 = - from(u in "users", where: parent_as(^:list).created_by_id == u.id, select: u.email) - - ref = - dynamic( - [l], - fragment( - "CASE WHEN ? THEN ? ELSE ? END", - l.from_template_id == ^ignore_template_id, - "", - subquery(subquery1) - ) + ref = + dynamic( + [l], + fragment( + "CASE WHEN ? THEN ? ELSE ? END", + l.from_template_id == ^ignore_template_id, + "", + subquery(subquery1) ) + ) - query = - from(l in "lists", - as: :list, - select: %{ - title: l.archived_at, - maxdue: subquery(subquery0), - user_email: subquery(subquery2) - }, - select_merge: ^%{template_name: ref} - ) + query = + from(l in "lists", + as: :list, + select: %{ + title: l.archived_at, + maxdue: subquery(subquery0), + user_email: subquery(subquery2) + }, + select_merge: ^%{template_name: ref} + ) - assert Macro.to_string(query.select.expr) == """ - %{\ - title: &0.archived_at(), \ - maxdue: {:subquery, 0}, \ - user_email: {:subquery, 1}, \ - template_name:\ - fragment({:raw, "CASE WHEN "},\ - {:expr, &0.from_template_id() == ^2},\ - {:raw, " THEN "}, {:expr, ""},\ - {:raw, " ELSE "}, {:expr, {:subquery, 2}},\ - {:raw, " END"})\ - }\ - """ - - assert length(query.select.subqueries) == 3 - assert query.select.params == [{:subquery, 0}, {:subquery, 1}, {ignore_template_id, {0, :from_template_id}}, {:subquery, 2}] - end + assert Macro.to_string(query.select.expr) == """ + %{\n\ + title: &0.archived_at(),\n\ + maxdue: {:subquery, 0},\n\ + user_email: {:subquery, 1},\n\ + template_name:\n\ + fragment(\n\ + {:raw, "CASE WHEN "},\n\ + {:expr, &0.from_template_id() == ^2},\n\ + {:raw, " THEN "},\n\ + {:expr, ""},\n\ + {:raw, " ELSE "},\n\ + {:expr, {:subquery, 2}},\n\ + {:raw, " END"}\n\ + )\n\ + }\ + """ + + assert length(query.select.subqueries) == 3 + assert query.select.params == [{:subquery, 0}, {:subquery, 1}, {ignore_template_id, {0, :from_template_id}}, {:subquery, 2}] end test "supports interpolated atom names in selected_as/2" do diff --git a/test/ecto/query/inspect_test.exs b/test/ecto/query/inspect_test.exs index 3f3ce545d1..f85c2c38b1 100644 --- a/test/ecto/query/inspect_test.exs +++ b/test/ecto/query/inspect_test.exs @@ -310,29 +310,15 @@ defmodule Ecto.Query.InspectTest do ~s{from p0 in Inspect.Post, lock: "FOOBAR"} end - # TODO: AST is represented as string differently on versions pre 1.13 - if Version.match?(System.version(), ">= 1.13.0-dev") do - test "preload" do - assert i(from(x in Post, preload: :comments)) == - ~s"from p0 in Inspect.Post, preload: [:comments]" + test "preload" do + assert i(from(x in Post, preload: :comments)) == + ~s"from p0 in Inspect.Post, preload: [:comments]" - assert i(from(x in Post, join: y in assoc(x, :comments), preload: [comments: y])) == - ~s"from p0 in Inspect.Post, join: c1 in assoc(p0, :comments), preload: [comments: c1]" + assert i(from(x in Post, join: y in assoc(x, :comments), preload: [comments: y])) == + ~s"from p0 in Inspect.Post, join: c1 in assoc(p0, :comments), preload: [comments: c1]" - assert i(from(x in Post, join: y in assoc(x, :comments), preload: [comments: {y, post: x}])) == - ~s"from p0 in Inspect.Post, join: c1 in assoc(p0, :comments), preload: [comments: {c1, post: p0}]" - end - else - test "preload" do - assert i(from(x in Post, preload: :comments)) == - ~s"from p0 in Inspect.Post, preload: [:comments]" - - assert i(from(x in Post, join: y in assoc(x, :comments), preload: [comments: y])) == - ~s"from p0 in Inspect.Post, join: c1 in assoc(p0, :comments), preload: [comments: c1]" - - assert i(from(x in Post, join: y in assoc(x, :comments), preload: [comments: {y, post: x}])) == - ~s"from p0 in Inspect.Post, join: c1 in assoc(p0, :comments), preload: [comments: {c1, [post: p0]}]" - end + assert i(from(x in Post, join: y in assoc(x, :comments), preload: [comments: {y, post: x}])) == + ~s"from p0 in Inspect.Post, join: c1 in assoc(p0, :comments), preload: [comments: {c1, post: p0}]" end test "fragments" do @@ -445,27 +431,14 @@ defmodule Ecto.Query.InspectTest do ) == string end - # TODO: AST is represented as string differently on versions pre 1.13 - if Version.match?(System.version(), ">= 1.13.0-dev") do - test "container values" do - assert i(from(Post, select: <<1, 2, 3>>)) == - "from p0 in Inspect.Post, select: \"\\x01\\x02\\x03\"" - - foo = <<1, 2, 3>> - - assert i(from(p in Post, select: {p, ^foo})) == - "from p0 in Inspect.Post, select: {p0, ^\"\\x01\\x02\\x03\"}" - end - else - test "container values" do - assert i(from(Post, select: <<1, 2, 3>>)) == - "from p0 in Inspect.Post, select: <<1, 2, 3>>" + test "container values" do + assert i(from(Post, select: <<1, 2, 3>>)) == + "from p0 in Inspect.Post, select: \"\\x01\\x02\\x03\"" - foo = <<1, 2, 3>> + foo = <<1, 2, 3>> - assert i(from(p in Post, select: {p, ^foo})) == - "from p0 in Inspect.Post, select: {p0, ^<<1, 2, 3>>}" - end + assert i(from(p in Post, select: {p, ^foo})) == + "from p0 in Inspect.Post, select: {p0, ^\"\\x01\\x02\\x03\"}" end test "select" do diff --git a/test/ecto/query/planner_test.exs b/test/ecto/query/planner_test.exs index 5e32c12704..790db850e5 100644 --- a/test/ecto/query/planner_test.exs +++ b/test/ecto/query/planner_test.exs @@ -482,67 +482,34 @@ defmodule Ecto.Query.PlannerTest do ~r"&1.post_id\(\) == &0.id\(\) and not[\s\(]is_nil\(&1.text\(\)\)\)?" end - # TODO: AST is represented as string differently on versions pre 1.13 - if Version.match?(System.version(), ">= 1.13.0-dev") do - test "plan: nested joins associations with custom queries" do - query = - from(p in Post, - join: c1 in assoc(p, :special_comments), - join: p2 in assoc(c1, :post), - join: cp in assoc(c1, :comment_posts), - join: c2 in assoc(cp, :special_comment), - join: c3 in assoc(cp, :special_long_comment) - ) - |> plan - |> elem(0) - - assert [join1, join2, join3, join4, join5] = query.joins - - assert {{"posts", _, _}, {"comments", _, _}, {"posts", _, _}, {"comment_posts", _, _}, - {"comments", _, _}, {"comments", _, _}} = query.sources - - assert Macro.to_string(join1.on.expr) =~ - ~r"&1.post_id\(\) == &0.id\(\) and not[\s\(]is_nil\(&1.text\(\)\)\)?" - - assert Macro.to_string(join2.on.expr) == "&2.id() == &1.post_id()" - assert Macro.to_string(join3.on.expr) == "&3.comment_id() == &1.id()" - - assert Macro.to_string(join4.on.expr) == - "&4.id() == &3.special_comment_id() and is_nil(&4.text())" - - assert Macro.to_string(join5.on.expr) == - "&5.id() == &3.special_long_comment_id() and\n fragment({:raw, \"LEN(\"}, {:expr, &5.text()}, {:raw, \") > 100\"})" - end - else - test "plan: nested joins associations with custom queries" do - query = - from(p in Post, - join: c1 in assoc(p, :special_comments), - join: p2 in assoc(c1, :post), - join: cp in assoc(c1, :comment_posts), - join: c2 in assoc(cp, :special_comment), - join: c3 in assoc(cp, :special_long_comment) - ) - |> plan - |> elem(0) + test "plan: nested joins associations with custom queries" do + query = + from(p in Post, + join: c1 in assoc(p, :special_comments), + join: p2 in assoc(c1, :post), + join: cp in assoc(c1, :comment_posts), + join: c2 in assoc(cp, :special_comment), + join: c3 in assoc(cp, :special_long_comment) + ) + |> plan + |> elem(0) - assert [join1, join2, join3, join4, join5] = query.joins + assert [join1, join2, join3, join4, join5] = query.joins - assert {{"posts", _, _}, {"comments", _, _}, {"posts", _, _}, {"comment_posts", _, _}, - {"comments", _, _}, {"comments", _, _}} = query.sources + assert {{"posts", _, _}, {"comments", _, _}, {"posts", _, _}, {"comment_posts", _, _}, + {"comments", _, _}, {"comments", _, _}} = query.sources - assert Macro.to_string(join1.on.expr) =~ - ~r"&1.post_id\(\) == &0.id\(\) and not[\s\(]is_nil\(&1.text\(\)\)\)?" + assert Macro.to_string(join1.on.expr) =~ + ~r"&1.post_id\(\) == &0.id\(\) and not[\s\(]is_nil\(&1.text\(\)\)\)?" - assert Macro.to_string(join2.on.expr) == "&2.id() == &1.post_id()" - assert Macro.to_string(join3.on.expr) == "&3.comment_id() == &1.id()" + assert Macro.to_string(join2.on.expr) == "&2.id() == &1.post_id()" + assert Macro.to_string(join3.on.expr) == "&3.comment_id() == &1.id()" - assert Macro.to_string(join4.on.expr) == - "&4.id() == &3.special_comment_id() and is_nil(&4.text())" + assert Macro.to_string(join4.on.expr) == + "&4.id() == &3.special_comment_id() and is_nil(&4.text())" - assert Macro.to_string(join5.on.expr) == - "&5.id() == &3.special_long_comment_id() and fragment({:raw, \"LEN(\"}, {:expr, &5.text()}, {:raw, \") > 100\"})" - end + assert Macro.to_string(join5.on.expr) == + "&5.id() == &3.special_long_comment_id() and\n fragment({:raw, \"LEN(\"}, {:expr, &5.text()}, {:raw, \") > 100\"})" end test "plan: raises on invalid binding index in join" do