@@ -70,6 +70,45 @@ func (suite *WriterReaderTestSuite) TearDownSuite() {
70
70
goleak .VerifyNone (suite .T ())
71
71
}
72
72
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
+
73
112
func (suite * WriterReaderTestSuite ) TestWriter_WriteMessage () {
74
113
invalidMessage := fakes .FakeMessage ()
75
114
invalidMessage .Broker = ""
@@ -516,3 +555,47 @@ func maxInt(x, y int) int {
516
555
}
517
556
return y
518
557
}
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