mirror of
1
Fork 0

Less boilerplate in models/ unit tests (#725)

This commit is contained in:
Ethan Koenig 2017-01-24 21:49:51 -05:00 committed by Lunny Xiao
parent 75f0b0c51c
commit 8093b3372e
6 changed files with 47 additions and 113 deletions

View File

@ -6,6 +6,7 @@ package models
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -19,10 +20,10 @@ var accessModes = []AccessMode{
func TestAccessLevel(t *testing.T) { func TestAccessLevel(t *testing.T) {
assert.NoError(t, PrepareTestDatabase()) assert.NoError(t, PrepareTestDatabase())
user1 := &User{ID: 2}; AssertExistsAndLoadBean(t, user1) user1 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
user2 := &User{ID: 4}; AssertExistsAndLoadBean(t, user2) user2 := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
repo1 := &Repository{OwnerID: 2, IsPrivate: false}; AssertExistsAndLoadBean(t, repo1) repo1 := AssertExistsAndLoadBean(t, &Repository{OwnerID: 2, IsPrivate: false}).(*Repository)
repo2 := &Repository{OwnerID: 3, IsPrivate: true}; AssertExistsAndLoadBean(t, repo2) repo2 := AssertExistsAndLoadBean(t, &Repository{OwnerID: 3, IsPrivate: true}).(*Repository)
level, err := AccessLevel(user1, repo1) level, err := AccessLevel(user1, repo1)
assert.NoError(t, err) assert.NoError(t, err)
@ -44,10 +45,10 @@ func TestAccessLevel(t *testing.T) {
func TestHasAccess(t *testing.T) { func TestHasAccess(t *testing.T) {
assert.NoError(t, PrepareTestDatabase()) assert.NoError(t, PrepareTestDatabase())
user1 := &User{ID: 2}; AssertExistsAndLoadBean(t, user1) user1 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
user2 := &User{ID: 4}; AssertExistsAndLoadBean(t, user2) user2 := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
repo1 := &Repository{OwnerID: 2, IsPrivate: false}; AssertExistsAndLoadBean(t, repo1) repo1 := AssertExistsAndLoadBean(t, &Repository{OwnerID: 2, IsPrivate: false}).(*Repository)
repo2 := &Repository{OwnerID: 3, IsPrivate: true}; AssertExistsAndLoadBean(t, repo2) repo2 := AssertExistsAndLoadBean(t, &Repository{OwnerID: 3, IsPrivate: true}).(*Repository)
for _, accessMode := range accessModes { for _, accessMode := range accessModes {
has, err := HasAccess(user1, repo1, accessMode) has, err := HasAccess(user1, repo1, accessMode)
@ -71,9 +72,7 @@ func TestHasAccess(t *testing.T) {
func TestUser_GetRepositoryAccesses(t *testing.T) { func TestUser_GetRepositoryAccesses(t *testing.T) {
assert.NoError(t, PrepareTestDatabase()) assert.NoError(t, PrepareTestDatabase())
user1 := &User{ID: 1}; AssertExistsAndLoadBean(t, user1) user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
user2 := &User{ID: 2}; AssertExistsAndLoadBean(t, user2)
accesses, err := user1.GetRepositoryAccesses() accesses, err := user1.GetRepositoryAccesses()
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, accesses, 0) assert.Len(t, accesses, 0)
@ -82,23 +81,21 @@ func TestUser_GetRepositoryAccesses(t *testing.T) {
func TestUser_GetAccessibleRepositories(t *testing.T) { func TestUser_GetAccessibleRepositories(t *testing.T) {
assert.NoError(t, PrepareTestDatabase()) assert.NoError(t, PrepareTestDatabase())
user1 := &User{ID: 1}; AssertExistsAndLoadBean(t, user1) user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
user2 := &User{ID: 2}; AssertExistsAndLoadBean(t, user2)
repos, err := user1.GetAccessibleRepositories(0) repos, err := user1.GetAccessibleRepositories(0)
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, repos, 0) assert.Len(t, repos, 0)
user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
repos, err = user2.GetAccessibleRepositories(0) repos, err = user2.GetAccessibleRepositories(0)
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, repos, 1) assert.Len(t, repos, 1)
} }
func TestRepository_RecalculateAccesses(t *testing.T) { func TestRepository_RecalculateAccesses(t *testing.T) {
// test with organization repo // test with organization repo
assert.NoError(t, PrepareTestDatabase()) assert.NoError(t, PrepareTestDatabase())
repo1 := &Repository{ID: 3}; AssertExistsAndLoadBean(t, repo1) repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
assert.NoError(t, repo1.GetOwner()) assert.NoError(t, repo1.GetOwner())
sess := x.NewSession() sess := x.NewSession()
@ -119,7 +116,7 @@ func TestRepository_RecalculateAccesses(t *testing.T) {
func TestRepository_RecalculateAccesses2(t *testing.T) { func TestRepository_RecalculateAccesses2(t *testing.T) {
// test with non-organization repo // test with non-organization repo
assert.NoError(t, PrepareTestDatabase()) assert.NoError(t, PrepareTestDatabase())
repo1 := &Repository{ID: 4}; AssertExistsAndLoadBean(t, repo1) repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository)
assert.NoError(t, repo1.GetOwner()) assert.NoError(t, repo1.GetOwner())
sess := x.NewSession() sess := x.NewSession()

View File

@ -29,10 +29,8 @@ func TestAction_GetRepoLink(t *testing.T) {
func TestNewRepoAction(t *testing.T) { func TestNewRepoAction(t *testing.T) {
assert.NoError(t, PrepareTestDatabase()) assert.NoError(t, PrepareTestDatabase())
user := &User{ID: 2} user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
AssertExistsAndLoadBean(t, user) repo := AssertExistsAndLoadBean(t, &Repository{OwnerID: user.ID}).(*Repository)
repo := &Repository{OwnerID: user.ID}
AssertExistsAndLoadBean(t, repo)
repo.Owner = user repo.Owner = user
actionBean := &Action{ actionBean := &Action{
@ -53,10 +51,8 @@ func TestNewRepoAction(t *testing.T) {
func TestRenameRepoAction(t *testing.T) { func TestRenameRepoAction(t *testing.T) {
assert.NoError(t, PrepareTestDatabase()) assert.NoError(t, PrepareTestDatabase())
user := &User{ID: 2} user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
AssertExistsAndLoadBean(t, user) repo := AssertExistsAndLoadBean(t, &Repository{OwnerID: user.ID}).(*Repository)
repo := &Repository{OwnerID: user.ID}
AssertExistsAndLoadBean(t, repo)
repo.Owner = user repo.Owner = user
oldRepoName := repo.Name oldRepoName := repo.Name
@ -179,10 +175,8 @@ func TestUpdateIssuesCommit(t *testing.T) {
}, },
} }
user := &User{ID: 2} user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
AssertExistsAndLoadBean(t, user) repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
repo := &Repository{ID: 1}
AssertExistsAndLoadBean(t, repo)
repo.Owner = user repo.Owner = user
commentBean := &Comment{ commentBean := &Comment{
@ -203,10 +197,8 @@ func TestUpdateIssuesCommit(t *testing.T) {
func TestCommitRepoAction(t *testing.T) { func TestCommitRepoAction(t *testing.T) {
assert.NoError(t, PrepareTestDatabase()) assert.NoError(t, PrepareTestDatabase())
user := &User{ID: 2} user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
AssertExistsAndLoadBean(t, user) repo := AssertExistsAndLoadBean(t, &Repository{ID: 2, OwnerID: user.ID}).(*Repository)
repo := &Repository{ID: 2, OwnerID: user.ID}
AssertExistsAndLoadBean(t, repo)
repo.Owner = user repo.Owner = user
pushCommits := NewPushCommits() pushCommits := NewPushCommits()
@ -255,12 +247,9 @@ func TestCommitRepoAction(t *testing.T) {
func TestTransferRepoAction(t *testing.T) { func TestTransferRepoAction(t *testing.T) {
assert.NoError(t, PrepareTestDatabase()) assert.NoError(t, PrepareTestDatabase())
user2 := &User{ID: 2} user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
AssertExistsAndLoadBean(t, user2) user4 := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
user4 := &User{ID: 4} repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, OwnerID: user2.ID}).(*Repository)
AssertExistsAndLoadBean(t, user4)
repo := &Repository{ID: 1, OwnerID: user2.ID}
AssertExistsAndLoadBean(t, repo)
repo.OwnerID = user4.ID repo.OwnerID = user4.ID
repo.Owner = user4 repo.Owner = user4
@ -281,13 +270,10 @@ func TestTransferRepoAction(t *testing.T) {
func TestMergePullRequestAction(t *testing.T) { func TestMergePullRequestAction(t *testing.T) {
assert.NoError(t, PrepareTestDatabase()) assert.NoError(t, PrepareTestDatabase())
user := &User{ID: 2} user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
AssertExistsAndLoadBean(t, user) repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, OwnerID: user.ID}).(*Repository)
repo := &Repository{ID: 1, OwnerID: user.ID}
AssertExistsAndLoadBean(t, repo)
repo.Owner = user repo.Owner = user
issue := &Issue{ID: 3, RepoID: repo.ID} issue := AssertExistsAndLoadBean(t, &Issue{ID: 3, RepoID: repo.ID}).(*Issue)
AssertExistsAndLoadBean(t, issue)
actionBean := &Action{ actionBean := &Action{
OpType: ActionMergePullRequest, OpType: ActionMergePullRequest,
@ -306,8 +292,7 @@ func TestMergePullRequestAction(t *testing.T) {
func TestGetFeeds(t *testing.T) { func TestGetFeeds(t *testing.T) {
// test with an individual user // test with an individual user
assert.NoError(t, PrepareTestDatabase()) assert.NoError(t, PrepareTestDatabase())
user := &User{ID: 2} user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
AssertExistsAndLoadBean(t, user)
actions, err := GetFeeds(user, user.ID, 0, false) actions, err := GetFeeds(user, user.ID, 0, false)
assert.NoError(t, err) assert.NoError(t, err)
@ -323,8 +308,7 @@ func TestGetFeeds(t *testing.T) {
func TestGetFeeds2(t *testing.T) { func TestGetFeeds2(t *testing.T) {
// test with an organization user // test with an organization user
assert.NoError(t, PrepareTestDatabase()) assert.NoError(t, PrepareTestDatabase())
user := &User{ID: 3} user := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
AssertExistsAndLoadBean(t, user)
actions, err := GetFeeds(user, user.ID, 0, false) actions, err := GetFeeds(user, user.ID, 0, false)
assert.NoError(t, err) assert.NoError(t, err)

View File

@ -11,21 +11,9 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
// getPullRequest load a fixture pull request from the test database
func loadFixturePullRequest(t *testing.T, id int64) *PullRequest {
sess := x.NewSession()
defer sess.Close()
pr := &PullRequest{ID: id}
has, err := sess.Get(pr)
assert.NoError(t, err)
assert.True(t, has)
return pr
}
func TestPullRequest_LoadAttributes(t *testing.T) { func TestPullRequest_LoadAttributes(t *testing.T) {
assert.NoError(t, PrepareTestDatabase()) assert.NoError(t, PrepareTestDatabase())
pr := loadFixturePullRequest(t, 1) pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
assert.NoError(t, pr.LoadAttributes()) assert.NoError(t, pr.LoadAttributes())
assert.NotNil(t, pr.Merger) assert.NotNil(t, pr.Merger)
assert.Equal(t, pr.MergerID, pr.Merger.ID) assert.Equal(t, pr.MergerID, pr.Merger.ID)
@ -33,7 +21,7 @@ func TestPullRequest_LoadAttributes(t *testing.T) {
func TestPullRequest_LoadIssue(t *testing.T) { func TestPullRequest_LoadIssue(t *testing.T) {
assert.NoError(t, PrepareTestDatabase()) assert.NoError(t, PrepareTestDatabase())
pr := loadFixturePullRequest(t, 1) pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
assert.NoError(t, pr.LoadIssue()) assert.NoError(t, pr.LoadIssue())
assert.NotNil(t, pr.Issue) assert.NotNil(t, pr.Issue)
assert.Equal(t, int64(2), pr.Issue.ID) assert.Equal(t, int64(2), pr.Issue.ID)
@ -46,7 +34,7 @@ func TestPullRequest_LoadIssue(t *testing.T) {
func TestPullRequest_GetBaseRepo(t *testing.T) { func TestPullRequest_GetBaseRepo(t *testing.T) {
assert.NoError(t, PrepareTestDatabase()) assert.NoError(t, PrepareTestDatabase())
pr := loadFixturePullRequest(t, 1) pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
assert.NoError(t, pr.GetBaseRepo()) assert.NoError(t, pr.GetBaseRepo())
assert.NotNil(t, pr.BaseRepo) assert.NotNil(t, pr.BaseRepo)
assert.Equal(t, pr.BaseRepoID, pr.BaseRepo.ID) assert.Equal(t, pr.BaseRepoID, pr.BaseRepo.ID)
@ -57,7 +45,7 @@ func TestPullRequest_GetBaseRepo(t *testing.T) {
func TestPullRequest_GetHeadRepo(t *testing.T) { func TestPullRequest_GetHeadRepo(t *testing.T) {
assert.NoError(t, PrepareTestDatabase()) assert.NoError(t, PrepareTestDatabase())
pr := loadFixturePullRequest(t, 1) pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
assert.NoError(t, pr.GetHeadRepo()) assert.NoError(t, pr.GetHeadRepo())
assert.NotNil(t, pr.HeadRepo) assert.NotNil(t, pr.HeadRepo)
assert.Equal(t, pr.HeadRepoID, pr.HeadRepo.ID) assert.Equal(t, pr.HeadRepoID, pr.HeadRepo.ID)
@ -175,12 +163,7 @@ func TestPullRequest_Update(t *testing.T) {
} }
pr.Update() pr.Update()
sess := x.NewSession() pr = AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
defer sess.Close()
pr = &PullRequest{ID: 1}
has, err := sess.Get(pr)
assert.NoError(t, err)
assert.True(t, has)
assert.Equal(t, int64(100), pr.IssueID) assert.Equal(t, int64(100), pr.IssueID)
assert.Equal(t, "baseBranch", pr.BaseBranch) assert.Equal(t, "baseBranch", pr.BaseBranch)
assert.Equal(t, "headBranch", pr.HeadBranch) assert.Equal(t, "headBranch", pr.HeadBranch)
@ -196,12 +179,7 @@ func TestPullRequest_UpdateCols(t *testing.T) {
} }
pr.UpdateCols("issue_id", "head_branch") pr.UpdateCols("issue_id", "head_branch")
sess := x.NewSession() pr = AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
defer sess.Close()
pr = &PullRequest{ID: 1}
has, err := sess.Get(pr)
assert.NoError(t, err)
assert.True(t, has)
assert.Equal(t, int64(100), pr.IssueID) assert.Equal(t, int64(100), pr.IssueID)
assert.Equal(t, "master", pr.BaseBranch) assert.Equal(t, "master", pr.BaseBranch)
assert.Equal(t, "headBranch", pr.HeadBranch) assert.Equal(t, "headBranch", pr.HeadBranch)
@ -214,14 +192,14 @@ func TestPullRequest_UpdateCols(t *testing.T) {
func TestPullRequest_AddToTaskQueue(t *testing.T) { func TestPullRequest_AddToTaskQueue(t *testing.T) {
assert.NoError(t, PrepareTestDatabase()) assert.NoError(t, PrepareTestDatabase())
pr := loadFixturePullRequest(t, 1) pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
pr.AddToTaskQueue() pr.AddToTaskQueue()
// briefly sleep so that background threads have time to run // briefly sleep so that background threads have time to run
time.Sleep(time.Millisecond) time.Sleep(time.Millisecond)
assert.True(t, pullRequestQueue.Exist(pr.ID)) assert.True(t, pullRequestQueue.Exist(pr.ID))
pr = loadFixturePullRequest(t, 1) pr = AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
assert.Equal(t, PullRequestStatusChecking, pr.Status) assert.Equal(t, PullRequestStatusChecking, pr.Status)
} }
@ -229,8 +207,8 @@ func TestPullRequestList_LoadAttributes(t *testing.T) {
assert.NoError(t, PrepareTestDatabase()) assert.NoError(t, PrepareTestDatabase())
prs := []*PullRequest{ prs := []*PullRequest{
loadFixturePullRequest(t, 1), AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest),
loadFixturePullRequest(t, 2), AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest),
} }
assert.NoError(t, PullRequestList(prs).LoadAttributes()) assert.NoError(t, PullRequestList(prs).LoadAttributes())
for _, pr := range prs { for _, pr := range prs {

View File

@ -59,10 +59,11 @@ func loadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error)
// AssertExistsAndLoadBean assert that a bean exists and load it from the test // AssertExistsAndLoadBean assert that a bean exists and load it from the test
// database // database
func AssertExistsAndLoadBean(t *testing.T, bean interface{}, conditions ...interface{}) { func AssertExistsAndLoadBean(t *testing.T, bean interface{}, conditions ...interface{}) interface{} {
exists, err := loadBeanIfExists(bean, conditions...) exists, err := loadBeanIfExists(bean, conditions...)
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, exists) assert.True(t, exists)
return bean
} }
// AssertNotExistsBean assert that a bean does not exist in the test database // AssertNotExistsBean assert that a bean does not exist in the test database

View File

@ -17,13 +17,7 @@ func TestNewAccessToken(t *testing.T) {
Name: "Token C", Name: "Token C",
} }
assert.NoError(t, NewAccessToken(token)) assert.NoError(t, NewAccessToken(token))
sess := x.NewSession() AssertExistsAndLoadBean(t, token)
defer sess.Close()
has, err := sess.Get(*token)
assert.NoError(t, err)
assert.True(t, has)
assert.Equal(t, int64(3), token.UID)
assert.Equal(t, "Token C", token.Name)
invalidToken := &AccessToken{ invalidToken := &AccessToken{
ID: token.ID, // duplicate ID: token.ID, // duplicate
@ -78,13 +72,7 @@ func TestUpdateAccessToken(t *testing.T) {
token.Name = "Token Z" token.Name = "Token Z"
assert.NoError(t, UpdateAccessToken(token)) assert.NoError(t, UpdateAccessToken(token))
AssertExistsAndLoadBean(t, token)
sess := x.NewSession()
defer sess.Close()
has, err := sess.Get(token)
assert.NoError(t, err)
assert.True(t, has)
assert.Equal(t, token.Name, "Token Z")
} }
func TestDeleteAccessTokenByID(t *testing.T) { func TestDeleteAccessTokenByID(t *testing.T) {
@ -95,11 +83,7 @@ func TestDeleteAccessTokenByID(t *testing.T) {
assert.Equal(t, int64(1), token.UID) assert.Equal(t, int64(1), token.UID)
assert.NoError(t, DeleteAccessTokenByID(token.ID, 1)) assert.NoError(t, DeleteAccessTokenByID(token.ID, 1))
sess := x.NewSession() AssertNotExistsBean(t, token)
defer sess.Close()
has, err := sess.Get(token)
assert.NoError(t, err)
assert.False(t, has)
err = DeleteAccessTokenByID(100, 100) err = DeleteAccessTokenByID(100, 100)
assert.Error(t, err) assert.Error(t, err)

View File

@ -23,13 +23,7 @@ func TestAddUpdateTask(t *testing.T) {
NewCommitID: "newCommitId4", NewCommitID: "newCommitId4",
} }
assert.NoError(t, AddUpdateTask(task)) assert.NoError(t, AddUpdateTask(task))
AssertExistsAndLoadBean(t, task)
sess := x.NewSession()
defer sess.Close()
has, err := sess.Get(task)
assert.NoError(t, err)
assert.True(t, has)
assert.Equal(t, "uuid4", task.UUID)
} }
func TestGetUpdateTaskByUUID(t *testing.T) { func TestGetUpdateTaskByUUID(t *testing.T) {
@ -49,11 +43,7 @@ func TestGetUpdateTaskByUUID(t *testing.T) {
func TestDeleteUpdateTaskByUUID(t *testing.T) { func TestDeleteUpdateTaskByUUID(t *testing.T) {
assert.NoError(t, PrepareTestDatabase()) assert.NoError(t, PrepareTestDatabase())
assert.NoError(t, DeleteUpdateTaskByUUID("uuid1")) assert.NoError(t, DeleteUpdateTaskByUUID("uuid1"))
sess := x.NewSession() AssertNotExistsBean(t, &UpdateTask{UUID: "uuid1"})
defer sess.Close()
has, err := sess.Get(&UpdateTask{UUID: "uuid1"})
assert.NoError(t, err)
assert.False(t, has)
assert.NoError(t, DeleteUpdateTaskByUUID("invalid")) assert.NoError(t, DeleteUpdateTaskByUUID("invalid"))
} }