forked from marcboeker/go-duckdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors_test.go
237 lines (186 loc) · 6.49 KB
/
errors_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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package duckdb
import (
"context"
"database/sql"
"database/sql/driver"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func testErrorInternal(t *testing.T, actual error, contains []string) {
for _, msg := range contains {
require.Contains(t, actual.Error(), msg)
}
levels := strings.Count(actual.Error(), driverErrMsg)
require.Equal(t, 1, levels)
}
func testError(t *testing.T, actual error, contains ...string) {
testErrorInternal(t, actual, contains)
}
func TestErrOpen(t *testing.T) {
t.Run(errParseDSN.Error(), func(t *testing.T) {
_, err := sql.Open("duckdb", ":mem ory:")
testError(t, err, errParseDSN.Error())
})
t.Run(errOpen.Error(), func(t *testing.T) {
_, err := sql.Open("duckdb", "?readonly")
testError(t, err, errOpen.Error(), duckdbErrMsg)
})
t.Run(errSetConfig.Error(), func(t *testing.T) {
_, err := sql.Open("duckdb", "?threads=NaN")
testError(t, err, errSetConfig.Error())
})
t.Run("local config option", func(t *testing.T) {
_, err := sql.Open("duckdb", "?schema=main")
testError(t, err, errSetConfig.Error())
})
}
func TestErrAppender(t *testing.T) {
t.Run(errAppenderInvalidCon.Error(), func(t *testing.T) {
var con driver.Conn
_, err := NewAppenderFromConn(con, "", "test")
testError(t, err, errAppenderInvalidCon.Error())
})
t.Run(errAppenderClosedCon.Error(), func(t *testing.T) {
c, err := NewConnector("", nil)
require.NoError(t, err)
con, err := c.Connect(context.Background())
require.NoError(t, err)
require.NoError(t, con.Close())
_, err = NewAppenderFromConn(con, "", "test")
testError(t, err, errAppenderClosedCon.Error())
require.NoError(t, c.Close())
})
t.Run(errAppenderCreation.Error(), func(t *testing.T) {
c, err := NewConnector("", nil)
require.NoError(t, err)
con, err := c.Connect(context.Background())
require.NoError(t, err)
_, err = NewAppenderFromConn(con, "", "does_not_exist")
testError(t, err, errAppenderCreation.Error(), duckdbErrMsg)
require.NoError(t, con.Close())
require.NoError(t, c.Close())
})
t.Run(errAppenderDoubleClose.Error(), func(t *testing.T) {
c, err := NewConnector("", nil)
require.NoError(t, err)
_, err = sql.OpenDB(c).Exec(`CREATE TABLE tbl (i INTEGER)`)
require.NoError(t, err)
con, err := c.Connect(context.Background())
require.NoError(t, err)
a, err := NewAppenderFromConn(con, "", "tbl")
require.NoError(t, err)
cleanupAppender(t, c, con, a)
err = a.Close()
testError(t, err, errAppenderDoubleClose.Error())
})
t.Run(unsupportedTypeErrMsg, func(t *testing.T) {
c, err := NewConnector("", nil)
require.NoError(t, err)
_, err = sql.OpenDB(c).Exec(`CREATE TABLE test AS SELECT MAP() AS m`)
require.NoError(t, err)
con, err := c.Connect(context.Background())
require.NoError(t, err)
_, err = NewAppenderFromConn(con, "", "test")
testError(t, err, errAppenderCreation.Error(), unsupportedTypeErrMsg)
require.NoError(t, con.Close())
require.NoError(t, c.Close())
})
t.Run(columnCountErrMsg, func(t *testing.T) {
c, con, a := prepareAppender(t, `CREATE TABLE test (a VARCHAR, b VARCHAR)`)
err := a.AppendRow("hello")
testError(t, err, errAppenderAppendRow.Error(), columnCountErrMsg)
cleanupAppender(t, c, con, a)
})
t.Run(errAppenderAppendAfterClose.Error(), func(t *testing.T) {
c, con, a := prepareAppender(t, `CREATE TABLE test (str VARCHAR)`)
require.NoError(t, a.Close())
err := a.AppendRow("hello")
testError(t, err, errAppenderAppendAfterClose.Error())
require.NoError(t, con.Close())
require.NoError(t, c.Close())
})
}
func TestErrAppend(t *testing.T) {
c, con, a := prepareAppender(t, `CREATE TABLE test (id BIGINT, str VARCHAR)`)
err := a.AppendRow("hello", "world")
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
err = a.AppendRow(false, 42)
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
cleanupAppender(t, c, con, a)
}
func TestErrAppendSimpleStruct(t *testing.T) {
c, con, a := prepareAppender(t, `
CREATE TABLE test (
simple_struct STRUCT(A INT, B VARCHAR)
)`)
err := a.AppendRow(1)
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
err = a.AppendRow("hello")
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
type other struct {
S string
I int
}
err = a.AppendRow(other{"hello", 1})
testError(t, err, errAppenderAppendRow.Error(), structFieldErrMsg)
err = a.AppendRow(
wrappedSimpleStruct{
"hello there",
simpleStruct{A: 0, B: "one billion ducks"},
},
)
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
err = a.AppendRow(
wrappedStruct{
"hello there",
simpleStruct{A: 0, B: "one billion ducks"},
},
)
testError(t, err, errAppenderAppendRow.Error(), structFieldErrMsg)
cleanupAppender(t, c, con, a)
}
func TestErrAppendStruct(t *testing.T) {
c, con, a := prepareAppender(t, `
CREATE TABLE test (
mix STRUCT(A STRUCT(L VARCHAR[]), B STRUCT(L INT[])[])
)`)
err := a.AppendRow(simpleStruct{1, "hello"})
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
cleanupAppender(t, c, con, a)
}
func TestErrAppendList(t *testing.T) {
c, con, a := prepareAppender(t, `CREATE TABLE test(intSlice INT[])`)
err := a.AppendRow([]string{"foo", "bar", "baz"})
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
err = a.AppendRow([][]int32{{1, 2, 3}, {4, 5, 6}})
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
cleanupAppender(t, c, con, a)
}
func TestErrAppendStructWithList(t *testing.T) {
c, con, a := prepareAppender(t, `CREATE TABLE test (struct_with_list STRUCT(L INT[]))`)
err := a.AppendRow([]int32{1, 2, 3})
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
l := struct{ L []string }{L: []string{"a", "b", "c"}}
testError(t, a.AppendRow(l), errAppenderAppendRow.Error(), castErrMsg)
cleanupAppender(t, c, con, a)
}
func TestErrAppendNestedStruct(t *testing.T) {
c, con, a := prepareAppender(t, `
CREATE TABLE test (
wrapped_simple_struct STRUCT(A VARCHAR, B STRUCT(A INT, B VARCHAR)),
)`)
err := a.AppendRow(simpleStruct{1, "hello"})
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
cleanupAppender(t, c, con, a)
}
func TestErrAppendNestedList(t *testing.T) {
c, con, a := prepareAppender(t, `CREATE TABLE test(int_slice INT[][][])`)
err := a.AppendRow([]int32{1, 2, 3})
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
err = a.AppendRow(1)
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
err = a.AppendRow([][]int32{{1, 2, 3}, {4, 5, 6}})
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
cleanupAppender(t, c, con, a)
}