2014-06-12 23:41:04 +00:00
|
|
|
package database
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"testing"
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
"github.com/drone/drone/shared/model"
|
2014-06-04 21:25:38 +00:00
|
|
|
)
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
func TestRepoFind(t *testing.T) {
|
2014-06-04 21:25:38 +00:00
|
|
|
setup()
|
|
|
|
defer teardown()
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
repos := NewRepoManager(db)
|
2014-06-04 21:25:38 +00:00
|
|
|
repo, err := repos.Find(1)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Want Repo from ID, got %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testRepo(t, repo)
|
|
|
|
}
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
func TestRepoFindName(t *testing.T) {
|
2014-06-04 21:25:38 +00:00
|
|
|
setup()
|
|
|
|
defer teardown()
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
repos := NewRepoManager(db)
|
2014-06-04 21:25:38 +00:00
|
|
|
user, err := repos.FindName("github.com", "lhofstadter", "lenwoloppali")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Want Repo by Name, got %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testRepo(t, user)
|
|
|
|
}
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
func TestRepoList(t *testing.T) {
|
2014-06-04 21:25:38 +00:00
|
|
|
setup()
|
|
|
|
defer teardown()
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
repos := NewRepoManager(db)
|
2014-06-04 21:25:38 +00:00
|
|
|
all, err := repos.List(1)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Want Repos, got %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var got, want = len(all), 2
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("Want %v Repos, got %v", want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
testRepo(t, all[0])
|
|
|
|
}
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
func TestRepoInsert(t *testing.T) {
|
2014-06-04 21:25:38 +00:00
|
|
|
setup()
|
|
|
|
defer teardown()
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
repo, _ := model.NewRepo("github.com", "mrwolowitz", "lenwoloppali")
|
|
|
|
repos := NewRepoManager(db)
|
2014-06-04 21:25:38 +00:00
|
|
|
if err := repos.Insert(repo); err != nil {
|
|
|
|
t.Errorf("Want Repo created, got %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify unique remote + owner + name login constraint
|
2014-06-12 23:41:04 +00:00
|
|
|
var err = repos.Insert(&model.Repo{Host: repo.Host, Owner: repo.Owner, Name: repo.Name})
|
2014-06-12 21:38:12 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Error("Want unique constraint violated")
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
func TestRepoUpdate(t *testing.T) {
|
2014-06-04 21:25:38 +00:00
|
|
|
setup()
|
|
|
|
defer teardown()
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
repos := NewRepoManager(db)
|
2014-06-04 21:25:38 +00:00
|
|
|
repo, err := repos.Find(1)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Want Repo from ID, got %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the repo's access token
|
|
|
|
repo.Active = false
|
|
|
|
repo.Private = false
|
|
|
|
repo.Privileged = false
|
|
|
|
repo.PostCommit = false
|
|
|
|
repo.PullRequest = false
|
|
|
|
if err := repos.Update(repo); err != nil {
|
|
|
|
t.Errorf("Want Repo updated, got %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
updated, _ := repos.Find(1)
|
|
|
|
var got, want = updated.Active, repo.Active
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("Want updated Active %v, got %v", want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
got, want = updated.Private, repo.Private
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("Want updated Private %v, got %v", want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
got, want = updated.Privileged, repo.Privileged
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("Want updated Privileged %v, got %v", want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
got, want = updated.PostCommit, repo.PostCommit
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("Want updated PostCommit %v, got %v", want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
got, want = updated.PullRequest, repo.PullRequest
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("Want updated PullRequest %v, got %v", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
func TestRepoDelete(t *testing.T) {
|
2014-06-04 21:25:38 +00:00
|
|
|
setup()
|
|
|
|
defer teardown()
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
repos := NewRepoManager(db)
|
2014-06-04 21:25:38 +00:00
|
|
|
repo, err := repos.Find(1)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Want Repo from ID, got %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete the repo
|
|
|
|
if err := repos.Delete(repo); err != nil {
|
|
|
|
t.Errorf("Want Repo deleted, got %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check to see if the deleted repo is actually gone
|
|
|
|
if _, err := repos.Find(1); err != sql.ErrNoRows {
|
|
|
|
t.Errorf("Want ErrNoRows, got %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// testRepo is a helper function that compares the repo
|
|
|
|
// to an expected set of fixed field values.
|
2014-06-12 23:41:04 +00:00
|
|
|
func testRepo(t *testing.T, repo *model.Repo) {
|
2014-06-04 21:25:38 +00:00
|
|
|
var got, want = repo.Remote, "github.com"
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("Want Remote %v, got %v", want, got)
|
|
|
|
}
|
|
|
|
|
2014-06-07 20:33:34 +00:00
|
|
|
got, want = repo.Host, "github.com"
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("Want Host %v, got %v", want, got)
|
|
|
|
}
|
|
|
|
|
2014-06-04 21:25:38 +00:00
|
|
|
got, want = repo.Owner, "lhofstadter"
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("Want Owner %v, got %v", want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
got, want = repo.Name, "lenwoloppali"
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("Want Name %v, got %v", want, got)
|
|
|
|
}
|
|
|
|
|
2014-06-05 22:04:59 +00:00
|
|
|
got, want = repo.CloneURL, "git://github.com/lhofstadter/lenwoloppali.git"
|
2014-06-04 21:25:38 +00:00
|
|
|
if got != want {
|
|
|
|
t.Errorf("Want URL %v, got %v", want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
got, want = repo.PublicKey, "publickey"
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("Want PublicKey %v, got %v", want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
got, want = repo.PrivateKey, "privatekey"
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("Want PrivateKey %v, got %v", want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
got, want = repo.Params, "params"
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("Want Params %v, got %v", want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
var gotBool, wantBool = repo.Active, true
|
|
|
|
if gotBool != wantBool {
|
|
|
|
t.Errorf("Want Active %v, got %v", wantBool, gotBool)
|
|
|
|
}
|
|
|
|
|
|
|
|
gotBool, wantBool = repo.Private, true
|
|
|
|
if gotBool != wantBool {
|
|
|
|
t.Errorf("Want Private %v, got %v", wantBool, gotBool)
|
|
|
|
}
|
|
|
|
|
|
|
|
gotBool, wantBool = repo.Privileged, true
|
|
|
|
if gotBool != wantBool {
|
|
|
|
t.Errorf("Want Privileged %v, got %v", wantBool, gotBool)
|
|
|
|
}
|
|
|
|
|
|
|
|
gotBool, wantBool = repo.PostCommit, true
|
|
|
|
if gotBool != wantBool {
|
|
|
|
t.Errorf("Want PostCommit %v, got %v", wantBool, gotBool)
|
|
|
|
}
|
|
|
|
|
|
|
|
gotBool, wantBool = repo.PullRequest, true
|
|
|
|
if gotBool != wantBool {
|
|
|
|
t.Errorf("Want PullRequest %v, got %v", wantBool, gotBool)
|
|
|
|
}
|
|
|
|
|
|
|
|
var gotInt64, wantInt64 = repo.ID, int64(1)
|
|
|
|
if gotInt64 != wantInt64 {
|
|
|
|
t.Errorf("Want ID %v, got %v", wantInt64, gotInt64)
|
|
|
|
}
|
|
|
|
|
|
|
|
gotInt64, wantInt64 = repo.Created, int64(1398065343)
|
|
|
|
if gotInt64 != wantInt64 {
|
|
|
|
t.Errorf("Want Created %v, got %v", wantInt64, gotInt64)
|
|
|
|
}
|
|
|
|
|
|
|
|
gotInt64, wantInt64 = repo.Updated, int64(1398065344)
|
|
|
|
if gotInt64 != wantInt64 {
|
|
|
|
t.Errorf("Want Updated %v, got %v", wantInt64, gotInt64)
|
|
|
|
}
|
|
|
|
|
|
|
|
gotInt64, wantInt64 = repo.Timeout, int64(900)
|
|
|
|
if gotInt64 != wantInt64 {
|
|
|
|
t.Errorf("Want Timeout %v, got %v", wantInt64, gotInt64)
|
|
|
|
}
|
|
|
|
}
|