-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrows_test.go
169 lines (139 loc) · 4.6 KB
/
rows_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
166
167
168
169
package unixodbc
import (
"context"
"database/sql/driver"
"errors"
"github.com/golang/mock/gomock"
"github.com/ninthclowd/unixodbc/internal/mocks"
"io"
"reflect"
"testing"
)
func testRows(t *testing.T) (ctrl *gomock.Controller, rows *Rows, mockRS *mocks.MockRecordSet, mockStmt *mocks.MockStatement, ctx context.Context) {
ctrl = gomock.NewController(t)
ctx = context.Background()
mockRS = mocks.NewMockRecordSet(ctrl)
mockStmt = mocks.NewMockStatement(ctrl)
rows = &Rows{
ctx: ctx,
odbcRecordset: mockRS,
closeStmtOnRSClose: mockStmt,
}
return
}
func TestRows_ColumnTypePrecisionScale(t *testing.T) {
ctrl, rows, mockRS, _, _ := testRows(t)
defer ctrl.Finish()
wantPrecision := int64(1)
wantOk := true
wantScale := int64(10)
mockCol := mocks.NewMockColumn(ctrl)
mockCol.EXPECT().Decimal().Return(wantPrecision, wantScale, wantOk).Times(1)
mockRS.EXPECT().Column(1).Return(mockCol)
gotPrecision, gotScale, gotOk := rows.ColumnTypePrecisionScale(1)
if gotPrecision != wantPrecision {
t.Errorf("got precision %v, want %v", gotPrecision, wantPrecision)
}
if gotOk != wantOk {
t.Errorf("got ok %v, want %v", gotOk, wantOk)
}
if gotScale != wantScale {
t.Errorf("got scale %v, want %v", gotScale, wantScale)
}
}
func TestRows_ColumnTypeScanType(t *testing.T) {
ctrl, rows, mockRS, _, _ := testRows(t)
defer ctrl.Finish()
wantScanType := reflect.TypeOf("string")
mockCol := mocks.NewMockColumn(ctrl)
mockCol.EXPECT().ScanType().Return(wantScanType).Times(1)
mockRS.EXPECT().Column(1).Return(mockCol)
gotScanType := rows.ColumnTypeScanType(1)
if gotScanType != wantScanType {
t.Errorf("got scanType %v, want %v", gotScanType, wantScanType)
}
}
func TestRows_ColumnTypeNullable(t *testing.T) {
ctrl, rows, mockRS, _, _ := testRows(t)
defer ctrl.Finish()
wantNullable := true
wantNullableOk := true
mockCol := mocks.NewMockColumn(ctrl)
mockCol.EXPECT().Nullable().Return(wantNullable, wantNullableOk).Times(1)
mockRS.EXPECT().Column(1).Return(mockCol)
gotNullable, gotNullableOk := rows.ColumnTypeNullable(1)
if gotNullable != wantNullable {
t.Errorf("got nullable %v, want %v", gotNullable, wantNullable)
}
if gotNullableOk != wantNullableOk {
t.Errorf("got nullable ok %v, want %v", gotNullableOk, wantNullableOk)
}
}
func TestRows_ColumnTypeLength(t *testing.T) {
ctrl, rows, mockRS, _, _ := testRows(t)
defer ctrl.Finish()
wantLength := int64(10)
wantLengthOk := true
mockCol := mocks.NewMockColumn(ctrl)
mockCol.EXPECT().VariableLength().Return(wantLength, wantLengthOk).Times(1)
mockRS.EXPECT().Column(1).Return(mockCol)
gotLength, gotLengthOk := rows.ColumnTypeLength(1)
if gotLength != wantLength {
t.Errorf("got length %v, want %v", gotLength, wantLength)
}
if gotLengthOk != wantLengthOk {
t.Errorf("got length ok %v, want %v", gotLengthOk, wantLengthOk)
}
}
func TestRows_Columns(t *testing.T) {
ctrl, rows, mockRS, _, _ := testRows(t)
defer ctrl.Finish()
wantColumns := []string{"one", "two"}
mockRS.EXPECT().ColumnNames().Return(wantColumns)
gotColumns := rows.Columns()
if !reflect.DeepEqual(gotColumns, wantColumns) {
t.Errorf("got columns %v, want %v", gotColumns, wantColumns)
}
}
func TestRows_Close(t *testing.T) {
ctrl, rows, mockRS, mockStmt, _ := testRows(t)
defer ctrl.Finish()
mockRS.EXPECT().Close().Return(nil).Times(1)
mockStmt.EXPECT().Close().Return(nil).Times(1)
gotErr := rows.Close()
if gotErr != nil {
t.Errorf("expected no error, got %v", gotErr)
}
}
func TestRows_Next(t *testing.T) {
ctrl, rows, mockRS, _, _ := testRows(t)
defer ctrl.Finish()
cols := []string{"col1", "col2"}
wantVal1 := int64(10)
wantVal2 := "stringVal"
mockCol1 := mocks.NewMockColumn(ctrl)
mockCol1.EXPECT().Name().Return(cols[0]).AnyTimes()
mockCol1.EXPECT().Value().Return(wantVal1, nil).AnyTimes()
mockRS.EXPECT().Column(0).Return(mockCol1).AnyTimes()
mockCol2 := mocks.NewMockColumn(ctrl)
mockCol2.EXPECT().Name().Return(cols[1]).AnyTimes()
mockCol2.EXPECT().Value().Return(wantVal2, nil).AnyTimes()
mockRS.EXPECT().Column(1).Return(mockCol2).AnyTimes()
mockRS.EXPECT().Fetch().Return(true, nil).Times(1)
gotRow1 := make([]driver.Value, 2)
gotRow1Err := rows.Next(gotRow1)
if gotRow1Err != nil {
t.Fatalf("expected no error, got %v", gotRow1Err)
}
if gotRow1[0] != wantVal1 {
t.Errorf("got value 1 %v, want %v", gotRow1[0], wantVal1)
}
if gotRow1[1] != wantVal2 {
t.Errorf("got value 2 %v, want %v", gotRow1[1], wantVal2)
}
mockRS.EXPECT().Fetch().Return(false, nil).Times(1)
gotRow2Err := rows.Next([]driver.Value{})
if !errors.Is(gotRow2Err, io.EOF) {
t.Fatalf("expected EOF on second fetch got %v", gotRow2Err)
}
}