-
Notifications
You must be signed in to change notification settings - Fork 655
Expand file tree
/
Copy pathquery_parameters_test.go
More file actions
148 lines (133 loc) · 3.93 KB
/
query_parameters_test.go
File metadata and controls
148 lines (133 loc) · 3.93 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package clickhouse
import (
"testing"
"time"
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
"github.com/stretchr/testify/assert"
)
func TestBindQueryOrAppendParameters(t *testing.T) {
testTime := time.Date(2023, 1, 1, 12, 0, 0, 0, time.UTC)
tests := []struct {
name string
param any
expectedValue string
expectError bool
}{
// Nil / NULL case (The fixed bug)
{
name: "nil translates to \\N",
param: Named("param", nil),
expectedValue: "\\N",
},
// Basic types
{
name: "boolean true",
param: Named("param", true),
expectedValue: "1",
},
{
name: "boolean false",
param: Named("param", false),
expectedValue: "0",
},
{
name: "string direct bypass",
param: Named("param", "hello_world"),
expectedValue: "hello_world",
},
{
name: "string with quotes bypass",
param: Named("param", "hello 'world'"),
expectedValue: "hello 'world'", // String bypasses format(), so it shouldn't have extra quotes added
},
{
name: "integer",
param: Named("param", 42),
expectedValue: "42",
},
{
name: "float",
param: Named("param", 3.1415),
expectedValue: "3.1415",
},
// Collections
{
name: "slice of ints",
param: Named("param", []int{1, 2, 3}),
expectedValue: "[1, 2, 3]",
},
{
name: "slice of strings",
param: Named("param", []string{"a", "b", "c"}),
expectedValue: "['a', 'b', 'c']",
},
// Time types
// formatTime adds quotes and toDateTime
{
name: "time.Time",
param: Named("param", testTime),
expectedValue: "toDateTime('2023-01-01 12:00:05', 'UTC')",
},
// formatTimeWithScale behavior
{
name: "NamedDateValue",
param: driver.NamedDateValue{
Name: "param",
Value: testTime,
Scale: uint8(Seconds),
},
expectedValue: "2023-01-01 12:00:00",
},
// Error cases
// Not a NamedValue or NamedDateValue
{
name: "unsupported type",
param: struct{ A int }{A: 1},
expectedValue: "",
expectError: true,
},
}
// The query must contain {param:Type}
query := `
SELECT *
FROM t
WHERE col = {param:String}`
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opts := &QueryOptions{parameters: make(Parameters)}
_, err := bindQueryOrAppendParameters(true, opts, query, time.UTC, tt.param)
if tt.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
// For time.Time standard format returns toDateTime('...', 'UTC'), we just verify it formats without error
if tt.name == "time.Time" {
assert.Contains(t, opts.parameters["param"], "2023-01-01")
} else {
assert.Equal(t, tt.expectedValue, opts.parameters["param"])
}
}
})
}
}
func TestBindQueryOrAppendParameters_NoProtocolSupport(t *testing.T) {
opts := &QueryOptions{parameters: make(Parameters)}
query := "SELECT * FROM t WHERE col = @param"
// If paramsProtocolSupport is false, it should fallback to legacy bind (which replaces @param directly)
resQuery, err := bindQueryOrAppendParameters(false, opts, query, time.UTC, Named("param", "val"))
assert.NoError(t, err)
assert.Equal(t, "SELECT * FROM t WHERE col = 'val'", resQuery)
assert.Empty(t, opts.parameters, "Parameters map should be empty when fallback to bind")
}
func TestBindQueryOrAppendParameters_ExplicitParams(t *testing.T) {
opts := &QueryOptions{parameters: Parameters{"param": "explicit_val"}}
query := `
SELECT *
FROM t
WHERE col = {param:String}`
// If explicit parameters are provided in options, args are ignored for native parameters
resQuery, err := bindQueryOrAppendParameters(true, opts, query, time.UTC, Named("param", "arg_val"))
assert.NoError(t, err)
assert.Equal(t, query, resQuery)
assert.Equal(t, "explicit_val", opts.parameters["param"], "Explicit parameters should be preferred")
}