Skip to content

Commit

Permalink
Tests, fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
ncruces committed Dec 2, 2023
1 parent c667a1f commit 8a0baed
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 12 deletions.
2 changes: 2 additions & 0 deletions ext/array/array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func Example() {
}

func Test_cursor_Column(t *testing.T) {
t.Parallel()

db, err := driver.Open(":memory:", func(c *sqlite3.Conn) error {
array.Register(c)
return nil
Expand Down
8 changes: 4 additions & 4 deletions ext/blob/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ func openBlob(ctx sqlite3.Context, arg ...sqlite3.Value) {
}

// This ensures the blob is closed if db, table, column or write change.
ctx.SetAuxData(0, blob)
ctx.SetAuxData(1, blob)
ctx.SetAuxData(2, blob)
ctx.SetAuxData(4, blob)
ctx.SetAuxData(0, blob) // db
ctx.SetAuxData(1, blob) // table
ctx.SetAuxData(2, blob) // column
ctx.SetAuxData(4, blob) // write
}

// OpenCallback is the type for the blob_open callback.
Expand Down
67 changes: 67 additions & 0 deletions ext/blob/blob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"io"
"log"
"os"
"reflect"
"testing"

"github.com/ncruces/go-sqlite3"
"github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/embed"
"github.com/ncruces/go-sqlite3/ext/array"
"github.com/ncruces/go-sqlite3/ext/blob"
_ "github.com/ncruces/go-sqlite3/vfs/memdb"
)
Expand Down Expand Up @@ -59,3 +62,67 @@ func Example() {
// Output:
// Hello BLOB!
}

func TestRegister(t *testing.T) {
t.Parallel()

db, err := sqlite3.Open(":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()

blob.Register(db)
array.Register(db)

err = db.Exec(`SELECT blob_open()`)
if err == nil {
t.Fatal("want error")
} else {
t.Log(err)
}

err = db.Exec(`
CREATE TABLE IF NOT EXISTS test1 (col);
CREATE TABLE IF NOT EXISTS test2 (col);
INSERT INTO test1 VALUES (x'cafe');
INSERT INTO test2 VALUES (x'babe');
`)
if err != nil {
t.Fatal(err)
}

stmt, _, err := db.Prepare(`SELECT blob_open('main', value, 'col', 1, false, ?) FROM array(?)`)
if err != nil {
t.Fatal(err)
}
defer stmt.Close()

var got []string
err = stmt.BindPointer(1, blob.OpenCallback(func(b *sqlite3.Blob, _ ...sqlite3.Value) error {
d, err := io.ReadAll(b)
if err != nil {
return err
}
got = append(got, string(d))
return nil
}))
if err != nil {
t.Fatal(err)
}

err = stmt.BindPointer(2, []string{"test1", "test2"})
if err != nil {
t.Fatal(err)
}

err = stmt.Exec()
if err != nil {
t.Fatal(err)
}

want := []string{"\xca\xfe", "\xba\xbe"}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
}
}
4 changes: 4 additions & 0 deletions ext/csv/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func Example() {
}

func TestRegister(t *testing.T) {
t.Parallel()

db, err := sqlite3.Open(":memory:")
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -106,6 +108,8 @@ Robert "Griesemer" "gri"`
}

func TestRegister_errors(t *testing.T) {
t.Parallel()

db, err := sqlite3.Open(":memory:")
if err != nil {
t.Fatal(err)
Expand Down
2 changes: 2 additions & 0 deletions ext/csv/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package csv
import "testing"

func Test_uintParam(t *testing.T) {
t.Parallel()

tests := []struct {
arg string
key string
Expand Down
2 changes: 2 additions & 0 deletions ext/csv/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package csv
import "testing"

func Test_getSchema(t *testing.T) {
t.Parallel()

tests := []struct {
header bool
columns int
Expand Down
8 changes: 8 additions & 0 deletions ext/lines/lines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ func Example() {
}

func Test_lines(t *testing.T) {
t.Parallel()

db, err := driver.Open(":memory:", func(c *sqlite3.Conn) error {
lines.Register(c)
return nil
Expand Down Expand Up @@ -95,6 +97,8 @@ func Test_lines(t *testing.T) {
}

func Test_lines_error(t *testing.T) {
t.Parallel()

db, err := driver.Open(":memory:", func(c *sqlite3.Conn) error {
lines.Register(c)
return nil
Expand All @@ -120,6 +124,8 @@ func Test_lines_error(t *testing.T) {
}

func Test_lines_read(t *testing.T) {
t.Parallel()

db, err := driver.Open(":memory:", func(c *sqlite3.Conn) error {
lines.Register(c)
return nil
Expand Down Expand Up @@ -149,6 +155,8 @@ func Test_lines_read(t *testing.T) {
}

func Test_lines_test(t *testing.T) {
t.Parallel()

db, err := driver.Open(":memory:", func(c *sqlite3.Conn) error {
lines.Register(c)
return nil
Expand Down
14 changes: 6 additions & 8 deletions ext/statement/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// Register registers the statement virtual table.
func Register(db *sqlite3.Conn) {
declare := func(db *sqlite3.Conn, _, _, _ string, arg ...string) (_ *table, err error) {
if arg == nil || len(arg[0]) < 3 {
if len(arg) == 0 || len(arg[0]) < 3 {
return nil, fmt.Errorf("statement: no statement provided")
}
sql := arg[0]
Expand Down Expand Up @@ -70,21 +70,19 @@ func (t *table) declare() error {
str.WriteString(sep)
name := stmt.ColumnName(i)
str.WriteString(sqlite3.QuoteIdentifier(name))
if typ := stmt.ColumnDeclType(i); typ != "" {
str.WriteByte(' ')
str.WriteString(typ)
}
str.WriteByte(' ')
str.WriteString(stmt.ColumnDeclType(i))
sep = ","
}
for i := 1; i <= t.inputs; i++ {
str.WriteString(sep)
name := stmt.BindName(i)
if name == "" {
str.WriteString("'")
str.WriteString("[")
str.WriteString(strconv.Itoa(i))
str.WriteString("' HIDDEN")
str.WriteString("] HIDDEN")
} else {
str.WriteString(sqlite3.QuoteIdentifier(name))
str.WriteString(sqlite3.QuoteIdentifier(name[1:]))
str.WriteString(" HIDDEN")
}
sep = ","
Expand Down
99 changes: 99 additions & 0 deletions ext/statement/stmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package statement_test
import (
"fmt"
"log"
"testing"

"github.com/ncruces/go-sqlite3"
_ "github.com/ncruces/go-sqlite3/embed"
Expand Down Expand Up @@ -45,3 +46,101 @@ func Example() {
// Output:
// Twosday was 2022-2-22
}

func TestRegister(t *testing.T) {
t.Parallel()

db, err := sqlite3.Open(":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()

statement.Register(db)

err = db.Exec(`
CREATE VIRTUAL TABLE arguments USING statement((SELECT ? AS a, ? AS b, ? AS c))
`)
if err != nil {
t.Fatal(err)
}

err = db.Exec(`
SELECT * from arguments WHERE [2] = 'y' AND [3] = 'z'
`)
if err != nil {
t.Fatal(err)
}

err = db.Exec(`
CREATE VIRTUAL TABLE hypot USING statement((SELECT sqrt(:x * :x + :y * :y) AS hypotenuse))
`)
if err != nil {
t.Fatal(err)
}

stmt, _, err := db.Prepare(`
SELECT x, y, * FROM hypot WHERE x = 3 AND y = 4
`)
if err != nil {
t.Fatal(err)
}
defer stmt.Close()

if stmt.Step() {
x := stmt.ColumnInt(0)
y := stmt.ColumnInt(1)
hypot := stmt.ColumnInt(2)
if x != 3 || y != 4 || hypot != 5 {
t.Errorf("hypot(%d, %d) = %d", x, y, hypot)
}
}

}

func TestRegister_errors(t *testing.T) {
t.Parallel()

db, err := sqlite3.Open(":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()

statement.Register(db)

err = db.Exec(`CREATE VIRTUAL TABLE split_date USING statement()`)
if err == nil {
t.Fatal("want error")
} else {
t.Log(err)
}

err = db.Exec(`CREATE VIRTUAL TABLE split_date USING statement(SELECT 1, SELECT 2)`)
if err == nil {
t.Fatal("want error")
} else {
t.Log(err)
}

err = db.Exec(`CREATE VIRTUAL TABLE split_date USING statement((SELECT 1, SELECT 2))`)
if err == nil {
t.Fatal("want error")
} else {
t.Log(err)
}

err = db.Exec(`CREATE VIRTUAL TABLE split_date USING statement((SELECT 1; SELECT 2))`)
if err == nil {
t.Fatal("want error")
} else {
t.Log(err)
}

err = db.Exec(`CREATE VIRTUAL TABLE split_date USING statement((CREATE TABLE x(val)))`)
if err == nil {
t.Fatal("want error")
} else {
t.Log(err)
}
}
6 changes: 6 additions & 0 deletions ext/stats/welford_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
)

func Test_welford(t *testing.T) {
t.Parallel()

var s1, s2 welford

s1.enqueue(4)
Expand Down Expand Up @@ -38,6 +40,8 @@ func Test_welford(t *testing.T) {
}

func Test_covar(t *testing.T) {
t.Parallel()

var c1, c2 welford2

c1.enqueue(3, 70)
Expand All @@ -64,6 +68,8 @@ func Test_covar(t *testing.T) {
}

func Test_correlation(t *testing.T) {
t.Parallel()

var c welford2
c.enqueue(1, 3)
c.enqueue(2, 2)
Expand Down
2 changes: 2 additions & 0 deletions ext/unicode/unicode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ func TestRegister_error(t *testing.T) {
}

func Test_like2regex(t *testing.T) {
t.Parallel()

const prefix = `(?is)\A`
const sufix = `\z`
tests := []struct {
Expand Down

0 comments on commit 8a0baed

Please sign in to comment.