github.com/wfusion/gofusion@v1.1.14/test/db/cases/scan_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 TestScan(t *testing.T) {
    16  	testingSuite := &Scan{Test: new(testDB.Test)}
    17  	testingSuite.Init(testingSuite)
    18  	suite.Run(t, testingSuite)
    19  }
    20  
    21  type Scan struct {
    22  	*testDB.Test
    23  }
    24  
    25  func (t *Scan) 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 *Scan) 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 *Scan) TestMysql() {
    38  	t.testDefault(nameMysqlRead, nameMysqlWrite)
    39  }
    40  
    41  func (t *Scan) TestPostgres() {
    42  	t.testDefault(namePostgres, namePostgres)
    43  }
    44  
    45  func (t *Scan) TestOpengauss() {
    46  	t.testDefault(nameOpenGauss, nameOpenGauss)
    47  }
    48  
    49  func (t *Scan) TestSqlserver() {
    50  	t.testDefault(nameSqlserver, nameSqlserver)
    51  }
    52  
    53  func (t *Scan) testDefault(read, write string) {
    54  	t.Catch(func() {
    55  		ctx := context.Background()
    56  		orm := db.Use(ctx, write, db.AppName(t.AppName()))
    57  
    58  		t.NoError(orm.Migrator().AutoMigrate(new(modelWithData)))
    59  		defer func() {
    60  			t.NoError(orm.Migrator().DropTable(new(modelWithData)))
    61  		}()
    62  
    63  		expected := []*modelWithData{
    64  			{Name: "test1"}, {Name: "test2"}, {Name: "test3"},
    65  			{Name: "test4"}, {Name: "test5"}, {Name: "test6"},
    66  			{Name: "test7"}, {Name: "test8"},
    67  		}
    68  		t.NoError(orm.Create(expected).Error)
    69  		defer func() { t.NoError(orm.Delete(expected).Error) }()
    70  
    71  		actual := make([]*modelWithData, 0, len(expected))
    72  		t.NoError(
    73  			db.Scan[modelWithData, []*modelWithData](
    74  				ctx,
    75  				func(mList []*modelWithData) bool { actual = append(actual, mList...); return true },
    76  				db.ScanDAL[modelWithData, []*modelWithData](modelWithDataDAL(read, write, t.AppName())),
    77  				db.ScanBatch(3),
    78  				db.ScanCursor("id > ?", []string{"id"}, 0),
    79  				db.ScanOrder("id ASC"),
    80  				db.AppName(t.AppName()),
    81  			),
    82  		)
    83  
    84  		t.EqualValues(expected, actual)
    85  	})
    86  }