github.com/hungdoo/bot@v0.0.0-20240325145135-dd1f386f7b81/src/__test__/main_test.go (about)

     1  package test
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"path"
     7  	"runtime"
     8  	"testing"
     9  
    10  	"github.com/hungdoo/bot/src/packages/db"
    11  	"github.com/hungdoo/bot/src/packages/dotenv"
    12  	"github.com/hungdoo/bot/src/packages/log"
    13  )
    14  
    15  func init() {
    16  	_, filename, _, _ := runtime.Caller(0)
    17  	dir := path.Join(path.Dir(filename), "../..")
    18  	err := os.Chdir(dir)
    19  	if err != nil {
    20  		panic(err)
    21  	}
    22  	dotenv.InitEnv()
    23  }
    24  
    25  func TestMain(m *testing.M) {
    26  	_db := db.GetDb()
    27  	log.GeneralLogger.Printf("Test db init. IsTestEnv: %v\n", _db.IsTestEnv)
    28  
    29  	defer func() {
    30  		if _db.IsTestEnv {
    31  			log.GeneralLogger.Println("Clean test db")
    32  			_db.GetCollection("commands").Drop(context.Background())
    33  		}
    34  		_db.Close()
    35  	}()
    36  
    37  	log.GeneralLogger.Println("Init tests!")
    38  	m.Run()
    39  	// runTests := m.Run()
    40  	log.GeneralLogger.Println("Done tests!")
    41  	// os.Exit(runTests)
    42  }