@@ -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
0 commit comments