-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathresult_rows_test.go
More file actions
101 lines (90 loc) · 2.48 KB
/
Copy pathresult_rows_test.go
File metadata and controls
101 lines (90 loc) · 2.48 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
package golake
import (
"encoding/json"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDecodeJSONResponseMaterializesTypedRows(t *testing.T) {
resp := QueryResponse{
ID: "json-query",
Settings: &Settings{
TimeZone: "Asia/Shanghai",
},
Schema: &[]DataField{
{Name: "n", Type: "Int32"},
{Name: "name", Type: "String"},
{Name: "ts", Type: "Timestamp"},
},
Data: [][]*string{{
strPtr("7"),
strPtr("alice"),
strPtr("2025-01-16 10:01:26.739219"),
}},
}
body, err := json.Marshal(resp)
require.NoError(t, err)
decoded, err := decodeQueryResponse(&rawHTTPResponse{
headers: http.Header{contentType: []string{jsonContentType}},
body: body,
})
require.NoError(t, err)
require.Len(t, decoded.typedRows, 1)
require.Len(t, decoded.typedRows[0], 3)
assert.Equal(t, "7", decoded.typedRows[0][0])
assert.Equal(t, "alice", decoded.typedRows[0][1])
ts, ok := decoded.typedRows[0][2].(time.Time)
require.True(t, ok)
loc, err := time.LoadLocation("Asia/Shanghai")
require.NoError(t, err)
assert.Equal(t, time.Date(2025, 1, 16, 10, 1, 26, 739219000, loc), ts)
}
func TestDecodeJSONResponseMaterializesBinaryRows(t *testing.T) {
testCases := []struct {
name string
settings *Settings
input string
want []byte
}{
{
name: "driver-mode-hex",
settings: &Settings{BinaryOutputFormat: "BASE64", HTTPJSONResultMode: "driver"},
input: "616263",
want: []byte("abc"),
},
{
name: "display-mode-base64",
settings: &Settings{BinaryOutputFormat: "BASE64", HTTPJSONResultMode: "display"},
input: "YWJj",
want: []byte("abc"),
},
{
name: "display-mode-utf8",
settings: &Settings{BinaryOutputFormat: "UTF-8", HTTPJSONResultMode: "display"},
input: "abc",
want: []byte("abc"),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
resp := QueryResponse{
ID: "json-binary-query",
Settings: tc.settings,
Schema: &[]DataField{{Name: "b", Type: "Binary"}},
Data: [][]*string{{strPtr(tc.input)}},
}
body, err := json.Marshal(resp)
require.NoError(t, err)
decoded, err := decodeQueryResponse(&rawHTTPResponse{
headers: http.Header{contentType: []string{jsonContentType}},
body: body,
})
require.NoError(t, err)
require.Len(t, decoded.typedRows, 1)
require.Len(t, decoded.typedRows[0], 1)
assert.Equal(t, tc.want, decoded.typedRows[0][0])
})
}
}