@@ -2,14 +2,17 @@ package impl
2
2
3
3
import (
4
4
"database/sql"
5
+ "database/sql/driver"
5
6
"encoding/json"
6
7
"reflect"
7
8
"testing"
8
9
9
10
"github.com/domonda/go-types/nullable"
11
+ "github.com/lib/pq"
12
+ "github.com/stretchr/testify/assert"
10
13
)
11
14
12
- func TestShouldWrapForArray (t * testing.T ) {
15
+ func TestShouldWrapForArrayScanning (t * testing.T ) {
13
16
tests := []struct {
14
17
v reflect.Value
15
18
want bool
@@ -21,14 +24,66 @@ func TestShouldWrapForArray(t *testing.T) {
21
24
{v : reflect .ValueOf (json .RawMessage ([]byte ("null" ))), want : false },
22
25
{v : reflect .ValueOf (nullable .JSON ([]byte ("null" ))), want : false },
23
26
{v : reflect .ValueOf (new (sql.NullInt64 )).Elem (), want : false },
27
+ {v : reflect .ValueOf (WrapForArrayScanning ([]int {0 , 1 })), want : false },
24
28
25
29
{v : reflect .ValueOf (new ([3 ]string )).Elem (), want : true },
26
30
{v : reflect .ValueOf (new ([]string )).Elem (), want : true },
27
31
{v : reflect .ValueOf (new ([]sql.NullString )).Elem (), want : true },
28
32
}
29
33
for _ , tt := range tests {
30
- if got := ShouldWrapForArray (tt .v ); got != tt .want {
31
- t .Errorf ("shouldWrapArray() = %v, want %v" , got , tt .want )
32
- }
34
+ got := ShouldWrapForArrayScanning (tt .v )
35
+ assert .Equal (t , tt .want , got )
33
36
}
34
37
}
38
+
39
+ func TestIsNonDriverValuerSliceOrArray (t * testing.T ) {
40
+ tests := []struct {
41
+ t reflect.Type
42
+ want bool
43
+ }{
44
+ {t : reflect .TypeOf (nil ), want : false },
45
+ {t : reflect .TypeOf (0 ), want : false },
46
+ {t : reflect .TypeOf (new (int )), want : false },
47
+ {t : reflect .TypeOf ("string" ), want : false },
48
+ {t : reflect .TypeOf ([]byte ("string" )), want : false },
49
+ {t : reflect .TypeOf (new ([]byte )), want : false },
50
+ {t : reflect .TypeOf (pq.BoolArray {true }), want : false },
51
+ {t : reflect .TypeOf (new (pq.BoolArray )), want : false },
52
+ {t : reflect .TypeOf (new (* []int )), want : false }, // pointer to a pointer to a slice
53
+ {t : reflect .TypeOf ((* driver .Valuer )(nil )), want : false },
54
+ {t : reflect .TypeOf ((* driver .Valuer )(nil )).Elem (), want : false },
55
+
56
+ {t : reflect .TypeOf ([3 ]int {1 , 2 , 3 }), want : true },
57
+ {t : reflect .TypeOf ((* [3 ]int )(nil )), want : true },
58
+ {t : reflect .TypeOf ([]int {1 , 2 , 3 }), want : true },
59
+ {t : reflect .TypeOf ((* []int )(nil )), want : true },
60
+ {t : reflect .TypeOf ((* [][]byte )(nil )), want : true },
61
+ }
62
+ for _ , tt := range tests {
63
+ got := IsNonDriverValuerSliceOrArray (tt .t )
64
+ assert .Equalf (t , tt .want , got , "IsNonDriverValuerSliceOrArray(%s)" , tt .t )
65
+ }
66
+ }
67
+
68
+ // func TestWrapArgsForArrays(t *testing.T) {
69
+ // tests := []struct {
70
+ // args []any
71
+ // want []any
72
+ // }{
73
+ // {args: nil, want: nil},
74
+ // {args: []any{}, want: []any{}},
75
+ // {args: []any{0}, want: []any{0}},
76
+ // {args: []any{nil}, want: []any{nil}},
77
+ // {args: []any{new(int)}, want: []any{new(int)}},
78
+ // {args: []any{0, []int{0, 1}, "string"}, want: []any{0, wrapArgForArray([]int{0, 1}), "string"}},
79
+ // {args: []any{wrapArgForArray([]int{0, 1})}, want: []any{wrapArgForArray([]int{0, 1})}},
80
+ // {args: []any{[]byte("don't wrap []byte")}, want: []any{[]byte("don't wrap []byte")}},
81
+ // {args: []any{pq.BoolArray{true}}, want: []any{pq.BoolArray{true}}},
82
+ // {args: []any{[3]int{1, 2, 3}}, want: []any{wrapArgForArray([3]int{1, 2, 3})}},
83
+ // {args: []any{wrapArgForArray([3]int{1, 2, 3})}, want: []any{wrapArgForArray([3]int{1, 2, 3})}},
84
+ // }
85
+ // for _, tt := range tests {
86
+ // got := WrapArgsForArrays(tt.args)
87
+ // assert.Equal(t, tt.want, got)
88
+ // }
89
+ // }
0 commit comments