-
Notifications
You must be signed in to change notification settings - Fork 0
/
Conn_test.go
77 lines (73 loc) · 1.86 KB
/
Conn_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
package elasticsearch
import (
"database/sql/driver"
"reflect"
"testing"
)
func TestConn_Prepare(t *testing.T) {
type args struct {
query string
}
tests := []struct {
name string
c *Conn
args args
want driver.Stmt
wantErr bool
}{
{"Success", &Conn{"http://localhost:9200"}, args{"SELECT 'dummy' AS dummy"}, &Stmt{"http://localhost:9200", "SELECT 'dummy' AS dummy"}, false},
{"Fail: Closed connection", &Conn{}, args{"SELECT 'dummy' AS dummy"}, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.c.Prepare(tt.args.query)
if (err != nil) != tt.wantErr {
t.Errorf("Conn.Prepare() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Conn.Prepare() = %v, want %v", got, tt.want)
}
})
}
}
func TestConn_Close(t *testing.T) {
tests := []struct {
name string
c *Conn
wantErr bool
}{
{"Success", &Conn{"http://localhost:9200"}, false},
{"Fail: Closed connection", &Conn{""}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.c.Close(); (err != nil) != tt.wantErr {
t.Errorf("Conn.Close() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestConn_Begin(t *testing.T) {
tests := []struct {
name string
c *Conn
want driver.Tx
wantErr bool
}{
{"Fail: Elasticsearch does not support transaction", &Conn{"http://localhost:9200"}, nil, true},
{"Fail: Elasticsearch does not support transaction", &Conn{}, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.c.Begin()
if (err != nil) != tt.wantErr {
t.Errorf("Conn.Begin() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Conn.Begin() = %v, want %v", got, tt.want)
}
})
}
}