Skip to content

Commit

Permalink
Fix unit tests for OTP 26 (#4301)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: José Valim <[email protected]>
  • Loading branch information
greg-rychlewski and josevalim authored Oct 17, 2023
1 parent d8ede0c commit a50afdf
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
matrix:
include:
- elixir: 1.15.6
otp: 25.3.2.6
otp: 26.1.2
lint: lint
- elixir: 1.15.6
otp: 24.3.4.13
Expand Down
11 changes: 9 additions & 2 deletions test/ecto/changeset_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2428,8 +2428,15 @@ defmodule Ecto.ChangesetTest do
assert inspect(%Ecto.Changeset{}) ==
"#Ecto.Changeset<action: nil, changes: %{}, errors: [], data: nil, valid?: false>"

assert inspect(changeset(%{"title" => "title", "body" => "hi"})) ==
"#Ecto.Changeset<action: nil, changes: %{body: \"hi\", title: \"title\"}, " <>
changes = %{"title" => "title", "body" => "hi"}

normalized_changes =
changes
|> Enum.map(fn {field, value} -> {String.to_atom(field), value} end)
|> Enum.into(%{})

assert inspect(changeset(changes)) ==
"#Ecto.Changeset<action: nil, changes: #{inspect normalized_changes}, " <>
"errors: [], data: #Ecto.ChangesetTest.Post<>, valid?: true>"

data = {%NoSchemaPost{title: "hello"}, %{title: :string, upvotes: :integer}}
Expand Down
2 changes: 1 addition & 1 deletion test/ecto/query/builder/from_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule Ecto.Query.Builder.FromTest do
types = %{num: :integer, text: :string}
query = from v in values(values, types)

types_kw = [num: :integer, text: :string]
types_kw = Enum.map(types, & &1)
assert query.from.source == {:values,[], [types_kw, length(values)]}

# Missing type
Expand Down
2 changes: 1 addition & 1 deletion test/ecto/query/builder/join_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defmodule Ecto.Query.Builder.JoinTest do
query = from p in "posts", join: v in values(values, types), on: true

[join] = query.joins
types_kw = [num: :integer, text: :string]
types_kw = Enum.map(types, & &1)
assert {:values, [], [types_kw, length(values)]} == join.source

# Missing type
Expand Down
9 changes: 6 additions & 3 deletions test/ecto/query/inspect_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,12 @@ defmodule Ecto.Query.InspectTest do
end

test "values lists" do
query = from v in values([%{a: 1, b: 2, c: 3}], %{a: :integer, b: :integer, c: :integer})
assert i(query) == "from v0 in values (a, b, c)"
assert i(plan(query)) == "from v0 in values (a, b, c)"
values_list = [%{a: 1, b: 2, c: 3}]
types = %{a: :integer, b: :integer, c: :integer}
fields = types |> Map.keys() |> Enum.map_join(", ", & &1)
query = from v in values(values_list, types)
assert i(query) == "from v0 in values (#{fields})"
assert i(plan(query)) == "from v0 in values (#{fields})"
end

def plan(query) do
Expand Down
39 changes: 27 additions & 12 deletions test/ecto/query/planner_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ defmodule Ecto.Query.PlannerTest do
test "plan: merges all parameters" do
uuid = Ecto.UUID.generate()
{:ok, dump_uuid} = Ecto.UUID.dump(uuid)
values = %{bid: uuid, text: "values"}
dump_values = %{bid: dump_uuid, text: "values"}
values_types = %{bid: Ecto.UUID, text: :string}
union = from p in Post, select: {p.title, ^"union"}
subquery = from Comment, where: [text: ^"subquery"]

Expand All @@ -174,7 +177,7 @@ defmodule Ecto.Query.PlannerTest do
on: c.text == ^"join",
join: p in Post,
on: f.title == p.title,
join: v in values([%{bid: uuid, text: "values"}], %{bid: Ecto.UUID, text: :string}),
join: v in values([values], values_types),
on: v.text == ^"on_values",
left_join: d in assoc(p, :comments),
union_all: ^union,
Expand All @@ -190,13 +193,13 @@ defmodule Ecto.Query.PlannerTest do

assert cast_params ==
["select", "fragment_source1", "fragment_source2", "subquery", "join"] ++
[uuid, "values", "on_values", "where", "group_by", "having", "windows"] ++
["union", "order_by", 0, 1]
Enum.map(values_types, fn {field, _} -> values[field] end) ++
["on_values", "where", "group_by", "having", "windows", "union", "order_by", 0, 1]

assert dump_params ==
["select", "fragment_source1", "fragment_source2", "subquery", "join"] ++
[dump_uuid, "values", "on_values", "where", "group_by", "having", "windows"] ++
["union", "order_by", 0, 1]
Enum.map(values_types, fn {field, _} -> dump_values[field] end) ++
["on_values", "where", "group_by", "having", "windows", "union", "order_by", 0, 1]
end

test "plan: checks from" do
Expand Down Expand Up @@ -1467,10 +1470,12 @@ defmodule Ecto.Query.PlannerTest do
q = from(v in values(values, types)) |> normalize()

start_param_ix = 0
types_kw = [bid: :uuid, num: :integer]
native_types = %{bid: :uuid, num: :integer}
types_kw = Enum.map(types, fn {field, _} -> {field, native_types[field]} end)
field_ast = Enum.map(types, fn {field, _} -> {{:., [], [{:&, [], [0]}, field]}, [], []} end)

assert q.from.source == {:values, [], [types_kw, start_param_ix, length(values)]}
assert q.select.fields == [{{:., [], [{:&, [], [0]}, :bid]}, [], []}, {{:., [], [{:&, [], [0]}, :num]}, [], []}]
assert q.select.fields == field_ast
end

test "normalize: join values list" do
Expand All @@ -1480,11 +1485,13 @@ defmodule Ecto.Query.PlannerTest do
q = from(f in fragment("select ?", ^1), join: v in values(values, types), on: true, select: v) |> normalize()

start_param_ix = 1
types_kw = [bid: :uuid, num: :integer]
native_types = %{bid: :uuid, num: :integer}
types_kw = Enum.map(types, fn {field, _} -> {field, native_types[field]} end)
field_ast = Enum.map(types, fn {field, _} -> {{:., [], [{:&, [], [1]}, field]}, [], []} end)
[join] = q.joins

assert join.source == {:values, [], [types_kw, start_param_ix, length(values)]}
assert q.select.fields == [{{:., [], [{:&, [], [1]}, :bid]}, [], []}, {{:., [], [{:&, [], [1]}, :num]}, [], []}]
assert q.select.fields == field_ast
end

test "normalize: select a value list field" do
Expand Down Expand Up @@ -1859,16 +1866,24 @@ defmodule Ecto.Query.PlannerTest do
end

test "normalize: preload assoc merges" do
{_, _, _, select} =
query =
from(p in Post)
|> join(:inner, [p], c in assoc(p, :comments))
|> join(:inner, [_, c], cp in assoc(c, :comment_posts))
|> join(:inner, [_, c], ip in assoc(c, :post))
|> preload([_, c, cp, _], comments: {c, comment_posts: cp})
|> preload([_, c, _, ip], comments: {c, post: ip})
|> normalize_with_params()

assert select.assocs == [comments: {1, [comment_posts: {2, []}, post: {3, []}]}]
{_, _, _, select} = normalize_with_params(query)

nested_assocs =
query.assocs
|> Enum.reduce(%{}, fn
{_, {_, [{assoc, ix_assocs}]}}, acc -> Map.put(acc, assoc, ix_assocs)
end)
|> Enum.map(& &1)

assert select.assocs == [comments: {1, nested_assocs}]
end

test "normalize: preload assoc errors" do
Expand Down
31 changes: 21 additions & 10 deletions test/ecto/repo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -595,19 +595,30 @@ defmodule Ecto.RepoTest do
[y: query, z: query]
])

assert_received {:insert_all, %{source: "my_schema"}, rows}
assert_received {:insert_all, %{source: "my_schema", header: header}, planned_rows}

assert [
[x: "x1", yyy: "y1"],
[x: {%Ecto.Query{} = query2, [^value]}, z: "z2"],
[x: {%Ecto.Query{} = query3, [^value]}, z: "z3"],
[yyy: {%Ecto.Query{} = query4y, [^value]}, z: {%Ecto.Query{} = query4x, [^value]}]
] = rows

assert [%{expr: {:==, _, [_, {:^, [], [2]}]}}] = query2.wheres
assert [%{expr: {:==, _, [_, {:^, [], [4]}]}}] = query3.wheres
assert [%{expr: {:==, _, [_, {:^, [], [6]}]}}] = query4y.wheres
assert [%{expr: {:==, _, [_, {:^, [], [7]}]}}] = query4x.wheres
[x: {%Ecto.Query{}, [^value]}, z: "z2"],
[x: {%Ecto.Query{}, [^value]}, z: "z3"],
[yyy: {%Ecto.Query{}, [^value]}, z: {%Ecto.Query{}, [^value]}]
] = planned_rows

{queries_with_index, _} =
for row <- planned_rows, field <- header, reduce: {[], 0} do
{queries, ix} ->
case row[field] do
{%Ecto.Query{} = query, _} -> {[{ix, query} | queries], ix + 1}
nil -> {queries, ix}
_ -> {queries, ix + 1}
end
end

assert length(queries_with_index) == 4

Enum.each(queries_with_index, fn {ix, query} ->
assert [%{expr: {:==, _, [_, {:^, [], [^ix]}]}}] = query.wheres
end)
end

test "takes query as datasource" do
Expand Down

0 comments on commit a50afdf

Please sign in to comment.