Skip to content

Commit 65a9efb

Browse files
committed
Add more tests
1 parent b095c32 commit 65a9efb

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
defmodule Ecto.Integration.PrecedenceTest do
2+
use Ecto.Integration.Case, async: true
3+
4+
alias Ecto.Integration.Post
5+
alias Ecto.Integration.TestRepo
6+
import Ecto.Query
7+
8+
@posted ~D[2014-01-01]
9+
@inserted_at ~N[2014-01-01 02:00:00]
10+
11+
setup do
12+
TestRepo.insert!(%Post{
13+
posted: @posted,
14+
inserted_at: @inserted_at,
15+
visits: 1,
16+
counter: 2,
17+
public: true
18+
})
19+
20+
:ok
21+
end
22+
23+
test "datetime_add parenthesizes a compound day count" do
24+
assert [~N[2014-01-04 02:00:00]] =
25+
TestRepo.all(
26+
from(p in Post, select: datetime_add(p.inserted_at, p.visits + p.counter, "day"))
27+
)
28+
end
29+
30+
test "datetime_add parenthesizes a compound week count" do
31+
assert [~N[2014-01-22 02:00:00]] =
32+
TestRepo.all(
33+
from(p in Post, select: datetime_add(p.inserted_at, p.visits + p.counter, "week"))
34+
)
35+
end
36+
37+
test "datetime_add parenthesizes a compound millisecond count" do
38+
TestRepo.delete_all(Post)
39+
40+
TestRepo.insert!(%Post{
41+
posted: @posted,
42+
inserted_at: @inserted_at,
43+
visits: 500,
44+
counter: 500
45+
})
46+
47+
assert [~N[2014-01-01 02:00:01]] =
48+
TestRepo.all(
49+
from(p in Post,
50+
select: datetime_add(p.inserted_at, p.visits + p.counter, "millisecond")
51+
)
52+
)
53+
end
54+
55+
test "date_add parenthesizes a compound day count" do
56+
assert [~D[2014-01-04]] =
57+
TestRepo.all(from(p in Post, select: date_add(p.posted, p.visits + p.counter, "day")))
58+
end
59+
60+
test "is_nil parenthesizes not" do
61+
assert [false] = TestRepo.all(from(p in Post, select: is_nil(not p.public)))
62+
end
63+
end

‎test/ecto/adapters/sqlite3/connection/select_test.exs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,16 @@ defmodule Ecto.Adapters.SQLite3.Connection.SelectTest do
480480

481481
assert ~s{SELECT 1 = (s0."x" IN (?,?,?)) FROM "schema" AS s0} == all(query)
482482
end
483+
484+
test "parenthesizes equality on the left of in" do
485+
query =
486+
Schema
487+
|> select([e], (e.x == e.y) in [true, false])
488+
|> plan()
489+
490+
assert ~s{SELECT (s0."x" = s0."y") IN (1,0) FROM "schema" AS s0} ==
491+
all(query)
492+
end
483493
end
484494

485495
test "in subquery" do
@@ -515,6 +525,20 @@ defmodule Ecto.Adapters.SQLite3.Connection.SelectTest do
515525
~s{))} == all(query)
516526
end
517527

528+
test "parenthesizes addition on the left of in subquery" do
529+
posts = subquery("posts" |> where(title: ^"hello") |> select([p], p.id))
530+
531+
query =
532+
"comments"
533+
|> where([c], (c.x + c.y) in subquery(posts))
534+
|> select([c], c.x)
535+
|> plan()
536+
537+
assert all(query) ==
538+
~s{SELECT c0."x" FROM "comments" AS c0 } <>
539+
~s{WHERE ((c0."x" + c0."y") IN (SELECT sp0."id" FROM "posts" AS sp0 WHERE (sp0."title" = ?)))}
540+
end
541+
518542
describe "arrays" do
519543
test "array of integers fragment is not supported" do
520544
assert_raise Ecto.QueryError, fn ->

0 commit comments

Comments
 (0)