-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpmx_select_struct_test.go
84 lines (71 loc) · 2.26 KB
/
pmx_select_struct_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
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 SelectStructSuite struct {
suite.Suite
conn *pgx.Conn
}
func (s *SelectStructSuite) SetupSuite() {
s.conn = test.Connect(context.Background())
}
func (s *SelectStructSuite) TearDownSuite() {
s.NoError(s.conn.Close(context.Background()))
}
func TestSelectStruct(t *testing.T) {
suite.Run(t, new(SelectStructSuite))
}
func (s *SelectStructSuite) TestStructPointer() {
var projection test.Projection
err := pmx.Select(context.Background(), s.conn, &projection,
"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"},
}, projection)
s.NoError(err)
}
func (s *SelectStructSuite) TestNull() {
var projection test.Projection
err := pmx.Select(context.Background(), s.conn, &projection, "select $1 as id, $2 as name", "projection-id", nil)
s.Equal(test.Projection{ID: "projection-id"}, projection)
s.NoError(err)
}
func (s *SelectStructSuite) TestUnmapped() {
var projection test.Projection
err := pmx.Select(context.Background(), s.conn, &projection, "select $1 as id, $2 as unmapped", "projection-id", "x")
s.Equal(test.Projection{ID: "projection-id"}, projection)
s.NoError(err)
}
func (s *SelectStructSuite) TestNoRows() {
var projection test.Projection
err := pmx.Select(context.Background(), s.conn, &projection, "select 1 limit 0")
s.ErrorIs(err, pmx.ErrNoRows)
}
func (s *SelectStructSuite) TestStructValue() {
var projection test.Projection
err := pmx.Select(context.Background(), s.conn, projection, "select 1")
s.ErrorIs(err, pmx.ErrInvalidRef)
}
func (s *SelectStructSuite) TestMapPointer() {
projection := map[string]string{}
err := pmx.Select(context.Background(), s.conn, &projection, "select 1")
s.ErrorIs(err, pmx.ErrInvalidRef)
}
func (s *SelectStructSuite) TestMapValue() {
projection := map[string]string{}
err := pmx.Select(context.Background(), s.conn, projection, "select 1")
s.ErrorIs(err, pmx.ErrInvalidRef)
}