From c766a4fed265398bc4bd3fbc92f1474175e64975 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Fri, 26 Jul 2024 13:29:24 +0100 Subject: [PATCH] Testing. --- ext/blobio/blob.go | 16 +++++----- ext/blobio/blob_test.go | 65 ++++++++++++++++++++++++++--------------- ext/fileio/fileio.go | 2 +- ext/fileio/write.go | 4 +-- ext/regexp/regexp.go | 8 ++--- ext/stats/percentile.go | 2 +- ext/unicode/unicode.go | 8 ++--- ext/uuid/uuid.go | 8 ++--- vfs/vfs.go | 10 ++----- 9 files changed, 67 insertions(+), 56 deletions(-) diff --git a/ext/blobio/blob.go b/ext/blobio/blob.go index 6202e0b..7649ff0 100644 --- a/ext/blobio/blob.go +++ b/ext/blobio/blob.go @@ -43,13 +43,13 @@ func readblob(ctx sqlite3.Context, arg ...sqlite3.Value) { blob, err := getAuxBlob(ctx, arg, false) if err != nil { ctx.ResultError(err) - return + return // notest } _, err = blob.Seek(arg[4].Int64(), io.SeekStart) if err != nil { ctx.ResultError(err) - return + return // notest } n := arg[5].Int64() @@ -61,7 +61,7 @@ func readblob(ctx sqlite3.Context, arg ...sqlite3.Value) { _, err = io.ReadFull(blob, buf) if err != nil { ctx.ResultError(err) - return + return // notest } ctx.ResultBlob(buf) @@ -72,19 +72,19 @@ func writeblob(ctx sqlite3.Context, arg ...sqlite3.Value) { blob, err := getAuxBlob(ctx, arg, true) if err != nil { ctx.ResultError(err) - return + return // notest } _, err = blob.Seek(arg[4].Int64(), io.SeekStart) if err != nil { ctx.ResultError(err) - return + return // notest } _, err = blob.Write(arg[5].RawBlob()) if err != nil { ctx.ResultError(err) - return + return // notest } setAuxBlob(ctx, blob, false) @@ -99,14 +99,14 @@ func openblob(ctx sqlite3.Context, arg ...sqlite3.Value) { blob, err := getAuxBlob(ctx, arg, arg[4].Bool()) if err != nil { ctx.ResultError(err) - return + return // notest } fn := arg[5].Pointer().(OpenCallback) err = fn(blob, arg[6:]...) if err != nil { ctx.ResultError(err) - return + return // notest } setAuxBlob(ctx, blob, true) diff --git a/ext/blobio/blob_test.go b/ext/blobio/blob_test.go index 9a754de..f20ff42 100644 --- a/ext/blobio/blob_test.go +++ b/ext/blobio/blob_test.go @@ -89,6 +89,7 @@ func Test_readblob(t *testing.T) { CREATE TABLE test1 (col); CREATE TABLE test2 (col); INSERT INTO test1 VALUES (x'cafe'); + INSERT INTO test1 VALUES (x'dead'); INSERT INTO test2 VALUES (x'babe'); `) if err != nil { @@ -107,34 +108,50 @@ func Test_readblob(t *testing.T) { t.Log(err) } - stmt, _, err := db.Prepare(`SELECT readblob('main', value, 'col', 1, 1, 1) FROM array(?)`) - if err != nil { - t.Fatal(err) - } - defer stmt.Close() - - err = stmt.BindPointer(1, []string{"test1", "test2"}) - if err != nil { - t.Fatal(err) + tests := []struct { + name string + sql string + want1 string + want2 string + }{ + {"rows", `SELECT readblob('main', 'test1', 'col', rowid, 1, 1) FROM test1`, "\xfe", "\xad"}, + {"tables", `SELECT readblob('main', value, 'col', 1, 1, 1) FROM array(?)`, "\xfe", "\xbe"}, } - if stmt.Step() { - got := stmt.ColumnText(0) - if got != "\xfe" { - t.Errorf("got %q", got) - } - } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + stmt, _, err := db.Prepare(tt.sql) + if err != nil { + t.Fatal(err) + } + defer stmt.Close() - if stmt.Step() { - got := stmt.ColumnText(0) - if got != "\xbe" { - t.Errorf("got %q", got) - } - } + if stmt.BindCount() == 1 { + err = stmt.BindPointer(1, []string{"test1", "test2"}) + if err != nil { + t.Fatal(err) + } + } - err = stmt.Err() - if err != nil { - t.Fatal(err) + if stmt.Step() { + got := stmt.ColumnText(0) + if got != tt.want1 { + t.Errorf("got %q", got) + } + } + + if stmt.Step() { + got := stmt.ColumnText(0) + if got != tt.want2 { + t.Errorf("got %q", got) + } + } + + err = stmt.Err() + if err != nil { + t.Fatal(err) + } + }) } } diff --git a/ext/fileio/fileio.go b/ext/fileio/fileio.go index 4c1bca7..c46b2b9 100644 --- a/ext/fileio/fileio.go +++ b/ext/fileio/fileio.go @@ -55,7 +55,7 @@ func readfile(fsys fs.FS) func(ctx sqlite3.Context, arg ...sqlite3.Value) { case err == nil: ctx.ResultBlob(data) case !errors.Is(err, fs.ErrNotExist): - ctx.ResultError(fmt.Errorf("readfile: %w", err)) + ctx.ResultError(fmt.Errorf("readfile: %w", err)) // notest } } } diff --git a/ext/fileio/write.go b/ext/fileio/write.go index 4c963a5..af7d966 100644 --- a/ext/fileio/write.go +++ b/ext/fileio/write.go @@ -39,7 +39,7 @@ func writefile(ctx sqlite3.Context, arg ...sqlite3.Value) { err := os.Chmod(file, mode.Perm()) if err != nil { ctx.ResultError(fmt.Errorf("writefile: %w", err)) - return + return // notest } } @@ -48,7 +48,7 @@ func writefile(ctx sqlite3.Context, arg ...sqlite3.Value) { err := os.Chtimes(file, time.Time{}, mtime) if err != nil { ctx.ResultError(fmt.Errorf("writefile: %w", err)) - return + return // notest } } } diff --git a/ext/regexp/regexp.go b/ext/regexp/regexp.go index 4e0e508..6b8ce35 100644 --- a/ext/regexp/regexp.go +++ b/ext/regexp/regexp.go @@ -44,7 +44,7 @@ func load(ctx sqlite3.Context, i int, expr string) (*regexp.Regexp, error) { func regex(ctx sqlite3.Context, arg ...sqlite3.Value) { re, err := load(ctx, 0, arg[0].Text()) if err != nil { - ctx.ResultError(err) + ctx.ResultError(err) // notest } else { ctx.ResultBool(re.Match(arg[1].RawText())) } @@ -53,7 +53,7 @@ func regex(ctx sqlite3.Context, arg ...sqlite3.Value) { func regexLike(ctx sqlite3.Context, arg ...sqlite3.Value) { re, err := load(ctx, 1, arg[1].Text()) if err != nil { - ctx.ResultError(err) + ctx.ResultError(err) // notest } else { ctx.ResultBool(re.Match(arg[0].RawText())) } @@ -62,7 +62,7 @@ func regexLike(ctx sqlite3.Context, arg ...sqlite3.Value) { func regexSubstr(ctx sqlite3.Context, arg ...sqlite3.Value) { re, err := load(ctx, 1, arg[1].Text()) if err != nil { - ctx.ResultError(err) + ctx.ResultError(err) // notest } else { ctx.ResultRawText(re.Find(arg[0].RawText())) } @@ -71,7 +71,7 @@ func regexSubstr(ctx sqlite3.Context, arg ...sqlite3.Value) { func regexReplace(ctx sqlite3.Context, arg ...sqlite3.Value) { re, err := load(ctx, 1, arg[1].Text()) if err != nil { - ctx.ResultError(err) + ctx.ResultError(err) // notest } else { ctx.ResultRawText(re.ReplaceAll(arg[0].RawText(), arg[2].RawText())) } diff --git a/ext/stats/percentile.go b/ext/stats/percentile.go index 9da7646..a4c3199 100644 --- a/ext/stats/percentile.go +++ b/ext/stats/percentile.go @@ -62,7 +62,7 @@ func (q *percentile) Value(ctx sqlite3.Context) { ctx.ResultJSON(floats) } if err != nil { - ctx.ResultError(fmt.Errorf("percentile: %w", err)) + ctx.ResultError(fmt.Errorf("percentile: %w", err)) // notest } } diff --git a/ext/unicode/unicode.go b/ext/unicode/unicode.go index 04c3e30..1d1861b 100644 --- a/ext/unicode/unicode.go +++ b/ext/unicode/unicode.go @@ -51,7 +51,7 @@ func Register(db *sqlite3.Conn) error { err := RegisterCollation(db, arg[0].Text(), name) if err != nil { ctx.ResultError(err) - return + return // notest } })) } @@ -75,7 +75,7 @@ func upper(ctx sqlite3.Context, arg ...sqlite3.Value) { t, err := language.Parse(arg[1].Text()) if err != nil { ctx.ResultError(err) - return + return // notest } c := cases.Upper(t) ctx.SetAuxData(1, c) @@ -94,7 +94,7 @@ func lower(ctx sqlite3.Context, arg ...sqlite3.Value) { t, err := language.Parse(arg[1].Text()) if err != nil { ctx.ResultError(err) - return + return // notest } c := cases.Lower(t) ctx.SetAuxData(1, c) @@ -109,7 +109,7 @@ func regex(ctx sqlite3.Context, arg ...sqlite3.Value) { r, err := regexp.Compile(arg[0].Text()) if err != nil { ctx.ResultError(err) - return + return // notest } re = r ctx.SetAuxData(0, r) diff --git a/ext/uuid/uuid.go b/ext/uuid/uuid.go index ae6c213..6dbd42b 100644 --- a/ext/uuid/uuid.go +++ b/ext/uuid/uuid.go @@ -107,7 +107,7 @@ func generate(ctx sqlite3.Context, arg ...sqlite3.Value) { ns = uuid.NameSpaceX500 default: ctx.ResultError(err) - return + return // notest } } if ver == 3 { @@ -121,7 +121,7 @@ func generate(ctx sqlite3.Context, arg ...sqlite3.Value) { } if err != nil { - ctx.ResultError(fmt.Errorf("uuid: %w", err)) + ctx.ResultError(fmt.Errorf("uuid: %w", err)) // notest } else { ctx.ResultText(u.String()) } @@ -152,7 +152,7 @@ func fromValue(arg sqlite3.Value) (u uuid.UUID, err error) { func toBlob(ctx sqlite3.Context, arg ...sqlite3.Value) { u, err := fromValue(arg[0]) if err != nil { - ctx.ResultError(err) + ctx.ResultError(err) // notest } else { ctx.ResultBlob(u[:]) } @@ -161,7 +161,7 @@ func toBlob(ctx sqlite3.Context, arg ...sqlite3.Value) { func toString(ctx sqlite3.Context, arg ...sqlite3.Value) { u, err := fromValue(arg[0]) if err != nil { - ctx.ResultError(err) + ctx.ResultError(err) // notest } else { ctx.ResultText(u.String()) } diff --git a/vfs/vfs.go b/vfs/vfs.go index e9a63cc..983f285 100644 --- a/vfs/vfs.go +++ b/vfs/vfs.go @@ -163,10 +163,7 @@ func vfsOpen(ctx context.Context, mod api.Module, pVfs, zPath, pFile uint32, fla func vfsClose(ctx context.Context, mod api.Module, pFile uint32) _ErrorCode { err := vfsFileClose(ctx, mod, pFile) - if err != nil { - return vfsErrorCode(err, _IOERR_CLOSE) - } - return _OK + return vfsErrorCode(err, _IOERR_CLOSE) } func vfsRead(ctx context.Context, mod api.Module, pFile, zBuf uint32, iAmt int32, iOfst int64) _ErrorCode { @@ -189,10 +186,7 @@ func vfsWrite(ctx context.Context, mod api.Module, pFile, zBuf uint32, iAmt int3 buf := util.View(mod, zBuf, uint64(iAmt)) _, err := file.WriteAt(buf, iOfst) - if err != nil { - return vfsErrorCode(err, _IOERR_WRITE) - } - return _OK + return vfsErrorCode(err, _IOERR_WRITE) } func vfsTruncate(ctx context.Context, mod api.Module, pFile uint32, nByte int64) _ErrorCode {