Skip to content

Commit 9d8ca39

Browse files
committed
more writer tests
1 parent 65c4fe6 commit 9d8ca39

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

errors.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package outbox
33
import "errors"
44

55
var (
6-
ErrTxNil = errors.New("tx is nil")
6+
ErrTxNil = errors.New("tx is nil")
7+
ErrTxUnsupportedType = errors.New("tx has unsupported type")
78

89
ErrTableEmpty = errors.New("table is empty")
910
)

writer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func queryRow(ctx context.Context, tx Tx, q string, args ...interface{}) (pgx.Ro
173173
case pgx.Tx:
174174
row = t.QueryRow(ctx, q, args...)
175175
default:
176-
return nil, errors.New("unsupported transaction type")
176+
return nil, fmt.Errorf("%w: %T", ErrTxUnsupportedType, tx)
177177
}
178178

179179
return row, nil

writer_reader_test.go

+83
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,45 @@ func (suite *WriterReaderTestSuite) TearDownSuite() {
7070
goleak.VerifyNone(suite.T())
7171
}
7272

73+
func (suite *WriterReaderTestSuite) TestWriter_New() {
74+
tests := []struct {
75+
name string
76+
table string
77+
options []outbox.WriteOption
78+
wantErr error
79+
}{
80+
{
81+
name: "empty table",
82+
table: "",
83+
wantErr: outbox.ErrTableEmpty,
84+
},
85+
{
86+
name: "non-empty table",
87+
table: "outbox_messages",
88+
},
89+
{
90+
name: "with options",
91+
table: "outbox_messages",
92+
options: []outbox.WriteOption{outbox.WithDisablePreparedBatch()},
93+
},
94+
}
95+
96+
for _, tt := range tests {
97+
suite.Run(tt.name, func() {
98+
t := suite.T()
99+
100+
writer, err := outbox.NewWriter(tt.table, tt.options...)
101+
if tt.wantErr != nil {
102+
require.ErrorIs(t, err, tt.wantErr)
103+
return
104+
}
105+
106+
require.NoError(t, err)
107+
assert.NotNil(t, writer)
108+
})
109+
}
110+
}
111+
73112
func (suite *WriterReaderTestSuite) TestWriter_WriteMessage() {
74113
invalidMessage := fakes.FakeMessage()
75114
invalidMessage.Broker = ""
@@ -516,3 +555,47 @@ func maxInt(x, y int) int {
516555
}
517556
return y
518557
}
558+
559+
func (suite *WriterReaderTestSuite) TestWriter_WriteWithNilTx() {
560+
message := fakes.FakeMessage()
561+
562+
tests := []struct {
563+
name string
564+
writeFn func() error
565+
wantErr error
566+
}{
567+
{
568+
name: "Write with nil tx",
569+
writeFn: func() error {
570+
_, err := suite.writer.Write(ctx, nil, message)
571+
return err
572+
},
573+
wantErr: outbox.ErrTxNil,
574+
},
575+
{
576+
name: "Write with unsupported tx type",
577+
writeFn: func() error {
578+
_, err := suite.writer.Write(ctx, struct{}{}, message)
579+
return err
580+
},
581+
wantErr: outbox.ErrTxUnsupportedType,
582+
},
583+
{
584+
name: "WriteBatch with nil tx",
585+
writeFn: func() error {
586+
_, err := suite.writer.WriteBatch(ctx, nil, []types.Message{message})
587+
return err
588+
},
589+
wantErr: outbox.ErrTxNil,
590+
},
591+
}
592+
593+
for _, tt := range tests {
594+
suite.Run(tt.name, func() {
595+
t := suite.T()
596+
597+
err := tt.writeFn()
598+
require.ErrorIs(t, err, tt.wantErr)
599+
})
600+
}
601+
}

0 commit comments

Comments
 (0)