-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rows_test.go
79 lines (72 loc) · 2.29 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
package elasticsearch
import (
"database/sql/driver"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestRows_Columns(t *testing.T) {
tests := []struct {
name string
r *Rows
want []string
}{
{"Success Col1", &Rows{columns: []string{"a"}}, []string{"a"}},
{"Success Col2", &Rows{columns: []string{"a", "c"}}, []string{"a", "c"}},
{"Success Col3", &Rows{columns: []string{"a", "b", "c"}}, []string{"a", "b", "c"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.Columns(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Rows.Columns() = %v, want %v", got, tt.want)
}
})
}
}
func TestRows_Close(t *testing.T) {
tests := []struct {
name string
r *Rows
wantErr bool
}{
{"Success", &Rows{dsn: "something"}, false},
{"Fail", &Rows{dsn: ""}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.r.Close(); (err != nil) != tt.wantErr {
t.Errorf("Rows.Close() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestRows_Next(t *testing.T) {
tsNextRows := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "{\"columns\":[{\"name\":\"a\",\"type\":\"integer\"},{\"name\":\"b\",\"type\":\"integer\"},{\"name\":\"c\",\"type\":\"integer\"}],\"rows\":[[1,2,3]]}")
}))
defer tsNextRows.Close()
type args struct {
dest []driver.Value
}
tests := []struct {
name string
r *Rows
args args
wantErr bool
}{
{"Fail: Closed connection", &Rows{}, args{[]driver.Value{}}, true},
{"Fail: Invalid argument value", &Rows{dsn: "http://localhost:9200"}, args{}, true},
{"Fail EOF", &Rows{dsn: "http://localhost:9200", cursor: ""}, args{[]driver.Value{}}, true},
{"Success bufferd", &Rows{dsn: "http://localhost:9200", columns: []string{"a", "b", "c"}, rows: [][]driver.Value{[]driver.Value{1, 2, 3}}}, args{[]driver.Value{}}, false},
{"Success nextRows", &Rows{dsn: tsNextRows.URL, columns: []string{"a", "b", "c"}, rows: [][]driver.Value{}, cursor: "cursor value"}, args{[]driver.Value{}}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.r.Next(tt.args.dest); (err != nil) != tt.wantErr {
t.Errorf("Rows.Next() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}