@@ -196,6 +196,7 @@ func Test_Changeset_Multi(t *testing.T) {
196196 // Create tables, and attach a session.
197197 mustExecute (conn , `CREATE TABLE table1 (id INTEGER PRIMARY KEY, name TEXT, gpa FLOAT, alive boolean)` )
198198 mustExecute (conn , `CREATE TABLE table2 (id INTEGER PRIMARY KEY, age INTEGER)` )
199+ mustExecute (conn , `CREATE TABLE table3 (id INTEGER PRIMARY KEY, company TEXT)` )
199200 var session * Session
200201 if err := conn .Raw (func (raw any ) error {
201202 var err error
@@ -219,6 +220,8 @@ func Test_Changeset_Multi(t *testing.T) {
219220 mustExecute (conn , `INSERT INTO table1 (name, gpa, alive) VALUES ('declan', 2.5, 0)` )
220221 mustExecute (conn , `INSERT INTO table2 (age) VALUES (20)` )
221222 mustExecute (conn , `INSERT INTO table2 (age) VALUES (30)` )
223+ mustExecute (conn , `INSERT INTO table3 (company) VALUES ('foo')` )
224+ mustExecute (conn , `UPDATE table3 SET company = 'bar' WHERE id = 1` )
222225
223226 // Prepare to iterate over the changes.
224227 changeset , err := NewChangeset (session )
@@ -233,31 +236,37 @@ func Test_Changeset_Multi(t *testing.T) {
233236 tt := []struct {
234237 table string
235238 op int
236- data []any
239+ old []any
240+ new []any
237241 }{
238242 {
239243 table : "table1" ,
240244 op : SQLITE_INSERT ,
241- data : []any {int64 (1 ), "fiona" , 3.5 , int64 (1 )},
245+ new : []any {int64 (1 ), "fiona" , 3.5 , int64 (1 )},
242246 },
243247 {
244248 table : "table1" ,
245249 op : SQLITE_INSERT ,
246- data : []any {int64 (2 ), "declan" , 2.5 , int64 (0 )},
250+ new : []any {int64 (2 ), "declan" , 2.5 , int64 (0 )},
247251 },
248252 {
249253 table : "table2" ,
250254 op : SQLITE_INSERT ,
251- data : []any {int64 (1 ), int64 (20 )},
255+ new : []any {int64 (1 ), int64 (20 )},
252256 },
253257 {
254258 table : "table2" ,
255259 op : SQLITE_INSERT ,
256- data : []any {int64 (2 ), int64 (30 )},
260+ new : []any {int64 (2 ), int64 (30 )},
261+ },
262+ {
263+ table : "table3" ,
264+ op : SQLITE_INSERT ,
265+ new : []any {int64 (1 ), "bar" },
257266 },
258267 }
259268
260- for _ , v := range tt {
269+ for i , v := range tt {
261270 if b , err := iter .Next (); err != nil {
262271 t .Fatalf ("Failed to get next changeset: %v" , err )
263272 } else if ! b {
@@ -271,20 +280,34 @@ func Test_Changeset_Multi(t *testing.T) {
271280 if exp , got := v .table , tblName ; exp != got {
272281 t .Fatalf ("Expected table name '%s', got '%s'" , exp , got )
273282 }
274- if exp , got := len (v .data ), nCol ; exp != got {
275- t .Fatalf ("Expected %d columns, got %d" , exp , got )
276- }
283+ // if exp, got := len(v.new ), nCol; exp != got {
284+ // t.Fatalf("Expected %d columns, got %d", exp, got)
285+ // }
277286 if exp , got := v .op , op ; exp != got {
278287 t .Fatalf ("Expected operation %d, got %d" , exp , got )
279288 }
280289
281- dest := make ([]any , nCol )
282- if err := iter .New (dest ); err != nil {
283- t .Fatalf ("Failed to get new row: %v" , err )
290+ if v .old != nil {
291+ dest := make ([]any , nCol )
292+ if err := iter .Old (dest ); err != nil {
293+ t .Fatalf ("Failed to get old row: %v" , err )
294+ }
295+ for j , d := range v .old {
296+ if exp , got := d , dest [j ]; exp != got {
297+ t .Fatalf ("Test %d, expected %v (%T) for dest[%d], got %v (%T)" , i , exp , exp , j , got , got )
298+ }
299+ }
284300 }
285- for j , d := range v .data {
286- if exp , got := d , dest [j ]; exp != got {
287- t .Fatalf ("Expected %v (%T) for dest[%d], got %v (%T)" , exp , exp , j , got , got )
301+
302+ if v .new != nil {
303+ dest := make ([]any , nCol )
304+ if err := iter .New (dest ); err != nil {
305+ t .Fatalf ("Failed to get new row: %v" , err )
306+ }
307+ for j , d := range v .new {
308+ if exp , got := d , dest [j ]; exp != got {
309+ t .Fatalf ("Test %d, expected %v (%T) for new dest[%d], got %v (%T)" , i , exp , exp , j , got , got )
310+ }
288311 }
289312 }
290313 }
0 commit comments