Skip to content

Commit 398270b

Browse files
committed
compare
1 parent 38d8fd0 commit 398270b

File tree

3 files changed

+85
-19
lines changed

3 files changed

+85
-19
lines changed

models/unittest/fixtures.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,17 @@ import (
1515
"xorm.io/xorm/schemas"
1616
)
1717

18-
var defaultFixturesLoader *fixturesLoader
18+
type FixturesLoader interface {
19+
Load() error
20+
}
21+
22+
var fixturesLoader FixturesLoader
1923

2024
// GetXORMEngine gets the XORM engine
2125
func GetXORMEngine() (x *xorm.Engine) {
2226
return db.GetEngine(db.DefaultContext).(*xorm.Engine)
2327
}
2428

25-
// InitFixtures initialize test fixtures for a test database
26-
func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) {
27-
defaultFixturesLoader = &fixturesLoader{engine: util.IfZero(util.OptionalArg(engine), GetXORMEngine()), opts: opts}
28-
29-
// register the dummy hash algorithm function used in the test fixtures
30-
_ = hash.Register("dummy", hash.NewDummyHasher)
31-
setting.PasswordHashAlgo, _ = hash.SetDefaultPasswordHashAlgorithm("dummy")
32-
return err
33-
}
34-
3529
func loadFixtureResetSeqPgsql(e *xorm.Engine) error {
3630
results, err := e.QueryString(`SELECT 'SELECT SETVAL(' ||
3731
quote_literal(quote_ident(PGT.schemaname) || '.' || quote_ident(S.relname)) ||
@@ -63,14 +57,24 @@ func loadFixtureResetSeqPgsql(e *xorm.Engine) error {
6357
return nil
6458
}
6559

60+
// InitFixtures initialize test fixtures for a test database
61+
func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) {
62+
fixturesLoader = newFixturesLoaderInternal(util.IfZero(util.OptionalArg(engine), GetXORMEngine()), opts)
63+
64+
// register the dummy hash algorithm function used in the test fixtures
65+
_ = hash.Register("dummy", hash.NewDummyHasher)
66+
setting.PasswordHashAlgo, _ = hash.SetDefaultPasswordHashAlgorithm("dummy")
67+
return err
68+
}
69+
6670
// LoadFixtures load fixtures for a test database
6771
func LoadFixtures() error {
68-
if err := defaultFixturesLoader.Load(); err != nil {
72+
if err := fixturesLoader.Load(); err != nil {
6973
return err
7074
}
7175
// Now if we're running postgres we need to tell it to update the sequences
72-
if defaultFixturesLoader.engine.Dialect().URI().DBType == schemas.POSTGRES {
73-
if err := loadFixtureResetSeqPgsql(defaultFixturesLoader.engine); err != nil {
76+
if GetXORMEngine().Dialect().URI().DBType == schemas.POSTGRES {
77+
if err := loadFixtureResetSeqPgsql(GetXORMEngine()); err != nil {
7478
return err
7579
}
7680
}

models/unittest/fixtures_loader.go renamed to models/unittest/fixtures_loader_internal.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ import (
1818
"xorm.io/xorm/schemas"
1919
)
2020

21-
type fixturesLoader struct {
21+
type fixturesLoaderInternal struct {
2222
engine *xorm.Engine
2323
opts FixturesOptions
2424
quoteObject func(string) string
2525
paramPlaceholder func(idx int) string
2626
}
2727

28-
func (f *fixturesLoader) prepareFieldValue(v any) any {
28+
func (f *fixturesLoaderInternal) prepareFieldValue(v any) any {
2929
if s, ok := v.(string); ok {
3030
if strings.HasPrefix(s, "0x") {
3131
b, _ := hex.DecodeString(s[2:])
@@ -35,7 +35,7 @@ func (f *fixturesLoader) prepareFieldValue(v any) any {
3535
return v
3636
}
3737

38-
func (f *fixturesLoader) mssqlTableHasIdentityColumn(q *sql.Tx, tableName string) (bool, error) {
38+
func (f *fixturesLoaderInternal) mssqlTableHasIdentityColumn(q *sql.Tx, tableName string) (bool, error) {
3939
row := q.QueryRow(`SELECT COUNT(*) FROM sys.identity_columns WHERE OBJECT_ID = OBJECT_ID(?)`, tableName)
4040
var count int
4141
if err := row.Scan(&count); err != nil {
@@ -44,7 +44,7 @@ func (f *fixturesLoader) mssqlTableHasIdentityColumn(q *sql.Tx, tableName string
4444
return count > 0, nil
4545
}
4646

47-
func (f *fixturesLoader) loadFixtures(tx *sql.Tx, file string) error {
47+
func (f *fixturesLoaderInternal) loadFixtures(tx *sql.Tx, file string) error {
4848
data, err := os.ReadFile(file)
4949
if err != nil {
5050
return fmt.Errorf("failed to read file %q: %w", file, err)
@@ -105,7 +105,7 @@ func (f *fixturesLoader) loadFixtures(tx *sql.Tx, file string) error {
105105
return nil
106106
}
107107

108-
func (f *fixturesLoader) Load() error {
108+
func (f *fixturesLoaderInternal) Load() error {
109109
goDB := f.engine.DB().DB
110110

111111
switch f.engine.Dialect().URI().DBType {
@@ -152,3 +152,7 @@ func (f *fixturesLoader) Load() error {
152152
}
153153
return tx.Commit()
154154
}
155+
156+
func newFixturesLoaderInternal(x *xorm.Engine, opts FixturesOptions) *fixturesLoaderInternal {
157+
return &fixturesLoaderInternal{engine: x, opts: opts}
158+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package unittest
5+
6+
import (
7+
"code.gitea.io/gitea/modules/auth/password/hash"
8+
"code.gitea.io/gitea/modules/setting"
9+
"fmt"
10+
11+
"github.com/go-testfixtures/testfixtures/v3"
12+
"xorm.io/xorm"
13+
"xorm.io/xorm/schemas"
14+
)
15+
16+
// InitFixtures initialize test fixtures for a test database
17+
func newFixturesLoaderVendor(e *xorm.Engine, opts FixturesOptions) *testfixtures.Loader {
18+
var fixtureOptionFiles func(*testfixtures.Loader) error
19+
if opts.Dir != "" {
20+
fixtureOptionFiles = testfixtures.Directory(opts.Dir)
21+
} else {
22+
fixtureOptionFiles = testfixtures.Files(opts.Files...)
23+
}
24+
var dialect string
25+
switch e.Dialect().URI().DBType {
26+
case schemas.POSTGRES:
27+
dialect = "postgres"
28+
case schemas.MYSQL:
29+
dialect = "mysql"
30+
case schemas.MSSQL:
31+
dialect = "mssql"
32+
case schemas.SQLITE:
33+
dialect = "sqlite3"
34+
default:
35+
panic(fmt.Errorf("unsupported RDBMS for integration tests: %q", e.Dialect().URI().DBType))
36+
}
37+
loaderOptions := []func(loader *testfixtures.Loader) error{
38+
testfixtures.Database(e.DB().DB),
39+
testfixtures.Dialect(dialect),
40+
testfixtures.DangerousSkipTestDatabaseCheck(),
41+
fixtureOptionFiles,
42+
}
43+
44+
if e.Dialect().URI().DBType == schemas.POSTGRES {
45+
loaderOptions = append(loaderOptions, testfixtures.SkipResetSequences())
46+
}
47+
48+
// register the dummy hash algorithm function used in the test fixtures
49+
_ = hash.Register("dummy", hash.NewDummyHasher)
50+
setting.PasswordHashAlgo, _ = hash.SetDefaultPasswordHashAlgorithm("dummy")
51+
52+
loader, err := testfixtures.New(loaderOptions...)
53+
if err != nil {
54+
panic(fmt.Errorf("failed to create fixtures loader: %w", err))
55+
}
56+
57+
return loader
58+
}

0 commit comments

Comments
 (0)