@@ -383,28 +383,45 @@ func TestScopesWithTransactions(t *testing.T) {
383383 setupScopeTestData (t )
384384
385385 // Test scopes within a transaction
386+ var foundUsers []User
386387 err := DB .Transaction (func (tx * gorm.DB ) error {
387388 transactionScope := func (db * gorm.DB ) * gorm.DB {
388389 return db .Where ("\" name\" = ?" , "ScopeUser1" )
389390 }
390391
391- var users []User
392- return tx .Scopes (transactionScope ).Find (& users ).Error
392+ return tx .Scopes (transactionScope ).Find (& foundUsers ).Error
393393 })
394394
395395 if err != nil {
396396 t .Errorf ("Scopes within transaction should work, got: %v" , err )
397397 }
398398
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+
399407 // Test scope that tries to start its own transaction (nested transaction scenario)
400408 nestedTxScope := func (db * gorm.DB ) * gorm.DB {
401409 return db .Begin ()
402410 }
403411
412+ var nestedUsers []User
404413 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
407415 })
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+ }
408425}
409426
410427func TestScopesWithRawSQL (t * testing.T ) {
0 commit comments