Consistency checks for action unit tests (#1079)

This commit is contained in:
Ethan Koenig 2017-02-27 20:42:10 -05:00 committed by Lunny Xiao
parent cf80e19157
commit 4b286f282a
3 changed files with 34 additions and 7 deletions

View File

@ -1,9 +1,11 @@
package models package models
import ( import (
"strings"
"testing" "testing"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -46,6 +48,7 @@ func TestNewRepoAction(t *testing.T) {
AssertNotExistsBean(t, actionBean) AssertNotExistsBean(t, actionBean)
assert.NoError(t, NewRepoAction(user, repo)) assert.NoError(t, NewRepoAction(user, repo))
AssertExistsAndLoadBean(t, actionBean) AssertExistsAndLoadBean(t, actionBean)
CheckConsistencyFor(t, &Action{})
} }
func TestRenameRepoAction(t *testing.T) { func TestRenameRepoAction(t *testing.T) {
@ -58,6 +61,7 @@ func TestRenameRepoAction(t *testing.T) {
oldRepoName := repo.Name oldRepoName := repo.Name
const newRepoName = "newRepoName" const newRepoName = "newRepoName"
repo.Name = newRepoName repo.Name = newRepoName
repo.LowerName = strings.ToLower(newRepoName)
actionBean := &Action{ actionBean := &Action{
OpType: ActionRenameRepo, OpType: ActionRenameRepo,
@ -72,6 +76,10 @@ func TestRenameRepoAction(t *testing.T) {
AssertNotExistsBean(t, actionBean) AssertNotExistsBean(t, actionBean)
assert.NoError(t, RenameRepoAction(user, oldRepoName, repo)) assert.NoError(t, RenameRepoAction(user, oldRepoName, repo))
AssertExistsAndLoadBean(t, actionBean) AssertExistsAndLoadBean(t, actionBean)
_, err := x.Id(repo.ID).Cols("name", "lower_name").Update(repo)
assert.NoError(t, err)
CheckConsistencyFor(t, &Action{})
} }
func TestPushCommits_ToAPIPayloadCommits(t *testing.T) { func TestPushCommits_ToAPIPayloadCommits(t *testing.T) {
@ -192,6 +200,7 @@ func TestUpdateIssuesCommit(t *testing.T) {
assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits)) assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits))
AssertExistsAndLoadBean(t, commentBean) AssertExistsAndLoadBean(t, commentBean)
AssertExistsAndLoadBean(t, issueBean, "is_closed=1") AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
CheckConsistencyFor(t, &Action{})
} }
func TestCommitRepoAction(t *testing.T) { func TestCommitRepoAction(t *testing.T) {
@ -242,6 +251,7 @@ func TestCommitRepoAction(t *testing.T) {
Commits: pushCommits, Commits: pushCommits,
})) }))
AssertExistsAndLoadBean(t, actionBean) AssertExistsAndLoadBean(t, actionBean)
CheckConsistencyFor(t, &Action{})
} }
func TestTransferRepoAction(t *testing.T) { func TestTransferRepoAction(t *testing.T) {
@ -266,6 +276,10 @@ func TestTransferRepoAction(t *testing.T) {
AssertNotExistsBean(t, actionBean) AssertNotExistsBean(t, actionBean)
assert.NoError(t, TransferRepoAction(user2, user2, repo)) assert.NoError(t, TransferRepoAction(user2, user2, repo))
AssertExistsAndLoadBean(t, actionBean) AssertExistsAndLoadBean(t, actionBean)
_, err := x.Id(repo.ID).Cols("owner_id").Update(repo)
assert.NoError(t, err)
CheckConsistencyFor(t, &Action{})
} }
func TestMergePullRequestAction(t *testing.T) { func TestMergePullRequestAction(t *testing.T) {
@ -287,6 +301,7 @@ func TestMergePullRequestAction(t *testing.T) {
AssertNotExistsBean(t, actionBean) AssertNotExistsBean(t, actionBean)
assert.NoError(t, MergePullRequestAction(user, repo, issue)) assert.NoError(t, MergePullRequestAction(user, repo, issue))
AssertExistsAndLoadBean(t, actionBean) AssertExistsAndLoadBean(t, actionBean)
CheckConsistencyFor(t, &Action{})
} }
func TestGetFeeds(t *testing.T) { func TestGetFeeds(t *testing.T) {
@ -318,7 +333,5 @@ func TestGetFeeds2(t *testing.T) {
actions, err = GetFeeds(user, user.ID, 0, true) actions, err = GetFeeds(user, user.ID, 0, true)
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, actions, 1) assert.Len(t, actions, 0)
assert.Equal(t, int64(2), actions[0].ID)
assert.Equal(t, user.ID, actions[0].UserID)
} }

View File

@ -6,6 +6,7 @@ package models
import ( import (
"reflect" "reflect"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -25,7 +26,8 @@ func CheckConsistencyForAll(t *testing.T) {
&PullRequest{}, &PullRequest{},
&Milestone{}, &Milestone{},
&Label{}, &Label{},
&Team{}) &Team{},
&Action{})
} }
// CheckConsistencyFor test that all matching database entries are consistent // CheckConsistencyFor test that all matching database entries are consistent
@ -37,7 +39,7 @@ func CheckConsistencyFor(t *testing.T, beansToCheck ...interface{}) {
ptrToSliceValue := reflect.New(sliceType) ptrToSliceValue := reflect.New(sliceType)
ptrToSliceValue.Elem().Set(sliceValue) ptrToSliceValue.Elem().Set(sliceValue)
assert.NoError(t, x.Find(ptrToSliceValue.Interface())) assert.NoError(t, x.Where(bean).Find(ptrToSliceValue.Interface()))
sliceValue = ptrToSliceValue.Elem() sliceValue = ptrToSliceValue.Elem()
for i := 0; i < sliceValue.Len(); i++ { for i := 0; i < sliceValue.Len(); i++ {
@ -80,6 +82,7 @@ func (user *User) CheckForConsistency(t *testing.T) {
} }
func (repo *Repository) CheckForConsistency(t *testing.T) { func (repo *Repository) CheckForConsistency(t *testing.T) {
assert.Equal(t, repo.LowerName, strings.ToLower(repo.Name), "repo: %+v", repo)
assertCount(t, &Star{RepoID: repo.ID}, repo.NumStars) assertCount(t, &Star{RepoID: repo.ID}, repo.NumStars)
assertCount(t, &Watch{RepoID: repo.ID}, repo.NumWatches) assertCount(t, &Watch{RepoID: repo.ID}, repo.NumWatches)
assertCount(t, &Milestone{RepoID: repo.ID}, repo.NumMilestones) assertCount(t, &Milestone{RepoID: repo.ID}, repo.NumMilestones)
@ -156,3 +159,14 @@ func (team *Team) CheckForConsistency(t *testing.T) {
assertCount(t, &TeamUser{TeamID: team.ID}, team.NumMembers) assertCount(t, &TeamUser{TeamID: team.ID}, team.NumMembers)
assertCount(t, &TeamRepo{TeamID: team.ID}, team.NumRepos) assertCount(t, &TeamRepo{TeamID: team.ID}, team.NumRepos)
} }
func (action *Action) CheckForConsistency(t *testing.T) {
repo := AssertExistsAndLoadBean(t, &Repository{ID: action.RepoID}).(*Repository)
owner := AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
actor := AssertExistsAndLoadBean(t, &User{ID: action.ActUserID}).(*User)
assert.Equal(t, repo.Name, action.RepoName, "action: %+v", action)
assert.Equal(t, repo.IsPrivate, action.IsPrivate, "action: %+v", action)
assert.Equal(t, owner.Name, action.RepoUserName, "action: %+v", action)
assert.Equal(t, actor.Name, action.ActUserName, "action: %+v", action)
}

View File

@ -17,8 +17,8 @@
act_user_name: user3 act_user_name: user3
repo_id: 3 repo_id: 3
repo_user_name: user3 repo_user_name: user3
repo_name: repo3 # TODO old or new name? repo_name: repo3
is_private: false is_private: true
content: oldRepoName content: oldRepoName
- -