forked from square/squalor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode_test.go
121 lines (115 loc) · 2.92 KB
/
encode_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package squalor
import (
"bytes"
"testing"
"time"
)
func TestEncodeSQL(t *testing.T) {
testCases := []struct {
arg interface{}
expected string
}{
{nil, "NULL"},
{(*int32)(nil), "NULL"},
{(*string)(nil), "NULL"},
{true, "1"},
{false, "0"},
{int(-1), "-1"},
{int32(-1), "-1"},
{int64(-1), "-1"},
{uint(1), "1"},
{uint32(1), "1"},
{uint64(1), "1"},
{1.23, "1.23"},
{"abcd", "'abcd'"},
{"workin' hard", "'workin\\' hard'"},
{"\x00'\"\b\n\r\t\x1A\\", `'\0\'\"\b\n\r\t\Z\\'`},
{[]byte(nil), "NULL"},
{[]byte{}, "X''"},
{[]byte("abcd"), "X'61626364'"},
{[]byte("\x00'\"\b\n\r\t\x1A\\"), "X'002722080a0d091a5c'"},
{time.Date(2012, time.February, 24, 23, 19, 43, 10, time.UTC), "'2012-02-24 23:19:43'"},
{time.Date(1999, 1, 2, 3, 4, 5, 0, time.UTC), "'1999-01-02 03:04:05'"},
{time.Date(2015, 3, 4, 5, 6, 7, 987654000, time.UTC), "'2015-03-04 05:06:07.987654'"},
// Three different representations of the same unicode string.
{"\xE7\xB1\xB3\xE6\xB4\xBE", "'米派'"},
{"\u7C73\u6D3E", "'米派'"},
{"米派", "'米派'"},
}
for _, c := range testCases {
var buf bytes.Buffer
if err := encodeSQLValue(&buf, c.arg); err != nil {
t.Error(err)
continue
}
encoded := buf.String()
if encoded != c.expected {
t.Errorf("Expected %q, but got %q", c.expected, encoded)
}
}
}
// Ensure dontEscape is not escaped
func TestDontEscape(t *testing.T) {
if encodeMap[dontEscape] != dontEscape {
t.Errorf("Encode fail: %v", encodeMap[dontEscape])
}
if decodeMap[dontEscape] != dontEscape {
t.Errorf("Decode fail: %v", decodeMap[dontEscape])
}
}
func TestEncodeSQLComment(t *testing.T) {
testCases := []struct {
arg string
expected string
}{
{"", "/* */"},
{"/*/", "/* /*\\/ */"},
{"foo", "/* foo */"},
{"/* foo */", "/* foo */"},
{"/*foo*/", "/*foo*/"},
{"foo */", "/* foo *\\/ */"},
{"/* foo */ */", "/* foo *\\/ */"},
{"-- foo", "/* -- foo */"},
{"# foo", "/* # foo */"},
{"米派", "/* 米派 */"},
}
for _, c := range testCases {
var buf bytes.Buffer
if err := encodeSQLComment(&buf, c.arg); err != nil {
t.Error(err)
continue
}
encoded := buf.String()
if encoded != c.expected {
t.Errorf("Expected %q, but got %q", c.expected, encoded)
}
}
}
func TestEscapeCommentContents(t *testing.T) {
testCases := []struct {
arg string
expected string
}{
{"", ""},
{"/*/", "/*\\/"},
{"foo", "foo"},
{"/* foo */", "/* foo *\\/"},
{"/*foo*/", "/*foo*\\/"},
{"*/ */ */", "*\\/ *\\/ *\\/"},
{"米派", "米派"},
}
for _, c := range testCases {
var buf bytes.Buffer
if err := escapeCommentContents(&buf, c.arg); err != nil {
t.Error(err)
continue
}
encoded := buf.String()
if encoded != c.expected {
t.Errorf("Expected %q, but got %q", c.expected, encoded)
}
}
}