forked from pashagolub/pgxmock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpectations_test.go
97 lines (87 loc) · 2.24 KB
/
expectations_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
package pgxmock
import (
"context"
"fmt"
"reflect"
"testing"
)
func ExampleExpectedExec() {
mock, _ := NewConn()
result := NewErrorResult(fmt.Errorf("some error"))
mock.ExpectExec("^INSERT (.+)").WillReturnResult(result)
res, _ := mock.Exec(context.Background(), "INSERT something")
s := res.String()
fmt.Println(s)
// Output: some error
}
func TestUnmonitoredPing(t *testing.T) {
mock, _ := NewConn()
p := mock.ExpectPing()
if p != nil {
t.Error("ExpectPing should return nil since MonitorPingsOption = false ")
}
}
func TestBuildQuery(t *testing.T) {
mock, _ := NewConn(MonitorPingsOption(true))
query := `
SELECT
name,
email,
address,
anotherfield
FROM user
where
name = 'John'
and
address = 'Jakarta'
`
mock.ExpectPing()
mock.ExpectQuery(query)
mock.ExpectExec(query)
mock.ExpectPrepare("foo", query)
_ = mock.Ping(context.Background())
mock.QueryRow(context.Background(), query)
_, _ = mock.Exec(context.Background(), query)
_, _ = mock.Prepare(context.Background(), "foo", query)
if err := mock.ExpectationsWereMet(); err != nil {
t.Error(err)
}
}
func TestQueryRowScan(t *testing.T) {
mock, _ := NewConn() //TODO New(ValueConverterOption(CustomConverter{}))
query := `
SELECT
name,
email,
address,
anotherfield
FROM user
where
name = 'John'
and
address = 'Jakarta'
`
expectedStringValue := "ValueOne"
expectedIntValue := 2
expectedArrayValue := []string{"Three", "Four"}
mock.ExpectQuery(query).WillReturnRows(mock.NewRows([]string{"One", "Two", "Three"}).AddRow(expectedStringValue, expectedIntValue, []string{"Three", "Four"}))
row := mock.QueryRow(context.Background(), query)
var stringValue string
var intValue int
var arrayValue []string
if e := row.Scan(&stringValue, &intValue, &arrayValue); e != nil {
t.Error(e)
}
if stringValue != expectedStringValue {
t.Errorf("Expectation %s does not met: %s", expectedStringValue, stringValue)
}
if intValue != expectedIntValue {
t.Errorf("Expectation %d does not met: %d", expectedIntValue, intValue)
}
if !reflect.DeepEqual(expectedArrayValue, arrayValue) {
t.Errorf("Expectation %v does not met: %v", expectedArrayValue, arrayValue)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Error(err)
}
}