github.com/wfusion/gofusion@v1.1.14/test/db/cases/transaction_test.go (about)

     1  package cases
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/suite"
     8  
     9  	"github.com/wfusion/gofusion/db"
    10  	"github.com/wfusion/gofusion/log"
    11  
    12  	testDB "github.com/wfusion/gofusion/test/db"
    13  )
    14  
    15  func TestTransaction(t *testing.T) {
    16  	testingSuite := &Transaction{Test: new(testDB.Test)}
    17  	testingSuite.Init(testingSuite)
    18  	suite.Run(t, testingSuite)
    19  }
    20  
    21  type Transaction struct {
    22  	*testDB.Test
    23  }
    24  
    25  func (t *Transaction) BeforeTest(suiteName, testName string) {
    26  	t.Catch(func() {
    27  		log.Info(context.Background(), "right before %s %s", suiteName, testName)
    28  	})
    29  }
    30  
    31  func (t *Transaction) AfterTest(suiteName, testName string) {
    32  	t.Catch(func() {
    33  		log.Info(context.Background(), "right after %s %s", suiteName, testName)
    34  	})
    35  }
    36  
    37  func (t *Transaction) TestMysql() {
    38  	t.testDefault(nameMysqlRead, nameMysqlWrite)
    39  }
    40  
    41  func (t *Transaction) TestPostgres() {
    42  	t.testDefault(namePostgres, namePostgres)
    43  }
    44  
    45  func (t *Transaction) TestOpenGauss() {
    46  	t.testDefault(nameOpenGauss, nameOpenGauss)
    47  }
    48  
    49  func (t *Transaction) TestSqlserver() {
    50  	t.testDefault(nameSqlserver, nameSqlserver)
    51  }
    52  
    53  func (t *Transaction) testDefault(read, write string) {
    54  	t.Catch(func() {
    55  		ctx := context.Background()
    56  		ctx = db.SetCtxGormDB(ctx, db.Use(ctx, read, db.AppName(t.AppName())))
    57  		ctx = db.SetCtxGormDB(ctx, db.Use(ctx, write, db.AppName(t.AppName())))
    58  		t.NoError(db.WithinTx(ctx,
    59  			func(ctx context.Context) (err error) { return },
    60  			db.TxUse(write),
    61  		))
    62  	})
    63  }