forked from databricks/databricks-sql-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
101 lines (95 loc) · 2.71 KB
/
util_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
package dbsql
import (
"database/sql/driver"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestEscapeArgs(t *testing.T) {
thyme := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
tests := map[string]struct {
args []driver.NamedValue
expected []string
expectError bool
}{
"strings": {
args: []driver.NamedValue{{Ordinal: 1, Value: "foo"}, {Ordinal: 2, Value: "bar"}},
expected: []string{
"'foo'",
"'bar'",
},
},
"strings with quotes": {
args: []driver.NamedValue{{Ordinal: 1, Value: "f'oo"}, {Ordinal: 2, Value: "bar"}},
expected: []string{
"'f''oo'",
"'bar'",
},
},
"lists": {
args: []driver.NamedValue{{Ordinal: 1, Value: []interface{}{"foo", "bar"}}},
expected: []string{
"('foo', 'bar')",
},
},
"time": {
args: []driver.NamedValue{{Ordinal: 1, Value: thyme}},
expected: []string{
"'2020-01-01 00:00:00'",
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
actual, err := EscapeArgs(test.args)
if test.expectError {
assert.Error(t, err)
} else {
assert.Equal(t, test.expected, actual)
}
})
}
}
func TestSubstituteArgs(t *testing.T) {
tests := map[string]struct {
sql string
args []driver.NamedValue
expected string
expectError bool
}{
"no args": {
sql: "SELECT * FROM foo",
expected: "SELECT * FROM foo",
},
"one arg": {
sql: "SELECT * FROM foo WHERE bar = ?",
args: []driver.NamedValue{{Ordinal: 1, Value: "baz"}},
expected: "SELECT * FROM foo WHERE bar = 'baz'",
},
"two args": {
sql: "SELECT * FROM foo WHERE bar = ? AND baz = ?",
args: []driver.NamedValue{{Ordinal: 1, Value: "baz"}, {Ordinal: 2, Value: "qux"}},
expected: "SELECT * FROM foo WHERE bar = 'baz' AND baz = 'qux'",
},
"two args with list": {
sql: "SELECT * FROM foo WHERE bar = ? AND baz IN ?",
args: []driver.NamedValue{{Ordinal: 1, Value: "baz"}, {Ordinal: 2, Value: []interface{}{"qux", "quux"}}},
expected: "SELECT * FROM foo WHERE bar = 'baz' AND baz IN ('qux', 'quux')",
},
"three args with list and time": {
sql: "SELECT * FROM foo WHERE bar = ? AND baz IN ? AND qux = ?",
args: []driver.NamedValue{{Ordinal: 1, Value: "baz"}, {Ordinal: 2, Value: []interface{}{"qux", "quux"}}, {Ordinal: 3, Value: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)}},
expected: "SELECT * FROM foo WHERE bar = 'baz' AND baz IN ('qux', 'quux') AND qux = '2020-01-01 00:00:00'",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
actual, err := SubstituteArgs(test.sql, test.args)
if test.expectError {
assert.Error(t, err)
} else {
assert.Equal(t, test.expected, actual)
}
})
}
}