]> source.dussan.org Git - gitea.git/commitdiff
Consistency checks for action unit tests (#1079)
authorEthan Koenig <etk39@cornell.edu>
Tue, 28 Feb 2017 01:42:10 +0000 (20:42 -0500)
committerLunny Xiao <xiaolunwen@gmail.com>
Tue, 28 Feb 2017 01:42:10 +0000 (09:42 +0800)
models/action_test.go
models/consistency_test.go
models/fixtures/action.yml

index f29ec65cfcc1728ae010a8a0133a2a39cfa4df2d..cb36966ec72b5df5007b14462d060299053e6bfa 100644 (file)
@@ -1,9 +1,11 @@
 package models
 
 import (
+       "strings"
        "testing"
 
        "code.gitea.io/gitea/modules/setting"
+
        "github.com/stretchr/testify/assert"
 )
 
@@ -46,6 +48,7 @@ func TestNewRepoAction(t *testing.T) {
        AssertNotExistsBean(t, actionBean)
        assert.NoError(t, NewRepoAction(user, repo))
        AssertExistsAndLoadBean(t, actionBean)
+       CheckConsistencyFor(t, &Action{})
 }
 
 func TestRenameRepoAction(t *testing.T) {
@@ -58,6 +61,7 @@ func TestRenameRepoAction(t *testing.T) {
        oldRepoName := repo.Name
        const newRepoName = "newRepoName"
        repo.Name = newRepoName
+       repo.LowerName = strings.ToLower(newRepoName)
 
        actionBean := &Action{
                OpType:       ActionRenameRepo,
@@ -72,6 +76,10 @@ func TestRenameRepoAction(t *testing.T) {
        AssertNotExistsBean(t, actionBean)
        assert.NoError(t, RenameRepoAction(user, oldRepoName, repo))
        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) {
@@ -192,6 +200,7 @@ func TestUpdateIssuesCommit(t *testing.T) {
        assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits))
        AssertExistsAndLoadBean(t, commentBean)
        AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
+       CheckConsistencyFor(t, &Action{})
 }
 
 func TestCommitRepoAction(t *testing.T) {
@@ -242,6 +251,7 @@ func TestCommitRepoAction(t *testing.T) {
                Commits:     pushCommits,
        }))
        AssertExistsAndLoadBean(t, actionBean)
+       CheckConsistencyFor(t, &Action{})
 }
 
 func TestTransferRepoAction(t *testing.T) {
@@ -266,6 +276,10 @@ func TestTransferRepoAction(t *testing.T) {
        AssertNotExistsBean(t, actionBean)
        assert.NoError(t, TransferRepoAction(user2, user2, repo))
        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) {
@@ -287,6 +301,7 @@ func TestMergePullRequestAction(t *testing.T) {
        AssertNotExistsBean(t, actionBean)
        assert.NoError(t, MergePullRequestAction(user, repo, issue))
        AssertExistsAndLoadBean(t, actionBean)
+       CheckConsistencyFor(t, &Action{})
 }
 
 func TestGetFeeds(t *testing.T) {
@@ -318,7 +333,5 @@ func TestGetFeeds2(t *testing.T) {
 
        actions, err = GetFeeds(user, user.ID, 0, true)
        assert.NoError(t, err)
-       assert.Len(t, actions, 1)
-       assert.Equal(t, int64(2), actions[0].ID)
-       assert.Equal(t, user.ID, actions[0].UserID)
+       assert.Len(t, actions, 0)
 }
index cab8f9a7827e1d9703284135c868609369c7b382..a8de8e3c2bdc0788d8dde8f65f7a8a82d0be22f4 100644 (file)
@@ -6,6 +6,7 @@ package models
 
 import (
        "reflect"
+       "strings"
        "testing"
 
        "github.com/stretchr/testify/assert"
@@ -25,7 +26,8 @@ func CheckConsistencyForAll(t *testing.T) {
                &PullRequest{},
                &Milestone{},
                &Label{},
-               &Team{})
+               &Team{},
+               &Action{})
 }
 
 // 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.Elem().Set(sliceValue)
 
-               assert.NoError(t, x.Find(ptrToSliceValue.Interface()))
+               assert.NoError(t, x.Where(bean).Find(ptrToSliceValue.Interface()))
                sliceValue = ptrToSliceValue.Elem()
 
                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) {
+       assert.Equal(t, repo.LowerName, strings.ToLower(repo.Name), "repo: %+v", repo)
        assertCount(t, &Star{RepoID: repo.ID}, repo.NumStars)
        assertCount(t, &Watch{RepoID: repo.ID}, repo.NumWatches)
        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, &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)
+}
index bf44837a1c99b875bc5446a5ababd6325cac5e93..94d8e5759cb98495660d5fb0aea1e2e616c5d86f 100644 (file)
@@ -17,8 +17,8 @@
   act_user_name: user3
   repo_id: 3
   repo_user_name: user3
-  repo_name: repo3 # TODO old or new name?
-  is_private: false
+  repo_name: repo3
+  is_private: true
   content: oldRepoName
 
 -