-
Notifications
You must be signed in to change notification settings - Fork 0
/
persister_test.go
99 lines (81 loc) · 2.19 KB
/
persister_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
package outbox
import (
"context"
"database/sql"
"testing"
"github.com/jackc/pgx/v5"
"github.com/stretchr/testify/suite"
"gorm.io/gorm"
)
// PgxPersisterTestSuite tests for pgx persister
type PgxPersisterTestSuite struct {
TestSuite
p *PgxPersister
r *Repository
}
func (suite *PgxPersisterTestSuite) SetupTest() {
suite.TestSuite.SetupTest()
suite.p = &PgxPersister{db: suite.pgxDB}
suite.r = &Repository{db: NewPGXAdapter(suite.pgxDB)}
}
func (suite *PgxPersisterTestSuite) TestPersistInTx() {
suite.pollute()
defer suite.cleanDB()
err := suite.p.PersistInTx(context.Background(), func(tx pgx.Tx) ([]Message, error) {
return []Message{
{ID: "f53ec986-345f-48a4-b248-430a7d7f342f", Payload: map[string]string{}, PartitionKey: sql.NullInt64{
Int64: 1,
Valid: true,
}},
{ID: "f53ec986-345f-48a4-b248-430a7d7f342e", Payload: map[string]string{}, PartitionKey: sql.NullInt64{
Int64: 2,
Valid: true,
}},
}, nil
})
if err != nil {
suite.Failf("Cannot persist messages", "%s", err)
}
c := map[string]struct{}{}
ch := suite.r.Fetch(context.TODO(), 100)
for m := range ch {
c[m.ID] = struct{}{}
}
suite.Equal(4, len(c))
}
func TestPgxPersister(t *testing.T) {
suite.Run(t, new(PgxPersisterTestSuite))
}
// GormPersisterTestSuite tests for gorm persister
type GormPersisterTestSuite struct {
TestSuite
p *GormPersister
r *Repository
}
func (suite *GormPersisterTestSuite) SetupTest() {
suite.TestSuite.SetupTest()
suite.p = &GormPersister{db: suite.gormDB}
suite.r = &Repository{db: NewGORMAdapter(suite.gormDB)}
}
func (suite *GormPersisterTestSuite) TestPersistInTx() {
suite.pollute()
defer suite.cleanDB()
err := suite.p.PersistInTx(func(tx *gorm.DB) ([]Message, error) {
return []Message{
{ID: "f53ec986-345f-48a4-b248-430a7d7f342f", Payload: map[string]string{}},
{ID: "f53ec986-345f-48a4-b248-430a7d7f342e", Payload: map[string]string{}},
}, nil
})
if err != nil {
suite.Failf("Cannot persist messages", "%s", err)
}
c := map[string]struct{}{}
ch := suite.r.Fetch(context.TODO(), 100)
for m := range ch {
c[m.ID] = struct{}{}
}
suite.Equal(4, len(c))
}
func TestGormPersister(t *testing.T) {
suite.Run(t, new(GormPersisterTestSuite))
}