-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_test.go
165 lines (148 loc) · 2.8 KB
/
sql_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
package optional
import (
"database/sql"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestScan(t *testing.T) {
t.Parallel()
t.Run("int64", func(t *testing.T) {
type data = Val[int64]
table := []struct {
in any
exp data
expErr bool
}{
0: {
in: nil,
exp: data{
hasVal: false,
value: 0,
},
expErr: false,
},
1: {
in: []byte("asdasdasd"),
exp: data{
hasVal: false,
value: 0,
},
expErr: true,
},
2: {
in: int64(0),
exp: data{
hasVal: true,
value: 0,
},
expErr: false,
},
3: {
in: int64(42),
exp: data{
hasVal: true,
value: 42,
},
expErr: false,
},
4: {
in: int64(42),
exp: data{
hasVal: true,
value: 42,
},
expErr: false,
},
}
for i := range table {
row := table[i]
t.Run("", func(t *testing.T) {
t.Parallel()
var res data
if err := res.Scan(row.in); row.expErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
assert.Equal(t, row.exp, res)
})
}
})
t.Run("sql_Scanner", func(t *testing.T) {
// this type is used to test scenario with Val[T].value.Scan
type data = Val[sql.NullBool]
table := []struct {
in any
exp data
}{
0: {
in: nil,
exp: data{
hasVal: true, // scanner of underlay type return no error. that means that input was successful parsed.
value: sql.NullBool{Bool: false, Valid: false},
},
},
1: {
in: true,
exp: data{
hasVal: true,
value: sql.NullBool{Bool: true, Valid: true},
},
},
2: {
in: false,
exp: data{
hasVal: true,
value: sql.NullBool{Bool: false, Valid: true},
},
},
3: {
in: []byte("false"),
exp: data{
hasVal: true,
value: sql.NullBool{Bool: false, Valid: true},
},
},
}
for i := range table {
row := table[i]
t.Run("", func(t *testing.T) {
t.Parallel()
var res data
err := res.Scan(row.in)
require.NoError(t, err)
assert.Equal(t, row.exp, res)
})
}
})
t.Run("sql_Scanner_bad_scenario", func(t *testing.T) {
var val Val[sql.NullBool]
err := val.Scan("not-valid-data")
require.Error(t, err)
})
}
func TestValue(t *testing.T) {
t.Parallel()
t.Run("empty", func(t *testing.T) {
t.Parallel()
val, err := Empty[int64]().Value()
require.NoError(t, err)
assert.Equal(t, nil, val)
})
t.Run("not_empty", func(t *testing.T) {
t.Parallel()
val, err := New[int64](42).Value()
require.NoError(t, err)
assert.Equal(t, int64(42), val)
})
t.Run("not_empty_valuer", func(t *testing.T) {
t.Parallel()
val, err := New[sql.NullBool](sql.NullBool{
Bool: true,
Valid: true,
}).Value()
require.NoError(t, err)
assert.Equal(t, true, val)
})
}