Files
sqlite3/error_test.go

169 lines
3.9 KiB
Go
Raw Permalink Normal View History

2023-02-10 16:42:49 +00:00
package sqlite3
import (
2023-02-25 15:11:07 +00:00
"errors"
2023-02-10 16:42:49 +00:00
"strings"
"testing"
2023-03-29 15:01:25 +01:00
"github.com/ncruces/go-sqlite3/internal/util"
2023-02-10 16:42:49 +00:00
)
2023-02-25 15:11:07 +00:00
func Test_assertErr(t *testing.T) {
2023-03-29 15:01:25 +01:00
err := util.AssertErr()
if s := err.Error(); !strings.HasPrefix(s, "sqlite3: assertion failed") || !strings.HasSuffix(s, "error_test.go:12)") {
2023-02-25 15:11:07 +00:00
t.Errorf("got %q", s)
}
}
2023-02-10 16:42:49 +00:00
func TestError(t *testing.T) {
2023-02-27 12:07:48 +00:00
t.Parallel()
2023-06-30 12:25:07 +01:00
var ecode ErrorCode
var xcode xErrorCode
err := &Error{code: 0x8080}
if !errors.As(err, &err) {
t.Fatal("want true")
2023-02-10 16:42:49 +00:00
}
2023-06-30 12:25:07 +01:00
if ecode := err.Code(); ecode != 0x80 {
t.Errorf("got %#x, want 0x80", uint8(ecode))
}
if ok := errors.As(err, &ecode); !ok || ecode != ErrorCode(0x80) {
t.Errorf("got %#x, want 0x80", uint8(ecode))
}
if !errors.Is(err, ErrorCode(0x80)) {
2023-02-25 15:11:07 +00:00
t.Errorf("want true")
}
2023-06-30 12:25:07 +01:00
if xcode := err.ExtendedCode(); xcode != 0x8080 {
t.Errorf("got %#x, want 0x8080", uint16(xcode))
2023-02-10 16:42:49 +00:00
}
2023-06-30 12:25:07 +01:00
if ok := errors.As(err, &xcode); !ok || xcode != xErrorCode(0x8080) {
t.Errorf("got %#x, want 0x8080", uint16(xcode))
}
if !errors.Is(err, xErrorCode(0x8080)) {
2023-02-25 15:11:07 +00:00
t.Errorf("want true")
}
2023-02-10 16:42:49 +00:00
if s := err.Error(); s != "sqlite3: 32896" {
t.Errorf("got %q", s)
}
2023-06-30 12:25:07 +01:00
if ok := errors.As(err.ExtendedCode(), &ecode); !ok || ecode != ErrorCode(0x80) {
t.Errorf("got %#x, want 0x80", uint8(ecode))
}
2023-02-25 15:11:07 +00:00
if !errors.Is(err.ExtendedCode(), ErrorCode(0x80)) {
t.Errorf("want true")
}
2023-02-10 16:42:49 +00:00
}
2023-02-25 15:11:07 +00:00
func TestError_Temporary(t *testing.T) {
2023-02-27 12:07:48 +00:00
t.Parallel()
2023-02-25 15:11:07 +00:00
tests := []struct {
name string
code uint64
want bool
}{
{"ERROR", uint64(ERROR), false},
{"BUSY", uint64(BUSY), true},
{"BUSY_RECOVERY", uint64(BUSY_RECOVERY), true},
{"BUSY_SNAPSHOT", uint64(BUSY_SNAPSHOT), true},
{"BUSY_TIMEOUT", uint64(BUSY_TIMEOUT), true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
{
err := &Error{code: tt.code}
if got := err.Temporary(); got != tt.want {
t.Errorf("Error.Temporary(%d) = %v, want %v", tt.code, got, tt.want)
}
}
{
err := ErrorCode(tt.code)
if got := err.Temporary(); got != tt.want {
t.Errorf("ErrorCode.Temporary(%d) = %v, want %v", tt.code, got, tt.want)
}
}
{
err := ExtendedErrorCode(tt.code)
if got := err.Temporary(); got != tt.want {
t.Errorf("ExtendedErrorCode.Temporary(%d) = %v, want %v", tt.code, got, tt.want)
}
}
})
}
}
func TestError_Timeout(t *testing.T) {
2023-02-27 12:07:48 +00:00
t.Parallel()
2023-02-25 15:11:07 +00:00
tests := []struct {
name string
code uint64
want bool
}{
{"ERROR", uint64(ERROR), false},
{"BUSY", uint64(BUSY), false},
{"BUSY_RECOVERY", uint64(BUSY_RECOVERY), false},
{"BUSY_SNAPSHOT", uint64(BUSY_SNAPSHOT), false},
{"BUSY_TIMEOUT", uint64(BUSY_TIMEOUT), true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
{
err := &Error{code: tt.code}
if got := err.Timeout(); got != tt.want {
t.Errorf("Error.Timeout(%d) = %v, want %v", tt.code, got, tt.want)
}
}
{
err := ExtendedErrorCode(tt.code)
if got := err.Timeout(); got != tt.want {
t.Errorf("Error.Timeout(%d) = %v, want %v", tt.code, got, tt.want)
}
}
})
}
}
func Test_ErrorCode_Error(t *testing.T) {
2023-02-27 12:07:48 +00:00
t.Parallel()
2023-02-25 15:11:07 +00:00
db, err := Open(":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
// Test all error codes.
for i := 0; i == int(ErrorCode(i)); i++ {
want := "sqlite3: "
2023-11-30 17:52:35 +00:00
r := db.call("sqlite3_errstr", uint64(i))
2023-11-24 17:25:02 +00:00
want += util.ReadString(db.mod, uint32(r), _MAX_NAME)
2023-02-25 15:11:07 +00:00
got := ErrorCode(i).Error()
if got != want {
t.Fatalf("got %q, want %q, with %d", got, want, i)
}
}
}
func Test_ExtendedErrorCode_Error(t *testing.T) {
2023-02-27 12:07:48 +00:00
t.Parallel()
2023-02-25 15:11:07 +00:00
db, err := Open(":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
// Test all extended error codes.
for i := 0; i == int(ExtendedErrorCode(i)); i++ {
want := "sqlite3: "
2023-11-30 17:52:35 +00:00
r := db.call("sqlite3_errstr", uint64(i))
2023-11-24 17:25:02 +00:00
want += util.ReadString(db.mod, uint32(r), _MAX_NAME)
2023-02-25 15:11:07 +00:00
got := ExtendedErrorCode(i).Error()
if got != want {
t.Fatalf("got %q, want %q, with %d", got, want, i)
}
2023-02-10 16:42:49 +00:00
}
}