-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils_test.go
71 lines (59 loc) · 1.52 KB
/
utils_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
package qry_test
import (
"database/sql"
"fmt"
"github.com/TomWright/qry"
"github.com/go-test/deep"
"testing"
)
func checkDiff(t *testing.T, exp any, got any) bool {
return checkDiffMsg(t, exp, got, "")
}
func checkDiffMsg(t *testing.T, exp any, got any, message string) bool {
if message != "" {
message += "\n"
}
if diffs := deep.Equal(exp, got); len(diffs) > 0 {
diffsMsg := ""
for _, diff := range diffs {
diffsMsg += fmt.Sprintf("- %s\n", diff)
}
t.Errorf("%sexpected:\n%v\ngot:\n%v\n%s", message, exp, got, diffsMsg)
return false
}
return true
}
func scanRowsToMapInterface(fields []qry.Field, rows *sql.Rows) ([]map[string]interface{}, error) {
defer func(rows *sql.Rows) {
_ = rows.Close()
}(rows)
res := make([]map[string]interface{}, 0)
for rows.Next() {
row, err := scanRowToMapInterface(fields, rows)
if err != nil {
return res, err
}
res = append(res, row)
}
return res, nil
}
func scanRowToMapInterface(fields []qry.Field, scanner qry.Scanner) (map[string]interface{}, error) {
numColumns := len(fields)
columns := make([]string, numColumns)
for index, column := range fields {
columns[index] = string(column)
}
values := make([]any, numColumns)
valuePointers := make([]any, numColumns)
for k := range values {
valuePointers[k] = &values[k]
}
if err := scanner.Scan(valuePointers...); err != nil {
return nil, fmt.Errorf("could not scan row: %v", err)
}
got := make(map[string]any)
for i, column := range columns {
got[column] = values[i]
}
return got, nil
}