mirror of
https://github.com/Eggbertx/gochan.git
synced 2025-08-05 04:36:24 -07:00
Move setup functionality in test to a separate function
This commit is contained in:
parent
90986e58b8
commit
beb048716e
1 changed files with 44 additions and 17 deletions
|
@ -4,7 +4,6 @@ import (
|
||||||
"path"
|
"path"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/gochan-org/gochan/cmd/gochan-migration/internal/common"
|
|
||||||
"github.com/gochan-org/gochan/pkg/config"
|
"github.com/gochan-org/gochan/pkg/config"
|
||||||
"github.com/gochan-org/gochan/pkg/gcsql"
|
"github.com/gochan-org/gochan/pkg/gcsql"
|
||||||
"github.com/gochan-org/gochan/pkg/gcutil/testutil"
|
"github.com/gochan-org/gochan/pkg/gcutil/testutil"
|
||||||
|
@ -15,40 +14,68 @@ const (
|
||||||
sqlite3DBPath = "tools/gochan-pre2021.sqlite3db" // relative to gochan project root
|
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)
|
dir, err := testutil.GoToGochanRoot(t)
|
||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
return
|
t.FailNow()
|
||||||
}
|
|
||||||
if !assert.NoError(t, common.InitTestMigrationLog(t)) {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
dbPath := path.Join(dir, sqlite3DBPath)
|
dbPath := path.Join(dir, sqlite3DBPath)
|
||||||
|
|
||||||
oldSQLConfig := config.SQLConfig{
|
oldSQLConfig := config.SQLConfig{
|
||||||
DBtype: "sqlite3",
|
DBtype: "sqlite3",
|
||||||
DBname: path.Base(dbPath),
|
DBname: path.Base(dbPath),
|
||||||
DBhost: dbPath,
|
DBhost: dbPath,
|
||||||
DBprefix: "gc_",
|
DBprefix: "gc_",
|
||||||
DBusername: "gochan",
|
DBusername: "gochan",
|
||||||
DBpassword: "password",
|
DBpassword: "password",
|
||||||
|
DBTimeoutSeconds: 600,
|
||||||
}
|
}
|
||||||
migrator := Pre2021Migrator{
|
migrator := &Pre2021Migrator{
|
||||||
config: Pre2021Config{
|
config: Pre2021Config{
|
||||||
SQLConfig: oldSQLConfig,
|
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_")
|
config.SetTestDBConfig("sqlite3", path.Join(outDir, "gochan-migrated.sqlite3db"), "gochan-migrated.sqlite3db", "gochan", "password", "gc_")
|
||||||
sqlConfig := config.GetSQLConfig()
|
sqlConfig := config.GetSQLConfig()
|
||||||
|
sqlConfig.DBTimeoutSeconds = 600
|
||||||
|
|
||||||
if !assert.NoError(t, gcsql.ConnectToDB(&sqlConfig)) {
|
if !assert.NoError(t, gcsql.ConnectToDB(&sqlConfig)) {
|
||||||
return
|
t.FailNow()
|
||||||
}
|
}
|
||||||
if !assert.NoError(t, gcsql.CheckAndInitializeDatabase("sqlite3", "4")) {
|
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")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue