Skip to content

Commit abd8759

Browse files
committed
Format and refactor tests
1 parent 51f8a17 commit abd8759

8 files changed

Lines changed: 585 additions & 288 deletions

File tree

test/ecto/query/builder/distinct_test.exs

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,26 @@ defmodule Ecto.Query.Builder.DistinctTest do
99
describe "escape" do
1010
test "handles expressions and params" do
1111
assert {true, {[], %{}}} ==
12-
escape(true, {[], %{}}, [x: 0], __ENV__)
12+
escape(true, {[], %{}}, [x: 0], __ENV__)
1313

14-
assert {Macro.escape(quote do [asc: &0.y()] end), {[], %{}}} ==
15-
escape(quote do x.y() end, {[], %{}}, [x: 0], __ENV__)
14+
assert {Macro.escape(quote(do: [asc: &0.y()])), {[], %{}}} ==
15+
escape(quote(do: x.y()), {[], %{}}, [x: 0], __ENV__)
1616

17-
assert {Macro.escape(quote do [asc: &0.x(), asc: &1.y()] end), {[], %{}}} ==
18-
escape(quote do [x.x(), y.y()] end, {[], %{}}, [x: 0, y: 1], __ENV__)
17+
assert {Macro.escape(quote(do: [asc: &0.x(), asc: &1.y()])), {[], %{}}} ==
18+
escape(quote(do: [x.x(), y.y()]), {[], %{}}, [x: 0, y: 1], __ENV__)
1919

20-
assert {Macro.escape(quote do [asc: &0.x(), desc: &1.y()] end), {[], %{}}} ==
21-
escape(quote do [x.x(), desc: y.y()] end, {[], %{}}, [x: 0, y: 1], __ENV__)
20+
assert {Macro.escape(quote(do: [asc: &0.x(), desc: &1.y()])), {[], %{}}} ==
21+
escape(quote(do: [x.x(), desc: y.y()]), {[], %{}}, [x: 0, y: 1], __ENV__)
2222

2323
import Kernel, except: [>: 2]
24-
assert {Macro.escape(quote do [asc: 1 > 2] end), {[], %{}}} ==
25-
escape(quote do 1 > 2 end, {[], %{}}, [], __ENV__)
24+
25+
assert {Macro.escape(quote(do: [asc: 1 > 2])), {[], %{}}} ==
26+
escape(quote(do: 1 > 2), {[], %{}}, [], __ENV__)
2627
end
2728

2829
test "raises on unbound variables" do
2930
assert_raise Ecto.Query.CompileError, ~r"unbound variable `x` in query", fn ->
30-
escape(quote do x.y end, {[], %{}}, [], __ENV__)
31+
escape(quote(do: x.y), {[], %{}}, [], __ENV__)
3132
end
3233
end
3334
end
@@ -41,7 +42,7 @@ defmodule Ecto.Query.Builder.DistinctTest do
4142

4243
test "accepts keyword lists" do
4344
kw = [desc: :title]
44-
assert distinct("q", [q], ^kw).distinct == distinct("q", [q], [desc: q.title]).distinct
45+
assert distinct("q", [q], ^kw).distinct == distinct("q", [q], desc: q.title).distinct
4546
end
4647

4748
test "accepts the boolean true" do
@@ -57,28 +58,41 @@ defmodule Ecto.Query.Builder.DistinctTest do
5758
]
5859

5960
%{distinct: distinct} = distinct("posts", ^order_by)
61+
6062
assert Macro.to_string(distinct.expr) ==
61-
"[asc: &0.foo() == ^0 and &0.bar() == ^1, desc: &0.bar(), asc: &0.baz() == ^2 and &0.bat() == ^3]"
63+
"[asc: &0.foo() == ^0 and &0.bar() == ^1, desc: &0.bar(), asc: &0.baz() == ^2 and &0.bat() == ^3]"
64+
6265
assert distinct.params ==
63-
[{1, {0, :foo}}, {"bar", {0, :bar}}, {2, {0, :baz}}, {"bat", {0, :bat}}]
66+
[{1, {0, :foo}}, {"bar", {0, :bar}}, {2, {0, :baz}}, {"bat", {0, :bat}}]
6467
end
6568

6669
test "supports subqueries" do
6770
distinct = [
68-
asc: dynamic([p], exists(from other_post in "posts", where: other_post.id == parent_as(:p).id))
71+
asc:
72+
dynamic(
73+
[p],
74+
exists(from other_post in "posts", where: other_post.id == parent_as(:p).id)
75+
)
6976
]
7077

7178
%{distinct: distinct} = from p in "posts", as: :p, distinct: ^distinct
72-
assert distinct.expr == [asc: {:exists, [], [subquery: 0]}]
79+
assert distinct.expr == [asc: {:exists, [], [subquery: 0]}]
7380
assert [_] = distinct.subqueries
7481

75-
%{distinct: distinct} = from p in "posts", as: :p, distinct: [asc: exists(from other_post in "posts", where: other_post.id == parent_as(:p).id)]
76-
assert distinct.expr == [asc: {:exists, [], [subquery: 0]}]
82+
%{distinct: distinct} =
83+
from p in "posts",
84+
as: :p,
85+
distinct: [
86+
asc: exists(from other_post in "posts", where: other_post.id == parent_as(:p).id)
87+
]
88+
89+
assert distinct.expr == [asc: {:exists, [], [subquery: 0]}]
7790
assert [_] = distinct.subqueries
7891
end
7992

8093
test "raises on non-atoms" do
8194
message = "expected a field as an atom in `distinct`, got: `\"temp\"`"
95+
8296
assert_raise ArgumentError, message, fn ->
8397
temp = "temp"
8498
distinct("posts", [p], [^temp])
@@ -87,6 +101,7 @@ defmodule Ecto.Query.Builder.DistinctTest do
87101

88102
test "raises non-lists" do
89103
message = ~r"`distinct` interpolated on root expects a field or a keyword list"
104+
90105
assert_raise ArgumentError, message, fn ->
91106
temp = "temp"
92107
distinct("posts", [p], ^temp)

test/ecto/query/builder/filter_test.exs

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,41 @@ defmodule Ecto.Query.Builder.FilterTest do
1010
test "handles expressions, params" do
1111
import Kernel, except: [==: 2, and: 2]
1212

13-
assert escape(:where, quote do [] end, 0, [x: 0], __ENV__) ===
14-
{true, {[], %{subqueries: []}}}
13+
assert escape(:where, quote(do: []), 0, [x: 0], __ENV__) ===
14+
{true, {[], %{subqueries: []}}}
1515

16-
assert escape(:where, quote do {x.x()} == {^"foo"} end, 0, [x: 0], __ENV__) ===
17-
{Macro.escape(quote do {&0.x()} == {^0} end),
18-
{[{"foo", {0, :x}}], %{subqueries: []}}}
16+
assert escape(:where, quote(do: {x.x()} == {^"foo"}), 0, [x: 0], __ENV__) ===
17+
{Macro.escape(quote(do: {&0.x()} == {^0})),
18+
{[{"foo", {0, :x}}], %{subqueries: []}}}
19+
20+
escaped = Macro.escape(quote(do: &0.x() == ^0 and &0.y() == ^1))
1921

20-
escaped = Macro.escape(quote do &0.x() == ^0 and &0.y() == ^1 end)
2122
assert {^escaped, {params, %{}}} =
22-
escape(:where, quote do [x: ^"foo", y: ^"bar"] end, 0, [x: 0], __ENV__)
23+
escape(:where, quote(do: [x: ^"foo", y: ^"bar"]), 0, [x: 0], __ENV__)
24+
2325
assert [{{_, _, ["bar", :y]}, {0, :y}}, {{_, _, ["foo", :x]}, {0, :x}}] = params
2426
end
2527

2628
test "raises on invalid expressions" do
2729
assert_raise Ecto.Query.CompileError,
28-
~r"expected a keyword list at compile time in where, got: `\[\{1, 2\}\]`", fn ->
29-
escape(:where, quote do [{1, 2}] end, 0, [], __ENV__)
30-
end
30+
~r"expected a keyword list at compile time in where, got: `\[\{1, 2\}\]`",
31+
fn ->
32+
escape(:where, quote(do: [{1, 2}]), 0, [], __ENV__)
33+
end
3134

3235
assert_raise Ecto.Query.CompileError,
33-
~r"Tuples can only be used in comparisons with literal tuples of the same size", fn ->
34-
escape(:where, quote do {1, 2} > ^foo end, 0, [], __ENV__)
35-
end
36+
~r"Tuples can only be used in comparisons with literal tuples of the same size",
37+
fn ->
38+
escape(:where, quote(do: {1, 2} > ^foo), 0, [], __ENV__)
39+
end
3640
end
3741

3842
test "raises on nils" do
3943
assert_raise Ecto.Query.CompileError,
40-
~r"nil given for `x`. Comparison with nil is forbidden as it is unsafe.", fn ->
41-
escape(:where, quote do [x: nil] end, 0, [], __ENV__)
42-
end
44+
~r"nil given for `x`. Comparison with nil is forbidden as it is unsafe.",
45+
fn ->
46+
escape(:where, quote(do: [x: nil]), 0, [], __ENV__)
47+
end
4348
end
4449

4550
test "works without Ecto.Query.subquery/1" do
@@ -49,7 +54,7 @@ defmodule Ecto.Query.Builder.FilterTest do
4954
%{wheres: [where]} = from(p in "posts", where: exists(s))
5055

5156
assert Macro.to_string(where.expr) ==
52-
"exists({:subquery, 0})"
57+
"exists({:subquery, 0})"
5358
end
5459
end
5560

@@ -61,28 +66,34 @@ defmodule Ecto.Query.Builder.FilterTest do
6166

6267
test "accepts keyword lists" do
6368
%{wheres: [where]} = where(from(p in "posts"), [p], ^[foo: 1, bar: "baz"])
69+
6470
assert Macro.to_string(where.expr) ==
65-
"&0.foo() == ^0 and &0.bar() == ^1"
71+
"&0.foo() == ^0 and &0.bar() == ^1"
72+
6673
assert where.params ==
67-
[{1, {0, :foo}}, {"baz", {0, :bar}}]
74+
[{1, {0, :foo}}, {"baz", {0, :bar}}]
6875
end
6976

7077
test "supports dynamic expressions" do
7178
dynamic = dynamic([p], p.foo == ^1 and p.bar == ^"baz")
7279
%{wheres: [where]} = where("posts", ^dynamic)
80+
7381
assert Macro.to_string(where.expr) ==
74-
"&0.foo() == ^0 and &0.bar() == ^1"
82+
"&0.foo() == ^0 and &0.bar() == ^1"
83+
7584
assert where.params ==
76-
[{1, {0, :foo}}, {"baz", {0, :bar}}]
85+
[{1, {0, :foo}}, {"baz", {0, :bar}}]
7786
end
7887

7988
test "in subquery" do
8089
s = from(p in "posts", select: p.id, where: p.public == ^true)
8190
%{wheres: [where]} = from(p in "posts", where: p.id in subquery(s))
91+
8292
assert Macro.to_string(where.expr) ==
83-
"&0.id() in {:subquery, 0}"
93+
"&0.id() in {:subquery, 0}"
94+
8495
assert where.params ==
85-
[{:subquery, 0}]
96+
[{:subquery, 0}]
8697
end
8798

8899
test "supports exists subquery expressions" do
@@ -91,20 +102,21 @@ defmodule Ecto.Query.Builder.FilterTest do
91102
%{wheres: [where]} = from(p in "posts", where: exists(s))
92103

93104
assert Macro.to_string(where.expr) ==
94-
"exists({:subquery, 0})"
105+
"exists({:subquery, 0})"
106+
95107
assert where.params ==
96-
[{:subquery, 0}]
108+
[{:subquery, 0}]
97109
end
98110

99111
test "supports comparison with subqueries with all and any quantifiers" do
100112
s = from(p in "posts", select: p.rating, order_by: [desc: p.created_at], limit: 10)
101113

102114
assert_quantified_subquery = fn %{wheres: [where]}, expected_quantifier ->
103115
assert Macro.to_string(where.expr) ==
104-
"&0.rating() >= #{expected_quantifier}({:subquery, 0})"
116+
"&0.rating() >= #{expected_quantifier}({:subquery, 0})"
105117

106118
assert where.params ==
107-
[{:subquery, 0}]
119+
[{:subquery, 0}]
108120
end
109121

110122
all_query = from(p in "posts", where: p.rating >= all(s))
@@ -120,9 +132,10 @@ defmodule Ecto.Query.Builder.FilterTest do
120132
%{wheres: [where]} = from(p in "posts", where: p.rating > subquery(s), select: p.id)
121133

122134
assert Macro.to_string(where.expr) ==
123-
"&0.rating() > {:subquery, 0}"
135+
"&0.rating() > {:subquery, 0}"
136+
124137
assert where.params ==
125-
[{:subquery, 0}]
138+
[{:subquery, 0}]
126139
end
127140

128141
test "raises on invalid keywords" do

test/ecto/query/builder/group_by_test.exs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,36 @@ defmodule Ecto.Query.Builder.GroupByTest do
88

99
describe "escape" do
1010
test "handles expressions and params" do
11-
assert {Macro.escape(quote do [&0.y()] end), {[], %{}}} ==
12-
escape(:group_by, quote do x.y() end, {[], %{}}, [x: 0], __ENV__)
11+
assert {Macro.escape(quote(do: [&0.y()])), {[], %{}}} ==
12+
escape(:group_by, quote(do: x.y()), {[], %{}}, [x: 0], __ENV__)
1313

14-
assert {Macro.escape(quote do [&0.x(), &1.y()] end), {[], %{}}} ==
15-
escape(:group_by, quote do [x.x(), y.y()] end, {[], %{}}, [x: 0, y: 1], __ENV__)
14+
assert {Macro.escape(quote(do: [&0.x(), &1.y()])), {[], %{}}} ==
15+
escape(:group_by, quote(do: [x.x(), y.y()]), {[], %{}}, [x: 0, y: 1], __ENV__)
1616

1717
import Kernel, except: [>: 2]
18-
assert {Macro.escape(quote do [1 > 2] end), {[], %{}}} ==
19-
escape(:group_by, quote do 1 > 2 end, {[], %{}}, [], __ENV__)
18+
19+
assert {Macro.escape(quote(do: [1 > 2])), {[], %{}}} ==
20+
escape(:group_by, quote(do: 1 > 2), {[], %{}}, [], __ENV__)
2021
end
2122

2223
test "raises on unbound variables" do
2324
message = ~r"unbound variable `x` in query"
25+
2426
assert_raise Ecto.Query.CompileError, message, fn ->
25-
escape(:group_by, quote do x.y end, {[], %{}}, [], __ENV__)
27+
escape(:group_by, quote(do: x.y), {[], %{}}, [], __ENV__)
2628
end
2729
end
2830

2931
test "can reference the alias of a selected value with selected_as/1" do
3032
query = from p in "posts", select: selected_as(p.id, :ident), group_by: selected_as(:ident)
31-
assert [{:selected_as, [], [:ident]}] = hd(query.group_bys).expr
33+
assert [{:selected_as, [], [:ident]}] = hd(query.group_bys).expr
3234
end
3335

3436
test "raises if name given to selected_as/1 is not an atom" do
3537
message = "expected literal atom or interpolated value in selected_as/1, got: `\"ident\"`"
3638

3739
assert_raise Ecto.Query.CompileError, message, fn ->
38-
escape(:group_by, quote do selected_as("ident") end, {[], %{}}, [], __ENV__)
40+
escape(:group_by, quote(do: selected_as("ident")), {[], %{}}, [], __ENV__)
3941
end
4042
end
4143
end
@@ -60,20 +62,27 @@ defmodule Ecto.Query.Builder.GroupByTest do
6062
assert group_by.expr == [{:exists, [], [{:subquery, 0}]}]
6163
assert [_] = group_by.subqueries
6264

63-
assert [group_by] = group_by("q", [q], exists(from other_q in "q", where: other_q.title == parent_as(:q).title)).group_bys
65+
assert [group_by] =
66+
group_by(
67+
"q",
68+
[q],
69+
exists(from other_q in "q", where: other_q.title == parent_as(:q).title)
70+
).group_bys
6471

6572
assert group_by.expr == [{:exists, [], [{:subquery, 0}]}]
6673
assert [_] = group_by.subqueries
6774
end
6875

6976
test "raises when no a field or a list of fields" do
7077
message = "expected a field as an atom in `group_by`, got: `\"temp\"`"
78+
7179
assert_raise ArgumentError, message, fn ->
7280
temp = "temp"
7381
group_by("posts", [p], [^temp])
7482
end
7583

7684
message = "expected a list of fields and dynamics in `group_by`, got: `\"temp\"`"
85+
7786
assert_raise ArgumentError, message, fn ->
7887
temp = "temp"
7988
group_by("posts", [p], ^temp)

0 commit comments

Comments
 (0)