github.com/larkox/mattermost-server@v5.11.1+incompatible/config/main_test.go (about)

     1  package config_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/go-sql-driver/mysql"
     8  	"github.com/mattermost/mattermost-server/model"
     9  	"github.com/mattermost/mattermost-server/testlib"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  var mainHelper *testlib.MainHelper
    14  
    15  func TestMain(m *testing.M) {
    16  	var options = testlib.HelperOptions{
    17  		EnableStore: true,
    18  	}
    19  
    20  	mainHelper = testlib.NewMainHelperWithOptions(&options)
    21  	defer mainHelper.Close()
    22  
    23  	mainHelper.Main(m)
    24  }
    25  
    26  // truncateTable clears the given table
    27  func truncateTable(t *testing.T, table string) {
    28  	t.Helper()
    29  	sqlSetting := mainHelper.GetSqlSettings()
    30  	sqlSupplier := mainHelper.GetSqlSupplier()
    31  
    32  	switch *sqlSetting.DriverName {
    33  	case model.DATABASE_DRIVER_MYSQL:
    34  		_, err := sqlSupplier.GetMaster().Db.Exec(fmt.Sprintf("TRUNCATE TABLE %s", table))
    35  		if err != nil {
    36  			if driverErr, ok := err.(*mysql.MySQLError); ok {
    37  				// Ignore if the Configurations table does not exist.
    38  				if driverErr.Number == 1146 {
    39  					return
    40  				}
    41  			}
    42  		}
    43  		require.NoError(t, err)
    44  
    45  	case model.DATABASE_DRIVER_POSTGRES:
    46  		_, err := sqlSupplier.GetMaster().Db.Exec(fmt.Sprintf("TRUNCATE TABLE %s", table))
    47  		require.NoError(t, err)
    48  
    49  	default:
    50  		t.Fatalf("unsupported driver name: %s", *sqlSetting.DriverName)
    51  	}
    52  }
    53  
    54  // truncateTables clears tables used by the config package for reuse in other tests
    55  func truncateTables(t *testing.T) {
    56  	t.Helper()
    57  
    58  	truncateTable(t, "Configurations")
    59  	truncateTable(t, "ConfigurationFiles")
    60  }