-
Notifications
You must be signed in to change notification settings - Fork 19
/
examples_test.go
92 lines (60 loc) · 1.91 KB
/
examples_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
package gockle
import (
"fmt"
"github.com/maraino/go-mock"
)
var mySession = &SessionMock{}
func ExampleIterator_ScanMap() {
var iteratorMock = &IteratorMock{}
iteratorMock.When("ScanMap", mock.Any).Call(func(m map[string]interface{}) bool {
m["id"] = 1
m["name"] = "alex"
return false
})
iteratorMock.When("Close").Return(nil)
var sessionMock = &SessionMock{}
const query = "select * from users"
sessionMock.When("ScanIterator", query, mock.Any).Return(iteratorMock)
sessionMock.When("Close").Return()
var session Session = sessionMock
var iterator = session.ScanIterator(query)
var row = map[string]interface{}{}
for more := true; more; {
more = iterator.ScanMap(row)
fmt.Printf("id = %v, name = %v\n", row["id"], row["name"])
}
if err := iterator.Close(); err != nil {
fmt.Println(err)
}
session.Close()
// Output: id = 1, name = alex
}
func ExampleSession_Batch() {
var batchMock = &BatchMock{}
batchMock.When("Add", "insert into users (id, name) values (1, 'alex')", mock.Any).Return()
batchMock.When("Exec").Return(fmt.Errorf("invalid"))
var sessionMock = &SessionMock{}
sessionMock.When("Batch", BatchLogged).Return(batchMock)
sessionMock.When("Close").Return()
var session Session = sessionMock
var batch = session.Batch(BatchLogged)
batch.Add("insert into users (id, name) values (1, 'alex')")
if err := batch.Exec(); err != nil {
fmt.Println(err)
}
session.Close()
// Output: invalid
}
func ExampleSession_ScanMapSlice() {
var sessionMock = &SessionMock{}
const query = "select * from users"
sessionMock.When("ScanMapSlice", query, mock.Any).Return([]map[string]interface{}{{"id": 1, "name": "alex"}}, nil)
sessionMock.When("Close").Return()
var session Session = sessionMock
var rows, _ = session.ScanMapSlice(query)
for _, row := range rows {
fmt.Printf("id = %v, name = %v\n", row["id"], row["name"])
}
session.Close()
// Output: id = 1, name = alex
}