[TESTS] tests.AddFixtures helper loads additional per-test fixtures

(cherry picked from commit 93a844dd13904c0ba1b7fd4a0a233002194a504b)
(cherry picked from commit 6d6d1a121ce3fc5cf7cd92ad1a38be3bdcbf7088)
(cherry picked from commit 8b101f2860dfbdfd99de71d30740c9e72e1cd9d5)
(cherry picked from commit 3e56212d6d1bca0aecdc1f224c7d78287ef9d35d)
(cherry picked from commit 4f619bc58583892c197ee2588ead929342336217)
(cherry picked from commit 06a47ea56efdb604c51d1bda91a9cd9eeee12bd2)
(cherry picked from commit 5a4d56e77b6b266f684bd36c652cb6496da8c1b4)
(cherry picked from commit 84b9d3a0c3a86e19f129cfb7ee4816e2eec12234)
(cherry picked from commit 1eb2eca71c1b4b58dcdb87b70b40b0666512c9d6)
(cherry picked from commit 11d0fe54009d34eca00827608ca8a97e21bc85db)
(cherry picked from commit c93b8b9d3c69bca079eb192eeb06850b80d901ef)
(cherry picked from commit 679a7e2efa85f2dcda3b17fc246c209a57d3dead)
(cherry picked from commit e31a3abb7d)
(cherry picked from commit 72bedf68a7a86aa214169e67d3e0d04cafd4ce4b)
(cherry picked from commit ef139ac06f29d561c82c847c5d1a648db38dc759)
(cherry picked from commit 134bf839825d720fd43c08076f1dad2ba23aca51)
(cherry picked from commit caf5780c5758851cc5d7d584ed5e78291e5d1d14)
This commit is contained in:
Earl Warren 2023-10-01 18:38:19 +02:00
parent 52e863ba5a
commit 27ee15e86e
No known key found for this signature in database
GPG Key ID: 0579CB2928A78A00
3 changed files with 30 additions and 0 deletions

View File

@ -7,6 +7,7 @@ package unittest
import (
"fmt"
"os"
"path/filepath"
"time"
"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)
}
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
func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) {
e := GetXORMEngine(engine...)
@ -37,6 +48,12 @@ func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) {
} else {
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"
switch e.Dialect().URI().DBType {
case schemas.POSTGRES:
@ -57,6 +74,7 @@ func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) {
testfixtures.DangerousSkipTestDatabaseCheck(),
fixtureOptionFiles,
}
loaderOptions = append(loaderOptions, fixtureOptionDirs...)
if e.Dialect().URI().DBType == schemas.POSTGRES {
loaderOptions = append(loaderOptions, testfixtures.SkipResetSequences())

View File

@ -208,6 +208,8 @@ func MainTest(m *testing.M, testOpts ...*TestOptions) {
type FixturesOptions struct {
Dir string
Files []string
Dirs []string
Base string
}
// CreateTestEngine creates a memory database and loads the fixture data from fixturesDir

View File

@ -267,3 +267,13 @@ func PrintCurrentTest(t testing.TB, skip ...int) func() {
func Printf(format string, args ...any) {
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,
},
)
}