Skip to content

Commit 0b4b2a6

Browse files
committed
fix
1 parent ff96873 commit 0b4b2a6

File tree

11 files changed

+375
-246
lines changed

11 files changed

+375
-246
lines changed

assets/go-licenses.json

Lines changed: 0 additions & 50 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ require (
6161
github.com/go-redsync/redsync/v4 v4.13.0
6262
github.com/go-sql-driver/mysql v1.8.1
6363
github.com/go-swagger/go-swagger v0.31.0
64-
github.com/go-testfixtures/testfixtures/v3 v3.11.0
6564
github.com/go-webauthn/webauthn v0.11.2
6665
github.com/gobwas/glob v0.2.3
6766
github.com/gogs/chardet v0.0.0-20211120154057-b7413eaefb8f
@@ -145,8 +144,6 @@ require (
145144
filippo.io/edwards25519 v1.1.0 // indirect
146145
git.sr.ht/~mariusor/go-xsd-duration v0.0.0-20220703122237-02e73435a078 // indirect
147146
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirect
148-
github.com/ClickHouse/ch-go v0.63.1 // indirect
149-
github.com/ClickHouse/clickhouse-go/v2 v2.24.0 // indirect
150147
github.com/DataDog/zstd v1.5.6 // indirect
151148
github.com/Masterminds/goutils v1.1.1 // indirect
152149
github.com/Masterminds/semver/v3 v3.3.0 // indirect
@@ -204,8 +201,6 @@ require (
204201
github.com/go-ap/errors v0.0.0-20240910140019-1e9d33cc1568 // indirect
205202
github.com/go-asn1-ber/asn1-ber v1.5.7 // indirect
206203
github.com/go-enry/go-oniguruma v1.2.1 // indirect
207-
github.com/go-faster/city v1.0.1 // indirect
208-
github.com/go-faster/errors v0.7.1 // indirect
209204
github.com/go-fed/httpsig v1.1.1-0.20201223112313-55836744818e // indirect
210205
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
211206
github.com/go-ini/ini v1.67.0 // indirect
@@ -270,7 +265,6 @@ require (
270265
github.com/oklog/ulid v1.3.1 // indirect
271266
github.com/olekukonko/tablewriter v0.0.5 // indirect
272267
github.com/onsi/ginkgo v1.16.5 // indirect
273-
github.com/paulmach/orb v0.11.1 // indirect
274268
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
275269
github.com/pierrec/lz4/v4 v4.1.21 // indirect
276270
github.com/pjbgf/sha1cd v0.3.0 // indirect
@@ -285,7 +279,6 @@ require (
285279
github.com/russross/blackfriday/v2 v2.1.0 // indirect
286280
github.com/sagikazarmark/locafero v0.6.0 // indirect
287281
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
288-
github.com/segmentio/asm v1.2.0 // indirect
289282
github.com/shopspring/decimal v1.4.0 // indirect
290283
github.com/shurcooL/httpfs v0.0.0-20230704072500-f1e31cf0ba5c // indirect
291284
github.com/sirupsen/logrus v1.9.3 // indirect
@@ -310,8 +303,6 @@ require (
310303
github.com/zeebo/blake3 v0.2.4 // indirect
311304
go.etcd.io/bbolt v1.3.11 // indirect
312305
go.mongodb.org/mongo-driver v1.17.1 // indirect
313-
go.opentelemetry.io/otel v1.31.0 // indirect
314-
go.opentelemetry.io/otel/trace v1.31.0 // indirect
315306
go.uber.org/atomic v1.11.0 // indirect
316307
go.uber.org/multierr v1.11.0 // indirect
317308
go.uber.org/zap v1.27.0 // indirect

go.sum

Lines changed: 0 additions & 58 deletions
Large diffs are not rendered by default.

models/fixtures/action_run_job.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
name: job2
6565
attempt: 1
6666
job_id: job2
67-
needs: [job1]
67+
needs: '["job1"]'
6868
task_id: 51
6969
status: 5
7070
started: 1683636528

models/fixtures/protected_tag.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22
id: 1
33
repo_id: 4
44
name_pattern: /v.+/
5-
allowlist_user_i_ds: []
6-
allowlist_team_i_ds: []
5+
allowlist_user_i_ds: "[]"
6+
allowlist_team_i_ds: "[]"
77
created_unix: 1715596037
88
updated_unix: 1715596037
99
-
1010
id: 2
1111
repo_id: 1
1212
name_pattern: v-*
13-
allowlist_user_i_ds: []
14-
allowlist_team_i_ds: []
13+
allowlist_user_i_ds: "[]"
14+
allowlist_team_i_ds: "[]"
1515
created_unix: 1715596037
1616
updated_unix: 1715596037
1717
-
1818
id: 3
1919
repo_id: 1
2020
name_pattern: v-1.1
21-
allowlist_user_i_ds: [2]
22-
allowlist_team_i_ds: []
21+
allowlist_user_i_ds: "[2]"
22+
allowlist_team_i_ds: "[]"
2323
created_unix: 1715596037
2424
updated_unix: 1715596037

models/migrations/base/tests.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func PrepareTestEnv(t *testing.T, skip int, syncModels ...any) (*xorm.Engine, fu
7676
t.Errorf("error whilst initializing fixtures from %s: %v", fixturesDir, err)
7777
return x, deferFn
7878
}
79-
if err := unittest.LoadFixtures(x); err != nil {
79+
if err := unittest.LoadFixtures(); err != nil {
8080
t.Errorf("error whilst loading fixtures from %s: %v", fixturesDir, err)
8181
return x, deferFn
8282
}

models/unittest/fixtures.go

Lines changed: 41 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -5,88 +5,29 @@ package unittest
55

66
import (
77
"fmt"
8-
"time"
98

109
"code.gitea.io/gitea/models/db"
1110
"code.gitea.io/gitea/modules/auth/password/hash"
1211
"code.gitea.io/gitea/modules/setting"
12+
"code.gitea.io/gitea/modules/util"
1313

14-
"github.com/go-testfixtures/testfixtures/v3"
1514
"xorm.io/xorm"
1615
"xorm.io/xorm/schemas"
1716
)
1817

19-
var fixturesLoader *testfixtures.Loader
20-
21-
// GetXORMEngine gets the XORM engine
22-
func GetXORMEngine(engine ...*xorm.Engine) (x *xorm.Engine) {
23-
if len(engine) == 1 {
24-
return engine[0]
25-
}
26-
return db.GetEngine(db.DefaultContext).(*xorm.Engine)
18+
type FixturesLoader interface {
19+
Load() error
2720
}
2821

29-
// InitFixtures initialize test fixtures for a test database
30-
func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) {
31-
e := GetXORMEngine(engine...)
32-
var fixtureOptionFiles func(*testfixtures.Loader) error
33-
if opts.Dir != "" {
34-
fixtureOptionFiles = testfixtures.Directory(opts.Dir)
35-
} else {
36-
fixtureOptionFiles = testfixtures.Files(opts.Files...)
37-
}
38-
var dialect string
39-
switch e.Dialect().URI().DBType {
40-
case schemas.POSTGRES:
41-
dialect = "postgres"
42-
case schemas.MYSQL:
43-
dialect = "mysql"
44-
case schemas.MSSQL:
45-
dialect = "mssql"
46-
case schemas.SQLITE:
47-
dialect = "sqlite3"
48-
default:
49-
return fmt.Errorf("unsupported RDBMS for integration tests: %q", e.Dialect().URI().DBType)
50-
}
51-
loaderOptions := []func(loader *testfixtures.Loader) error{
52-
testfixtures.Database(e.DB().DB),
53-
testfixtures.Dialect(dialect),
54-
testfixtures.DangerousSkipTestDatabaseCheck(),
55-
fixtureOptionFiles,
56-
}
57-
58-
if e.Dialect().URI().DBType == schemas.POSTGRES {
59-
loaderOptions = append(loaderOptions, testfixtures.SkipResetSequences())
60-
}
22+
var fixturesLoader FixturesLoader
6123

62-
fixturesLoader, err = testfixtures.New(loaderOptions...)
63-
if err != nil {
64-
return err
65-
}
66-
67-
// register the dummy hash algorithm function used in the test fixtures
68-
_ = hash.Register("dummy", hash.NewDummyHasher)
69-
setting.PasswordHashAlgo, _ = hash.SetDefaultPasswordHashAlgorithm("dummy")
70-
return err
24+
// GetXORMEngine gets the XORM engine
25+
func GetXORMEngine() (x *xorm.Engine) {
26+
return db.GetEngine(db.DefaultContext).(*xorm.Engine)
7127
}
7228

73-
// LoadFixtures load fixtures for a test database
74-
func LoadFixtures(engine ...*xorm.Engine) error {
75-
e := GetXORMEngine(engine...)
76-
var err error
77-
// (doubt) database transaction conflicts could occur and result in ROLLBACK? just try for a few times.
78-
for i := 0; i < 5; i++ {
79-
if err = fixturesLoader.Load(); err == nil {
80-
break
81-
}
82-
time.Sleep(200 * time.Millisecond)
83-
}
84-
if err != nil {
85-
return fmt.Errorf("LoadFixtures failed after retries: %w", err)
86-
}
87-
// Now if we're running postgres we need to tell it to update the sequences
88-
if e.Dialect().URI().DBType == schemas.POSTGRES {
89-
results, err := e.QueryString(`SELECT 'SELECT SETVAL(' ||
29+
func loadFixtureResetSeqPgsql(e *xorm.Engine) error {
30+
results, err := e.QueryString(`SELECT 'SELECT SETVAL(' ||
9031
quote_literal(quote_ident(PGT.schemaname) || '.' || quote_ident(S.relname)) ||
9132
', COALESCE(MAX(' ||quote_ident(C.attname)|| '), 1) ) FROM ' ||
9233
quote_ident(PGT.schemaname)|| '.'||quote_ident(T.relname)|| ';'
@@ -102,19 +43,42 @@ func LoadFixtures(engine ...*xorm.Engine) error {
10243
AND D.refobjsubid = C.attnum
10344
AND T.relname = PGT.tablename
10445
ORDER BY S.relname;`)
105-
if err != nil {
106-
return fmt.Errorf("failed to generate sequence update: %w", err)
107-
}
108-
for _, r := range results {
109-
for _, value := range r {
110-
_, err = e.Exec(value)
111-
if err != nil {
112-
return fmt.Errorf("failed to update sequence: %s, error: %w", value, err)
113-
}
46+
if err != nil {
47+
return fmt.Errorf("failed to generate sequence update: %w", err)
48+
}
49+
for _, r := range results {
50+
for _, value := range r {
51+
_, err = e.Exec(value)
52+
if err != nil {
53+
return fmt.Errorf("failed to update sequence: %s, error: %w", value, err)
11454
}
11555
}
11656
}
57+
return nil
58+
}
59+
60+
// InitFixtures initialize test fixtures for a test database
61+
func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) {
62+
xormEngine := util.IfZero(util.OptionalArg(engine), GetXORMEngine())
63+
fixturesLoader, err = NewFixturesLoader(xormEngine, opts)
64+
// fixturesLoader = NewFixturesLoaderVendor(xormEngine, opts)
65+
66+
// register the dummy hash algorithm function used in the test fixtures
11767
_ = hash.Register("dummy", hash.NewDummyHasher)
11868
setting.PasswordHashAlgo, _ = hash.SetDefaultPasswordHashAlgorithm("dummy")
69+
return err
70+
}
71+
72+
// LoadFixtures load fixtures for a test database
73+
func LoadFixtures() error {
74+
if err := fixturesLoader.Load(); err != nil {
75+
return err
76+
}
77+
// Now if we're running postgres we need to tell it to update the sequences
78+
if GetXORMEngine().Dialect().URI().DBType == schemas.POSTGRES {
79+
if err := loadFixtureResetSeqPgsql(GetXORMEngine()); err != nil {
80+
return err
81+
}
82+
}
11983
return nil
12084
}

0 commit comments

Comments
 (0)