forked from minus5/gofreetds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
result_test.go
135 lines (119 loc) · 2.59 KB
/
result_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
package freetds
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var now = time.Now()
func testResult() *Result {
r := NewResult()
r.addColumn("i", 0, 0)
r.addColumn("s", 0, 0)
r.addColumn("tm", 0, 0)
r.addColumn("f", 0, 0)
r.addColumn("int", 0, 0)
r.addColumn("int8", 0, 0)
r.addColumn("int16", 0, 0)
r.addColumn("int32", 0, 0)
r.addColumn("int64", 0, 0)
r.addColumn("float32", 0, 0)
r.addColumn("float64", 0, 0)
for i := 0; i < 3; i++ {
r.addValue(i, 0, int32(i+1))
r.addValue(i, 1, "two")
r.addValue(i, 2, now)
r.addValue(i, 3, float64(123.45))
r.addValue(i, 4, int(1))
r.addValue(i, 5, int8(2))
r.addValue(i, 6, int16(3))
r.addValue(i, 7, int32(4))
r.addValue(i, 8, int64(5))
r.addValue(i, 9, float32(5.5))
r.addValue(i, 10, float64(6.5))
}
return r
}
func TestResultScan(t *testing.T) {
r := testResult()
var i int
var s string
var tm time.Time
var f float64
assert.True(t, r.Next())
err := r.Scan(&i, &s, &tm, &f)
assert.Nil(t, err)
assert.Equal(t, i, 1)
assert.Equal(t, s, "two")
assert.Equal(t, tm, now)
assert.Equal(t, f, float64(123.45))
}
func TestResultNext(t *testing.T) {
r := testResult()
assert.Equal(t, len(r.Rows), 3)
assert.True(t, r.Next())
assert.True(t, r.Next())
assert.True(t, r.Next())
assert.False(t, r.Next())
}
func TestResultScanWithoutNext(t *testing.T) {
r := testResult()
var i int
var s string
var tm time.Time
var f float64
err := r.Scan(&i, &s, &tm, &f)
assert.NotNil(t, err)
}
func TestResultScanOnNonPointerValues(t *testing.T) {
r := testResult()
var i int
var s string
var tm time.Time
var f float64
assert.True(t, r.Next())
err := r.Scan(&i, &s, &tm, f)
assert.NotNil(t, err) //error is raised
}
func TestResultScanIntoStruct(t *testing.T) {
r := testResult()
var s struct {
I int
S string
Tm time.Time
F float64
}
r.Next()
err := r.Scan(&s)
assert.Nil(t, err)
assert.Equal(t, s.I, 1)
assert.Equal(t, s.S, "two")
assert.Equal(t, s.Tm, now)
assert.Equal(t, s.F, float64(123.45))
assert.Equal(t, 4, r.scanCount)
err = r.MustScan(4, &s)
assert.Nil(t, err)
err = r.MustScan(5, &s)
assert.NotNil(t, err)
}
func TestScanTypesInStructDoesNotMatchThoseInResult(t *testing.T) {
r := testResult()
var s struct {
Int int
Int8 int
Int16 int
Int32 int
Int64 int
Float32 float64
Float64 float32
}
r.Next()
err := r.Scan(&s)
assert.Nil(t, err)
assert.Equal(t, s.Int, 1)
assert.Equal(t, s.Int8, 2)
assert.Equal(t, s.Int16, 3)
assert.Equal(t, s.Int32, 4)
assert.Equal(t, s.Int64, 5)
assert.Equal(t, s.Float32, 5.5)
assert.EqualValues(t, s.Float64, 6.5)
}