Files
sqlite3/tests/quote_test.go

88 lines
1.8 KiB
Go
Raw Permalink Normal View History

2023-11-03 07:43:05 -07:00
package tests
import (
2024-10-07 14:42:09 +01:00
"database/sql"
"encoding/json"
2023-11-03 07:43:05 -07:00
"math"
"testing"
"time"
"github.com/ncruces/go-sqlite3"
)
func TestQuote(t *testing.T) {
tests := []struct {
val any
want string
}{
{`abc`, "'abc'"},
{`a"bc`, "'a\"bc'"},
{`a'bc`, "'a''bc'"},
{"\x07bc", "'\abc'"},
{"\x1c\n", "'\x1c\n'"},
2024-10-07 14:42:09 +01:00
{"\xB0\x00\x0B", "'\xB0'"},
2023-11-03 07:43:05 -07:00
{[]byte("\xB0\x00\x0B"), "x'B0000B'"},
{0, "0"},
{true, "1"},
{false, "0"},
{nil, "NULL"},
{math.NaN(), "NULL"},
{math.Inf(1), "9.0e999"},
{math.Inf(-1), "-9.0e999"},
{math.Pi, "3.141592653589793"},
{int64(math.MaxInt64), "9223372036854775807"},
{time.Unix(0, 0).UTC(), "'1970-01-01T00:00:00Z'"},
{sqlite3.ZeroBlob(4), "x'00000000'"},
2024-10-07 14:42:09 +01:00
{int8(0), "0"},
{uint(0), "0"},
{float32(0), "0"},
{(*string)(nil), "NULL"},
{json.Number("0"), "'0'"},
{&sql.RawBytes{'0'}, "x'30'"},
{t, ""}, // panic
2023-11-03 07:43:05 -07:00
}
for _, tt := range tests {
t.Run(tt.want, func(t *testing.T) {
defer func() {
if r := recover(); r != nil && tt.want != "" {
t.Errorf("Quote(%q) = %v", tt.val, r)
}
}()
2025-01-24 10:46:05 +00:00
if got := sqlite3.Quote(tt.val); got != tt.want {
2023-11-03 07:43:05 -07:00
t.Errorf("Quote(%v) = %q, want %q", tt.val, got, tt.want)
}
})
}
}
func TestQuoteIdentifier(t *testing.T) {
tests := []struct {
id string
want string
}{
{`abc`, `"abc"`},
{`a"bc`, `"a""bc"`},
{`a'bc`, `"a'bc"`},
{"\x07bc", "\"\abc\""},
{"\x1c\n", "\"\x1c\n\""},
2024-10-07 14:42:09 +01:00
{"\xB0\x00\x0B", ""}, // panic
2023-11-03 07:43:05 -07:00
}
for _, tt := range tests {
t.Run(tt.want, func(t *testing.T) {
defer func() {
if r := recover(); r != nil && tt.want != "" {
t.Errorf("QuoteIdentifier(%q) = %v", tt.id, r)
}
}()
2025-01-24 10:46:05 +00:00
if got := sqlite3.QuoteIdentifier(tt.id); got != tt.want {
2023-11-03 07:43:05 -07:00
t.Errorf("QuoteIdentifier(%v) = %q, want %q", tt.id, got, tt.want)
}
})
}
}