forked from pashagolub/pgxmock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expectations_test.go
154 lines (139 loc) · 3.94 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
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
package pgxmock
import (
"context"
"errors"
"fmt"
"reflect"
"testing"
"time"
"github.com/jackc/pgx/v5"
)
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 TestUnexpectedPing(t *testing.T) {
mock, _ := NewConn(MonitorPingsOption(true))
err := mock.Ping(context.Background())
if err == nil {
t.Error("Ping should return error for unexpected call")
}
mock.ExpectExec("foo")
err = mock.Ping(context.Background())
if err == nil {
t.Error("Ping should return error for unexpected call")
}
}
func TestUnexpectedPrepare(t *testing.T) {
mock, _ := NewConn()
_, err := mock.Prepare(context.Background(), "foo", "bar")
if err == nil {
t.Error("Prepare should return error for unexpected call")
}
mock.ExpectExec("foo")
_, err = mock.Prepare(context.Background(), "foo", "bar")
if err == nil {
t.Error("Prepare should return error for unexpected call")
}
}
func TestUnexpectedCopyFrom(t *testing.T) {
mock, _ := NewConn()
_, err := mock.CopyFrom(context.Background(), pgx.Identifier{"schema", "table"}, []string{"foo", "bar"}, nil)
if err == nil {
t.Error("CopyFrom should return error for unexpected call")
}
mock.ExpectExec("foo")
_, err = mock.CopyFrom(context.Background(), pgx.Identifier{"schema", "table"}, []string{"foo", "bar"}, nil)
if err == nil {
t.Error("CopyFrom should return error for unexpected call")
}
}
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().WillDelayFor(1 * time.Second).WillReturnError(errors.New("no ping please"))
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)
}
}
func TestMissingWithArgs(t *testing.T) {
mock, _ := NewConn()
// No arguments expected
mock.ExpectExec("INSERT something")
// Receiving argument
_, err := mock.Exec(context.Background(), "INSERT something", "something")
if err == nil {
t.Error("arguments do not match error was expected")
}
if err := mock.ExpectationsWereMet(); err == nil {
t.Error("expectation was not matched error was expected")
}
}