Added LoadDefaultSchema to platform testing utils to make test setup easier. (#38706)
<!-- Add the related story/sub-task/bug number, like Resolves #123, or remove if NA --> **Related issue:** Resolves #38234 Minor developer experience improvement for tests using MySQL. This is a code review follow up.
This commit is contained in:
@@ -3,8 +3,6 @@ package testutils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -29,10 +27,7 @@ func SetupTestDB(t *testing.T, testNamePrefix string) *TestDB {
|
||||
UniqueTestName: testNamePrefix + "_" + t.Name(),
|
||||
})
|
||||
|
||||
_, thisFile, _, _ := runtime.Caller(0)
|
||||
schemaPath := filepath.Join(filepath.Dir(thisFile), "../../../../server/datastore/mysql/schema.sql")
|
||||
mysql_testing_utils.LoadSchema(t, testName, opts, schemaPath)
|
||||
|
||||
mysql_testing_utils.LoadDefaultSchema(t, testName, opts)
|
||||
config := mysql_testing_utils.MysqlTestConfig(testName)
|
||||
db, err := common_mysql.NewDB(config, &common_mysql.DBOptions{}, "")
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -15,9 +15,7 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -366,9 +364,7 @@ func setupRealReplica(t testing.TB, testName string, ds *Datastore, options *com
|
||||
// MySQL. This is much faster than running the full set of migrations on each
|
||||
// test.
|
||||
func initializeDatabase(t testing.TB, testName string, opts *testing_utils.DatastoreTestOptions) *Datastore {
|
||||
_, filename, _, _ := runtime.Caller(0)
|
||||
schemaPath := path.Join(path.Dir(filename), "schema.sql")
|
||||
testing_utils.LoadSchema(t, testName, opts, schemaPath)
|
||||
testing_utils.LoadDefaultSchema(t, testName, opts)
|
||||
return connectMySQL(t, testName, opts)
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -107,6 +108,14 @@ type DatastoreTestOptions struct {
|
||||
UniqueTestName string
|
||||
}
|
||||
|
||||
// LoadDefaultSchema loads the default database schema for testing.
|
||||
func LoadDefaultSchema(t testing.TB, testName string, opts *DatastoreTestOptions) {
|
||||
_, thisFile, _, _ := runtime.Caller(0)
|
||||
schemaPath := filepath.Join(filepath.Dir(thisFile), "../../../datastore/mysql/schema.sql")
|
||||
LoadSchema(t, testName, opts, schemaPath)
|
||||
}
|
||||
|
||||
// LoadSchema loads a database schema from the specified path for testing.
|
||||
func LoadSchema(t testing.TB, testName string, opts *DatastoreTestOptions, schemaPath string) {
|
||||
schema, err := os.ReadFile(schemaPath)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user