Skip to content

Commit e422342

Browse files
authored
1 parent 01214c8 commit e422342

File tree

31 files changed

+314
-138
lines changed

31 files changed

+314
-138
lines changed

models/db/iterate_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ func TestIterate(t *testing.T) {
1919
xe := unittest.GetXORMEngine()
2020
assert.NoError(t, xe.Sync(&repo_model.RepoUnit{}))
2121

22-
var repoCnt int
23-
err := db.Iterate(db.DefaultContext, nil, func(ctx context.Context, repo *repo_model.RepoUnit) error {
24-
repoCnt++
22+
cnt, err := db.GetEngine(db.DefaultContext).Count(&repo_model.RepoUnit{})
23+
assert.NoError(t, err)
24+
25+
var repoUnitCnt int
26+
err = db.Iterate(db.DefaultContext, nil, func(ctx context.Context, repo *repo_model.RepoUnit) error {
27+
repoUnitCnt++
2528
return nil
2629
})
2730
assert.NoError(t, err)
28-
assert.EqualValues(t, 89, repoCnt)
31+
assert.EqualValues(t, cnt, repoUnitCnt)
2932

3033
err = db.Iterate(db.DefaultContext, nil, func(ctx context.Context, repoUnit *repo_model.RepoUnit) error {
3134
reopUnit2 := repo_model.RepoUnit{ID: repoUnit.ID}

models/db/list_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,20 @@ func TestFind(t *testing.T) {
3131
xe := unittest.GetXORMEngine()
3232
assert.NoError(t, xe.Sync(&repo_model.RepoUnit{}))
3333

34+
var repoUnitCount int
35+
_, err := db.GetEngine(db.DefaultContext).SQL("SELECT COUNT(*) FROM repo_unit").Get(&repoUnitCount)
36+
assert.NoError(t, err)
37+
assert.NotEmpty(t, repoUnitCount)
38+
3439
opts := mockListOptions{}
3540
var repoUnits []repo_model.RepoUnit
36-
err := db.Find(db.DefaultContext, &opts, &repoUnits)
41+
err = db.Find(db.DefaultContext, &opts, &repoUnits)
3742
assert.NoError(t, err)
38-
assert.EqualValues(t, 89, len(repoUnits))
43+
assert.EqualValues(t, repoUnitCount, len(repoUnits))
3944

4045
cnt, err := db.Count(db.DefaultContext, &opts, new(repo_model.RepoUnit))
4146
assert.NoError(t, err)
42-
assert.EqualValues(t, 89, cnt)
47+
assert.EqualValues(t, repoUnitCount, cnt)
4348

4449
repoUnits = make([]repo_model.RepoUnit, 0, 10)
4550
newCnt, err := db.FindAndCount(db.DefaultContext, &opts, &repoUnits)

models/dbfs/dbfs_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import (
1212
"code.gitea.io/gitea/models/db"
1313

1414
"github.com/stretchr/testify/assert"
15-
16-
_ "github.com/mattn/go-sqlite3"
1715
)
1816

1917
func changeDefaultFileBlockSize(n int64) (restore func()) {

models/fixtures/repo_unit.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,3 +601,9 @@
601601
repo_id: 57
602602
type: 5
603603
created_unix: 946684810
604+
605+
-
606+
id: 90
607+
repo_id: 52
608+
type: 1
609+
created_unix: 946684810

models/fixtures/repository.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,6 +1560,7 @@
15601560
owner_name: user30
15611561
lower_name: empty
15621562
name: empty
1563+
default_branch: master
15631564
num_watches: 0
15641565
num_stars: 0
15651566
num_forks: 0

models/fixtures/user.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,7 @@
10911091
max_repo_creation: -1
10921092
is_active: true
10931093
is_admin: false
1094-
is_restricted: true
1094+
is_restricted: false
10951095
allow_git_hook: false
10961096
allow_import_local: false
10971097
allow_create_organization: true

models/repo/repo.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,12 @@ func (repo *Repository) IsBroken() bool {
225225
return repo.Status == RepositoryBroken
226226
}
227227

228+
// MarkAsBrokenEmpty marks the repo as broken and empty
229+
func (repo *Repository) MarkAsBrokenEmpty() {
230+
repo.Status = RepositoryBroken
231+
repo.IsEmpty = true
232+
}
233+
228234
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
229235
func (repo *Repository) AfterLoad() {
230236
repo.NumOpenIssues = repo.NumIssues - repo.NumClosedIssues
@@ -729,7 +735,7 @@ func IsRepositoryExist(ctx context.Context, u *user_model.User, repoName string)
729735
return false, err
730736
}
731737
isDir, err := util.IsDir(RepoPath(u.Name, repoName))
732-
return has && isDir, err
738+
return has || isDir, err
733739
}
734740

735741
// GetTemplateRepo populates repo.TemplateRepo for a generated repository and

models/unittest/fixtures.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"xorm.io/xorm/schemas"
1818
)
1919

20-
var fixtures *testfixtures.Loader
20+
var fixturesLoader *testfixtures.Loader
2121

2222
// GetXORMEngine gets the XORM engine
2323
func GetXORMEngine(engine ...*xorm.Engine) (x *xorm.Engine) {
@@ -30,11 +30,11 @@ func GetXORMEngine(engine ...*xorm.Engine) (x *xorm.Engine) {
3030
// InitFixtures initialize test fixtures for a test database
3131
func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) {
3232
e := GetXORMEngine(engine...)
33-
var testfiles func(*testfixtures.Loader) error
33+
var fixtureOptionFiles func(*testfixtures.Loader) error
3434
if opts.Dir != "" {
35-
testfiles = testfixtures.Directory(opts.Dir)
35+
fixtureOptionFiles = testfixtures.Directory(opts.Dir)
3636
} else {
37-
testfiles = testfixtures.Files(opts.Files...)
37+
fixtureOptionFiles = testfixtures.Files(opts.Files...)
3838
}
3939
dialect := "unknown"
4040
switch e.Dialect().URI().DBType {
@@ -54,14 +54,14 @@ func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) {
5454
testfixtures.Database(e.DB().DB),
5555
testfixtures.Dialect(dialect),
5656
testfixtures.DangerousSkipTestDatabaseCheck(),
57-
testfiles,
57+
fixtureOptionFiles,
5858
}
5959

6060
if e.Dialect().URI().DBType == schemas.POSTGRES {
6161
loaderOptions = append(loaderOptions, testfixtures.SkipResetSequences())
6262
}
6363

64-
fixtures, err = testfixtures.New(loaderOptions...)
64+
fixturesLoader, err = testfixtures.New(loaderOptions...)
6565
if err != nil {
6666
return err
6767
}
@@ -78,11 +78,9 @@ func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) {
7878
func LoadFixtures(engine ...*xorm.Engine) error {
7979
e := GetXORMEngine(engine...)
8080
var err error
81-
// Database transaction conflicts could occur and result in ROLLBACK
82-
// As a simple workaround, we just retry 20 times.
83-
for i := 0; i < 20; i++ {
84-
err = fixtures.Load()
85-
if err == nil {
81+
// (doubt) database transaction conflicts could occur and result in ROLLBACK? just try for a few times.
82+
for i := 0; i < 5; i++ {
83+
if err = fixturesLoader.Load(); err == nil {
8684
break
8785
}
8886
time.Sleep(200 * time.Millisecond)

models/user/user_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package user_test
55

66
import (
77
"context"
8+
"fmt"
89
"math/rand"
910
"strings"
1011
"testing"
@@ -64,9 +65,10 @@ func TestSearchUsers(t *testing.T) {
6465
testSuccess := func(opts *user_model.SearchUserOptions, expectedUserOrOrgIDs []int64) {
6566
users, _, err := user_model.SearchUsers(opts)
6667
assert.NoError(t, err)
67-
if assert.Len(t, users, len(expectedUserOrOrgIDs), opts) {
68+
cassText := fmt.Sprintf("ids: %v, opts: %v", expectedUserOrOrgIDs, opts)
69+
if assert.Len(t, users, len(expectedUserOrOrgIDs), "case: %s", cassText) {
6870
for i, expectedID := range expectedUserOrOrgIDs {
69-
assert.EqualValues(t, expectedID, users[i].ID)
71+
assert.EqualValues(t, expectedID, users[i].ID, "case: %s", cassText)
7072
}
7173
}
7274
}
@@ -118,7 +120,7 @@ func TestSearchUsers(t *testing.T) {
118120
[]int64{1})
119121

120122
testUserSuccess(&user_model.SearchUserOptions{ListOptions: db.ListOptions{Page: 1}, IsRestricted: util.OptionalBoolTrue},
121-
[]int64{29, 30})
123+
[]int64{29})
122124

123125
testUserSuccess(&user_model.SearchUserOptions{ListOptions: db.ListOptions{Page: 1}, IsProhibitLogin: util.OptionalBoolTrue},
124126
[]int64{30})

modules/context/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func (ctx *Context) serverErrorInternal(logMsg string, logErr error) {
301301

302302
// it's safe to show internal error to admin users, and it helps
303303
if !setting.IsProd || (ctx.Doer != nil && ctx.Doer.IsAdmin) {
304-
ctx.Data["ErrorMsg"] = logErr
304+
ctx.Data["ErrorMsg"] = fmt.Sprintf("%s, %s", logMsg, logErr)
305305
}
306306
}
307307

0 commit comments

Comments
 (0)