-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquote_test.go
78 lines (66 loc) · 1.66 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
77
78
package postgres
import (
"database/sql/driver"
"testing"
"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{}
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{}
cases := []struct {
input string
want string
}{
{`foo`, `'foo'`},
{`foo bar baz`, `'foo bar baz'`},
{`foo'bar`, `'foo''bar'`},
{`foo\bar`, ` E'foo\\bar'`},
{`foo\ba'r`, ` E'foo\\ba''r'`},
{`foo"bar`, `'foo"bar'`},
{`foo\x00bar`, ` E'foo\\x00bar'`},
{`\x00foo`, ` E'\\x00foo'`},
{`'`, `''''`},
{`''`, `''''''`},
{`\`, ` E'\\'`},
{`'abc'; DROP TABLE users;`, `'''abc''; DROP TABLE users;'`},
{`\'`, ` E'\\'''`},
{`E'\''`, ` E'E''\\'''''`},
{`e'\''`, ` E'e''\\'''''`},
{`E'\'abc\'; DROP TABLE users;'`, ` E'E''\\''abc\\''; DROP TABLE users;'''`},
{`e'\'abc\'; DROP TABLE users;'`, ` E'e''\\''abc\\''; DROP TABLE users;'''`},
}
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)
}