forked from chrisyxlee/pgxpoolmock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
order_dao_example_test.go
141 lines (105 loc) · 3.65 KB
/
order_dao_example_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
package pgxpoolmock_test
import (
"context"
"fmt"
"testing"
"github.com/golang/mock/gomock"
"github.com/jackc/pgx/v5"
"github.com/spacetab-io/pgxpoolmock"
"github.com/spacetab-io/pgxpoolmock/sqlc"
"github.com/spacetab-io/pgxpoolmock/testdata"
"github.com/stretchr/testify/assert"
)
func TestName(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
// given
mockPool := pgxpoolmock.NewMockPgxIface(ctrl)
columns := []string{"id", "price"}
pgxRows := pgxpoolmock.NewRows(columns).AddRow(100, 100000.9).ToPgxRows()
mockPool.EXPECT().Query(gomock.Any(), gomock.Any(), gomock.Any()).Return(pgxRows, nil)
orderDao := testdata.OrderDAO{
Pool: mockPool,
}
// when
actualOrder := orderDao.GetOrderByID(1)
// then
assert.NotNil(t, actualOrder)
assert.Equal(t, 100, actualOrder.ID)
assert.Equal(t, 100000.9, actualOrder.Price)
}
func TestMap(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
// given
mockPool := pgxpoolmock.NewMockPgxIface(ctrl)
columns := []string{"id", "price"}
pgxRows := pgxpoolmock.NewRows(columns).AddRow(100, 100000.9).ToPgxRows()
assert.NotEqual(t, "with empty rows", fmt.Sprintf("%s", pgxRows))
mockPool.EXPECT().Query(gomock.Any(), gomock.Any(), gomock.Any()).Return(pgxRows, nil)
orderDao := testdata.OrderDAO{
Pool: mockPool,
}
// when
actualOrder := orderDao.GetOrderMapByID(1)
// then
assert.NotNil(t, actualOrder)
assert.Equal(t, 100, actualOrder["ID"])
assert.Equal(t, 100000.9, actualOrder["Price"])
}
func TestTx(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockPool := pgxpoolmock.NewMockPgxIface(ctrl)
// begin tx - given
mockPool.EXPECT().BeginTx(gomock.Any(), gomock.Any()).Return(mockPool, nil)
tx, err := mockPool.BeginTx(context.Background(), pgx.TxOptions{})
assert.NoError(t, err)
mockPool.EXPECT().QueryRow(gomock.Any(), "blah").Return(
pgxpoolmock.NewRow("1"))
row := tx.QueryRow(context.Background(), "blah")
var s string
err = row.Scan(&s)
assert.NoError(t, err)
assert.EqualValues(t, s, "1")
mockPool.EXPECT().Rollback(gomock.Any()).Return(nil)
assert.NoError(t, tx.Rollback(context.Background()))
}
func TestQueryMatcher(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockPool := pgxpoolmock.NewMockPgxIface(ctrl)
mockPool.EXPECT().QueryRow(gomock.Any(), pgxpoolmock.QueryContains("blah")).Return(
pgxpoolmock.NewRow("1"))
row := mockPool.QueryRow(context.Background(), "SELECT blah FROM some_table;")
var s string
err := row.Scan(&s)
assert.NoError(t, err)
}
func TestBatchResults(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockPool := pgxpoolmock.NewMockPgxIface(ctrl)
mockBatch := pgxpoolmock.NewMockBatchResults(ctrl)
// Unfortunately pgx.Batch isn't passed as an interface, so there's no way to mock the
// intermediate data. The best bet is probably to separate the data creation and handling
// into a separate function to test separately.
mockPool.EXPECT().SendBatch(gomock.Any(), gomock.Any()).Return(mockBatch)
mockBatch.EXPECT().QueryRow().Return(pgxpoolmock.NewRow(int32(1)))
mockBatch.EXPECT().QueryRow().Return(pgxpoolmock.NewRow(int32(2)))
mockBatch.EXPECT().QueryRow().Return(pgxpoolmock.NewRow(int32(3)))
mockBatch.EXPECT().QueryRow().Return(pgxpoolmock.NewRow(int32(0)).WithError(pgxpoolmock.ErrEndBatchResult))
q := sqlc.New(mockPool)
var inserted int32
q.InsertAuthors(context.Background(), []string{"a", "b", "c"}).QueryRow(func(i int, authorID int32, err error) {
inserted++
assert.Equal(t, inserted, authorID)
assert.NoError(t, err)
})
assert.Equal(t, int32(3), inserted)
}