@@ -2,6 +2,7 @@ package pgmq
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"errors"
6
7
"fmt"
7
8
"os"
@@ -20,8 +21,8 @@ import (
20
21
var q * PGMQ
21
22
22
23
var (
23
- testMsg1 = map [ string ] any {"foo" : "bar1" }
24
- testMsg2 = map [ string ] any {"foo" : "bar2" }
24
+ testMsg1 = json . RawMessage ( ` {"foo": "bar1"}` )
25
+ testMsg2 = json . RawMessage ( ` {"foo": "bar2"}` )
25
26
)
26
27
27
28
func TestMain (m * testing.M ) {
@@ -124,14 +125,53 @@ func TestSend(t *testing.T) {
124
125
require .EqualValues (t , 2 , id )
125
126
}
126
127
128
+ func TestSendAMarshalledStruct (t * testing.T ) {
129
+ type A struct {
130
+ Val int `json:"val"`
131
+ }
132
+
133
+ a := A {3 }
134
+ b , err := json .Marshal (a )
135
+ require .NoError (t , err )
136
+
137
+ ctx := context .Background ()
138
+ queue := t .Name ()
139
+
140
+ err = q .CreateQueue (ctx , queue )
141
+ require .NoError (t , err )
142
+
143
+ _ , err = q .Send (ctx , queue , b )
144
+ require .NoError (t , err )
145
+
146
+ msg , err := q .Read (ctx , queue , 0 )
147
+ require .NoError (t , err )
148
+
149
+ var aa A
150
+ err = json .Unmarshal (msg .Message , & aa )
151
+ require .NoError (t , err )
152
+
153
+ require .EqualValues (t , a , aa )
154
+ }
155
+
156
+ func TestSendInvalidJSONFails (t * testing.T ) {
157
+ ctx := context .Background ()
158
+ queue := t .Name ()
159
+
160
+ err := q .CreateQueue (ctx , queue )
161
+ require .NoError (t , err )
162
+
163
+ _ , err = q .Send (ctx , queue , json .RawMessage (`{"foo":}` ))
164
+ require .Error (t , err )
165
+ }
166
+
127
167
func TestSendBatch (t * testing.T ) {
128
168
ctx := context .Background ()
129
169
queue := t .Name ()
130
170
131
171
err := q .CreateQueue (ctx , queue )
132
172
require .NoError (t , err )
133
173
134
- ids , err := q .SendBatch (ctx , queue , []map [ string ] any {testMsg1 , testMsg2 })
174
+ ids , err := q .SendBatch (ctx , queue , []json. RawMessage {testMsg1 , testMsg2 })
135
175
require .NoError (t , err )
136
176
require .Equal (t , []int64 {1 , 2 }, ids )
137
177
}
@@ -174,7 +214,7 @@ func TestReadBatch(t *testing.T) {
174
214
err := q .CreateQueue (ctx , queue )
175
215
require .NoError (t , err )
176
216
177
- _ , err = q .SendBatch (ctx , queue , []map [ string ] any {testMsg1 , testMsg2 })
217
+ _ , err = q .SendBatch (ctx , queue , []json. RawMessage {testMsg1 , testMsg2 })
178
218
require .NoError (t , err )
179
219
180
220
time .Sleep (time .Second )
@@ -264,7 +304,7 @@ func TestArchiveBatch(t *testing.T) {
264
304
err := q .CreateQueue (ctx , queue )
265
305
require .NoError (t , err )
266
306
267
- ids , err := q .SendBatch (ctx , queue , []map [ string ] any {testMsg1 , testMsg2 })
307
+ ids , err := q .SendBatch (ctx , queue , []json. RawMessage {testMsg1 , testMsg2 })
268
308
require .NoError (t , err )
269
309
270
310
archived , err := q .ArchiveBatch (ctx , queue , ids )
@@ -318,7 +358,7 @@ func TestDeleteBatch(t *testing.T) {
318
358
err := q .CreateQueue (ctx , queue )
319
359
require .NoError (t , err )
320
360
321
- ids , err := q .SendBatch (ctx , queue , []map [ string ] any {testMsg1 , testMsg2 })
361
+ ids , err := q .SendBatch (ctx , queue , []json. RawMessage {testMsg1 , testMsg2 })
322
362
require .NoError (t , err )
323
363
324
364
deleted , err := q .DeleteBatch (ctx , queue , ids )
@@ -372,7 +412,7 @@ func TestErrorCases(t *testing.T) {
372
412
373
413
t .Run ("sendBatchError" , func (t * testing.T ) {
374
414
mockDB .EXPECT ().Query (ctx , "SELECT * FROM pgmq.send_batch($1, $2::jsonb[], $3)" , queue , gomock .Any (), 0 ).Return (nil , testErr )
375
- ids , err := q .SendBatch (ctx , queue , []map [ string ] any {testMsg1 })
415
+ ids , err := q .SendBatch (ctx , queue , []json. RawMessage {testMsg1 })
376
416
require .Nil (t , ids )
377
417
require .ErrorContains (t , err , "postgres error" )
378
418
})
0 commit comments