-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpmx_insert_test.go
105 lines (86 loc) · 2.54 KB
/
pmx_insert_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
package pmx_test
import (
"context"
"testing"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/stretchr/testify/suite"
"github.com/wcamarao/pmx"
"github.com/wcamarao/pmx/test"
)
type InsertSuite struct {
suite.Suite
conn *pgx.Conn
}
func (s *InsertSuite) SetupSuite() {
s.conn = test.Connect(context.Background())
}
func (s *InsertSuite) TearDownSuite() {
s.NoError(s.conn.Close(context.Background()))
}
func TestInsert(t *testing.T) {
suite.Run(t, new(InsertSuite))
}
func (s *InsertSuite) TestStructPointer() {
projection := test.Projection{
ID: "projection-id",
Name: "projection-name",
Metadata: map[string]int{"index": 1},
Slice: []string{"value"},
}
tag, err := pmx.Insert(context.Background(), s.conn, &projection)
s.Equal(pgconn.NewCommandTag("INSERT 0 1"), tag)
s.NoError(err)
var id, name string
var metadata map[string]int
var slice []string
row := s.conn.QueryRow(context.Background(), "select * from projections where id = $1", "projection-id")
err = row.Scan(&id, &name, &metadata, &slice)
s.NoError(err)
s.Equal("projection-id", id)
s.Equal("projection-name", name)
s.Equal(map[string]int{"index": 1}, metadata)
s.Equal([]string{"value"}, slice)
}
func (s *InsertSuite) TestAutoGenerated() {
event := test.Event{
RecordedBy: "recorded-by-user-id",
}
tag, err := pmx.Insert(context.Background(), s.conn, &event)
s.Equal(pgconn.NewCommandTag("INSERT 0 1"), tag)
s.NoError(err)
s.EqualValues(42, event.Position)
s.Equal(time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), event.RecordedAt)
s.Equal("recorded-by-user-id", event.RecordedBy)
}
func (s *InsertSuite) TestUniqueViolation() {
projection := test.Projection{
ID: "unique-violation-id",
}
tag, err := pmx.Insert(context.Background(), s.conn, &projection)
s.Equal(pgconn.NewCommandTag("INSERT 0 1"), tag)
s.NoError(err)
tag, err = pmx.Insert(context.Background(), s.conn, &projection)
s.Zero(tag)
s.Error(err)
s.True(pmx.UniqueViolation(err))
}
func (s *InsertSuite) TestStructValue() {
var projection test.Projection
tag, err := pmx.Insert(context.Background(), s.conn, projection)
s.Zero(tag)
s.ErrorIs(err, pmx.ErrInvalidRef)
}
func (s *InsertSuite) TestMapPointer() {
var projection map[string]string
tag, err := pmx.Insert(context.Background(), s.conn, &projection)
s.Zero(tag)
s.ErrorIs(err, pmx.ErrInvalidRef)
}
func (s *InsertSuite) TestMapValue() {
var projection map[string]string
tag, err := pmx.Insert(context.Background(), s.conn, projection)
s.Zero(tag)
s.ErrorIs(err, pmx.ErrInvalidRef)
}