-
Notifications
You must be signed in to change notification settings - Fork 2
/
transform_test.go
386 lines (325 loc) · 8.21 KB
/
transform_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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// Copyright 2021 taichi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package kra
import (
"context"
"crypto/rand"
"database/sql"
"fmt"
"math/big"
"testing"
"time"
"github.com/jackc/pgtype"
_ "github.com/jackc/pgx/v4/stdlib"
"github.com/stretchr/testify/assert"
)
type TestTable struct {
name string
create string
drop string
insert string
find string
findAll string
count string
core *Core
db *sql.DB
}
type fixture struct {
TestKey string `db:"test_key"`
TestValue string `db:"test_value"`
Len pgtype.Interval
Num *MyNum
}
type MyNum struct {
Value pgtype.Numeric
}
func (num *MyNum) Scan(value interface{}) error {
result := pgtype.Numeric{}
if err := result.Scan(value); err != nil {
return err
}
*num = MyNum{Value: result}
return nil
}
func setup(t *testing.T) (*TestTable, error) {
table := newTestTable()
table.core = NewCore(PostgreSQL)
rawDb, err := sql.Open("pgx", "user=test password=test host=localhost port=5432 database=test sslmode=disable")
if err != nil {
return nil, err
}
table.db = rawDb
if _, eErr := table.db.ExecContext(context.Background(), table.create); eErr != nil {
return nil, eErr
}
t.Cleanup(cleanup(t, table))
if err := insertData(t, table); err != nil {
return nil, err
}
return table, nil
}
func newTestTable() *TestTable {
b := make([]byte, 8)
if _, err := rand.Read(b); err != nil {
panic(err)
}
result := TestTable{
name: fmt.Sprintf("test_%s_%x", time.Now().Format("20060102_150405"), b),
}
result.create = fmt.Sprintf(`CREATE TABLE %s (
test_key VARCHAR,
test_value VARCHAR,
len interval hour to minute,
num numeric
);`, result.name)
result.drop = fmt.Sprintf("DROP TABLE IF EXISTS %s", result.name)
result.insert = fmt.Sprintf("INSERT INTO %s (test_key, test_value, len, num) VALUES ($1, $2, $3, 34)", result.name)
result.find = fmt.Sprintf("SELECT test_key, test_value as testValue, len, num FROM %s WHERE test_key= $1", result.name)
result.findAll = fmt.Sprintf("SELECT test_key, test_value as Testvalue, len FROM %s ORDER BY test_key", result.name)
result.count = fmt.Sprintf("SELECT COUNT(*) FROM %s", result.name)
return &result
}
func cleanup(t *testing.T, table *TestTable) func() {
return func() {
if _, err := table.db.ExecContext(context.Background(), table.drop); err != nil {
t.Error(err)
return
}
if err := table.db.Close(); err != nil {
t.Error(err)
}
}
}
func insertData(t *testing.T, table *TestTable) error {
data := [][]interface{}{
{"1111", "aaaa", pgtype.Interval{Microseconds: 5400000000, Status: pgtype.Present}},
{"2222", "bbbb", pgtype.Interval{Microseconds: 9000000000, Status: pgtype.Present}},
{"3333", "cccc", pgtype.Interval{Microseconds: 5400000000, Status: pgtype.Present}},
}
for _, fix := range data {
if res, err := table.db.ExecContext(context.Background(), table.insert, fix[0], fix[1], fix[2]); err != nil {
return err
} else if count, err := res.RowsAffected(); err != nil {
return err
} else {
assert.Equal(t, int64(1), count)
}
}
return nil
}
func TestDefaultTransformer_Transform_Scanner(t *testing.T) {
table, err := setup(t)
if err != nil {
t.Error(err)
return
}
query := fmt.Sprintf("SELECT len FROM %s WHERE test_key= $1", table.name) // nolint:gosec
rows, err := table.db.Query(query, "1111")
if err != nil {
t.Error(err)
return
}
if rows.Err() != nil {
t.Error(rows.Err())
return
}
defer rows.Close()
assert.True(t, rows.Next())
target := table.core.NewTransformer()
var result pgtype.Interval
if err := target.Transform(rows, &result); err != nil {
t.Error(err)
return
}
assert.Equal(t, pgtype.Interval{Microseconds: 5400000000, Status: pgtype.Present}, result)
}
func TestDefaultTransformer_Transform_Struct(t *testing.T) {
table, err := setup(t)
if err != nil {
t.Error(err)
return
}
rows, err := table.db.Query(table.find, "1111")
if err != nil {
t.Error(err)
return
}
if rows.Err() != nil {
t.Error(rows.Err())
return
}
defer rows.Close()
assert.True(t, rows.Next())
target := table.core.NewTransformer()
var result fixture
if err := target.Transform(rows, &result); err != nil {
t.Error(err)
return
}
assert.Equal(t, "1111", result.TestKey)
assert.Equal(t, pgtype.Interval{Microseconds: 5400000000, Status: pgtype.Present}, result.Len)
assert.Equal(t, &MyNum{pgtype.Numeric{Int: big.NewInt(34), Exp: 0, Status: pgtype.Present}}, result.Num)
}
func TestDefaultTransformer_Transform_Map(t *testing.T) {
table, err := setup(t)
if err != nil {
t.Error(err)
return
}
rows, err := table.db.Query(table.find, "1111")
if err != nil {
t.Error(err)
return
}
if rows.Err() != nil {
t.Error(rows.Err())
return
}
defer rows.Close()
assert.True(t, rows.Next())
target := table.core.NewTransformer()
var result map[string]interface{}
if err := target.Transform(rows, &result); err != nil {
t.Error(err)
return
}
assert.Equal(t, "1111", result["test_key"])
assert.Equal(t, "01:30:00", result["len"])
}
func TestDefaultTransformer_Transform_int(t *testing.T) {
table, err := setup(t)
if err != nil {
t.Error(err)
return
}
rows, err := table.db.Query(table.count)
if err != nil {
t.Error(err)
return
}
if rows.Err() != nil {
t.Error(rows.Err())
return
}
defer rows.Close()
assert.True(t, rows.Next())
target := table.core.NewTransformer()
var result int
if err := target.Transform(rows, &result); err != nil {
t.Error(err)
return
}
assert.Equal(t, 3, result)
}
func TestDefaultTransformer_TransformAll_Scanner(t *testing.T) {
table, err := setup(t)
if err != nil {
t.Error(err)
return
}
query := fmt.Sprintf("SELECT len FROM %s ORDER BY test_key", table.name) // nolint:gosec
rows, err := table.db.Query(query)
if err != nil {
t.Error(err)
return
}
if rows.Err() != nil {
t.Error(rows.Err())
return
}
defer rows.Close()
target := table.core.NewTransformer()
var result []pgtype.Interval
if err := target.TransformAll(rows, &result); err != nil {
t.Error(err)
return
}
assert.Equal(t, pgtype.Interval{Microseconds: 5400000000, Status: pgtype.Present}, result[0])
assert.Equal(t, 3, len(result))
}
func TestDefaultTransformer_TransformAll_String(t *testing.T) {
table, err := setup(t)
if err != nil {
t.Error(err)
return
}
query := fmt.Sprintf("SELECT test_key FROM %s ORDER BY test_key", table.name) // nolint:gosec
rows, err := table.db.Query(query)
if err != nil {
t.Error(err)
return
}
if rows.Err() != nil {
t.Error(rows.Err())
return
}
defer rows.Close()
target := table.core.NewTransformer()
var result []string
if err := target.TransformAll(rows, &result); err != nil {
t.Error(err)
return
}
assert.Equal(t, "1111", result[0])
assert.Equal(t, 3, len(result))
}
func TestDefaultTransformer_TransformAll_Struct(t *testing.T) {
table, err := setup(t)
if err != nil {
t.Error(err)
return
}
rows, err := table.db.Query(table.findAll)
if err != nil {
t.Error(err)
return
}
if rows.Err() != nil {
t.Error(rows.Err())
return
}
defer rows.Close()
target := table.core.NewTransformer()
var result []fixture
if err := target.TransformAll(rows, &result); err != nil {
t.Error(err)
return
}
assert.Equal(t, "1111", result[0].TestKey)
}
func TestDefaultTransformer_TransformAll_Map(t *testing.T) {
table, err := setup(t)
if err != nil {
t.Error(err)
return
}
rows, err := table.db.Query(table.findAll)
if err != nil {
t.Error(err)
return
}
if rows.Err() != nil {
t.Error(rows.Err())
return
}
defer rows.Close()
target := table.core.NewTransformer()
var result []map[string]interface{}
if err := target.TransformAll(rows, &result); err != nil {
t.Error(err)
return
}
assert.Equal(t, "1111", result[0]["test_key"])
assert.Equal(t, "01:30:00", result[0]["len"])
}