added commit and perm unit tests

This commit is contained in:
Brad Rydzewski 2014-09-27 13:09:55 -07:00
parent ae95232419
commit d9db7b9a8d
3 changed files with 334 additions and 5 deletions

View file

@ -42,7 +42,7 @@ func (db *Commitstore) GetCommitLast(repo *model.Repo, branch string) (*model.Co
// from the datastore for the specified repository.
func (db *Commitstore) GetCommitList(repo *model.Repo) ([]*model.Commit, error) {
var commits []*model.Commit
var err = meddler.QueryAll(db, &commits, rebind(commitListQuery))
var err = meddler.QueryAll(db, &commits, rebind(commitListQuery), repo.ID)
return commits, err
}
@ -115,9 +115,6 @@ LIMIT 1
// SQL statement to cancel all running Commits.
const commitKillStmt = `
UPDATE commits SET
commit_status = ?,
commit_started = ?,
commit_finished = ?
UPDATE commits SET commit_status = 'Killed'
WHERE commit_status IN ('Started', 'Pending');
`

View file

@ -1 +1,230 @@
package database
import (
"testing"
"github.com/drone/drone/shared/model"
"github.com/franela/goblin"
)
func TestCommitstore(t *testing.T) {
db := mustConnectTest()
cs := NewCommitstore(db)
//ps := NewPermstore(db)
defer db.Close()
g := goblin.Goblin(t)
g.Describe("Commitstore", func() {
// before each test be sure to purge the package
// table data from the database.
g.BeforeEach(func() {
db.Exec("DELETE FROM perms")
db.Exec("DELETE FROM commits")
})
g.It("Should Put a Commit", func() {
commit := model.Commit{
RepoID: 1,
Branch: "foo",
Sha: "85f8c029b902ed9400bc600bac301a0aadb144ac",
}
err := cs.PutCommit(&commit)
g.Assert(err == nil).IsTrue()
g.Assert(commit.ID != 0).IsTrue()
})
g.It("Should Post a Commit", func() {
commit := model.Commit{
RepoID: 1,
Branch: "foo",
Sha: "85f8c029b902ed9400bc600bac301a0aadb144ac",
}
err := cs.PostCommit(&commit)
g.Assert(err == nil).IsTrue()
g.Assert(commit.ID != 0).IsTrue()
})
g.It("Should Get a Commit by ID", func() {
commit := model.Commit{
RepoID: 1,
Branch: "foo",
Sha: "85f8c029b902ed9400bc600bac301a0aadb144ac",
Status: model.StatusSuccess,
Created: 1398065343,
Updated: 1398065344,
}
cs.PostCommit(&commit)
getcommit, err := cs.GetCommit(commit.ID)
g.Assert(err == nil).IsTrue()
g.Assert(commit.ID).Equal(getcommit.ID)
g.Assert(commit.RepoID).Equal(getcommit.RepoID)
g.Assert(commit.Branch).Equal(getcommit.Branch)
g.Assert(commit.Sha).Equal(getcommit.Sha)
g.Assert(commit.Status).Equal(getcommit.Status)
g.Assert(commit.Created).Equal(getcommit.Created)
g.Assert(commit.Updated).Equal(getcommit.Updated)
})
g.It("Should Delete a Commit", func() {
commit := model.Commit{
RepoID: 1,
Branch: "foo",
Sha: "85f8c029b902ed9400bc600bac301a0aadb144ac",
}
cs.PostCommit(&commit)
_, err1 := cs.GetCommit(commit.ID)
err2 := cs.DelCommit(&commit)
_, err3 := cs.GetCommit(commit.ID)
g.Assert(err1 == nil).IsTrue()
g.Assert(err2 == nil).IsTrue()
g.Assert(err3 == nil).IsFalse()
})
g.It("Should Kill Pending or Started Commits", func() {
commit1 := model.Commit{
RepoID: 1,
Branch: "foo",
Sha: "85f8c029b902ed9400bc600bac301a0aadb144ac",
Status: model.StatusEnqueue,
}
commit2 := model.Commit{
RepoID: 1,
Branch: "bar",
Sha: "85f8c029b902ed9400bc600bac301a0aadb144ac",
Status: model.StatusEnqueue,
}
cs.PutCommit(&commit1)
cs.PutCommit(&commit2)
err := cs.KillCommits()
g.Assert(err == nil).IsTrue()
getcommit1, _ := cs.GetCommit(commit1.ID)
getcommit2, _ := cs.GetCommit(commit1.ID)
g.Assert(getcommit1.Status).Equal(model.StatusKilled)
g.Assert(getcommit2.Status).Equal(model.StatusKilled)
})
g.It("Should Get a Commit by Sha", func() {
commit := model.Commit{
RepoID: 1,
Branch: "foo",
Sha: "85f8c029b902ed9400bc600bac301a0aadb144ac",
}
cs.PostCommit(&commit)
getcommit, err := cs.GetCommitSha(&model.Repo{ID: 1}, commit.Branch, commit.Sha)
g.Assert(err == nil).IsTrue()
g.Assert(commit.ID).Equal(getcommit.ID)
g.Assert(commit.RepoID).Equal(getcommit.RepoID)
g.Assert(commit.Branch).Equal(getcommit.Branch)
g.Assert(commit.Sha).Equal(getcommit.Sha)
})
g.It("Should get the last Commit by Branch", func() {
commit1 := model.Commit{
RepoID: 1,
Branch: "foo",
Sha: "85f8c029b902ed9400bc600bac301a0aadb144ac",
Status: model.StatusFailure,
}
commit2 := model.Commit{
RepoID: 1,
Branch: "foo",
Sha: "0a74b46d7d62b737b6906897f48dbeb72cfda222",
Status: model.StatusSuccess,
}
cs.PutCommit(&commit1)
cs.PutCommit(&commit2)
lastcommit, err := cs.GetCommitLast(&model.Repo{ID: 1}, commit1.Branch)
g.Assert(err == nil).IsTrue()
g.Assert(commit2.ID).Equal(lastcommit.ID)
g.Assert(commit2.RepoID).Equal(lastcommit.RepoID)
g.Assert(commit2.Branch).Equal(lastcommit.Branch)
g.Assert(commit2.Sha).Equal(lastcommit.Sha)
})
g.It("Should get the recent Commit List for a Repo", func() {
commit1 := model.Commit{
RepoID: 1,
Branch: "foo",
Sha: "85f8c029b902ed9400bc600bac301a0aadb144ac",
Status: model.StatusFailure,
}
commit2 := model.Commit{
RepoID: 1,
Branch: "foo",
Sha: "0a74b46d7d62b737b6906897f48dbeb72cfda222",
Status: model.StatusSuccess,
}
cs.PutCommit(&commit1)
cs.PutCommit(&commit2)
commits, err := cs.GetCommitList(&model.Repo{ID: 1})
g.Assert(err == nil).IsTrue()
g.Assert(len(commits)).Equal(2)
g.Assert(commits[0].ID).Equal(commit2.ID)
g.Assert(commits[0].RepoID).Equal(commit2.RepoID)
g.Assert(commits[0].Branch).Equal(commit2.Branch)
g.Assert(commits[0].Sha).Equal(commit2.Sha)
})
g.It("Should get the recent Commit List for a User")
/*
g.It("Should get the recent Commit List for a User", func() {
perm1 := model.Perm{
RepoID: 1,
UserID: 1,
Read: true,
Write: true,
Admin: true,
}
commit1 := model.Commit{
RepoID: 1,
Branch: "foo",
Sha: "85f8c029b902ed9400bc600bac301a0aadb144ac",
Status: model.StatusFailure,
}
commit2 := model.Commit{
RepoID: 1,
Branch: "foo",
Sha: "0a74b46d7d62b737b6906897f48dbeb72cfda222",
Status: model.StatusSuccess,
}
commit3 := model.Commit{
RepoID: 2,
Branch: "baz",
Sha: "0a74b46d7d62b737b6906897f48dbeb72cfda222",
Status: model.StatusSuccess,
}
cs.PutCommit(&commit1)
cs.PutCommit(&commit2)
cs.PutCommit(&commit3)
ps.PutPerm(&perm1)
commits, err := cs.GetCommitListUser(&model.User{ID: 1})
g.Assert(err == nil).IsTrue()
g.Assert(len(commits)).Equal(2)
g.Assert(commits[0].ID).Equal(commit2.ID)
g.Assert(commits[0].RepoID).Equal(commit2.RepoID)
g.Assert(commits[0].Branch).Equal(commit2.Branch)
g.Assert(commits[0].Sha).Equal(commit2.Sha)
})
*/
g.It("Should enforce unique Sha + Branch", func() {
commit1 := model.Commit{
RepoID: 1,
Branch: "foo",
Sha: "85f8c029b902ed9400bc600bac301a0aadb144ac",
Status: model.StatusEnqueue,
}
commit2 := model.Commit{
RepoID: 1,
Branch: "foo",
Sha: "85f8c029b902ed9400bc600bac301a0aadb144ac",
Status: model.StatusEnqueue,
}
err1 := cs.PutCommit(&commit1)
err2 := cs.PutCommit(&commit2)
g.Assert(err1 == nil).IsTrue()
g.Assert(err2 == nil).IsFalse()
})
})
}

View file

@ -1 +1,104 @@
package database
import (
"testing"
"github.com/drone/drone/shared/model"
"github.com/franela/goblin"
)
func TestPermstore(t *testing.T) {
db := mustConnectTest()
ps := NewPermstore(db)
defer db.Close()
g := goblin.Goblin(t)
g.Describe("Permstore", func() {
// before each test be sure to purge the package
// table data from the database.
g.BeforeEach(func() {
db.Exec("DELETE FROM perms")
})
g.It("Should Put a Perm", func() {
perm1 := model.Perm{
UserID: 1,
RepoID: 2,
Read: true,
Write: true,
Admin: true,
}
err := ps.PutPerm(&perm1)
g.Assert(err == nil).IsTrue()
g.Assert(perm1.ID != 0).IsTrue()
})
g.It("Should Post a Perm", func() {
perm1 := model.Perm{
UserID: 1,
RepoID: 2,
Read: true,
Write: true,
Admin: true,
}
err := ps.PostPerm(&perm1)
g.Assert(err == nil).IsTrue()
g.Assert(perm1.ID != 0).IsTrue()
})
g.It("Should Get a Perm", func() {
ps.PostPerm(&model.Perm{
UserID: 1,
RepoID: 2,
Read: true,
Write: true,
Admin: true,
})
getperm, err := ps.GetPerm(&model.User{ID: 1}, &model.Repo{ID: 2})
g.Assert(err == nil).IsTrue()
g.Assert(getperm.ID != 0).IsTrue()
g.Assert(getperm.Admin).IsTrue()
g.Assert(getperm.Write).IsTrue()
g.Assert(getperm.Admin).IsTrue()
g.Assert(getperm.UserID).Equal(int64(1))
g.Assert(getperm.RepoID).Equal(int64(2))
})
g.It("Should Del a Perm", func() {
perm1 := model.Perm{
UserID: 1,
RepoID: 2,
Read: true,
Write: true,
Admin: true,
}
ps.PostPerm(&perm1)
_, err1 := ps.GetPerm(&model.User{ID: 1}, &model.Repo{ID: 2})
err2 := ps.DelPerm(&perm1)
_, err3 := ps.GetPerm(&model.User{ID: 1}, &model.Repo{ID: 2})
g.Assert(err1 == nil).IsTrue()
g.Assert(err2 == nil).IsTrue()
g.Assert(err3 == nil).IsFalse()
})
g.It("Should Enforce Unique Perm", func() {
err1 := ps.PostPerm(&model.Perm{
UserID: 1,
RepoID: 2,
Read: true,
Write: true,
Admin: true,
})
err2 := ps.PostPerm(&model.Perm{
UserID: 1,
RepoID: 2,
Read: true,
Write: true,
Admin: true,
})
g.Assert(err1 == nil).IsTrue()
g.Assert(err2 == nil).IsFalse()
})
})
}