github.com/wfusion/gofusion@v1.1.14/test/db/test.go (about) 1 package db 2 3 import ( 4 "context" 5 "fmt" 6 "reflect" 7 "sync" 8 9 "github.com/stretchr/testify/suite" 10 "go.uber.org/atomic" 11 12 "github.com/wfusion/gofusion/common/utils" 13 "github.com/wfusion/gofusion/log" 14 "github.com/wfusion/gofusion/test" 15 ) 16 17 var ( 18 component = "db" 19 ) 20 21 type Test struct { 22 test.Suite 23 24 once sync.Once 25 exits []func() 26 27 testName string 28 testsLefts atomic.Int64 29 } 30 31 func (t *Test) SetupTest() { 32 t.Catch(func() { 33 log.Info(context.Background(), fmt.Sprintf("------------ %s test case begin ------------", component)) 34 35 files := append(t.ConfigFiles(), "sqlite.db") 36 t.once.Do(func() { 37 t.exits = append(t.exits, t.Suite.Copy(files, t.testName, 1)) 38 }) 39 40 t.exits = append(t.exits, t.Suite.Init(files, t.testName, 1)) 41 }) 42 } 43 44 func (t *Test) TearDownTest() { 45 t.Catch(func() { 46 log.Info(context.Background(), fmt.Sprintf("------------ %s test case end ------------", component)) 47 if t.testsLefts.Add(-1) == 0 { 48 for i := len(t.exits) - 1; i >= 0; i-- { 49 t.exits[i]() 50 } 51 } 52 }) 53 } 54 55 func (t *Test) AppName() string { 56 return fmt.Sprintf("%s.%s", component, t.testName) 57 } 58 59 func (t *Test) Init(testingSuite suite.TestingSuite) { 60 methodFinder := reflect.TypeOf(testingSuite) 61 numMethod := methodFinder.NumMethod() 62 63 numTestLeft := int64(0) 64 for i := 0; i < numMethod; i++ { 65 method := methodFinder.Method(i) 66 ok, _ := test.MethodFilter(method.Name) 67 if !ok { 68 continue 69 } 70 numTestLeft++ 71 } 72 t.testName = utils.IndirectType(methodFinder).Name() 73 t.testsLefts.Add(numTestLeft) 74 }