github.com/haalcala/mattermost-server-change-repo@v0.0.0-20210713015153-16753fbeee5f/config/main_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package config_test 5 6 import ( 7 "fmt" 8 "testing" 9 10 "github.com/go-sql-driver/mysql" 11 "github.com/lib/pq" 12 "github.com/stretchr/testify/require" 13 14 "github.com/mattermost/mattermost-server/v5/mlog" 15 "github.com/mattermost/mattermost-server/v5/model" 16 "github.com/mattermost/mattermost-server/v5/testlib" 17 ) 18 19 var mainHelper *testlib.MainHelper 20 21 func TestMain(m *testing.M) { 22 var options = testlib.HelperOptions{ 23 EnableStore: true, 24 } 25 26 mlog.DisableZap() 27 28 mainHelper = testlib.NewMainHelperWithOptions(&options) 29 defer mainHelper.Close() 30 31 mainHelper.Main(m) 32 } 33 34 // truncateTable clears the given table 35 func truncateTable(t *testing.T, table string) { 36 t.Helper() 37 sqlSetting := mainHelper.GetSQLSettings() 38 sqlStore := mainHelper.GetSQLStore() 39 40 switch *sqlSetting.DriverName { 41 case model.DATABASE_DRIVER_MYSQL: 42 _, err := sqlStore.GetMaster().Db.Exec(fmt.Sprintf("TRUNCATE TABLE %s", table)) 43 if err != nil { 44 if driverErr, ok := err.(*mysql.MySQLError); ok { 45 // Ignore if the Configurations table does not exist. 46 if driverErr.Number == 1146 { 47 return 48 } 49 } 50 } 51 require.NoError(t, err) 52 53 case model.DATABASE_DRIVER_POSTGRES: 54 _, err := sqlStore.GetMaster().Db.Exec(fmt.Sprintf("TRUNCATE TABLE %s", table)) 55 if err != nil { 56 if driverErr, ok := err.(*pq.Error); ok { 57 // Ignore if the Configurations table does not exist. 58 if driverErr.Code == "42P01" { 59 return 60 } 61 } 62 } 63 require.NoError(t, err) 64 65 default: 66 require.Failf(t, "failed", "unsupported driver name: %s", *sqlSetting.DriverName) 67 } 68 } 69 70 // truncateTables clears tables used by the config package for reuse in other tests 71 func truncateTables(t *testing.T) { 72 t.Helper() 73 74 truncateTable(t, "Configurations") 75 truncateTable(t, "ConfigurationFiles") 76 }