forked from elixir-sqlite/exqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b8c790
commit 9f1558d
Showing
2 changed files
with
69 additions
and
85 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,88 @@ | ||
defmodule Exqlite.ExtensionsTest do | ||
use ExUnit.Case | ||
|
||
alias Exqlite.Basic | ||
use ExUnit.Case, async: true | ||
alias Exqlite.Sqlite3 | ||
|
||
describe "enable_load_extension" do | ||
test "loading can be enabled / disabled" do | ||
{:ok, path} = Temp.path() | ||
{:ok, conn} = Basic.open(path) | ||
:ok = Basic.enable_load_extension(conn) | ||
setup do | ||
{:ok, conn} = Sqlite3.open(":memory:") | ||
on_exit(fn -> :ok = Sqlite3.close(conn) end) | ||
{:ok, conn: conn} | ||
end | ||
|
||
{:ok, [[nil]], _} = | ||
Basic.load_extension(conn, ExSqlean.path_for("re")) |> Basic.rows() | ||
test "loading can be enabled / disabled", %{conn: conn} do | ||
assert :ok = Sqlite3.enable_load_extension(conn, true) | ||
|
||
{:ok, [[1]], _} = | ||
Basic.exec(conn, "select regexp_like('the year is 2021', '2021')") | ||
|> Basic.rows() | ||
assert {:ok, [[nil]]} = | ||
prepare_fetch_all( | ||
conn, | ||
"select load_extension(?)", | ||
[ExSqlean.path_for("re")] | ||
) | ||
|
||
:ok = Basic.disable_load_extension(conn) | ||
assert :ok = Sqlite3.enable_load_extension(conn, false) | ||
|
||
{:error, "not authorized"} = | ||
Basic.load_extension(conn, ExSqlean.path_for("re")) |> Basic.rows() | ||
assert {:error, "not authorized"} = | ||
prepare_fetch_all( | ||
conn, | ||
"select load_extension(?)", | ||
[ExSqlean.path_for("re")] | ||
) | ||
end | ||
|
||
test "works for 're' (regex)" do | ||
{:ok, path} = Temp.path() | ||
{:ok, conn} = Basic.open(path) | ||
|
||
:ok = Basic.enable_load_extension(conn) | ||
test "works for 're' (regex)", %{conn: conn} do | ||
:ok = Sqlite3.enable_load_extension(conn, true) | ||
|
||
{:ok, [[nil]], _} = | ||
Basic.load_extension(conn, ExSqlean.path_for("re")) |> Basic.rows() | ||
{:ok, _} = | ||
prepare_fetch_all( | ||
conn, | ||
"select load_extension(?)", | ||
[ExSqlean.path_for("re")] | ||
) | ||
|
||
{:ok, [[0]], _} = | ||
Basic.exec(conn, "select regexp_like('the year is 2021', '2k21')") | ||
|> Basic.rows() | ||
assert {:ok, [[0]]} = | ||
prepare_fetch_all( | ||
conn, | ||
"select regexp_like('the year is 2021', ?)", | ||
["2k21"] | ||
) | ||
|
||
{:ok, [[1]], _} = | ||
Basic.exec(conn, "select regexp_like('the year is 2021', '2021')") | ||
|> Basic.rows() | ||
assert {:ok, [[1]]} = | ||
prepare_fetch_all( | ||
conn, | ||
"select regexp_like('the year is 2021', ?)", | ||
["2021"] | ||
) | ||
end | ||
|
||
test "stats extension" do | ||
{:ok, path} = Temp.path() | ||
{:ok, conn} = Basic.open(path) | ||
test "stats extension", %{conn: conn} do | ||
:ok = Sqlite3.enable_load_extension(conn, true) | ||
|
||
:ok = Basic.enable_load_extension(conn) | ||
Basic.load_extension(conn, ExSqlean.path_for("stats")) | ||
Basic.load_extension(conn, ExSqlean.path_for("series")) | ||
for ext <- ["stats", "series"] do | ||
{:ok, _} = | ||
prepare_fetch_all( | ||
conn, | ||
"select load_extension(?)", | ||
[ExSqlean.path_for(ext)] | ||
) | ||
end | ||
|
||
assert {:ok, [[50.5]]} = | ||
prepare_fetch_all( | ||
conn, | ||
"select median(value) from generate_series(1, 100)" | ||
) | ||
end | ||
end | ||
|
||
{:ok, [[50.5]], ["median(value)"]} = | ||
Basic.exec(conn, "select median(value) from generate_series(1, 100)") | ||
|> Basic.rows() | ||
defp prepare_fetch_all(conn, sql, args \\ []) do | ||
with {:ok, stmt} <- Sqlite3.prepare(conn, sql) do | ||
try do | ||
with :ok <- Sqlite3.bind(conn, stmt, args) do | ||
Sqlite3.fetch_all(conn, stmt) | ||
end | ||
after | ||
Sqlite3.release(conn, stmt) | ||
end | ||
end | ||
end | ||
end |