From 4b286f282a38d55b2271c43db80d3ac938746c79 Mon Sep 17 00:00:00 2001
From: Ethan Koenig <etk39@cornell.edu>
Date: Mon, 27 Feb 2017 20:42:10 -0500
Subject: [PATCH] Consistency checks for action unit tests (#1079)

---
 models/action_test.go      | 19 ++++++++++++++++---
 models/consistency_test.go | 18 ++++++++++++++++--
 models/fixtures/action.yml |  4 ++--
 3 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/models/action_test.go b/models/action_test.go
index f29ec65cfc..cb36966ec7 100644
--- a/models/action_test.go
+++ b/models/action_test.go
@@ -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)
 }
diff --git a/models/consistency_test.go b/models/consistency_test.go
index cab8f9a782..a8de8e3c2b 100644
--- a/models/consistency_test.go
+++ b/models/consistency_test.go
@@ -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)
+}
diff --git a/models/fixtures/action.yml b/models/fixtures/action.yml
index bf44837a1c..94d8e5759c 100644
--- a/models/fixtures/action.yml
+++ b/models/fixtures/action.yml
@@ -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
 
 -