|
1 | 1 | defmodule Exqlite.ExtensionsTest do
|
2 |
| - use ExUnit.Case |
3 |
| - |
4 |
| - alias Exqlite.Basic |
| 2 | + use ExUnit.Case, async: true |
| 3 | + alias Exqlite.Sqlite3 |
5 | 4 |
|
6 | 5 | describe "enable_load_extension" do
|
7 |
| - test "loading can be enabled / disabled" do |
8 |
| - {:ok, path} = Temp.path() |
9 |
| - {:ok, conn} = Basic.open(path) |
10 |
| - :ok = Basic.enable_load_extension(conn) |
| 6 | + setup do |
| 7 | + {:ok, conn} = Sqlite3.open(":memory:") |
| 8 | + on_exit(fn -> :ok = Sqlite3.close(conn) end) |
| 9 | + {:ok, conn: conn} |
| 10 | + end |
11 | 11 |
|
12 |
| - {:ok, [[nil]], _} = |
13 |
| - Basic.load_extension(conn, ExSqlean.path_for("re")) |> Basic.rows() |
| 12 | + test "loading can be enabled / disabled", %{conn: conn} do |
| 13 | + assert :ok = Sqlite3.enable_load_extension(conn, true) |
14 | 14 |
|
15 |
| - {:ok, [[1]], _} = |
16 |
| - Basic.exec(conn, "select regexp_like('the year is 2021', '2021')") |
17 |
| - |> Basic.rows() |
| 15 | + assert {:ok, [[nil]]} = |
| 16 | + prepare_fetch_all( |
| 17 | + conn, |
| 18 | + "select load_extension(?)", |
| 19 | + [ExSqlean.path_for("re")] |
| 20 | + ) |
18 | 21 |
|
19 |
| - :ok = Basic.disable_load_extension(conn) |
| 22 | + assert :ok = Sqlite3.enable_load_extension(conn, false) |
20 | 23 |
|
21 |
| - {:error, "not authorized"} = |
22 |
| - Basic.load_extension(conn, ExSqlean.path_for("re")) |> Basic.rows() |
| 24 | + assert {:error, "not authorized"} = |
| 25 | + prepare_fetch_all( |
| 26 | + conn, |
| 27 | + "select load_extension(?)", |
| 28 | + [ExSqlean.path_for("re")] |
| 29 | + ) |
23 | 30 | end
|
24 | 31 |
|
25 |
| - test "works for 're' (regex)" do |
26 |
| - {:ok, path} = Temp.path() |
27 |
| - {:ok, conn} = Basic.open(path) |
28 |
| - |
29 |
| - :ok = Basic.enable_load_extension(conn) |
| 32 | + test "works for 're' (regex)", %{conn: conn} do |
| 33 | + :ok = Sqlite3.enable_load_extension(conn, true) |
30 | 34 |
|
31 |
| - {:ok, [[nil]], _} = |
32 |
| - Basic.load_extension(conn, ExSqlean.path_for("re")) |> Basic.rows() |
| 35 | + {:ok, _} = |
| 36 | + prepare_fetch_all( |
| 37 | + conn, |
| 38 | + "select load_extension(?)", |
| 39 | + [ExSqlean.path_for("re")] |
| 40 | + ) |
33 | 41 |
|
34 |
| - {:ok, [[0]], _} = |
35 |
| - Basic.exec(conn, "select regexp_like('the year is 2021', '2k21')") |
36 |
| - |> Basic.rows() |
| 42 | + assert {:ok, [[0]]} = |
| 43 | + prepare_fetch_all( |
| 44 | + conn, |
| 45 | + "select regexp_like('the year is 2021', ?)", |
| 46 | + ["2k21"] |
| 47 | + ) |
37 | 48 |
|
38 |
| - {:ok, [[1]], _} = |
39 |
| - Basic.exec(conn, "select regexp_like('the year is 2021', '2021')") |
40 |
| - |> Basic.rows() |
| 49 | + assert {:ok, [[1]]} = |
| 50 | + prepare_fetch_all( |
| 51 | + conn, |
| 52 | + "select regexp_like('the year is 2021', ?)", |
| 53 | + ["2021"] |
| 54 | + ) |
41 | 55 | end
|
42 | 56 |
|
43 |
| - test "stats extension" do |
44 |
| - {:ok, path} = Temp.path() |
45 |
| - {:ok, conn} = Basic.open(path) |
| 57 | + test "stats extension", %{conn: conn} do |
| 58 | + :ok = Sqlite3.enable_load_extension(conn, true) |
46 | 59 |
|
47 |
| - :ok = Basic.enable_load_extension(conn) |
48 |
| - Basic.load_extension(conn, ExSqlean.path_for("stats")) |
49 |
| - Basic.load_extension(conn, ExSqlean.path_for("series")) |
| 60 | + for ext <- ["stats", "series"] do |
| 61 | + {:ok, _} = |
| 62 | + prepare_fetch_all( |
| 63 | + conn, |
| 64 | + "select load_extension(?)", |
| 65 | + [ExSqlean.path_for(ext)] |
| 66 | + ) |
| 67 | + end |
| 68 | + |
| 69 | + assert {:ok, [[50.5]]} = |
| 70 | + prepare_fetch_all( |
| 71 | + conn, |
| 72 | + "select median(value) from generate_series(1, 100)" |
| 73 | + ) |
| 74 | + end |
| 75 | + end |
50 | 76 |
|
51 |
| - {:ok, [[50.5]], ["median(value)"]} = |
52 |
| - Basic.exec(conn, "select median(value) from generate_series(1, 100)") |
53 |
| - |> Basic.rows() |
| 77 | + defp prepare_fetch_all(conn, sql, args \\ []) do |
| 78 | + with {:ok, stmt} <- Sqlite3.prepare(conn, sql) do |
| 79 | + try do |
| 80 | + with :ok <- Sqlite3.bind(conn, stmt, args) do |
| 81 | + Sqlite3.fetch_all(conn, stmt) |
| 82 | + end |
| 83 | + after |
| 84 | + Sqlite3.release(conn, stmt) |
| 85 | + end |
54 | 86 | end
|
55 | 87 | end
|
56 | 88 | end
|
0 commit comments