-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool_query_test.go
43 lines (35 loc) · 1.09 KB
/
pool_query_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
package pgxpoolgo_test
import (
"context"
"github.com/dalikewara/pgxpoolgo"
"github.com/stretchr/testify/assert"
"testing"
)
func poolQueryGetUserIDs(ctx context.Context, pool pgxpoolgo.Pool) ([]uint32, error) {
var ids []uint32
rows, err := pool.Query(ctx, `SELECT id FROM users`)
if err != nil {
return ids, err
}
defer rows.Close()
for rows.Next() {
var id uint32
if err = rows.Scan(&id); err != nil {
return ids, err
}
ids = append(ids, id)
}
return ids, nil
}
func TestPoolQueryGetUsersIDs_OK(t *testing.T) {
ctx := context.Background()
mockPool := pgxpoolgo.NewMockPool(t)
assert.Implements(t, (*pgxpoolgo.Pool)(nil), mockPool)
mockRows := pgxpoolgo.NewMockRows([]string{"id"}).AddRow(uint32(1)).AddRow(uint32(2)).AddRow(uint32(3)).Compose()
mockPool.On("Query", ctx, `SELECT id FROM users`).Return(mockRows, nil).Once()
ids, err := poolQueryGetUserIDs(ctx, mockPool)
assert.Equal(t, true, mockPool.AssertCalled(t, "Query", ctx, `SELECT id FROM users`))
assert.Equal(t, true, mockPool.AssertExpectations(t))
assert.Nil(t, err)
assert.Equal(t, []uint32{1, 2, 3}, ids)
}