github.com/wfusion/gofusion@v1.1.14/test/mongo/cases/conn_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/log"
    10  	"github.com/wfusion/gofusion/mongo"
    11  
    12  	testMgo "github.com/wfusion/gofusion/test/mongo"
    13  	mgoDrv "go.mongodb.org/mongo-driver/mongo"
    14  )
    15  
    16  func TestConn(t *testing.T) {
    17  	testingSuite := &Conn{Test: new(testMgo.Test)}
    18  	testingSuite.Init(testingSuite)
    19  	suite.Run(t, testingSuite)
    20  }
    21  
    22  type Conn struct {
    23  	*testMgo.Test
    24  }
    25  
    26  func (t *Conn) BeforeTest(suiteName, testName string) {
    27  	t.Catch(func() {
    28  		log.Info(context.Background(), "right before %s %s", suiteName, testName)
    29  	})
    30  }
    31  
    32  func (t *Conn) AfterTest(suiteName, testName string) {
    33  	t.Catch(func() {
    34  		log.Info(context.Background(), "right after %s %s", suiteName, testName)
    35  	})
    36  }
    37  
    38  func (t *Conn) TestPing() {
    39  	t.Catch(func() {
    40  		ctx, cancel := context.WithCancel(context.Background())
    41  		defer cancel()
    42  		mgoCli := mongo.Use(nameDefault, mongo.AppName(t.AppName()))
    43  		err := mgoCli.Client().Ping(ctx, nil)
    44  		t.NoError(err)
    45  	})
    46  }
    47  
    48  func (t *Conn) TestCollections() {
    49  	t.Catch(func() {
    50  		ctx, cancel := context.WithCancel(context.Background())
    51  		defer cancel()
    52  		name := "TestCollections"
    53  
    54  		db := mongo.Use(nameDefault, mongo.AppName(t.AppName()))
    55  		err := db.CreateCollection(ctx, name)
    56  		t.NoError(err)
    57  
    58  		coll := db.Collection(name)
    59  		defer func() {
    60  			t.NoError(coll.Drop(ctx))
    61  		}()
    62  
    63  		err = coll.FindOne(ctx, nil).Err()
    64  		t.EqualValues(mgoDrv.ErrNilDocument, err)
    65  	})
    66  }