mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in NOTICE, alongside LICENSE. The header lint accepts any copyright line above the SPDX line, so it no longer demands an upstream one. Co-authored-by: Hanzo Dev <[email protected]>
121 lines
3.2 KiB
Go
121 lines
3.2 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package migrationtest
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/hanzoai/git/models/db"
|
|
"github.com/hanzoai/git/models/unittest"
|
|
"github.com/hanzoai/git/modules/git"
|
|
"github.com/hanzoai/git/modules/setting"
|
|
"github.com/hanzoai/git/modules/testlogger"
|
|
|
|
"github.com/hanzoai/xorm/schemas"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// PrepareTestEnv prepares the test environment and reset the database. The skip parameter should usually be 0.
|
|
// Provide models to be sync'd with the database - in particular any models you expect fixtures to be loaded from.
|
|
//
|
|
// fixtures in `models/migrations/fixtures/<TestName>` will be loaded automatically
|
|
func PrepareTestEnv(t *testing.T, skip int, syncModels ...any) (db.EngineMigration, func()) {
|
|
t.Helper()
|
|
ourSkip := 2
|
|
ourSkip += skip
|
|
deferFn := testlogger.PrintCurrentTest(t, ourSkip)
|
|
gitRoot := setting.GetGitTestSourceRoot()
|
|
require.NoError(t, unittest.SyncDirs(filepath.Join(gitRoot, "tests/git-repositories-meta"), setting.RepoRootPath))
|
|
|
|
cleanup, err := unittest.ResetTestDatabase()
|
|
if err != nil {
|
|
t.Fatalf("unable to reset database: %v", err)
|
|
return nil, deferFn
|
|
}
|
|
{
|
|
oldDefer := deferFn
|
|
deferFn = func() {
|
|
cleanup()
|
|
oldDefer()
|
|
}
|
|
}
|
|
|
|
err = db.InitEngine(t.Context())
|
|
if !assert.NoError(t, err) {
|
|
return nil, deferFn
|
|
}
|
|
x := unittest.GetXORMEngine()
|
|
{
|
|
oldDefer := deferFn
|
|
deferFn = func() {
|
|
_ = x.Close()
|
|
oldDefer()
|
|
}
|
|
}
|
|
|
|
if len(syncModels) > 0 {
|
|
if err := x.Sync(syncModels...); err != nil {
|
|
t.Errorf("error during sync: %v", err)
|
|
return x, deferFn
|
|
}
|
|
}
|
|
|
|
fixturesDir := filepath.Join(gitRoot, "models", "migrations", "fixtures", t.Name())
|
|
|
|
if _, err := os.Stat(fixturesDir); err == nil {
|
|
t.Logf("initializing fixtures from: %s", fixturesDir)
|
|
if err := unittest.InitFixtures(
|
|
unittest.FixturesOptions{
|
|
Dir: fixturesDir,
|
|
}); err != nil {
|
|
t.Errorf("error whilst initializing fixtures from %s: %v", fixturesDir, err)
|
|
return x, deferFn
|
|
}
|
|
if err := unittest.LoadFixtures(); err != nil {
|
|
t.Errorf("error whilst loading fixtures from %s: %v", fixturesDir, err)
|
|
return x, deferFn
|
|
}
|
|
} else if !os.IsNotExist(err) {
|
|
t.Errorf("unexpected error whilst checking for existence of fixtures: %v", err)
|
|
} else {
|
|
t.Logf("no fixtures found in: %s", fixturesDir)
|
|
}
|
|
|
|
return x, deferFn
|
|
}
|
|
|
|
func LoadTableSchemasMap(t *testing.T, x db.EngineMigration) map[string]*schemas.Table {
|
|
tables, err := x.DBMetas()
|
|
require.NoError(t, err)
|
|
tableMap := make(map[string]*schemas.Table)
|
|
for _, table := range tables {
|
|
tableMap[table.Name] = table
|
|
}
|
|
return tableMap
|
|
}
|
|
|
|
func mainTest(m *testing.M) int {
|
|
testlogger.Init()
|
|
err := setting.PrepareIntegrationTestConfig()
|
|
if err != nil {
|
|
return testlogger.MainErrorf("Unable to prepare integration test config: %v", err)
|
|
}
|
|
setting.SetupGitTestEnv()
|
|
|
|
if err = git.InitFull(); err != nil {
|
|
return testlogger.MainErrorf("Unable to InitFull: %v", err)
|
|
}
|
|
setting.Database.SlowQueryThreshold = 0
|
|
setting.LoadDBSetting()
|
|
setting.InitLoggersForTest()
|
|
return m.Run()
|
|
}
|
|
|
|
func MainTest(m *testing.M) {
|
|
os.Exit(mainTest(m))
|
|
}
|