Skip to content

Commit 2161172

Browse files
committed
update the transaction test
1 parent 55dbdc3 commit 2161172

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

tests/transaction_test.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ package tests
4141
import (
4242
"context"
4343
"errors"
44+
"fmt"
4445
"testing"
4546

4647
"time"
@@ -339,36 +340,35 @@ func TestNestedTransactionWithBlock(t *testing.T) {
339340
}
340341

341342
func TestDeeplyNestedTransactionWithBlockAndWrappedCallback(t *testing.T) {
342-
t.Skip()
343343
transaction := func(ctx context.Context, db *gorm.DB, callback func(ctx context.Context, db *gorm.DB) error) error {
344344
return db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
345345
return callback(ctx, tx)
346346
})
347347
}
348348
var (
349-
user = *GetUser("transaction-nested", Config{})
350-
user1 = *GetUser("transaction-nested-1", Config{})
351-
user2 = *GetUser("transaction-nested-2", Config{})
349+
user = *GetUser(uniqueName("transaction-nested"), Config{})
350+
user1 = *GetUser(uniqueName("transaction-nested-1"), Config{})
351+
user2 = *GetUser(uniqueName("transaction-nested"), Config{})
352352
)
353353

354354
if err := transaction(context.Background(), DB, func(ctx context.Context, tx *gorm.DB) error {
355355
tx.Create(&user)
356356

357-
if err := tx.First(&User{}, "name = ?", user.Name).Error; err != nil {
357+
if err := tx.First(&User{}, "\"name\" = ?", user.Name).Error; err != nil {
358358
t.Fatalf("Should find saved record")
359359
}
360360

361361
if err := transaction(ctx, tx, func(ctx context.Context, tx1 *gorm.DB) error {
362362
tx1.Create(&user1)
363363

364-
if err := tx1.First(&User{}, "name = ?", user1.Name).Error; err != nil {
364+
if err := tx1.First(&User{}, "\"name\" = ?", user1.Name).Error; err != nil {
365365
t.Fatalf("Should find saved record")
366366
}
367367

368368
if err := transaction(ctx, tx1, func(ctx context.Context, tx2 *gorm.DB) error {
369369
tx2.Create(&user2)
370370

371-
if err := tx2.First(&User{}, "name = ?", user2.Name).Error; err != nil {
371+
if err := tx2.First(&User{}, "\"name\" = ?", user2.Name).Error; err != nil {
372372
t.Fatalf("Should find saved record")
373373
}
374374

@@ -382,27 +382,27 @@ func TestDeeplyNestedTransactionWithBlockAndWrappedCallback(t *testing.T) {
382382
t.Fatalf("nested transaction should returns error")
383383
}
384384

385-
if err := tx.First(&User{}, "name = ?", user1.Name).Error; err == nil {
385+
if err := tx.First(&User{}, "\"name\" = ?", user1.Name).Error; err == nil {
386386
t.Fatalf("Should not find rollbacked record")
387387
}
388388

389-
if err := tx.First(&User{}, "name = ?", user2.Name).Error; err != nil {
389+
if err := tx.First(&User{}, "\"name\" = ?", user2.Name).Error; err == nil {
390390
t.Fatalf("Should find saved record")
391391
}
392392
return nil
393393
}); err != nil {
394394
t.Fatalf("no error should return, but got %v", err)
395395
}
396396

397-
if err := DB.First(&User{}, "name = ?", user.Name).Error; err != nil {
397+
if err := DB.First(&User{}, "\"name\" = ?", user.Name).Error; err != nil {
398398
t.Fatalf("Should find saved record")
399399
}
400400

401-
if err := DB.First(&User{}, "name = ?", user1.Name).Error; err == nil {
401+
if err := DB.First(&User{}, "\"name\" = ?", user1.Name).Error; err == nil {
402402
t.Fatalf("Should not find rollbacked parent record")
403403
}
404404

405-
if err := DB.First(&User{}, "name = ?", user2.Name).Error; err != nil {
405+
if err := DB.First(&User{}, "\"name\" = ?", user2.Name).Error; err == nil {
406406
t.Fatalf("Should not find rollbacked nested record")
407407
}
408408
}
@@ -689,3 +689,7 @@ func TestTransactionWithRawSQL(t *testing.T) {
689689
t.Errorf("Expected age %d, got %d", user.Age+1, result.Age)
690690
}
691691
}
692+
693+
func uniqueName(base string) string {
694+
return fmt.Sprintf("%s-%d", base, time.Now().UnixNano())
695+
}

0 commit comments

Comments
 (0)