Skip to content

Commit b013448

Browse files
v0idpwnjosevalim
andauthored
Handle new Macro.to_string results on tests (#3758)
* Handle new Macro.to_string results on tests Ecto frequently prints AST as code, and some tests were breaking because they do assumptions about how `Macro.to_string/1` will represent exprs. * Update test/ecto/query/inspect_test.exs Co-authored-by: José Valim <[email protected]> * Update test/ecto/changeset_test.exs Co-authored-by: José Valim <[email protected]> * Update test/ecto/query/builder/dynamic_test.exs Co-authored-by: José Valim <[email protected]> Co-authored-by: José Valim <[email protected]>
1 parent e38f9dd commit b013448

File tree

3 files changed

+116
-47
lines changed

3 files changed

+116
-47
lines changed

test/ecto/changeset_test.exs

Lines changed: 78 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,46 +1590,91 @@ defmodule Ecto.ChangesetTest do
15901590
assert Macro.to_string(check_expr) == "&0.body() == ^0"
15911591
end
15921592

1593-
test "generates correct where clause for single primary key without query option" do
1594-
body_change = cast(%SinglePkSchema{id: 0, body: "hi"}, %{body: "ho"}, [:body])
1595-
unsafe_validate_unique(body_change, :body, MockRepo)
1596-
assert_receive [MockRepo, function: :one, query: %Ecto.Query{wheres: wheres}, opts: []]
1597-
assert [%{expr: pk_expr}, %{expr: check_expr}] = wheres
1593+
# TODO: AST is represented as string differently on versions pre 1.13
1594+
if Version.match?(System.version(), ">= 1.13.0-dev") do
1595+
test "generates correct where clause for single primary key without query option" do
1596+
body_change = cast(%SinglePkSchema{id: 0, body: "hi"}, %{body: "ho"}, [:body])
1597+
unsafe_validate_unique(body_change, :body, MockRepo)
1598+
assert_receive [MockRepo, function: :one, query: %Ecto.Query{wheres: wheres}, opts: []]
1599+
assert [%{expr: pk_expr}, %{expr: check_expr}] = wheres
1600+
1601+
assert Macro.to_string(pk_expr) == "not (&0.id() == ^0)"
1602+
assert Macro.to_string(check_expr) == "&0.body() == ^0"
1603+
end
15981604

1599-
assert Macro.to_string(pk_expr) == "not(&0.id() == ^0)"
1600-
assert Macro.to_string(check_expr) == "&0.body() == ^0"
1601-
end
1605+
test "generates correct where clause for composite primary keys without query option" do
1606+
body_change = changeset(%Post{id: 0, token: 1, body: "hi"}, %{body: "ho"})
1607+
unsafe_validate_unique(body_change, :body, MockRepo)
1608+
assert_receive [MockRepo, function: :one, query: %Ecto.Query{wheres: wheres}, opts: []]
1609+
assert [%{expr: pk_expr}, %{expr: check_expr}] = wheres
16021610

1603-
test "generates correct where clause for composite primary keys without query option" do
1604-
body_change = changeset(%Post{id: 0, token: 1, body: "hi"}, %{body: "ho"})
1605-
unsafe_validate_unique(body_change, :body, MockRepo)
1606-
assert_receive [MockRepo, function: :one, query: %Ecto.Query{wheres: wheres}, opts: []]
1607-
assert [%{expr: pk_expr}, %{expr: check_expr}] = wheres
1611+
assert Macro.to_string(pk_expr) == "not (&0.id() == ^0 and &0.token() == ^1)"
1612+
assert Macro.to_string(check_expr) == "&0.body() == ^0"
1613+
end
16081614

1609-
assert Macro.to_string(pk_expr) == "not(&0.id() == ^0 and &0.token() == ^1)"
1610-
assert Macro.to_string(check_expr) == "&0.body() == ^0"
1611-
end
1615+
test "generates correct where clause for single primary key with query option" do
1616+
body_change = cast(%SinglePkSchema{id: 0, body: "hi"}, %{body: "ho"}, [:body])
1617+
unsafe_validate_unique(body_change, :body, MockRepo, query: Ecto.Query.from(p in SinglePkSchema, where: is_nil(p.published_at)))
1618+
assert_receive [MockRepo, function: :one, query: %Ecto.Query{wheres: wheres}, opts: []]
1619+
assert [%{expr: query_expr}, %{expr: pk_expr}, %{expr: check_expr}] = wheres
16121620

1613-
test "generates correct where clause for single primary key with query option" do
1614-
body_change = cast(%SinglePkSchema{id: 0, body: "hi"}, %{body: "ho"}, [:body])
1615-
unsafe_validate_unique(body_change, :body, MockRepo, query: Ecto.Query.from(p in SinglePkSchema, where: is_nil(p.published_at)))
1616-
assert_receive [MockRepo, function: :one, query: %Ecto.Query{wheres: wheres}, opts: []]
1617-
assert [%{expr: query_expr}, %{expr: pk_expr}, %{expr: check_expr}] = wheres
1621+
assert Macro.to_string(query_expr) == "is_nil(&0.published_at())"
1622+
assert Macro.to_string(pk_expr) == "not (&0.id() == ^0)"
1623+
assert Macro.to_string(check_expr) == "&0.body() == ^0"
1624+
end
16181625

1619-
assert Macro.to_string(query_expr) == "is_nil(&0.published_at())"
1620-
assert Macro.to_string(pk_expr) == "not(&0.id() == ^0)"
1621-
assert Macro.to_string(check_expr) == "&0.body() == ^0"
1622-
end
1626+
test "generates correct where clause for composite primary keys with query option" do
1627+
body_change = changeset(%Post{id: 0, token: 1, body: "hi"}, %{body: "ho"})
1628+
unsafe_validate_unique(body_change, :body, MockRepo, query: Ecto.Query.from(p in Post, where: is_nil(p.published_at)))
1629+
assert_receive [MockRepo, function: :one, query: %Ecto.Query{wheres: wheres}, opts: []]
1630+
assert [%{expr: query_expr}, %{expr: pk_expr}, %{expr: check_expr}] = wheres
16231631

1624-
test "generates correct where clause for composite primary keys with query option" do
1625-
body_change = changeset(%Post{id: 0, token: 1, body: "hi"}, %{body: "ho"})
1626-
unsafe_validate_unique(body_change, :body, MockRepo, query: Ecto.Query.from(p in Post, where: is_nil(p.published_at)))
1627-
assert_receive [MockRepo, function: :one, query: %Ecto.Query{wheres: wheres}, opts: []]
1628-
assert [%{expr: query_expr}, %{expr: pk_expr}, %{expr: check_expr}] = wheres
1632+
assert Macro.to_string(query_expr) == "is_nil(&0.published_at())"
1633+
assert Macro.to_string(pk_expr) == "not (&0.id() == ^0 and &0.token() == ^1)"
1634+
assert Macro.to_string(check_expr) == "&0.body() == ^0"
1635+
end
1636+
else
1637+
test "generates correct where clause for single primary key without query option" do
1638+
body_change = cast(%SinglePkSchema{id: 0, body: "hi"}, %{body: "ho"}, [:body])
1639+
unsafe_validate_unique(body_change, :body, MockRepo)
1640+
assert_receive [MockRepo, function: :one, query: %Ecto.Query{wheres: wheres}, opts: []]
1641+
assert [%{expr: pk_expr}, %{expr: check_expr}] = wheres
1642+
1643+
assert Macro.to_string(pk_expr) == "not(&0.id() == ^0)"
1644+
assert Macro.to_string(check_expr) == "&0.body() == ^0"
1645+
end
16291646

1630-
assert Macro.to_string(query_expr) == "is_nil(&0.published_at())"
1631-
assert Macro.to_string(pk_expr) == "not(&0.id() == ^0 and &0.token() == ^1)"
1632-
assert Macro.to_string(check_expr) == "&0.body() == ^0"
1647+
test "generates correct where clause for composite primary keys without query option" do
1648+
body_change = changeset(%Post{id: 0, token: 1, body: "hi"}, %{body: "ho"})
1649+
unsafe_validate_unique(body_change, :body, MockRepo)
1650+
assert_receive [MockRepo, function: :one, query: %Ecto.Query{wheres: wheres}, opts: []]
1651+
assert [%{expr: pk_expr}, %{expr: check_expr}] = wheres
1652+
1653+
assert Macro.to_string(pk_expr) == "not(&0.id() == ^0 and &0.token() == ^1)"
1654+
assert Macro.to_string(check_expr) == "&0.body() == ^0"
1655+
end
1656+
1657+
test "generates correct where clause for single primary key with query option" do
1658+
body_change = cast(%SinglePkSchema{id: 0, body: "hi"}, %{body: "ho"}, [:body])
1659+
unsafe_validate_unique(body_change, :body, MockRepo, query: Ecto.Query.from(p in SinglePkSchema, where: is_nil(p.published_at)))
1660+
assert_receive [MockRepo, function: :one, query: %Ecto.Query{wheres: wheres}, opts: []]
1661+
assert [%{expr: query_expr}, %{expr: pk_expr}, %{expr: check_expr}] = wheres
1662+
1663+
assert Macro.to_string(query_expr) == "is_nil(&0.published_at())"
1664+
assert Macro.to_string(pk_expr) == "not(&0.id() == ^0)"
1665+
assert Macro.to_string(check_expr) == "&0.body() == ^0"
1666+
end
1667+
1668+
test "generates correct where clause for composite primary keys with query option" do
1669+
body_change = changeset(%Post{id: 0, token: 1, body: "hi"}, %{body: "ho"})
1670+
unsafe_validate_unique(body_change, :body, MockRepo, query: Ecto.Query.from(p in Post, where: is_nil(p.published_at)))
1671+
assert_receive [MockRepo, function: :one, query: %Ecto.Query{wheres: wheres}, opts: []]
1672+
assert [%{expr: query_expr}, %{expr: pk_expr}, %{expr: check_expr}] = wheres
1673+
1674+
assert Macro.to_string(query_expr) == "is_nil(&0.published_at())"
1675+
assert Macro.to_string(pk_expr) == "not(&0.id() == ^0 and &0.token() == ^1)"
1676+
assert Macro.to_string(check_expr) == "&0.body() == ^0"
1677+
end
16331678
end
16341679

16351680
test "only queries the db when necessary" do

test/ecto/query/builder/dynamic_test.exs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,25 @@ defmodule Ecto.Query.Builder.DynamicTest do
2727
assert params == [{1, {0, :foo}}]
2828
end
2929

30-
test "with dynamic interpolation" do
31-
dynamic = dynamic([p], p.bar == ^2)
32-
dynamic = dynamic([p], p.foo == ^1 and ^dynamic or p.baz == ^3)
33-
assert {expr, _, params, [], _, _} = fully_expand(query(), dynamic)
34-
assert Macro.to_string(expr) ==
35-
"&0.foo() == ^0 and &0.bar() == ^1 or &0.baz() == ^2"
36-
assert params == [{1, {0, :foo}}, {2, {0, :bar}}, {3, {0, :baz}}]
30+
# TODO: AST is represented as string differently on versions pre 1.13
31+
if Version.match?(System.version(), ">= 1.13.0-dev") do
32+
test "with dynamic interpolation" do
33+
dynamic = dynamic([p], p.bar == ^2)
34+
dynamic = dynamic([p], p.foo == ^1 and ^dynamic or p.baz == ^3)
35+
assert {expr, _, params, [], _, _} = fully_expand(query(), dynamic)
36+
assert Macro.to_string(expr) ==
37+
"(&0.foo() == ^0 and &0.bar() == ^1) or &0.baz() == ^2"
38+
assert params == [{1, {0, :foo}}, {2, {0, :bar}}, {3, {0, :baz}}]
39+
end
40+
else
41+
test "with dynamic interpolation" do
42+
dynamic = dynamic([p], p.bar == ^2)
43+
dynamic = dynamic([p], p.foo == ^1 and ^dynamic or p.baz == ^3)
44+
assert {expr, _, params, [], _, _} = fully_expand(query(), dynamic)
45+
assert Macro.to_string(expr) ==
46+
"&0.foo() == ^0 and &0.bar() == ^1 or &0.baz() == ^2"
47+
assert params == [{1, {0, :foo}}, {2, {0, :bar}}, {3, {0, :baz}}]
48+
end
3749
end
3850

3951
test "with subquery and dynamic interpolation" do

test/ecto/query/inspect_test.exs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -348,13 +348,25 @@ defmodule Ecto.Query.InspectTest do
348348
) == string
349349
end
350350

351-
test "container values" do
352-
assert i(from(Post, select: <<1, 2, 3>>)) ==
353-
"from p0 in Inspect.Post, select: <<1, 2, 3>>"
354-
355-
foo = <<1, 2, 3>>
356-
assert i(from(p in Post, select: {p, ^foo})) ==
357-
"from p0 in Inspect.Post, select: {p0, ^<<1, 2, 3>>}"
351+
# TODO: AST is represented as string differently on versions pre 1.13
352+
if Version.match?(System.version(), ">= 1.13.0-dev") do
353+
test "container values" do
354+
assert i(from(Post, select: <<1, 2, 3>>)) ==
355+
"from p0 in Inspect.Post, select: \"\\x01\\x02\\x03\""
356+
357+
foo = <<1, 2, 3>>
358+
assert i(from(p in Post, select: {p, ^foo})) ==
359+
"from p0 in Inspect.Post, select: {p0, ^\"\\x01\\x02\\x03\"}"
360+
end
361+
else
362+
test "container values" do
363+
assert i(from(Post, select: <<1, 2, 3>>)) ==
364+
"from p0 in Inspect.Post, select: <<1, 2, 3>>"
365+
366+
foo = <<1, 2, 3>>
367+
assert i(from(p in Post, select: {p, ^foo})) ==
368+
"from p0 in Inspect.Post, select: {p0, ^<<1, 2, 3>>}"
369+
end
358370
end
359371

360372
test "select" do

0 commit comments

Comments
 (0)