-
Notifications
You must be signed in to change notification settings - Fork 343
Expand file tree
/
Copy pathsql_test.exs
More file actions
57 lines (47 loc) · 1.75 KB
/
Copy pathsql_test.exs
File metadata and controls
57 lines (47 loc) · 1.75 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
defmodule Ecto.Adapters.SQLTest do
use ExUnit.Case, async: true
defp comments(list) do
{pre, post} = Ecto.Adapters.SQL.comments(list)
{IO.iodata_to_binary(pre), IO.iodata_to_binary(post)}
end
defp wrap(sql, opts) do
sql |> Ecto.Adapters.SQL.wrap_comments(opts) |> IO.iodata_to_binary()
end
describe "comments/1" do
test "empty list renders nothing" do
assert comments([]) == {"", ""}
end
test "renders :pre leading and :post trailing" do
assert comments(pre: "list_users") == {"/* list_users */ ", ""}
assert comments(post: "list_users") == {"", " /* list_users */"}
assert comments(pre: "a", post: "b") == {"/* a */ ", " /* b */"}
end
test "preserves order and supports multiples" do
assert comments(pre: "a", pre: "b") == {"/* a */ /* b */ ", ""}
end
test "rejects comment-delimiter sequences and null bytes" do
for bad <- ["evil */ x", "evil /* x", "x\0y"] do
assert_raise ArgumentError, ~r/cannot contain/, fn ->
Ecto.Adapters.SQL.comments(pre: bad)
end
end
end
test "rejects bad shapes" do
assert_raise ArgumentError, ~r/expected \{:pre/, fn ->
Ecto.Adapters.SQL.comments(foo: "bar")
end
assert_raise ArgumentError, ~r/keyword list/, fn ->
Ecto.Adapters.SQL.comments("nope")
end
end
end
describe "wrap_comments/2" do
test "wraps the sql with pre/post from the :comments option" do
assert wrap("INSERT INTO posts ...", comments: [pre: "create_post", post: "v2"]) ==
"/* create_post */ INSERT INTO posts ... /* v2 */"
end
test "is a no-op without the :comments option" do
assert wrap("INSERT INTO posts ...", timeout: 5000) == "INSERT INTO posts ..."
end
end
end