From d36f82784c2c8cbd027c2e68f2f1ad7ac79355b9 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Fri, 26 Sep 2014 00:39:07 -0700 Subject: [PATCH] added tests for repositories --- server/datastore/database/database.go | 2 +- .../datastore/database/{util.go => helper.go} | 0 server/datastore/database/perm.go | 8 +- server/datastore/database/repo.go | 6 +- server/datastore/database/repo_test.go | 162 ++++++++++++++++++ server/datastore/database/user_test.go | 2 +- 6 files changed, 171 insertions(+), 9 deletions(-) rename server/datastore/database/{util.go => helper.go} (100%) diff --git a/server/datastore/database/database.go b/server/datastore/database/database.go index 51a83407b..ce9de04b2 100644 --- a/server/datastore/database/database.go +++ b/server/datastore/database/database.go @@ -40,7 +40,7 @@ func Connect(driver, datasource string) (*sql.DB, error) { return migration.Open(driver, datasource, migrations) } -// MustConnect is a helper function that create a +// MustConnect is a helper function that creates a // new database commention and auto-generates the // database schema. An error causes a panic. func MustConnect(driver, datasource string) *sql.DB { diff --git a/server/datastore/database/util.go b/server/datastore/database/helper.go similarity index 100% rename from server/datastore/database/util.go rename to server/datastore/database/helper.go diff --git a/server/datastore/database/perm.go b/server/datastore/database/perm.go index 1a71c1953..5fe6e09e5 100644 --- a/server/datastore/database/perm.go +++ b/server/datastore/database/perm.go @@ -15,24 +15,24 @@ func NewPermstore(db meddler.DB) *Permstore { // GetPerm retrieves the User's permission from // the datastore for the given repository. -func (db *Repostore) GetPerm(user *model.User, repo *model.Repo) (*model.Perm, error) { +func (db *Permstore) GetPerm(user *model.User, repo *model.Repo) (*model.Perm, error) { var perm = new(model.Perm) var err = meddler.QueryRow(db, perm, rebind(permQuery), user.ID, repo.ID) return perm, err } // PostPerm saves permission in the datastore. -func (db *Repostore) PostPerm(perm *model.Perm) error { +func (db *Permstore) PostPerm(perm *model.Perm) error { return meddler.Save(db, permTable, perm) } // PutPerm saves permission in the datastore. -func (db *Repostore) PutPerm(perm *model.Perm) error { +func (db *Permstore) PutPerm(perm *model.Perm) error { return meddler.Save(db, permTable, perm) } // DelPerm removes permission from the datastore. -func (db *Repostore) DelPerm(perm *model.Perm) error { +func (db *Permstore) DelPerm(perm *model.Perm) error { var _, err = db.Exec(rebind(permDeleteStmt), perm.ID) return err } diff --git a/server/datastore/database/repo.go b/server/datastore/database/repo.go index a0ae3e835..10073f186 100644 --- a/server/datastore/database/repo.go +++ b/server/datastore/database/repo.go @@ -33,7 +33,7 @@ func (db *Repostore) GetRepoName(remote, owner, name string) (*model.Repo, error // the datastore accessible by the given user ID. func (db *Repostore) GetRepoList(user *model.User) ([]*model.Repo, error) { var repos []*model.Repo - var err = meddler.QueryAll(db, &repos, rebind(repoListQuery)) + var err = meddler.QueryAll(db, &repos, rebind(repoListQuery), user.ID) return repos, err } @@ -80,6 +80,6 @@ WHERE repo_id IN ( // SQL statement to delete a User by ID. const repoDeleteStmt = ` -DELETE FROM users -WHERE user_id = ? +DELETE FROM repos +WHERE repo_id = ? ` diff --git a/server/datastore/database/repo_test.go b/server/datastore/database/repo_test.go index 636bab89a..c4252b2fc 100644 --- a/server/datastore/database/repo_test.go +++ b/server/datastore/database/repo_test.go @@ -1 +1,163 @@ package database + +import ( + "testing" + + "github.com/drone/drone/shared/model" + "github.com/franela/goblin" +) + +func TestRepostore(t *testing.T) { + db := MustConnect("sqlite3", ":memory:") + rs := NewRepostore(db) + ps := NewPermstore(db) + defer db.Close() + + g := goblin.Goblin(t) + g.Describe("Repostore", 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 repos") + db.Exec("DELETE FROM users") + }) + + g.It("Should Put a Repo", func() { + repo := model.Repo{ + UserID: 1, + Remote: "enterprise.github.com", + Host: "github.drone.io", + Owner: "bradrydzewski", + Name: "drone", + } + err := rs.PostRepo(&repo) + g.Assert(err == nil).IsTrue() + g.Assert(repo.ID != 0).IsTrue() + }) + + g.It("Should Post a Repo", func() { + repo := model.Repo{ + UserID: 1, + Remote: "enterprise.github.com", + Host: "github.drone.io", + Owner: "bradrydzewski", + Name: "drone", + } + err := rs.PostRepo(&repo) + g.Assert(err == nil).IsTrue() + g.Assert(repo.ID != 0).IsTrue() + }) + + g.It("Should Get a Repo by ID", func() { + repo := model.Repo{ + UserID: 1, + Remote: "enterprise.github.com", + Host: "github.drone.io", + Owner: "bradrydzewski", + Name: "drone", + } + rs.PostRepo(&repo) + getrepo, err := rs.GetRepo(repo.ID) + g.Assert(err == nil).IsTrue() + g.Assert(repo.ID).Equal(getrepo.ID) + g.Assert(repo.UserID).Equal(getrepo.UserID) + g.Assert(repo.Remote).Equal(getrepo.Remote) + g.Assert(repo.Host).Equal(getrepo.Host) + g.Assert(repo.Owner).Equal(getrepo.Owner) + g.Assert(repo.Name).Equal(getrepo.Name) + }) + + g.It("Should Get a Repo by Name", func() { + repo := model.Repo{ + UserID: 1, + Remote: "enterprise.github.com", + Host: "github.drone.io", + Owner: "bradrydzewski", + Name: "drone", + } + rs.PostRepo(&repo) + getrepo, err := rs.GetRepoName(repo.Host, repo.Owner, repo.Name) + g.Assert(err == nil).IsTrue() + g.Assert(repo.ID).Equal(getrepo.ID) + g.Assert(repo.UserID).Equal(getrepo.UserID) + g.Assert(repo.Remote).Equal(getrepo.Remote) + g.Assert(repo.Host).Equal(getrepo.Host) + g.Assert(repo.Owner).Equal(getrepo.Owner) + g.Assert(repo.Name).Equal(getrepo.Name) + }) + + g.It("Should Get a Repo List by User", func() { + repo1 := model.Repo{ + UserID: 1, + Remote: "enterprise.github.com", + Host: "github.drone.io", + Owner: "bradrydzewski", + Name: "drone", + } + repo2 := model.Repo{ + UserID: 1, + Remote: "enterprise.github.com", + Host: "github.drone.io", + Owner: "bradrydzewski", + Name: "drone-dart", + } + rs.PostRepo(&repo1) + rs.PostRepo(&repo2) + ps.PostPerm(&model.Perm{ + RepoID: repo1.ID, + UserID: 1, + Read: true, + Write: true, + Admin: true, + }) + repos, err := rs.GetRepoList(&model.User{ID: 1}) + g.Assert(err == nil).IsTrue() + g.Assert(len(repos)).Equal(1) + g.Assert(repos[0].UserID).Equal(repo1.UserID) + g.Assert(repos[0].Remote).Equal(repo1.Remote) + g.Assert(repos[0].Host).Equal(repo1.Host) + g.Assert(repos[0].Owner).Equal(repo1.Owner) + g.Assert(repos[0].Name).Equal(repo1.Name) + }) + + g.It("Should Delete a Repo", func() { + repo := model.Repo{ + UserID: 1, + Remote: "enterprise.github.com", + Host: "github.drone.io", + Owner: "bradrydzewski", + Name: "drone", + } + rs.PostRepo(&repo) + _, err1 := rs.GetRepo(repo.ID) + err2 := rs.DelRepo(&repo) + _, err3 := rs.GetRepo(repo.ID) + g.Assert(err1 == nil).IsTrue() + g.Assert(err2 == nil).IsTrue() + g.Assert(err3 == nil).IsFalse() + }) + + g.It("Should Enforce Unique Repo Name", func() { + repo1 := model.Repo{ + UserID: 1, + Remote: "enterprise.github.com", + Host: "github.drone.io", + Owner: "bradrydzewski", + Name: "drone", + } + repo2 := model.Repo{ + UserID: 2, + Remote: "enterprise.github.com", + Host: "github.drone.io", + Owner: "bradrydzewski", + Name: "drone", + } + err1 := rs.PostRepo(&repo1) + err2 := rs.PostRepo(&repo2) + g.Assert(err1 == nil).IsTrue() + g.Assert(err2 == nil).IsFalse() + }) + }) +} diff --git a/server/datastore/database/user_test.go b/server/datastore/database/user_test.go index 846899567..e0518b75e 100644 --- a/server/datastore/database/user_test.go +++ b/server/datastore/database/user_test.go @@ -7,7 +7,7 @@ import ( "github.com/franela/goblin" ) -func TestPackagestore(t *testing.T) { +func TestUserstore(t *testing.T) { db := MustConnect("sqlite3", ":memory:") us := NewUserstore(db) defer db.Close()