forked from elixir-sqlite/ecto_sqlite3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggregates_test.exs
More file actions
78 lines (61 loc) · 1.77 KB
/
aggregates_test.exs
File metadata and controls
78 lines (61 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
defmodule Ecto.Adapters.SQLite3.Connection.AggregatesTest do
use ExUnit.Case, async: true
import Ecto.Query
import Ecto.Adapters.SQLite3.TestHelpers
alias EctoSQLite3.Schemas.Schema
test "counts" do
query =
Schema
|> select([r], count(r.x))
|> plan()
assert ~s{SELECT count(s0."x") FROM "schema" AS s0} == all(query)
end
test "raises when counting a source" do
query =
Schema
|> select([r], count(r))
|> plan()
msg = ~r"The argument to `count/1` must be a column in SQLite3"
assert_raise Ecto.QueryError, msg, fn ->
all(query)
end
end
test "distinct counts" do
query =
Schema
|> select([r], count(r.x, :distinct))
|> plan()
assert ~s{SELECT count(distinct s0."x") FROM "schema" AS s0} == all(query)
end
test "allows naked count" do
query =
Schema
|> select([r], count())
|> plan()
assert ~s{SELECT count(*) FROM "schema" AS s0} == all(query)
end
test "aggregate count with a filter" do
query =
Schema
|> select([r], count(r.x) |> filter(r.x > 10))
|> plan()
assert ~s{SELECT count(s0."x") FILTER (WHERE s0."x" > 10) FROM "schema" AS s0} ==
all(query)
end
test "aggregate count with a more complex filter" do
query =
Schema
|> select([r], count(r.x) |> filter(r.x > 10 and r.x < 50))
|> plan()
assert ~s{SELECT count(s0."x") FILTER (WHERE (s0."x" > 10) AND (s0."x" < 50)) FROM "schema" AS s0} ==
all(query)
end
test "aggregate naked count with a filter" do
query =
Schema
|> select([r], count() |> filter(r.x > 10))
|> plan()
assert ~s{SELECT count(*) FILTER (WHERE s0."x" > 10) FROM "schema" AS s0} ==
all(query)
end
end