-
Notifications
You must be signed in to change notification settings - Fork 18
/
order_dao_example_test.go
100 lines (85 loc) · 2.52 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
package pgxpoolmock_test
import (
"fmt"
"testing"
"github.com/driftprogramming/pgxpoolmock"
"github.com/driftprogramming/pgxpoolmock/testdata"
"github.com/golang/mock/gomock"
"github.com/jackc/pgconn"
"github.com/stretchr/testify/assert"
)
func TestInsertOrder(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockPool := pgxpoolmock.NewMockPgxPool(ctrl)
commandTag := pgconn.CommandTag("INSERT 0 1")
mockPool.EXPECT().Exec(gomock.Any(), "insert into order (id, price) values ($1, $2)", 1, 2.3).Return(commandTag, nil)
orderDao := testdata.OrderDAO{
Pool: mockPool,
}
order := &testdata.Order{
ID: 1,
Price: 2.3,
}
err := orderDao.InsertOrder(order)
if err != nil {
t.Fatalf("%v", err)
}
}
func TestGetOneOrderByID(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockPool := pgxpoolmock.NewMockPgxPool(ctrl)
columns := []string{"id", "price"}
pgxRow := pgxpoolmock.NewRow(columns, 1, 2.3)
mockPool.EXPECT().QueryRow(gomock.Any(), "select id, price from order where id = $1", 1).Return(pgxRow)
orderDao := testdata.OrderDAO{
Pool: mockPool,
}
actualOrder, err := orderDao.GetOneOrderByID(1)
if err != nil {
t.Fatalf("%v", err)
}
assert.NotNil(t, actualOrder)
assert.Equal(t, 1, actualOrder.ID)
assert.Equal(t, 2.3, actualOrder.Price)
}
func TestName(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
// given
mockPool := pgxpoolmock.NewMockPgxPool(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.NewMockPgxPool(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"])
}