Skip to content

Commit

Permalink
add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslandoga committed Oct 9, 2024
1 parent 1ca4b0a commit 7159f99
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 10 deletions.
20 changes: 10 additions & 10 deletions lib/exqlite/sqlite3.ex
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,8 @@ defmodule Exqlite.Sqlite3 do
@doc """
Binds a text value to a prepared statement.
iex> {:ok, db} = Sqlite3.open(":memory:", [:readonly])
iex> {:ok, stmt} = Sqlite3.prepare(db, "SELECT ?")
iex> {:ok, conn} = Sqlite3.open(":memory:", [:readonly])
iex> {:ok, stmt} = Sqlite3.prepare(conn, "SELECT ?")
iex> Sqlite3.bind_text(stmt, 1, "Alice")
:ok
Expand All @@ -323,8 +323,8 @@ defmodule Exqlite.Sqlite3 do
@doc """
Binds a blob value to a prepared statement.
iex> {:ok, db} = Sqlite3.open(":memory:", [:readonly])
iex> {:ok, stmt} = Sqlite3.prepare(db, "SELECT ?")
iex> {:ok, conn} = Sqlite3.open(":memory:", [:readonly])
iex> {:ok, stmt} = Sqlite3.prepare(conn, "SELECT ?")
iex> Sqlite3.bind_blob(stmt, 1, <<0, 0, 0>>)
:ok
Expand All @@ -340,8 +340,8 @@ defmodule Exqlite.Sqlite3 do
@doc """
Binds an integer value to a prepared statement.
iex> {:ok, db} = Sqlite3.open(":memory:", [:readonly])
iex> {:ok, stmt} = Sqlite3.prepare(db, "SELECT ?")
iex> {:ok, conn} = Sqlite3.open(":memory:", [:readonly])
iex> {:ok, stmt} = Sqlite3.prepare(conn, "SELECT ?")
iex> Sqlite3.bind_integer(stmt, 1, 42)
:ok
Expand All @@ -357,8 +357,8 @@ defmodule Exqlite.Sqlite3 do
@doc """
Binds a float value to a prepared statement.
iex> {:ok, db} = Sqlite3.open(":memory:", [:readonly])
iex> {:ok, stmt} = Sqlite3.prepare(db, "SELECT ?")
iex> {:ok, conn} = Sqlite3.open(":memory:", [:readonly])
iex> {:ok, stmt} = Sqlite3.prepare(conn, "SELECT ?")
iex> Sqlite3.bind_float(stmt, 1, 3.14)
:ok
Expand All @@ -374,8 +374,8 @@ defmodule Exqlite.Sqlite3 do
@doc """
Binds a null value to a prepared statement.
iex> {:ok, db} = Sqlite3.open(":memory:", [:readonly])
iex> {:ok, stmt} = Sqlite3.prepare(db, "SELECT ?")
iex> {:ok, conn} = Sqlite3.open(":memory:", [:readonly])
iex> {:ok, stmt} = Sqlite3.prepare(conn, "SELECT ?")
iex> Sqlite3.bind_null(stmt, 1)
:ok
Expand Down
67 changes: 67 additions & 0 deletions test/exqlite/sqlite3_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,73 @@ defmodule Exqlite.Sqlite3Test do
end
end

describe ".bind_text/3" do
setup do
{:ok, conn} = Sqlite3.open(":memory:", [:readonly])
{:ok, stmt} = Sqlite3.prepare(conn, "select ?")
{:ok, conn: conn, stmt: stmt}
end

test "binds text value", %{conn: conn, stmt: stmt} do
assert :ok = Sqlite3.bind_text(stmt, 1, "hello")
assert {:row, ["hello"]} = Sqlite3.step(conn, stmt)
end

test "binds emojis", %{conn: conn, stmt: stmt} do
assert :ok = Sqlite3.bind_text(stmt, 1, "hello πŸ‘‹ world 🌏")
assert {:row, ["hello πŸ‘‹ world 🌏"]} = Sqlite3.step(conn, stmt)
end

test "errors on invalid statement" do
assert_raise ArgumentError, "argument error: nil", fn ->
Sqlite3.bind_text(_not_stmt = nil, 1, "hello")
end
end

test "errors on invalid index", %{stmt: stmt} do
assert_raise Exqlite.Error, "column index out of range", fn ->
Sqlite3.bind_text(stmt, _out_of_range = 2, "hello")
end
end

test "errors on invalid text argument", %{stmt: stmt} do
assert_raise ArgumentError, "argument error: 1", fn ->
Sqlite3.bind_text(stmt, 1, _not_text = 1)
end
end
end

describe ".bind_blob/3" do
setup do
{:ok, conn} = Sqlite3.open(":memory:", [:readonly])
{:ok, stmt} = Sqlite3.prepare(conn, "select ?")
{:ok, conn: conn, stmt: stmt}
end

test "binds binary value", %{conn: conn, stmt: stmt} do
assert :ok = Sqlite3.bind_blob(stmt, 1, <<0, 0, 0>>)
assert {:row, [<<0, 0, 0>>]} = Sqlite3.step(conn, stmt)
end

test "errors on invalid statement" do
assert_raise ArgumentError, "argument error: nil", fn ->
Sqlite3.bind_blob(_not_stmt = nil, 1, "hello")
end
end

test "errors on invalid index", %{stmt: stmt} do
assert_raise Exqlite.Error, "column index out of range", fn ->
Sqlite3.bind_blob(stmt, _out_of_range = 2, "hello")
end
end

test "errors on invalid blob argument", %{stmt: stmt} do
assert_raise ArgumentError, "argument error: 1", fn ->
Sqlite3.bind_blob(stmt, 1, _not_text = 1)
end
end
end

describe ".columns/2" do
test "returns the column definitions" do
{:ok, conn} = Sqlite3.open(":memory:")
Expand Down

0 comments on commit 7159f99

Please sign in to comment.