-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpmx_select_slice_test.go
91 lines (77 loc) · 2.54 KB
/
pmx_select_slice_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
package pmx_test
import (
"context"
"testing"
"github.com/jackc/pgx/v5"
"github.com/stretchr/testify/suite"
"github.com/wcamarao/pmx"
"github.com/wcamarao/pmx/test"
)
type SelectSliceSuite struct {
suite.Suite
conn *pgx.Conn
}
func (s *SelectSliceSuite) SetupSuite() {
s.conn = test.Connect(context.Background())
}
func (s *SelectSliceSuite) TearDownSuite() {
s.NoError(s.conn.Close(context.Background()))
}
func TestSelectSlice(t *testing.T) {
suite.Run(t, new(SelectSliceSuite))
}
func (s *SelectSliceSuite) TestSlicePointer() {
var projections []*test.Projection
err := pmx.Select(context.Background(), s.conn, &projections,
"select $1 as id, $2 as name, $3::jsonb as metadata, $4::jsonb as slice",
"projection-id",
"projection-name",
map[string]int{"index": 1},
[]string{"value"},
)
s.Equal([]*test.Projection{{
ID: "projection-id",
Name: "projection-name",
Metadata: map[string]int{"index": 1},
Slice: []string{"value"},
}}, projections)
s.NoError(err)
}
func (s *SelectSliceSuite) TestNull() {
var projections []*test.Projection
err := pmx.Select(context.Background(), s.conn, &projections, "select $1 as id, $2 as name", "projection-id", nil)
s.Equal([]*test.Projection{{ID: "projection-id"}}, projections)
s.NoError(err)
}
func (s *SelectSliceSuite) TestUnmapped() {
var projections []*test.Projection
err := pmx.Select(context.Background(), s.conn, &projections, "select $1 as id, $2 as unmapped", "projection-id", "x")
s.Equal([]*test.Projection{{ID: "projection-id"}}, projections)
s.NoError(err)
}
func (s *SelectSliceSuite) TestNoRows() {
var projections []*test.Projection
err := pmx.Select(context.Background(), s.conn, &projections, "select 1 limit 0")
s.Empty(projections)
s.NoError(err)
}
func (s *SelectSliceSuite) TestSliceValue() {
var projections []*test.Projection
err := pmx.Select(context.Background(), s.conn, projections, "select 1")
s.ErrorIs(err, pmx.ErrInvalidRef)
}
func (s *SelectSliceSuite) TestSlicePointerOfStructValue() {
var projections []test.Projection
err := pmx.Select(context.Background(), s.conn, &projections, "select 1")
s.ErrorIs(err, pmx.ErrInvalidRef)
}
func (s *SelectSliceSuite) TestSlicePointerOfMapPointer() {
var projections []*map[string]string
err := pmx.Select(context.Background(), s.conn, &projections, "select 1")
s.ErrorIs(err, pmx.ErrInvalidRef)
}
func (s *SelectSliceSuite) TestSlicePointerOfMapValue() {
var projections []map[string]string
err := pmx.Select(context.Background(), s.conn, &projections, "select 1")
s.ErrorIs(err, pmx.ErrInvalidRef)
}