forked from go-rel/mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquote_test.go
76 lines (63 loc) · 1.49 KB
/
quote_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package mysql
import (
"database/sql/driver"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestQuote_Panic(t *testing.T) {
quoter := Quote{}
assert.PanicsWithValue(t, "unsupported value", func() {
quoter.Value(1)
})
}
func TestQuote_ID(t *testing.T) {
quoter := Quote{}
var cases = []struct {
input string
want string
}{
{`foo`, "`foo`"},
{`foo bar baz`, "`foo bar baz`"},
{"foo`bar", "`foo``bar`"},
{"foo\x00bar", "`foo`"},
{"\x00foo", "``"},
}
for _, test := range cases {
assert.Equal(t, test.want, quoter.ID(test.input))
}
}
func TestQuote_Value(t *testing.T) {
quoter := Quote{}
var cases = []struct {
input string
want string
}{
{"foo\x00bar", "'foo\\0bar'"},
{"foo\nbar", "'foo\\nbar'"},
{"foo\rbar", "'foo\\rbar'"},
{"foo\x1abar", "'foo\\Zbar'"},
{"foo\"bar", "'foo\\\"bar'"},
{"foo\\bar", "'foo\\\\bar'"},
{"foo'bar", "'foo\\'bar'"},
}
for _, test := range cases {
assert.Equal(t, test.want, quoter.Value(test.input))
}
}
type customType int
func (c customType) Value() (driver.Value, error) {
return int(c), nil
}
func TestValueConvert_CustomType(t *testing.T) {
valuer := ValueConvert{}
v, err := valuer.ConvertValue(customType(1))
assert.EqualError(t, err, "non-Value type int returned from Value")
assert.Nil(t, v)
}
func TestValueConvert_DateTime(t *testing.T) {
valuer := ValueConvert{}
v, err := valuer.ConvertValue(time.Unix(1633934368, 0).UTC())
assert.NoError(t, err)
assert.Equal(t, "2021-10-11 06:39:28", v)
}