-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcolumntype_test.go
143 lines (117 loc) · 3.42 KB
/
columntype_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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package godatabend
import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestColumnType(t *testing.T) {
tests := []struct {
typeDesc string
input string
want any
}{
{typeDesc: "String", input: "123", want: "123"},
{typeDesc: "Nullable(String)", input: "123", want: "123"},
{typeDesc: "Boolean", input: "1", want: true},
{typeDesc: "Int8", input: "123", want: int8(123)},
{typeDesc: "Int16", input: "123", want: int16(123)},
{typeDesc: "Int32", input: "123", want: int32(123)},
{typeDesc: "Int64", input: "123", want: int64(123)},
{typeDesc: "UInt8", input: "123", want: uint8(123)},
{typeDesc: "UInt16", input: "123", want: uint16(123)},
{typeDesc: "UInt32", input: "123", want: uint32(123)},
{typeDesc: "UInt64", input: "123", want: uint64(123)},
{typeDesc: "Float32", input: "123.0", want: float32(123)},
{typeDesc: "Float64", input: "123.0", want: float64(123)},
{typeDesc: "Timestamp", input: "2025-01-16 02:01:26.739219", want: time.Date(2025, 1, 16, 2, 1, 26, 739219000, time.UTC)},
{typeDesc: "Timestamp NULL", input: "NULL", want: time.Time{}},
{typeDesc: "Date", input: "2025-01-16", want: time.Date(2025, 1, 16, 0, 0, 0, 0, time.UTC)},
{typeDesc: "Decimal(10, 2)", input: "123.45", want: "123.45"},
}
for _, tc := range tests {
t.Run(fmt.Sprintf("%s::%s", tc.input, tc.typeDesc), func(t *testing.T) {
colType, err := NewColumnType(tc.typeDesc, nil)
require.NoError(t, err)
v, err := colType.Parse(tc.input)
require.NoError(t, err)
require.True(t, driver.IsValue(v))
if tc.want != nil {
require.Equal(t, reflect.TypeOf(tc.want).Name(), colType.ScanType().Name())
}
desc, err := ParseTypeDesc(tc.typeDesc)
require.NoError(t, err)
desc = desc.Normalize()
desc2, err := ParseTypeDesc(colType.DatabaseTypeName())
require.NoError(t, err)
require.Equal(t, desc, desc2)
runScan(t, tc.typeDesc, tc.input, tc.want)
})
}
}
func runScan(t *testing.T, desc string, input string, want any) {
db := sql.OpenDB(&fakeConnector{
resp: &QueryResponse{
Schema: &[]DataField{{Name: "x", Type: desc}},
Data: [][]*string{{&input}},
},
})
rows, err := db.Query("x")
require.NoError(t, err)
rows.Next()
types, err := rows.ColumnTypes()
require.NoError(t, err)
a := reflect.New(types[0].ScanType()).Interface()
rows.Scan(a)
require.Equal(t, want, reflect.ValueOf(a).Elem().Interface())
}
type fakeConnector struct {
resp *QueryResponse
}
func (c *fakeConnector) Driver() driver.Driver {
return nil
}
func (c *fakeConnector) Connect(ctx context.Context) (driver.Conn, error) {
return &fakeConn{c.resp}, nil
}
type fakeConn struct {
resp *QueryResponse
}
func (c *fakeConn) Prepare(query string) (driver.Stmt, error) {
return &fakeStmt{
resp: c.resp,
}, nil
}
func (c *fakeConn) Close() error {
return nil
}
func (c *fakeConn) Begin() (driver.Tx, error) {
return nil, nil
}
type fakeStmt struct {
resp *QueryResponse
}
func (s *fakeStmt) Close() error {
return nil
}
func (s *fakeStmt) Exec(args []driver.Value) (driver.Result, error) {
return nil, nil
}
func (s *fakeStmt) NumInput() int {
return 0
}
func (s *fakeStmt) Query(args []driver.Value) (driver.Rows, error) {
schema, err := parse_schema(s.resp.Schema, nil)
if err != nil {
return nil, err
}
return &nextRows{
dc: &DatabendConn{},
respData: s.resp,
resultSchema: *schema,
}, nil
}