1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-03 03:36:22 -07:00

Move setup functionality in test to a separate function

This commit is contained in:
Eggbertx 2025-01-01 17:58:09 -08:00
parent 90986e58b8
commit beb048716e

View file

@ -4,7 +4,6 @@ import (
"path"
"testing"
"github.com/gochan-org/gochan/cmd/gochan-migration/internal/common"
"github.com/gochan-org/gochan/pkg/config"
"github.com/gochan-org/gochan/pkg/gcsql"
"github.com/gochan-org/gochan/pkg/gcutil/testutil"
@ -15,40 +14,68 @@ const (
sqlite3DBPath = "tools/gochan-pre2021.sqlite3db" // relative to gochan project root
)
func TestMigrateToNewDB(t *testing.T) {
func setupMigrationTest(t *testing.T) *Pre2021Migrator {
dir, err := testutil.GoToGochanRoot(t)
if !assert.NoError(t, err) {
return
}
if !assert.NoError(t, common.InitTestMigrationLog(t)) {
return
t.FailNow()
}
dbPath := path.Join(dir, sqlite3DBPath)
oldSQLConfig := config.SQLConfig{
DBtype: "sqlite3",
DBname: path.Base(dbPath),
DBhost: dbPath,
DBprefix: "gc_",
DBusername: "gochan",
DBpassword: "password",
DBtype: "sqlite3",
DBname: path.Base(dbPath),
DBhost: dbPath,
DBprefix: "gc_",
DBusername: "gochan",
DBpassword: "password",
DBTimeoutSeconds: 600,
}
migrator := Pre2021Migrator{
migrator := &Pre2021Migrator{
config: Pre2021Config{
SQLConfig: oldSQLConfig,
},
}
outDir := t.TempDir()
db, err := gcsql.Open(&oldSQLConfig)
if !assert.NoError(t, err) {
t.FailNow()
}
migrator.db = db
outDir := t.TempDir()
config.SetTestDBConfig("sqlite3", path.Join(outDir, "gochan-migrated.sqlite3db"), "gochan-migrated.sqlite3db", "gochan", "password", "gc_")
sqlConfig := config.GetSQLConfig()
sqlConfig.DBTimeoutSeconds = 600
if !assert.NoError(t, gcsql.ConnectToDB(&sqlConfig)) {
return
t.FailNow()
}
if !assert.NoError(t, gcsql.CheckAndInitializeDatabase("sqlite3", "4")) {
return
t.FailNow()
}
assert.NoError(t, migrator.migrateBoardsToNewDB())
return migrator
}
func TestMigrateToNewDB(t *testing.T) {
migrator := setupMigrationTest(t)
assert.NoError(t, migrator.migrateBoardsToNewDB())
newBoards, err := gcsql.GetAllBoards(false)
if !assert.NoError(t, err) {
return
}
assert.GreaterOrEqual(t, len(newBoards), 2, "Expected new boards list to have at least 2 boards") // old DB has 2 boards, /test/ and /hidden/
hiddenBoard, err := gcsql.GetBoardFromDir("hidden")
if !assert.NoError(t, err) {
return
}
t.Logf("Hidden board section ID: %d", hiddenBoard.SectionID)
hiddenSection, err := gcsql.GetSectionFromID(hiddenBoard.SectionID)
if !assert.NoError(t, err) {
return
}
assert.Equal(t, "Hidden", hiddenSection.Name, "Expected Hidden section to have name 'Hidden'")
assert.True(t, hiddenSection.Hidden, "Expected Hidden section to be hidden")
}