Files
sqlite3/sqlite_test.go

220 lines
4.1 KiB
Go
Raw Permalink Normal View History

2023-01-26 11:12:00 +00:00
package sqlite3
import (
"bytes"
"math"
"testing"
2023-03-29 15:01:25 +01:00
"github.com/ncruces/go-sqlite3/internal/util"
2023-01-26 11:12:00 +00:00
)
2023-03-29 15:01:25 +01:00
func init() {
Path = "./embed/sqlite3.wasm"
}
2023-07-04 02:29:38 +01:00
func Test_sqlite_error_OOM(t *testing.T) {
2023-02-27 03:20:23 +00:00
t.Parallel()
2023-07-04 02:29:38 +01:00
sqlite, err := instantiateSQLite()
2023-02-27 03:20:23 +00:00
if err != nil {
t.Fatal(err)
}
2023-07-04 02:29:38 +01:00
defer sqlite.close()
2023-02-27 03:20:23 +00:00
defer func() { _ = recover() }()
2025-01-21 01:42:57 +00:00
sqlite.error(res_t(NOMEM), 0)
2023-02-27 03:20:23 +00:00
t.Error("want panic")
}
2023-07-04 02:29:38 +01:00
func Test_sqlite_call_closed(t *testing.T) {
2023-02-24 17:49:16 +00:00
t.Parallel()
2023-07-04 02:29:38 +01:00
sqlite, err := instantiateSQLite()
2023-02-24 17:49:16 +00:00
if err != nil {
t.Fatal(err)
}
2023-07-04 02:29:38 +01:00
sqlite.close()
2023-02-24 17:49:16 +00:00
defer func() { _ = recover() }()
2024-09-09 13:21:33 +01:00
sqlite.call("sqlite3_free")
2023-02-24 17:49:16 +00:00
t.Error("want panic")
}
2023-07-04 02:29:38 +01:00
func Test_sqlite_new(t *testing.T) {
2023-02-15 16:24:34 +00:00
t.Parallel()
2023-07-04 02:29:38 +01:00
sqlite, err := instantiateSQLite()
2023-01-26 11:12:00 +00:00
if err != nil {
t.Fatal(err)
}
2023-07-04 02:29:38 +01:00
defer sqlite.close()
2023-01-26 11:12:00 +00:00
2023-04-14 00:09:40 +01:00
t.Run("MaxUint32", func(t *testing.T) {
2023-02-27 03:20:23 +00:00
defer func() { _ = recover() }()
2023-07-04 02:29:38 +01:00
sqlite.new(math.MaxUint32)
2023-02-27 03:20:23 +00:00
t.Error("want panic")
2023-04-14 00:09:40 +01:00
})
2023-01-26 11:12:00 +00:00
}
2023-07-04 02:29:38 +01:00
func Test_sqlite_newArena(t *testing.T) {
2023-02-15 16:24:34 +00:00
t.Parallel()
2023-07-04 02:29:38 +01:00
sqlite, err := instantiateSQLite()
2023-02-14 11:34:24 +00:00
if err != nil {
t.Fatal(err)
}
2023-07-04 02:29:38 +01:00
defer sqlite.close()
2023-02-14 11:34:24 +00:00
2025-01-21 01:42:57 +00:00
arena := sqlite.newArena()
2023-02-24 14:31:41 +00:00
defer arena.free()
2023-02-14 11:34:24 +00:00
const title = "Lorem ipsum"
ptr := arena.string(title)
if ptr == 0 {
t.Fatalf("got nullptr")
}
2025-01-21 01:42:57 +00:00
if got := util.ReadString(sqlite.mod, ptr, math.MaxInt); got != title {
2023-02-14 11:34:24 +00:00
t.Errorf("got %q, want %q", got, title)
}
const body = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
ptr = arena.string(body)
if ptr == 0 {
t.Fatalf("got nullptr")
}
2025-01-21 01:42:57 +00:00
if got := util.ReadString(sqlite.mod, ptr, math.MaxInt); got != body {
2023-02-14 11:34:24 +00:00
t.Errorf("got %q, want %q", got, body)
}
2023-05-29 16:50:48 +01:00
ptr = arena.bytes(nil)
if ptr != 0 {
t.Errorf("want nullptr")
}
ptr = arena.bytes([]byte(title))
if ptr == 0 {
t.Fatalf("got nullptr")
}
2025-01-21 01:42:57 +00:00
if got := util.View(sqlite.mod, ptr, int64(len(title))); string(got) != title {
2023-05-29 16:50:48 +01:00
t.Errorf("got %q, want %q", got, title)
}
2023-02-24 14:31:41 +00:00
arena.free()
2023-02-14 11:34:24 +00:00
}
2023-07-04 02:29:38 +01:00
func Test_sqlite_newBytes(t *testing.T) {
2023-02-15 16:24:34 +00:00
t.Parallel()
2023-07-04 02:29:38 +01:00
sqlite, err := instantiateSQLite()
2023-01-26 11:12:00 +00:00
if err != nil {
t.Fatal(err)
}
2023-07-04 02:29:38 +01:00
defer sqlite.close()
2023-01-26 11:12:00 +00:00
2023-07-04 02:29:38 +01:00
ptr := sqlite.newBytes(nil)
2023-01-26 11:12:00 +00:00
if ptr != 0 {
2023-02-10 16:42:49 +00:00
t.Errorf("got %#x, want nullptr", ptr)
2023-01-26 11:12:00 +00:00
}
buf := []byte("sqlite3")
2023-07-04 02:29:38 +01:00
ptr = sqlite.newBytes(buf)
2023-01-26 11:12:00 +00:00
if ptr == 0 {
2023-01-26 12:15:34 +00:00
t.Fatal("got nullptr, want a pointer")
2023-01-26 11:12:00 +00:00
}
want := buf
2025-01-21 01:42:57 +00:00
if got := util.View(sqlite.mod, ptr, int64(len(want))); !bytes.Equal(got, want) {
2023-01-26 12:15:34 +00:00
t.Errorf("got %q, want %q", got, want)
2023-01-26 11:12:00 +00:00
}
2023-10-13 17:06:05 +01:00
ptr = sqlite.newBytes(buf[:0])
if ptr == 0 {
t.Fatal("got nullptr, want a pointer")
}
2023-01-26 11:12:00 +00:00
}
2023-07-04 02:29:38 +01:00
func Test_sqlite_newString(t *testing.T) {
2023-02-15 16:24:34 +00:00
t.Parallel()
2023-07-04 02:29:38 +01:00
sqlite, err := instantiateSQLite()
2023-01-26 11:12:00 +00:00
if err != nil {
t.Fatal(err)
}
2023-07-04 02:29:38 +01:00
defer sqlite.close()
2023-01-26 11:12:00 +00:00
2023-07-04 02:29:38 +01:00
ptr := sqlite.newString("")
2023-01-26 11:12:00 +00:00
if ptr == 0 {
2023-01-26 12:15:34 +00:00
t.Error("got nullptr, want a pointer")
2023-01-26 11:12:00 +00:00
}
str := "sqlite3\000sqlite3"
2023-07-04 02:29:38 +01:00
ptr = sqlite.newString(str)
2023-01-26 11:12:00 +00:00
if ptr == 0 {
2023-01-26 12:15:34 +00:00
t.Fatal("got nullptr, want a pointer")
2023-01-26 11:12:00 +00:00
}
want := str + "\000"
2025-01-21 01:42:57 +00:00
if got := util.View(sqlite.mod, ptr, int64(len(want))); string(got) != want {
2023-01-26 12:15:34 +00:00
t.Errorf("got %q, want %q", got, want)
2023-01-26 11:12:00 +00:00
}
}
2023-07-04 02:29:38 +01:00
func Test_sqlite_getString(t *testing.T) {
2023-02-15 16:24:34 +00:00
t.Parallel()
2023-07-04 02:29:38 +01:00
sqlite, err := instantiateSQLite()
2023-01-26 11:12:00 +00:00
if err != nil {
t.Fatal(err)
}
2023-07-04 02:29:38 +01:00
defer sqlite.close()
2023-01-26 11:12:00 +00:00
2023-07-04 02:29:38 +01:00
ptr := sqlite.newString("")
2023-01-26 11:12:00 +00:00
if ptr == 0 {
2023-01-26 12:15:34 +00:00
t.Error("got nullptr, want a pointer")
2023-01-26 11:12:00 +00:00
}
str := "sqlite3" + "\000 drop this"
2023-07-04 02:29:38 +01:00
ptr = sqlite.newString(str)
2023-01-26 11:12:00 +00:00
if ptr == 0 {
2023-01-26 12:15:34 +00:00
t.Fatal("got nullptr, want a pointer")
2023-01-26 11:12:00 +00:00
}
want := "sqlite3"
2025-01-21 01:42:57 +00:00
if got := util.ReadString(sqlite.mod, ptr, math.MaxInt); got != want {
2023-01-26 12:15:34 +00:00
t.Errorf("got %q, want %q", got, want)
2023-01-26 11:12:00 +00:00
}
2023-07-04 02:29:38 +01:00
if got := util.ReadString(sqlite.mod, ptr, 0); got != "" {
2023-01-26 12:15:34 +00:00
t.Errorf("got %q, want empty", got)
2023-01-26 11:12:00 +00:00
}
func() {
defer func() { _ = recover() }()
2025-01-21 01:42:57 +00:00
util.ReadString(sqlite.mod, ptr, int64(len(want))/2)
2023-02-10 16:42:49 +00:00
t.Error("want panic")
2023-01-26 11:12:00 +00:00
}()
func() {
defer func() { _ = recover() }()
2025-01-21 01:42:57 +00:00
util.ReadString(sqlite.mod, 0, math.MaxInt)
2023-02-10 16:42:49 +00:00
t.Error("want panic")
2023-01-26 11:12:00 +00:00
}()
}
2023-07-04 02:29:38 +01:00
func Test_sqlite_free(t *testing.T) {
2023-02-15 16:24:34 +00:00
t.Parallel()
2023-07-04 02:29:38 +01:00
sqlite, err := instantiateSQLite()
2023-01-26 11:12:00 +00:00
if err != nil {
t.Fatal(err)
}
2023-07-04 02:29:38 +01:00
defer sqlite.close()
2023-01-26 11:12:00 +00:00
2023-07-04 02:29:38 +01:00
sqlite.free(0)
2023-01-26 11:12:00 +00:00
2023-07-04 02:29:38 +01:00
ptr := sqlite.new(1)
2023-01-26 11:12:00 +00:00
if ptr == 0 {
2023-01-26 12:15:34 +00:00
t.Error("got nullptr, want a pointer")
2023-01-26 11:12:00 +00:00
}
2023-07-04 02:29:38 +01:00
sqlite.free(ptr)
2023-01-26 11:12:00 +00:00
}