@@ -383,28 +383,45 @@ func TestScopesWithTransactions(t *testing.T) {
383
383
setupScopeTestData (t )
384
384
385
385
// Test scopes within a transaction
386
+ var foundUsers []User
386
387
err := DB .Transaction (func (tx * gorm.DB ) error {
387
388
transactionScope := func (db * gorm.DB ) * gorm.DB {
388
389
return db .Where ("\" name\" = ?" , "ScopeUser1" )
389
390
}
390
391
391
- var users []User
392
- return tx .Scopes (transactionScope ).Find (& users ).Error
392
+ return tx .Scopes (transactionScope ).Find (& foundUsers ).Error
393
393
})
394
394
395
395
if err != nil {
396
396
t .Errorf ("Scopes within transaction should work, got: %v" , err )
397
397
}
398
398
399
+ // Verify the result
400
+ if len (foundUsers ) != 1 {
401
+ t .Errorf ("Expected 1 user in transaction scope, got %d" , len (foundUsers ))
402
+ }
403
+ if len (foundUsers ) > 0 && foundUsers [0 ].Name != "ScopeUser1" {
404
+ t .Errorf ("Expected user name 'ScopeUser1', got '%s'" , foundUsers [0 ].Name )
405
+ }
406
+
399
407
// Test scope that tries to start its own transaction (nested transaction scenario)
400
408
nestedTxScope := func (db * gorm.DB ) * gorm.DB {
401
409
return db .Begin ()
402
410
}
403
411
412
+ var nestedUsers []User
404
413
err = DB .Transaction (func (tx * gorm.DB ) error {
405
- var users []User
406
- return tx .Scopes (nestedTxScope ).Find (& users ).Error
414
+ return tx .Scopes (nestedTxScope ).Find (& nestedUsers ).Error
407
415
})
416
+
417
+ if err != nil {
418
+ t .Logf ("Nested transaction scope failed as expected: %v" , err )
419
+ } else {
420
+ t .Logf ("Nested transaction scope succeeded unexpectedly" )
421
+ if len (nestedUsers ) == 0 {
422
+ t .Error ("If nested transaction succeeds, should return some users" )
423
+ }
424
+ }
408
425
}
409
426
410
427
func TestScopesWithRawSQL (t * testing.T ) {
0 commit comments