Skip to content

Commit 7159f99

Browse files
committed
add some tests
1 parent 1ca4b0a commit 7159f99

File tree

2 files changed

+77
-10
lines changed

2 files changed

+77
-10
lines changed

lib/exqlite/sqlite3.ex

+10-10
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,8 @@ defmodule Exqlite.Sqlite3 do
306306
@doc """
307307
Binds a text value to a prepared statement.
308308
309-
iex> {:ok, db} = Sqlite3.open(":memory:", [:readonly])
310-
iex> {:ok, stmt} = Sqlite3.prepare(db, "SELECT ?")
309+
iex> {:ok, conn} = Sqlite3.open(":memory:", [:readonly])
310+
iex> {:ok, stmt} = Sqlite3.prepare(conn, "SELECT ?")
311311
iex> Sqlite3.bind_text(stmt, 1, "Alice")
312312
:ok
313313
@@ -323,8 +323,8 @@ defmodule Exqlite.Sqlite3 do
323323
@doc """
324324
Binds a blob value to a prepared statement.
325325
326-
iex> {:ok, db} = Sqlite3.open(":memory:", [:readonly])
327-
iex> {:ok, stmt} = Sqlite3.prepare(db, "SELECT ?")
326+
iex> {:ok, conn} = Sqlite3.open(":memory:", [:readonly])
327+
iex> {:ok, stmt} = Sqlite3.prepare(conn, "SELECT ?")
328328
iex> Sqlite3.bind_blob(stmt, 1, <<0, 0, 0>>)
329329
:ok
330330
@@ -340,8 +340,8 @@ defmodule Exqlite.Sqlite3 do
340340
@doc """
341341
Binds an integer value to a prepared statement.
342342
343-
iex> {:ok, db} = Sqlite3.open(":memory:", [:readonly])
344-
iex> {:ok, stmt} = Sqlite3.prepare(db, "SELECT ?")
343+
iex> {:ok, conn} = Sqlite3.open(":memory:", [:readonly])
344+
iex> {:ok, stmt} = Sqlite3.prepare(conn, "SELECT ?")
345345
iex> Sqlite3.bind_integer(stmt, 1, 42)
346346
:ok
347347
@@ -357,8 +357,8 @@ defmodule Exqlite.Sqlite3 do
357357
@doc """
358358
Binds a float value to a prepared statement.
359359
360-
iex> {:ok, db} = Sqlite3.open(":memory:", [:readonly])
361-
iex> {:ok, stmt} = Sqlite3.prepare(db, "SELECT ?")
360+
iex> {:ok, conn} = Sqlite3.open(":memory:", [:readonly])
361+
iex> {:ok, stmt} = Sqlite3.prepare(conn, "SELECT ?")
362362
iex> Sqlite3.bind_float(stmt, 1, 3.14)
363363
:ok
364364
@@ -374,8 +374,8 @@ defmodule Exqlite.Sqlite3 do
374374
@doc """
375375
Binds a null value to a prepared statement.
376376
377-
iex> {:ok, db} = Sqlite3.open(":memory:", [:readonly])
378-
iex> {:ok, stmt} = Sqlite3.prepare(db, "SELECT ?")
377+
iex> {:ok, conn} = Sqlite3.open(":memory:", [:readonly])
378+
iex> {:ok, stmt} = Sqlite3.prepare(conn, "SELECT ?")
379379
iex> Sqlite3.bind_null(stmt, 1)
380380
:ok
381381

test/exqlite/sqlite3_test.exs

+67
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,73 @@ defmodule Exqlite.Sqlite3Test do
297297
end
298298
end
299299

300+
describe ".bind_text/3" do
301+
setup do
302+
{:ok, conn} = Sqlite3.open(":memory:", [:readonly])
303+
{:ok, stmt} = Sqlite3.prepare(conn, "select ?")
304+
{:ok, conn: conn, stmt: stmt}
305+
end
306+
307+
test "binds text value", %{conn: conn, stmt: stmt} do
308+
assert :ok = Sqlite3.bind_text(stmt, 1, "hello")
309+
assert {:row, ["hello"]} = Sqlite3.step(conn, stmt)
310+
end
311+
312+
test "binds emojis", %{conn: conn, stmt: stmt} do
313+
assert :ok = Sqlite3.bind_text(stmt, 1, "hello 👋 world 🌏")
314+
assert {:row, ["hello 👋 world 🌏"]} = Sqlite3.step(conn, stmt)
315+
end
316+
317+
test "errors on invalid statement" do
318+
assert_raise ArgumentError, "argument error: nil", fn ->
319+
Sqlite3.bind_text(_not_stmt = nil, 1, "hello")
320+
end
321+
end
322+
323+
test "errors on invalid index", %{stmt: stmt} do
324+
assert_raise Exqlite.Error, "column index out of range", fn ->
325+
Sqlite3.bind_text(stmt, _out_of_range = 2, "hello")
326+
end
327+
end
328+
329+
test "errors on invalid text argument", %{stmt: stmt} do
330+
assert_raise ArgumentError, "argument error: 1", fn ->
331+
Sqlite3.bind_text(stmt, 1, _not_text = 1)
332+
end
333+
end
334+
end
335+
336+
describe ".bind_blob/3" do
337+
setup do
338+
{:ok, conn} = Sqlite3.open(":memory:", [:readonly])
339+
{:ok, stmt} = Sqlite3.prepare(conn, "select ?")
340+
{:ok, conn: conn, stmt: stmt}
341+
end
342+
343+
test "binds binary value", %{conn: conn, stmt: stmt} do
344+
assert :ok = Sqlite3.bind_blob(stmt, 1, <<0, 0, 0>>)
345+
assert {:row, [<<0, 0, 0>>]} = Sqlite3.step(conn, stmt)
346+
end
347+
348+
test "errors on invalid statement" do
349+
assert_raise ArgumentError, "argument error: nil", fn ->
350+
Sqlite3.bind_blob(_not_stmt = nil, 1, "hello")
351+
end
352+
end
353+
354+
test "errors on invalid index", %{stmt: stmt} do
355+
assert_raise Exqlite.Error, "column index out of range", fn ->
356+
Sqlite3.bind_blob(stmt, _out_of_range = 2, "hello")
357+
end
358+
end
359+
360+
test "errors on invalid blob argument", %{stmt: stmt} do
361+
assert_raise ArgumentError, "argument error: 1", fn ->
362+
Sqlite3.bind_blob(stmt, 1, _not_text = 1)
363+
end
364+
end
365+
end
366+
300367
describe ".columns/2" do
301368
test "returns the column definitions" do
302369
{:ok, conn} = Sqlite3.open(":memory:")

0 commit comments

Comments
 (0)