Skip to content

Commit 4b286f2

Browse files
ethantkoeniglunny
authored andcommitted
Consistency checks for action unit tests (#1079)
1 parent cf80e19 commit 4b286f2

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

models/action_test.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package models
22

33
import (
4+
"strings"
45
"testing"
56

67
"code.gitea.io/gitea/modules/setting"
8+
79
"github.com/stretchr/testify/assert"
810
)
911

@@ -46,6 +48,7 @@ func TestNewRepoAction(t *testing.T) {
4648
AssertNotExistsBean(t, actionBean)
4749
assert.NoError(t, NewRepoAction(user, repo))
4850
AssertExistsAndLoadBean(t, actionBean)
51+
CheckConsistencyFor(t, &Action{})
4952
}
5053

5154
func TestRenameRepoAction(t *testing.T) {
@@ -58,6 +61,7 @@ func TestRenameRepoAction(t *testing.T) {
5861
oldRepoName := repo.Name
5962
const newRepoName = "newRepoName"
6063
repo.Name = newRepoName
64+
repo.LowerName = strings.ToLower(newRepoName)
6165

6266
actionBean := &Action{
6367
OpType: ActionRenameRepo,
@@ -72,6 +76,10 @@ func TestRenameRepoAction(t *testing.T) {
7276
AssertNotExistsBean(t, actionBean)
7377
assert.NoError(t, RenameRepoAction(user, oldRepoName, repo))
7478
AssertExistsAndLoadBean(t, actionBean)
79+
80+
_, err := x.Id(repo.ID).Cols("name", "lower_name").Update(repo)
81+
assert.NoError(t, err)
82+
CheckConsistencyFor(t, &Action{})
7583
}
7684

7785
func TestPushCommits_ToAPIPayloadCommits(t *testing.T) {
@@ -192,6 +200,7 @@ func TestUpdateIssuesCommit(t *testing.T) {
192200
assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits))
193201
AssertExistsAndLoadBean(t, commentBean)
194202
AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
203+
CheckConsistencyFor(t, &Action{})
195204
}
196205

197206
func TestCommitRepoAction(t *testing.T) {
@@ -242,6 +251,7 @@ func TestCommitRepoAction(t *testing.T) {
242251
Commits: pushCommits,
243252
}))
244253
AssertExistsAndLoadBean(t, actionBean)
254+
CheckConsistencyFor(t, &Action{})
245255
}
246256

247257
func TestTransferRepoAction(t *testing.T) {
@@ -266,6 +276,10 @@ func TestTransferRepoAction(t *testing.T) {
266276
AssertNotExistsBean(t, actionBean)
267277
assert.NoError(t, TransferRepoAction(user2, user2, repo))
268278
AssertExistsAndLoadBean(t, actionBean)
279+
280+
_, err := x.Id(repo.ID).Cols("owner_id").Update(repo)
281+
assert.NoError(t, err)
282+
CheckConsistencyFor(t, &Action{})
269283
}
270284

271285
func TestMergePullRequestAction(t *testing.T) {
@@ -287,6 +301,7 @@ func TestMergePullRequestAction(t *testing.T) {
287301
AssertNotExistsBean(t, actionBean)
288302
assert.NoError(t, MergePullRequestAction(user, repo, issue))
289303
AssertExistsAndLoadBean(t, actionBean)
304+
CheckConsistencyFor(t, &Action{})
290305
}
291306

292307
func TestGetFeeds(t *testing.T) {
@@ -318,7 +333,5 @@ func TestGetFeeds2(t *testing.T) {
318333

319334
actions, err = GetFeeds(user, user.ID, 0, true)
320335
assert.NoError(t, err)
321-
assert.Len(t, actions, 1)
322-
assert.Equal(t, int64(2), actions[0].ID)
323-
assert.Equal(t, user.ID, actions[0].UserID)
336+
assert.Len(t, actions, 0)
324337
}

models/consistency_test.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package models
66

77
import (
88
"reflect"
9+
"strings"
910
"testing"
1011

1112
"github.com/stretchr/testify/assert"
@@ -25,7 +26,8 @@ func CheckConsistencyForAll(t *testing.T) {
2526
&PullRequest{},
2627
&Milestone{},
2728
&Label{},
28-
&Team{})
29+
&Team{},
30+
&Action{})
2931
}
3032

3133
// CheckConsistencyFor test that all matching database entries are consistent
@@ -37,7 +39,7 @@ func CheckConsistencyFor(t *testing.T, beansToCheck ...interface{}) {
3739
ptrToSliceValue := reflect.New(sliceType)
3840
ptrToSliceValue.Elem().Set(sliceValue)
3941

40-
assert.NoError(t, x.Find(ptrToSliceValue.Interface()))
42+
assert.NoError(t, x.Where(bean).Find(ptrToSliceValue.Interface()))
4143
sliceValue = ptrToSliceValue.Elem()
4244

4345
for i := 0; i < sliceValue.Len(); i++ {
@@ -80,6 +82,7 @@ func (user *User) CheckForConsistency(t *testing.T) {
8082
}
8183

8284
func (repo *Repository) CheckForConsistency(t *testing.T) {
85+
assert.Equal(t, repo.LowerName, strings.ToLower(repo.Name), "repo: %+v", repo)
8386
assertCount(t, &Star{RepoID: repo.ID}, repo.NumStars)
8487
assertCount(t, &Watch{RepoID: repo.ID}, repo.NumWatches)
8588
assertCount(t, &Milestone{RepoID: repo.ID}, repo.NumMilestones)
@@ -156,3 +159,14 @@ func (team *Team) CheckForConsistency(t *testing.T) {
156159
assertCount(t, &TeamUser{TeamID: team.ID}, team.NumMembers)
157160
assertCount(t, &TeamRepo{TeamID: team.ID}, team.NumRepos)
158161
}
162+
163+
func (action *Action) CheckForConsistency(t *testing.T) {
164+
repo := AssertExistsAndLoadBean(t, &Repository{ID: action.RepoID}).(*Repository)
165+
owner := AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
166+
actor := AssertExistsAndLoadBean(t, &User{ID: action.ActUserID}).(*User)
167+
168+
assert.Equal(t, repo.Name, action.RepoName, "action: %+v", action)
169+
assert.Equal(t, repo.IsPrivate, action.IsPrivate, "action: %+v", action)
170+
assert.Equal(t, owner.Name, action.RepoUserName, "action: %+v", action)
171+
assert.Equal(t, actor.Name, action.ActUserName, "action: %+v", action)
172+
}

models/fixtures/action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
act_user_name: user3
1818
repo_id: 3
1919
repo_user_name: user3
20-
repo_name: repo3 # TODO old or new name?
21-
is_private: false
20+
repo_name: repo3
21+
is_private: true
2222
content: oldRepoName
2323

2424
-

0 commit comments

Comments
 (0)