-
Notifications
You must be signed in to change notification settings - Fork 11
/
sql_test.go
120 lines (91 loc) · 3.77 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
package nan
import (
"database/sql"
"database/sql/driver"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type sqlNanCoder interface {
sql.Scanner
Value() (driver.Value, error)
}
func doSQLTest(t *testing.T, val1, val2 interface{}, nanVal1, nanVal2 sqlNanCoder) {
val, err := nanVal1.Value()
assert.NoError(t, err)
assert.EqualValues(t, val1, val) // sql.Null* types return int64 for all integer types
assert.NoError(t, nanVal2.Scan(val2))
val, err = nanVal2.Value()
assert.NoError(t, err)
assert.EqualValues(t, val2, val)
}
func TestSQLNullBool(t *testing.T) {
v1, v2 := true, true
doSQLTest(t, v1, v2, &NullBool{Bool: v1, Valid: true}, &NullBool{})
v1, v2 = false, false
doSQLTest(t, v1, v2, &NullBool{Bool: v1, Valid: true}, &NullBool{})
doSQLTest(t, nil, nil, &NullBool{Valid: false}, &NullBool{})
doSQLTest(t, nil, nil, &NullBool{Valid: false}, &NullBool{Valid: true})
}
func TestSQLNullFloat32(t *testing.T) {
v1, v2 := float32(7676.76), float32(7676.76)
doSQLTest(t, v1, v2, &NullFloat32{Float32: v1, Valid: true}, &NullFloat32{})
doSQLTest(t, nil, nil, &NullFloat32{Valid: false}, &NullFloat32{})
doSQLTest(t, nil, nil, &NullFloat32{Valid: false}, &NullFloat32{Valid: true})
}
func TestSQLNullFloat64(t *testing.T) {
v1, v2 := float64(7676.76), float64(7676.76)
doSQLTest(t, v1, v2, &NullFloat64{Float64: v1, Valid: true}, &NullFloat64{})
doSQLTest(t, nil, nil, &NullFloat64{Valid: false}, &NullFloat64{})
doSQLTest(t, nil, nil, &NullFloat64{Valid: false}, &NullFloat64{Valid: true})
}
func TestSQLNullInt(t *testing.T) {
v1, v2 := int(7676), int(7676)
doSQLTest(t, v1, v2, &NullInt{Int: v1, Valid: true}, &NullInt{})
doSQLTest(t, nil, nil, &NullInt{Valid: false}, &NullInt{})
doSQLTest(t, nil, nil, &NullInt{Valid: false}, &NullInt{Valid: true})
}
func TestSQLNullInt8(t *testing.T) {
v1, v2 := int8(76), int8(76)
doSQLTest(t, v1, v2, &NullInt8{Int8: v1, Valid: true}, &NullInt8{})
doSQLTest(t, nil, nil, &NullInt8{Valid: false}, &NullInt8{})
doSQLTest(t, nil, nil, &NullInt8{Valid: false}, &NullInt8{Valid: true})
}
func TestSQLNullInt16(t *testing.T) {
v1, v2 := int16(7676), int16(7676)
doSQLTest(t, v1, v2, &NullInt16{Int16: v1, Valid: true}, &NullInt16{})
doSQLTest(t, nil, nil, &NullInt16{Valid: false}, &NullInt16{})
doSQLTest(t, nil, nil, &NullInt16{Valid: false}, &NullInt16{Valid: true})
}
func TestSQLNullInt32(t *testing.T) {
v1, v2 := int32(7676), int32(7676)
doSQLTest(t, v1, v2, &NullInt32{Int32: v1, Valid: true}, &NullInt32{})
doSQLTest(t, nil, nil, &NullInt32{Valid: false}, &NullInt32{})
doSQLTest(t, nil, nil, &NullInt32{Valid: false}, &NullInt32{Valid: true})
}
func TestSQLNullInt64(t *testing.T) {
v1, v2 := int64(7676), int64(7676)
doSQLTest(t, v1, v2, &NullInt64{Int64: v1, Valid: true}, &NullInt64{})
doSQLTest(t, nil, nil, &NullInt64{Valid: false}, &NullInt64{})
doSQLTest(t, nil, nil, &NullInt64{Valid: false}, &NullInt64{Valid: true})
}
func TestSQLNullString(t *testing.T) {
v1, v2 := "7676", "7676"
doSQLTest(t, v1, v2, &NullString{String: v1, Valid: true}, &NullString{})
v1, v2 = "", ""
doSQLTest(t, v1, v2, &NullString{String: v1, Valid: true}, &NullString{})
doSQLTest(t, nil, nil, &NullString{Valid: false}, &NullString{})
doSQLTest(t, nil, nil, &NullString{Valid: false}, &NullString{Valid: true})
}
func TestSQLNullTime(t *testing.T) {
v1, v2 := time.Now().UTC(), time.Now().UTC()
doSQLTest(t, v1, v2, &NullTime{Time: v1, Valid: true}, &NullTime{})
v1, v2 = time.Time{}, time.Time{}
doSQLTest(t, v1, v2, &NullTime{Valid: true}, &NullTime{})
doSQLTest(t, nil, nil, &NullTime{Valid: false}, &NullTime{})
doSQLTest(t, nil, nil, &NullTime{Valid: false}, &NullTime{Valid: true})
}
func TestSQLInitialTemplateType(t *testing.T) {
v1, v2 := initialTemplateType(""), initialTemplateType("")
doSQLTest(t, v1, v2, &v1, &v2)
}