Skip to content

Commit 2cd3bd9

Browse files
committed
use unit test bridge instead of unit test api
1 parent d97a960 commit 2cd3bd9

File tree

7 files changed

+95
-85
lines changed

7 files changed

+95
-85
lines changed

integrations/integration_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ import (
3434
"code.gitea.io/gitea/modules/queue"
3535
"code.gitea.io/gitea/modules/setting"
3636
"code.gitea.io/gitea/modules/storage"
37-
"code.gitea.io/gitea/modules/unittestapi"
38-
"code.gitea.io/gitea/modules/unittestassert"
3937
"code.gitea.io/gitea/modules/util"
4038
"code.gitea.io/gitea/modules/web"
4139
"code.gitea.io/gitea/routers"
@@ -86,7 +84,7 @@ func NewNilResponseHashSumRecorder() *NilResponseHashSumRecorder {
8684
func TestMain(m *testing.M) {
8785
defer log.Close()
8886

89-
unittestapi.SetNewAsserterFunc(unittestassert.NewTestifyAsserter)
87+
unittest.InitUnitTestBridge()
9088
managerCtx, cancel := context.WithCancel(context.Background())
9189
graceful.InitManager(managerCtx)
9290
defer cancel()

models/consistency.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import (
99
"strings"
1010

1111
"code.gitea.io/gitea/models/db"
12-
"code.gitea.io/gitea/modules/unittestapi"
12+
"code.gitea.io/gitea/modules/unittestbridge"
1313

1414
"xorm.io/builder"
1515
)
1616

1717
// CheckConsistencyFor test that all matching database entries are consistent
18-
func CheckConsistencyFor(t unittestapi.Tester, beansToCheck ...interface{}) {
19-
ta := unittestapi.NewAsserter(t)
18+
func CheckConsistencyFor(t unittestbridge.Tester, beansToCheck ...interface{}) {
19+
ta := unittestbridge.NewAsserter(t)
2020
for _, bean := range beansToCheck {
2121
sliceType := reflect.SliceOf(reflect.TypeOf(bean))
2222
sliceValue := reflect.MakeSlice(sliceType, 0, 10)
@@ -34,7 +34,7 @@ func CheckConsistencyFor(t unittestapi.Tester, beansToCheck ...interface{}) {
3434
}
3535
}
3636

37-
func checkForConsistency(ta unittestapi.Asserter, bean interface{}) {
37+
func checkForConsistency(ta unittestbridge.Asserter, bean interface{}) {
3838
switch b := bean.(type) {
3939
case *User:
4040
checkForUserConsistency(b, ta)
@@ -58,19 +58,19 @@ func checkForConsistency(ta unittestapi.Asserter, bean interface{}) {
5858
}
5959

6060
// getCount get the count of database entries matching bean
61-
func getCount(ta unittestapi.Asserter, e db.Engine, bean interface{}) int64 {
61+
func getCount(ta unittestbridge.Asserter, e db.Engine, bean interface{}) int64 {
6262
count, err := e.Count(bean)
6363
ta.NoError(err)
6464
return count
6565
}
6666

6767
// assertCount test the count of database entries matching bean
68-
func assertCount(ta unittestapi.Asserter, bean interface{}, expected int) {
68+
func assertCount(ta unittestbridge.Asserter, bean interface{}, expected int) {
6969
ta.EqualValues(expected, getCount(ta, db.GetEngine(db.DefaultContext), bean),
7070
"Failed consistency test, the counted bean (of type %T) was %+v", bean, bean)
7171
}
7272

73-
func checkForUserConsistency(user *User, ta unittestapi.Asserter) {
73+
func checkForUserConsistency(user *User, ta unittestbridge.Asserter) {
7474
assertCount(ta, &Repository{OwnerID: user.ID}, user.NumRepos)
7575
assertCount(ta, &Star{UID: user.ID}, user.NumStars)
7676
assertCount(ta, &OrgUser{OrgID: user.ID}, user.NumMembers)
@@ -83,7 +83,7 @@ func checkForUserConsistency(user *User, ta unittestapi.Asserter) {
8383
}
8484
}
8585

86-
func checkForRepoConsistency(repo *Repository, ta unittestapi.Asserter) {
86+
func checkForRepoConsistency(repo *Repository, ta unittestbridge.Asserter) {
8787
ta.Equal(repo.LowerName, strings.ToLower(repo.Name), "repo: %+v", repo)
8888
assertCount(ta, &Star{RepoID: repo.ID}, repo.NumStars)
8989
assertCount(ta, &Milestone{RepoID: repo.ID}, repo.NumMilestones)
@@ -117,7 +117,7 @@ func checkForRepoConsistency(repo *Repository, ta unittestapi.Asserter) {
117117
"Unexpected number of closed milestones for repo %+v", repo)
118118
}
119119

120-
func checkForIssueConsistency(issue *Issue, ta unittestapi.Asserter) {
120+
func checkForIssueConsistency(issue *Issue, ta unittestbridge.Asserter) {
121121
actual := getCount(ta, db.GetEngine(db.DefaultContext).Where("type=?", CommentTypeComment), &Comment{IssueID: issue.ID})
122122
ta.EqualValues(issue.NumComments, actual,
123123
"Unexpected number of comments for issue %+v", issue)
@@ -127,13 +127,13 @@ func checkForIssueConsistency(issue *Issue, ta unittestapi.Asserter) {
127127
}
128128
}
129129

130-
func checkForPullRequestConsistency(pr *PullRequest, ta unittestapi.Asserter) {
130+
func checkForPullRequestConsistency(pr *PullRequest, ta unittestbridge.Asserter) {
131131
issue := db.AssertExistsAndLoadBean(ta, &Issue{ID: pr.IssueID}).(*Issue)
132132
ta.True(issue.IsPull)
133133
ta.EqualValues(issue.Index, pr.Index)
134134
}
135135

136-
func checkForMilestoneConsistency(milestone *Milestone, ta unittestapi.Asserter) {
136+
func checkForMilestoneConsistency(milestone *Milestone, ta unittestbridge.Asserter) {
137137
assertCount(ta, &Issue{MilestoneID: milestone.ID}, milestone.NumIssues)
138138

139139
actual := getCount(ta, db.GetEngine(db.DefaultContext).Where("is_closed=?", true), &Issue{MilestoneID: milestone.ID})
@@ -147,7 +147,7 @@ func checkForMilestoneConsistency(milestone *Milestone, ta unittestapi.Asserter)
147147
ta.Equal(completeness, milestone.Completeness)
148148
}
149149

150-
func checkForLabelConsistency(label *Label, ta unittestapi.Asserter) {
150+
func checkForLabelConsistency(label *Label, ta unittestbridge.Asserter) {
151151
issueLabels := make([]*IssueLabel, 0, 10)
152152
ta.NoError(db.GetEngine(db.DefaultContext).Find(&issueLabels, &IssueLabel{LabelID: label.ID}))
153153
ta.EqualValues(label.NumIssues, len(issueLabels),
@@ -166,12 +166,12 @@ func checkForLabelConsistency(label *Label, ta unittestapi.Asserter) {
166166
"Unexpected number of closed issues for label %+v", label)
167167
}
168168

169-
func checkForTeamConsistency(team *Team, ta unittestapi.Asserter) {
169+
func checkForTeamConsistency(team *Team, ta unittestbridge.Asserter) {
170170
assertCount(ta, &TeamUser{TeamID: team.ID}, team.NumMembers)
171171
assertCount(ta, &TeamRepo{TeamID: team.ID}, team.NumRepos)
172172
}
173173

174-
func checkForActionConsistency(action *Action, ta unittestapi.Asserter) {
174+
func checkForActionConsistency(action *Action, ta unittestbridge.Asserter) {
175175
repo := db.AssertExistsAndLoadBean(ta, &Repository{ID: action.RepoID}).(*Repository)
176176
ta.Equal(repo.IsPrivate, action.IsPrivate, "action: %+v", action)
177177
}

models/db/unit_tests.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"context"
99
"math"
1010

11-
"code.gitea.io/gitea/modules/unittestapi"
11+
"code.gitea.io/gitea/modules/unittestbridge"
1212

1313
"xorm.io/xorm"
1414
)
@@ -58,16 +58,16 @@ func LoadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error)
5858
}
5959

6060
// BeanExists for testing, check if a bean exists
61-
func BeanExists(t unittestapi.Tester, bean interface{}, conditions ...interface{}) bool {
62-
ta := unittestapi.NewAsserter(t)
61+
func BeanExists(t unittestbridge.Tester, bean interface{}, conditions ...interface{}) bool {
62+
ta := unittestbridge.NewAsserter(t)
6363
exists, err := LoadBeanIfExists(bean, conditions...)
6464
ta.NoError(err)
6565
return exists
6666
}
6767

6868
// AssertExistsAndLoadBean assert that a bean exists and load it from the test database
69-
func AssertExistsAndLoadBean(t unittestapi.Tester, bean interface{}, conditions ...interface{}) interface{} {
70-
ta := unittestapi.NewAsserter(t)
69+
func AssertExistsAndLoadBean(t unittestbridge.Tester, bean interface{}, conditions ...interface{}) interface{} {
70+
ta := unittestbridge.NewAsserter(t)
7171
exists, err := LoadBeanIfExists(bean, conditions...)
7272
ta.NoError(err)
7373
ta.True(exists,
@@ -77,8 +77,8 @@ func AssertExistsAndLoadBean(t unittestapi.Tester, bean interface{}, conditions
7777
}
7878

7979
// GetCount get the count of a bean
80-
func GetCount(t unittestapi.Tester, bean interface{}, conditions ...interface{}) int {
81-
ta := unittestapi.NewAsserter(t)
80+
func GetCount(t unittestbridge.Tester, bean interface{}, conditions ...interface{}) int {
81+
ta := unittestbridge.NewAsserter(t)
8282
sess := x.NewSession()
8383
defer sess.Close()
8484
whereConditions(sess, conditions)
@@ -88,38 +88,38 @@ func GetCount(t unittestapi.Tester, bean interface{}, conditions ...interface{})
8888
}
8989

9090
// AssertNotExistsBean assert that a bean does not exist in the test database
91-
func AssertNotExistsBean(t unittestapi.Tester, bean interface{}, conditions ...interface{}) {
92-
ta := unittestapi.NewAsserter(t)
91+
func AssertNotExistsBean(t unittestbridge.Tester, bean interface{}, conditions ...interface{}) {
92+
ta := unittestbridge.NewAsserter(t)
9393
exists, err := LoadBeanIfExists(bean, conditions...)
9494
ta.NoError(err)
9595
ta.False(exists)
9696
}
9797

9898
// AssertExistsIf asserts that a bean exists or does not exist, depending on
9999
// what is expected.
100-
func AssertExistsIf(t unittestapi.Tester, expected bool, bean interface{}, conditions ...interface{}) {
101-
ta := unittestapi.NewAsserter(t)
100+
func AssertExistsIf(t unittestbridge.Tester, expected bool, bean interface{}, conditions ...interface{}) {
101+
ta := unittestbridge.NewAsserter(t)
102102
exists, err := LoadBeanIfExists(bean, conditions...)
103103
ta.NoError(err)
104104
ta.Equal(expected, exists)
105105
}
106106

107107
// AssertSuccessfulInsert assert that beans is successfully inserted
108-
func AssertSuccessfulInsert(t unittestapi.Tester, beans ...interface{}) {
109-
ta := unittestapi.NewAsserter(t)
108+
func AssertSuccessfulInsert(t unittestbridge.Tester, beans ...interface{}) {
109+
ta := unittestbridge.NewAsserter(t)
110110
_, err := x.Insert(beans...)
111111
ta.NoError(err)
112112
}
113113

114114
// AssertCount assert the count of a bean
115-
func AssertCount(t unittestapi.Tester, bean, expected interface{}) {
116-
ta := unittestapi.NewAsserter(t)
115+
func AssertCount(t unittestbridge.Tester, bean, expected interface{}) {
116+
ta := unittestbridge.NewAsserter(t)
117117
ta.EqualValues(expected, GetCount(ta, bean))
118118
}
119119

120120
// AssertInt64InRange assert value is in range [low, high]
121-
func AssertInt64InRange(t unittestapi.Tester, low, high, value int64) {
122-
ta := unittestapi.NewAsserter(t)
121+
func AssertInt64InRange(t unittestbridge.Tester, low, high, value int64) {
122+
ta := unittestbridge.NewAsserter(t)
123123
ta.True(value >= low && value <= high,
124124
"Expected value in range [%d, %d], found %d", low, high, value)
125125
}

modules/unittestassert/testassert.go renamed to models/unittest/bridge.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,53 @@
22
// Use of this source code is governed by a MIT-style
33
// license that can be found in the LICENSE file.
44

5-
package unittestassert
5+
package unittest
66

77
import (
8-
"code.gitea.io/gitea/modules/unittestapi"
8+
"code.gitea.io/gitea/modules/unittestbridge"
99
"github.com/stretchr/testify/assert"
1010
)
1111

12+
// For legacy code only, please refer to the `unittestbridge` package.
13+
1214
// TestifyAsserter uses "stretchr/testify/assert" to do assert
1315
type TestifyAsserter struct {
14-
t unittestapi.Tester
16+
t unittestbridge.Tester
1517
}
1618

17-
//Errorf Errorf
19+
// Errorf assert Errorf
1820
func (ta TestifyAsserter) Errorf(format string, args ...interface{}) {
1921
ta.t.Errorf(format, args)
2022
}
2123

22-
//NoError NoError
24+
// NoError assert NoError
2325
func (ta TestifyAsserter) NoError(err error, msgAndArgs ...interface{}) bool {
2426
return assert.NoError(ta, err, msgAndArgs...)
2527
}
2628

27-
//EqualValues EqualValues
29+
// EqualValues assert EqualValues
2830
func (ta TestifyAsserter) EqualValues(expected, actual interface{}, msgAndArgs ...interface{}) bool {
2931
return assert.EqualValues(ta, expected, actual, msgAndArgs...)
3032
}
3133

32-
//Equal Equal
34+
// Equal assert Equal
3335
func (ta TestifyAsserter) Equal(expected, actual interface{}, msgAndArgs ...interface{}) bool {
3436
return assert.Equal(ta, expected, actual, msgAndArgs...)
3537
}
3638

37-
//True True
39+
// True assert True
3840
func (ta TestifyAsserter) True(value bool, msgAndArgs ...interface{}) bool {
3941
return assert.True(ta, value, msgAndArgs...)
4042
}
4143

42-
//False False
44+
// False assert False
4345
func (ta TestifyAsserter) False(value bool, msgAndArgs ...interface{}) bool {
4446
return assert.False(ta, value, msgAndArgs...)
4547
}
4648

47-
//NewTestifyAsserter returns a new asserter
48-
func NewTestifyAsserter(t unittestapi.Tester) unittestapi.Asserter {
49-
return &TestifyAsserter{t: t}
49+
// InitUnitTestBridge init the unit test bridge. eg: models.CheckConsistencyFor can use testing and assert frameworks
50+
func InitUnitTestBridge() {
51+
unittestbridge.SetNewAsserterFunc(func(t unittestbridge.Tester) unittestbridge.Asserter {
52+
return &TestifyAsserter{t: t}
53+
})
5054
}

models/unittest/testdb.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import (
1515
"code.gitea.io/gitea/modules/base"
1616
"code.gitea.io/gitea/modules/setting"
1717
"code.gitea.io/gitea/modules/storage"
18-
"code.gitea.io/gitea/modules/unittestapi"
19-
"code.gitea.io/gitea/modules/unittestassert"
2018
"code.gitea.io/gitea/modules/util"
2119

2220
"github.com/stretchr/testify/assert"
@@ -45,7 +43,7 @@ func fatalTestError(fmtStr string, args ...interface{}) {
4543
// test database. Creates the test database, and sets necessary settings.
4644
func MainTest(m *testing.M, pathToGiteaRoot string, fixtureFiles ...string) {
4745
var err error
48-
unittestapi.SetNewAsserterFunc(unittestassert.NewTestifyAsserter)
46+
InitUnitTestBridge()
4947
giteaRoot = pathToGiteaRoot
5048
fixturesDir = filepath.Join(pathToGiteaRoot, "models", "fixtures")
5149

modules/unittestapi/unittestapi.go

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package unittestbridge
6+
7+
// Usage: generally, non-unit-test code shouldn't depend on unit test code.
8+
// However, we have some code like models.CheckConsistencyFor, which need to do some unit test works.
9+
// Now we can not decouple models.CheckConsistencyFor from unit test code easily (cycle-import reasons).
10+
// So we introduce this `unit test bridge`:
11+
// * When a release binary is built, no testing/assert framework would be compiled into the binary, and CheckConsistencyFor won't run unit test related code
12+
// * When a unit test binary is built, the unit test code will init this bridge, then CheckConsistencyFor can run unit test related code
13+
//
14+
// Tester/Assert are intermediate interfaces, they should NOT be used in new code.
15+
// One day, if CheckConsistencyFor is clean enough, we can remove these intermediate interfaces.
16+
17+
// Tester is the same as TestingT in "stretchr/testify/assert"
18+
// Tester can be used in non-unit-test code (ex: models.CheckConsistencyFor), it is used to isolate dependencies
19+
type Tester interface {
20+
Errorf(format string, args ...interface{})
21+
}
22+
23+
// Asserter can be used in non-unit-test code (ex: models.CheckConsistencyFor), it is used to isolate dependencies
24+
type Asserter interface {
25+
Tester
26+
NoError(err error, msgAndArgs ...interface{}) bool
27+
EqualValues(expected, actual interface{}, msgAndArgs ...interface{}) bool
28+
Equal(expected, actual interface{}, msgAndArgs ...interface{}) bool
29+
True(value bool, msgAndArgs ...interface{}) bool
30+
False(value bool, msgAndArgs ...interface{}) bool
31+
}
32+
33+
var newAsserterFunc func(t Tester) Asserter
34+
35+
// NewAsserter returns a new asserter, only works in unit test
36+
func NewAsserter(t Tester) Asserter {
37+
if newAsserterFunc == nil {
38+
panic("the newAsserterFunc is not set. you can only use assert in unit test.")
39+
}
40+
return newAsserterFunc(t)
41+
}
42+
43+
// SetNewAsserterFunc in unit test, the asserter must be set first
44+
func SetNewAsserterFunc(f func(t Tester) Asserter) {
45+
newAsserterFunc = f
46+
}

0 commit comments

Comments
 (0)