github.com/ncruces/go-sqlite3@v0.15.1-0.20240520133447-53eef1510ff0/tests/quote_test.go (about)

     1  package tests
     2  
     3  import (
     4  	"math"
     5  	"reflect"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/ncruces/go-sqlite3"
    10  )
    11  
    12  func TestQuote(t *testing.T) {
    13  	tests := []struct {
    14  		val  any
    15  		want string
    16  	}{
    17  		{`abc`, "'abc'"},
    18  		{`a"bc`, "'a\"bc'"},
    19  		{`a'bc`, "'a''bc'"},
    20  		{"\x07bc", "'\abc'"},
    21  		{"\x1c\n", "'\x1c\n'"},
    22  		{[]byte("\xB0\x00\x0B"), "x'B0000B'"},
    23  		{"\xB0\x00\x0B", ""},
    24  
    25  		{0, "0"},
    26  		{true, "1"},
    27  		{false, "0"},
    28  		{nil, "NULL"},
    29  		{math.NaN(), "NULL"},
    30  		{math.Inf(1), "9.0e999"},
    31  		{math.Inf(-1), "-9.0e999"},
    32  		{math.Pi, "3.141592653589793"},
    33  		{int64(math.MaxInt64), "9223372036854775807"},
    34  		{time.Unix(0, 0).UTC(), "'1970-01-01T00:00:00Z'"},
    35  		{sqlite3.ZeroBlob(4), "x'00000000'"},
    36  		{sqlite3.ZeroBlob(1e9), ""},
    37  	}
    38  
    39  	for _, tt := range tests {
    40  		t.Run(tt.want, func(t *testing.T) {
    41  			defer func() {
    42  				if r := recover(); r != nil && tt.want != "" {
    43  					t.Errorf("Quote(%q) = %v", tt.val, r)
    44  				}
    45  			}()
    46  
    47  			got := sqlite3.Quote(tt.val)
    48  			if !reflect.DeepEqual(got, tt.want) {
    49  				t.Errorf("Quote(%v) = %q, want %q", tt.val, got, tt.want)
    50  			}
    51  		})
    52  	}
    53  }
    54  
    55  func TestQuoteIdentifier(t *testing.T) {
    56  	tests := []struct {
    57  		id   string
    58  		want string
    59  	}{
    60  		{`abc`, `"abc"`},
    61  		{`a"bc`, `"a""bc"`},
    62  		{`a'bc`, `"a'bc"`},
    63  		{"\x07bc", "\"\abc\""},
    64  		{"\x1c\n", "\"\x1c\n\""},
    65  		{"\xB0\x00\x0B", ""},
    66  	}
    67  
    68  	for _, tt := range tests {
    69  		t.Run(tt.want, func(t *testing.T) {
    70  			defer func() {
    71  				if r := recover(); r != nil && tt.want != "" {
    72  					t.Errorf("QuoteIdentifier(%q) = %v", tt.id, r)
    73  				}
    74  			}()
    75  
    76  			got := sqlite3.QuoteIdentifier(tt.id)
    77  			if !reflect.DeepEqual(got, tt.want) {
    78  				t.Errorf("QuoteIdentifier(%v) = %q, want %q", tt.id, got, tt.want)
    79  			}
    80  		})
    81  	}
    82  }