From 089cf9a3baeb8bca2fdf6187298f540d609b7380 Mon Sep 17 00:00:00 2001 From: Victor Lyuboslavsky <2685025+getvictor@users.noreply.github.com> Date: Mon, 26 Jan 2026 16:02:12 -0600 Subject: [PATCH] Added LoadDefaultSchema to platform testing utils to make test setup easier. (#38706) **Related issue:** Resolves #38234 Minor developer experience improvement for tests using MySQL. This is a code review follow up. --- server/activity/internal/testutils/testutils.go | 7 +------ server/datastore/mysql/testing_utils.go | 6 +----- server/platform/mysql/testing_utils/testing_utils.go | 9 +++++++++ 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/server/activity/internal/testutils/testutils.go b/server/activity/internal/testutils/testutils.go index 7b40d79f5f..b7ed2674c6 100644 --- a/server/activity/internal/testutils/testutils.go +++ b/server/activity/internal/testutils/testutils.go @@ -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) diff --git a/server/datastore/mysql/testing_utils.go b/server/datastore/mysql/testing_utils.go index 439681fa87..67e2dd5507 100644 --- a/server/datastore/mysql/testing_utils.go +++ b/server/datastore/mysql/testing_utils.go @@ -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) } diff --git a/server/platform/mysql/testing_utils/testing_utils.go b/server/platform/mysql/testing_utils/testing_utils.go index 52454c491b..ce86fbccbf 100644 --- a/server/platform/mysql/testing_utils/testing_utils.go +++ b/server/platform/mysql/testing_utils/testing_utils.go @@ -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 {