mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-24 22:20:35 +00:00
[TESTS] tests.AddFixtures helper loads additional per-test fixtures
(cherry picked from commit93a844dd13
) (cherry picked from commit6d6d1a121c
) (cherry picked from commit8b101f2860
) (cherry picked from commit3e56212d6d
) (cherry picked from commit4f619bc585
)
This commit is contained in:
parent
e9aa373db5
commit
3380217da1
|
@ -7,6 +7,7 @@ package unittest
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.gitea.io/gitea/models/db"
|
"code.gitea.io/gitea/models/db"
|
||||||
|
@ -28,6 +29,16 @@ func GetXORMEngine(engine ...*xorm.Engine) (x *xorm.Engine) {
|
||||||
return db.DefaultContext.(*db.Context).Engine().(*xorm.Engine)
|
return db.DefaultContext.(*db.Context).Engine().(*xorm.Engine)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func OverrideFixtures(opts FixturesOptions, engine ...*xorm.Engine) func() {
|
||||||
|
old := fixturesLoader
|
||||||
|
if err := InitFixtures(opts, engine...); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return func() {
|
||||||
|
fixturesLoader = old
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// InitFixtures initialize test fixtures for a test database
|
// InitFixtures initialize test fixtures for a test database
|
||||||
func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) {
|
func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) {
|
||||||
e := GetXORMEngine(engine...)
|
e := GetXORMEngine(engine...)
|
||||||
|
@ -37,6 +48,12 @@ func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) {
|
||||||
} else {
|
} else {
|
||||||
fixtureOptionFiles = testfixtures.Files(opts.Files...)
|
fixtureOptionFiles = testfixtures.Files(opts.Files...)
|
||||||
}
|
}
|
||||||
|
var fixtureOptionDirs []func(*testfixtures.Loader) error
|
||||||
|
if opts.Dirs != nil {
|
||||||
|
for _, dir := range opts.Dirs {
|
||||||
|
fixtureOptionDirs = append(fixtureOptionDirs, testfixtures.Directory(filepath.Join(opts.Base, dir)))
|
||||||
|
}
|
||||||
|
}
|
||||||
dialect := "unknown"
|
dialect := "unknown"
|
||||||
switch e.Dialect().URI().DBType {
|
switch e.Dialect().URI().DBType {
|
||||||
case schemas.POSTGRES:
|
case schemas.POSTGRES:
|
||||||
|
@ -57,6 +74,7 @@ func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) {
|
||||||
testfixtures.DangerousSkipTestDatabaseCheck(),
|
testfixtures.DangerousSkipTestDatabaseCheck(),
|
||||||
fixtureOptionFiles,
|
fixtureOptionFiles,
|
||||||
}
|
}
|
||||||
|
loaderOptions = append(loaderOptions, fixtureOptionDirs...)
|
||||||
|
|
||||||
if e.Dialect().URI().DBType == schemas.POSTGRES {
|
if e.Dialect().URI().DBType == schemas.POSTGRES {
|
||||||
loaderOptions = append(loaderOptions, testfixtures.SkipResetSequences())
|
loaderOptions = append(loaderOptions, testfixtures.SkipResetSequences())
|
||||||
|
|
|
@ -198,6 +198,8 @@ func MainTest(m *testing.M, testOpts *TestOptions) {
|
||||||
type FixturesOptions struct {
|
type FixturesOptions struct {
|
||||||
Dir string
|
Dir string
|
||||||
Files []string
|
Files []string
|
||||||
|
Dirs []string
|
||||||
|
Base string
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateTestEngine creates a memory database and loads the fixture data from fixturesDir
|
// CreateTestEngine creates a memory database and loads the fixture data from fixturesDir
|
||||||
|
|
|
@ -264,3 +264,13 @@ func PrintCurrentTest(t testing.TB, skip ...int) func() {
|
||||||
func Printf(format string, args ...any) {
|
func Printf(format string, args ...any) {
|
||||||
testlogger.Printf(format, args...)
|
testlogger.Printf(format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AddFixtures(dirs ...string) func() {
|
||||||
|
return unittest.OverrideFixtures(
|
||||||
|
unittest.FixturesOptions{
|
||||||
|
Dir: filepath.Join(filepath.Dir(setting.AppPath), "models/fixtures/"),
|
||||||
|
Base: filepath.Dir(setting.AppPath),
|
||||||
|
Dirs: dirs,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue