@@ -17,12 +17,14 @@ package postgres
1717import (
1818 "context"
1919 "fmt"
20+ "math/big"
2021 "strings"
2122 "testing"
2223
2324 "github.com/conduitio/conduit-commons/opencdc"
2425 "github.com/conduitio/conduit-connector-postgres/test"
2526 sdk "github.com/conduitio/conduit-connector-sdk"
27+ "github.com/google/go-cmp/cmp"
2628 "github.com/jackc/pgx/v5"
2729 "github.com/matryer/is"
2830)
@@ -71,11 +73,13 @@ func TestDestination_Write(t *testing.T) {
7173 "column1" : "foo" ,
7274 "column2" : 123 ,
7375 "column3" : true ,
76+ "column4" : nil ,
7477 "UppercaseColumn1" : 222 ,
7578 },
7679 },
7780 },
78- }, {
81+ },
82+ {
7983 name : "create" ,
8084 record : opencdc.Record {
8185 Position : opencdc .Position ("foo" ),
@@ -87,11 +91,13 @@ func TestDestination_Write(t *testing.T) {
8791 "column1" : "foo" ,
8892 "column2" : 456 ,
8993 "column3" : false ,
94+ "column4" : nil ,
9095 "UppercaseColumn1" : 333 ,
9196 },
9297 },
9398 },
94- }, {
99+ },
100+ {
95101 name : "insert on update (upsert)" ,
96102 record : opencdc.Record {
97103 Position : opencdc .Position ("foo" ),
@@ -103,11 +109,13 @@ func TestDestination_Write(t *testing.T) {
103109 "column1" : "bar" ,
104110 "column2" : 567 ,
105111 "column3" : true ,
112+ "column4" : nil ,
106113 "UppercaseColumn1" : 444 ,
107114 },
108115 },
109116 },
110- }, {
117+ },
118+ {
111119 name : "update on conflict" ,
112120 record : opencdc.Record {
113121 Position : opencdc .Position ("foo" ),
@@ -119,11 +127,13 @@ func TestDestination_Write(t *testing.T) {
119127 "column1" : "foobar" ,
120128 "column2" : 567 ,
121129 "column3" : true ,
130+ "column4" : nil ,
122131 "UppercaseColumn1" : 555 ,
123132 },
124133 },
125134 },
126- }, {
135+ },
136+ {
127137 name : "delete" ,
128138 record : opencdc.Record {
129139 Position : opencdc .Position ("foo" ),
@@ -132,6 +142,24 @@ func TestDestination_Write(t *testing.T) {
132142 Key : opencdc.StructuredData {"id" : 4 },
133143 },
134144 },
145+ {
146+ name : "write a big.Rat" ,
147+ record : opencdc.Record {
148+ Position : opencdc .Position ("foo" ),
149+ Operation : opencdc .OperationSnapshot ,
150+ Metadata : map [string ]string {opencdc .MetadataCollection : tableName },
151+ Key : opencdc.StructuredData {"id" : 123 },
152+ Payload : opencdc.Change {
153+ After : opencdc.StructuredData {
154+ "column1" : "abcdef" ,
155+ "column2" : 567 ,
156+ "column3" : true ,
157+ "column4" : big .NewRat (123 , 100 ),
158+ "UppercaseColumn1" : 555 ,
159+ },
160+ },
161+ },
162+ },
135163 }
136164 for _ , tt := range tests {
137165 t .Run (tt .name , func (t * testing.T ) {
@@ -146,7 +174,16 @@ func TestDestination_Write(t *testing.T) {
146174 switch tt .record .Operation {
147175 case opencdc .OperationCreate , opencdc .OperationSnapshot , opencdc .OperationUpdate :
148176 is .NoErr (err )
149- is .Equal (tt .record .Payload .After , got )
177+ is .Equal (
178+ "" ,
179+ cmp .Diff (
180+ tt .record .Payload .After ,
181+ got ,
182+ cmp .Comparer (func (x , y * big.Rat ) bool {
183+ return x .Cmp (y ) == 0
184+ }),
185+ ),
186+ ) // -want, +got
150187 case opencdc .OperationDelete :
151188 is .Equal (err , pgx .ErrNoRows )
152189 }
@@ -179,43 +216,50 @@ func TestDestination_Batch(t *testing.T) {
179216 is .NoErr (err )
180217 }()
181218
182- records := []opencdc.Record {{
183- Position : opencdc .Position ("foo1" ),
184- Operation : opencdc .OperationCreate ,
185- Key : opencdc.StructuredData {"id" : 5 },
186- Payload : opencdc.Change {
187- After : opencdc.StructuredData {
188- "column1" : "foo1" ,
189- "column2" : 1 ,
190- "column3" : false ,
191- "UppercaseColumn1" : 111 ,
219+ records := []opencdc.Record {
220+ {
221+ Position : opencdc .Position ("foo1" ),
222+ Operation : opencdc .OperationCreate ,
223+ Key : opencdc.StructuredData {"id" : 5 },
224+ Payload : opencdc.Change {
225+ After : opencdc.StructuredData {
226+ "column1" : "foo1" ,
227+ "column2" : 1 ,
228+ "column3" : false ,
229+ "column4" : nil ,
230+ "UppercaseColumn1" : 111 ,
231+ },
192232 },
193233 },
194- }, {
195- Position : opencdc .Position ("foo2" ),
196- Operation : opencdc .OperationCreate ,
197- Key : opencdc.StructuredData {"id" : 6 },
198- Payload : opencdc.Change {
199- After : opencdc.StructuredData {
200- "column1" : "foo2" ,
201- "column2" : 2 ,
202- "column3" : true ,
203- "UppercaseColumn1" : 222 ,
234+ {
235+ Position : opencdc .Position ("foo2" ),
236+ Operation : opencdc .OperationCreate ,
237+ Key : opencdc.StructuredData {"id" : 6 },
238+ Payload : opencdc.Change {
239+ After : opencdc.StructuredData {
240+ "column1" : "foo2" ,
241+ "column2" : 2 ,
242+ "column3" : true ,
243+ "column4" : nil ,
244+ "UppercaseColumn1" : 222 ,
245+ },
204246 },
205247 },
206- }, {
207- Position : opencdc .Position ("foo3" ),
208- Operation : opencdc .OperationCreate ,
209- Key : opencdc.StructuredData {"id" : 7 },
210- Payload : opencdc.Change {
211- After : opencdc.StructuredData {
212- "column1" : "foo3" ,
213- "column2" : 3 ,
214- "column3" : false ,
215- "UppercaseColumn1" : 333 ,
248+ {
249+ Position : opencdc .Position ("foo3" ),
250+ Operation : opencdc .OperationCreate ,
251+ Key : opencdc.StructuredData {"id" : 7 },
252+ Payload : opencdc.Change {
253+ After : opencdc.StructuredData {
254+ "column1" : "foo3" ,
255+ "column2" : 3 ,
256+ "column3" : false ,
257+ "column4" : nil ,
258+ "UppercaseColumn1" : 333 ,
259+ },
216260 },
217261 },
218- }}
262+ }
219263
220264 i , err := d .Write (ctx , records )
221265 is .NoErr (err )
@@ -231,25 +275,36 @@ func TestDestination_Batch(t *testing.T) {
231275func queryTestTable (ctx context.Context , conn test.Querier , tableName string , id any ) (opencdc.StructuredData , error ) {
232276 row := conn .QueryRow (
233277 ctx ,
234- fmt .Sprintf (`SELECT column1, column2, column3, "UppercaseColumn1" FROM %q WHERE id = $1` , tableName ),
278+ fmt .Sprintf (`SELECT column1, column2, column3, column4, "UppercaseColumn1" FROM %q WHERE id = $1` , tableName ),
235279 id ,
236280 )
237281
238282 var (
239283 col1 string
240284 col2 int
241285 col3 bool
286+ col4Str * string
242287 uppercaseCol1 int
243288 )
244- err := row .Scan (& col1 , & col2 , & col3 , & uppercaseCol1 )
289+
290+ err := row .Scan (& col1 , & col2 , & col3 , & col4Str , & uppercaseCol1 )
245291 if err != nil {
246292 return nil , err
247293 }
248294
295+ // Handle the potential nil case for col4
296+ var col4 interface {}
297+ if col4Str != nil {
298+ r := new (big.Rat )
299+ r .SetString (* col4Str )
300+ col4 = r
301+ }
302+
249303 return opencdc.StructuredData {
250304 "column1" : col1 ,
251305 "column2" : col2 ,
252306 "column3" : col3 ,
307+ "column4" : col4 ,
253308 "UppercaseColumn1" : uppercaseCol1 ,
254309 }, nil
255310}
0 commit comments