From 01eae26623a135bc80976d9bcdf112c7b7f4da30 Mon Sep 17 00:00:00 2001 From: Ben Schumacher Date: Tue, 12 May 2015 11:06:00 -0600 Subject: [PATCH] Fix Datastore mock and badge_test.go --- datastore/mock/mock.go | 230 +++++++++++++++++++++++------------------ server/badge_test.go | 38 ++++--- 2 files changed, 152 insertions(+), 116 deletions(-) diff --git a/datastore/mock/mock.go b/datastore/mock/mock.go index b999fefc44..1f4e578f89 100644 --- a/datastore/mock/mock.go +++ b/datastore/mock/mock.go @@ -11,12 +11,8 @@ type Datastore struct { mock.Mock } -func New() *Datastore { - return new(Datastore) -} - -func (m *Datastore) User(_a0 string) (*common.User, error) { - ret := m.Called(_a0) +func (m *Datastore) User(id int64) (*common.User, error) { + ret := m.Called(id) var r0 *common.User if ret.Get(0) != nil { @@ -26,10 +22,13 @@ func (m *Datastore) User(_a0 string) (*common.User, error) { return r0, r1 } -func (m *Datastore) UserCount() (int, error) { - ret := m.Called() +func (m *Datastore) UserLogin(_a0 string) (*common.User, error) { + ret := m.Called(_a0) - r0 := ret.Get(0).(int) + var r0 *common.User + if ret.Get(0) != nil { + r0 = ret.Get(0).(*common.User) + } r1 := ret.Error(1) return r0, r1 @@ -45,14 +44,33 @@ func (m *Datastore) UserList() ([]*common.User, error) { return r0, r1 } -func (m *Datastore) SetUser(_a0 *common.User) error { +func (m *Datastore) UserFeed(_a0 *common.User, _a1 int, _a2 int) ([]*common.RepoCommit, error) { + ret := m.Called(_a0, _a1, _a2) + + var r0 []*common.RepoCommit + if ret.Get(0) != nil { + r0 = ret.Get(0).([]*common.RepoCommit) + } + r1 := ret.Error(1) + + return r0, r1 +} +func (m *Datastore) UserCount() (int, error) { + ret := m.Called() + + r0 := ret.Get(0).(int) + r1 := ret.Error(1) + + return r0, r1 +} +func (m *Datastore) AddUser(_a0 *common.User) error { ret := m.Called(_a0) r0 := ret.Error(0) return r0 } -func (m *Datastore) SetUserNotExists(_a0 *common.User) error { +func (m *Datastore) SetUser(_a0 *common.User) error { ret := m.Called(_a0) r0 := ret.Error(0) @@ -66,7 +84,18 @@ func (m *Datastore) DelUser(_a0 *common.User) error { return r0 } -func (m *Datastore) Token(_a0 string, _a1 string) (*common.Token, error) { +func (m *Datastore) Token(_a0 int64) (*common.Token, error) { + ret := m.Called(_a0) + + var r0 *common.Token + if ret.Get(0) != nil { + r0 = ret.Get(0).(*common.Token) + } + r1 := ret.Error(1) + + return r0, r1 +} +func (m *Datastore) TokenLabel(_a0 *common.User, _a1 string) (*common.Token, error) { ret := m.Called(_a0, _a1) var r0 *common.Token @@ -77,7 +106,7 @@ func (m *Datastore) Token(_a0 string, _a1 string) (*common.Token, error) { return r0, r1 } -func (m *Datastore) TokenList(_a0 string) ([]*common.Token, error) { +func (m *Datastore) TokenList(_a0 *common.User) ([]*common.Token, error) { ret := m.Called(_a0) var r0 []*common.Token @@ -88,7 +117,7 @@ func (m *Datastore) TokenList(_a0 string) ([]*common.Token, error) { return r0, r1 } -func (m *Datastore) SetToken(_a0 *common.Token) error { +func (m *Datastore) AddToken(_a0 *common.Token) error { ret := m.Called(_a0) r0 := ret.Error(0) @@ -102,7 +131,7 @@ func (m *Datastore) DelToken(_a0 *common.Token) error { return r0 } -func (m *Datastore) Subscribed(_a0 string, _a1 string) (bool, error) { +func (m *Datastore) Starred(_a0 *common.User, _a1 *common.Repo) (bool, error) { ret := m.Called(_a0, _a1) r0 := ret.Get(0).(bool) @@ -110,22 +139,22 @@ func (m *Datastore) Subscribed(_a0 string, _a1 string) (bool, error) { return r0, r1 } -func (m *Datastore) SetSubscriber(_a0 string, _a1 string) error { +func (m *Datastore) AddStar(_a0 *common.User, _a1 *common.Repo) error { ret := m.Called(_a0, _a1) r0 := ret.Error(0) return r0 } -func (m *Datastore) DelSubscriber(_a0 string, _a1 string) error { +func (m *Datastore) DelStar(_a0 *common.User, _a1 *common.Repo) error { ret := m.Called(_a0, _a1) r0 := ret.Error(0) return r0 } -func (m *Datastore) Repo(_a0 string) (*common.Repo, error) { - ret := m.Called(_a0) +func (m *Datastore) Repo(id int64) (*common.Repo, error) { + ret := m.Called(id) var r0 *common.Repo if ret.Get(0) != nil { @@ -135,62 +164,37 @@ func (m *Datastore) Repo(_a0 string) (*common.Repo, error) { return r0, r1 } -func (m *Datastore) RepoList(_a0 string) ([]*common.Repo, error) { - ret := m.Called(_a0) +func (m *Datastore) RepoName(owner string, name string) (*common.Repo, error) { + ret := m.Called(owner, name) - var r0 []*common.Repo - if ret.Get(0) != nil { - r0 = ret.Get(0).([]*common.Repo) - } - r1 := ret.Error(1) - - return r0, r1 -} -func (m *Datastore) RepoParams(_a0 string) (map[string]string, error) { - ret := m.Called(_a0) - - var r0 map[string]string + var r0 *common.Repo if ret.Get(0) != nil { - r0 = ret.Get(0).(map[string]string) + r0 = ret.Get(0).(*common.Repo) } r1 := ret.Error(1) return r0, r1 } -func (m *Datastore) RepoKeypair(_a0 string) (*common.Keypair, error) { +func (m *Datastore) RepoList(_a0 *common.User) ([]*common.Repo, error) { ret := m.Called(_a0) - var r0 *common.Keypair + var r0 []*common.Repo if ret.Get(0) != nil { - r0 = ret.Get(0).(*common.Keypair) + r0 = ret.Get(0).([]*common.Repo) } r1 := ret.Error(1) return r0, r1 } -func (m *Datastore) SetRepo(_a0 *common.Repo) error { +func (m *Datastore) AddRepo(_a0 *common.Repo) error { ret := m.Called(_a0) r0 := ret.Error(0) return r0 } -func (m *Datastore) SetRepoNotExists(_a0 *common.User, _a1 *common.Repo) error { - ret := m.Called(_a0, _a1) - - r0 := ret.Error(0) - - return r0 -} -func (m *Datastore) SetRepoParams(_a0 string, _a1 map[string]string) error { - ret := m.Called(_a0, _a1) - - r0 := ret.Error(0) - - return r0 -} -func (m *Datastore) SetRepoKeypair(_a0 string, _a1 *common.Keypair) error { - ret := m.Called(_a0, _a1) +func (m *Datastore) SetRepo(_a0 *common.Repo) error { + ret := m.Called(_a0) r0 := ret.Error(0) @@ -203,128 +207,148 @@ func (m *Datastore) DelRepo(_a0 *common.Repo) error { return r0 } -func (m *Datastore) Build(_a0 string, _a1 int) (*common.Build, error) { - ret := m.Called(_a0, _a1) +func (m *Datastore) Commit(_a0 int64) (*common.Commit, error) { + ret := m.Called(_a0) - var r0 *common.Build + var r0 *common.Commit if ret.Get(0) != nil { - r0 = ret.Get(0).(*common.Build) + r0 = ret.Get(0).(*common.Commit) } r1 := ret.Error(1) return r0, r1 } -func (m *Datastore) BuildList(_a0 string) ([]*common.Build, error) { - ret := m.Called(_a0) +func (m *Datastore) CommitSeq(_a0 *common.Repo, _a1 int) (*common.Commit, error) { + ret := m.Called(_a0, _a1) - var r0 []*common.Build + var r0 *common.Commit if ret.Get(0) != nil { - r0 = ret.Get(0).([]*common.Build) + r0 = ret.Get(0).(*common.Commit) } r1 := ret.Error(1) return r0, r1 } -func (m *Datastore) BuildLast(_a0 string) (*common.Build, error) { - ret := m.Called(_a0) +func (m *Datastore) CommitLast(_a0 *common.Repo, _a1 string) (*common.Commit, error) { + ret := m.Called(_a0, _a1) - var r0 *common.Build + var r0 *common.Commit if ret.Get(0) != nil { - r0 = ret.Get(0).(*common.Build) + r0 = ret.Get(0).(*common.Commit) } r1 := ret.Error(1) return r0, r1 } -func (m *Datastore) BuildAgent(_a0 string, _a1 int) (*common.Agent, error) { - ret := m.Called(_a0, _a1) +func (m *Datastore) CommitList(_a0 *common.Repo, _a1 int, _a2 int) ([]*common.Commit, error) { + ret := m.Called(_a0, _a1, _a2) - var r0 *common.Agent + var r0 []*common.Commit if ret.Get(0) != nil { - r0 = ret.Get(0).(*common.Agent) + r0 = ret.Get(0).([]*common.Commit) } r1 := ret.Error(1) return r0, r1 } -func (m *Datastore) SetBuild(_a0 string, _a1 *common.Build) error { - ret := m.Called(_a0, _a1) +func (m *Datastore) AddCommit(_a0 *common.Commit) error { + ret := m.Called(_a0) r0 := ret.Error(0) return r0 } -func (m *Datastore) Status(_a0 string, _a1 int, _a2 string) (*common.Status, error) { - ret := m.Called(_a0, _a1, _a2) +func (m *Datastore) SetCommit(_a0 *common.Commit) error { + ret := m.Called(_a0) + + r0 := ret.Error(0) + + return r0 +} +func (m *Datastore) KillCommits() error { + ret := m.Called() + + r0 := ret.Error(0) + + return r0 +} +func (m *Datastore) Build(_a0 int64) (*common.Build, error) { + ret := m.Called(_a0) - var r0 *common.Status + var r0 *common.Build if ret.Get(0) != nil { - r0 = ret.Get(0).(*common.Status) + r0 = ret.Get(0).(*common.Build) } r1 := ret.Error(1) return r0, r1 } -func (m *Datastore) StatusList(_a0 string, _a1 int) ([]*common.Status, error) { +func (m *Datastore) BuildSeq(_a0 *common.Commit, _a1 int) (*common.Build, error) { ret := m.Called(_a0, _a1) - var r0 []*common.Status + var r0 *common.Build if ret.Get(0) != nil { - r0 = ret.Get(0).([]*common.Status) + r0 = ret.Get(0).(*common.Build) } r1 := ret.Error(1) return r0, r1 } -func (m *Datastore) SetStatus(_a0 string, _a1 int, _a2 *common.Status) error { - ret := m.Called(_a0, _a1, _a2) +func (m *Datastore) BuildList(_a0 *common.Commit) ([]*common.Build, error) { + ret := m.Called(_a0) + + var r0 []*common.Build + if ret.Get(0) != nil { + r0 = ret.Get(0).([]*common.Build) + } + r1 := ret.Error(1) + + return r0, r1 +} +func (m *Datastore) SetBuild(_a0 *common.Build) error { + ret := m.Called(_a0) r0 := ret.Error(0) return r0 } -func (m *Datastore) LogReader(_a0 string, _a1 int, _a2 int) (io.Reader, error) { - ret := m.Called(_a0, _a1, _a2) +func (m *Datastore) GetBlob(path string) ([]byte, error) { + ret := m.Called(path) - r0 := ret.Get(0).(io.Reader) + var r0 []byte + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } r1 := ret.Error(1) return r0, r1 } +func (m *Datastore) GetBlobReader(path string) (io.ReadCloser, error) { + ret := m.Called(path) -func (m *Datastore) SetLogs(_a0 string, _a1 int, _a2 int, _a3 io.Reader) error { - ret := m.Called(_a0, _a1, _a2, _a3) - - r0 := ret.Error(0) + r0 := ret.Get(0).(io.ReadCloser) + r1 := ret.Error(1) - return r0 + return r0, r1 } -func (m *Datastore) SetBuildState(_a0 string, _a1 *common.Build) error { - ret := m.Called(_a0, _a1) +func (m *Datastore) SetBlob(path string, data []byte) error { + ret := m.Called(path, data) r0 := ret.Error(0) return r0 } -func (m *Datastore) SetBuildStatus(_a0 string, _a1 int, _a2 *common.Status) error { - ret := m.Called(_a0, _a1, _a2) +func (m *Datastore) SetBlobReader(path string, r io.Reader) error { + ret := m.Called(path, r) r0 := ret.Error(0) return r0 } -func (m *Datastore) SetBuildTask(_a0 string, _a1 int, _a2 *common.Task) error { - ret := m.Called(_a0, _a1, _a2) +func (m *Datastore) DelBlob(path string) error { + ret := m.Called(path) r0 := ret.Error(0) return r0 } - -func (m *Datastore) SetBuildAgent(_a0 string, _a1 int, _a2 *common.Agent) error { - return m.Called(_a0, _a1, _a2).Error(0) -} - -func (m *Datastore) DelBuildAgent(_a0 string, _a1 int) error { - return m.Called(_a0, _a1).Error(0) -} diff --git a/server/badge_test.go b/server/badge_test.go index 84b5c88e0e..bfdcc178ac 100644 --- a/server/badge_test.go +++ b/server/badge_test.go @@ -1,6 +1,7 @@ package server import ( + "database/sql" "encoding/xml" "net/http" "net/url" @@ -8,26 +9,29 @@ import ( "github.com/drone/drone/common" "github.com/drone/drone/common/ccmenu" - "github.com/drone/drone/datastore" "github.com/drone/drone/datastore/mock" "github.com/drone/drone/server/recorder" + . "github.com/franela/goblin" "github.com/gin-gonic/gin" + "github.com/stretchr/testify/mock" ) var badgeTests = []struct { + branch string badge []byte state string activity string status string err error }{ - {badgeSuccess, common.StateSuccess, "Sleeping", "Success", nil}, - {badgeStarted, common.StateRunning, "Building", "Unknown", nil}, - {badgeError, common.StateError, "Sleeping", "Exception", nil}, - {badgeError, common.StateKilled, "Sleeping", "Exception", nil}, - {badgeFailure, common.StateFailure, "Sleeping", "Failure", nil}, - {badgeNone, "", "", "", datastore.ErrKeyNotFound}, + {"", badgeSuccess, common.StateSuccess, "Sleeping", "Success", nil}, + {"master", badgeSuccess, common.StateSuccess, "Sleeping", "Success", nil}, + {"", badgeStarted, common.StateRunning, "Building", "Unknown", nil}, + {"", badgeError, common.StateError, "Sleeping", "Exception", nil}, + {"", badgeError, common.StateKilled, "Sleeping", "Exception", nil}, + {"", badgeFailure, common.StateFailure, "Sleeping", "Failure", nil}, + {"", badgeNone, "", "", "", sql.ErrNoRows}, } func TestBadges(t *testing.T) { @@ -41,16 +45,21 @@ func TestBadges(t *testing.T) { for _, test := range badgeTests { rw := recorder.New() ctx := &gin.Context{Engine: gin.Default(), Writer: rw} - - repo := &common.Repo{FullName: "foo/bar"} - if len(test.state) != 0 { - repo.Last = &common.Build{State: test.state} + ctx.Request = &http.Request{ + Form: url.Values{}, + } + if len(test.branch) != 0 { + ctx.Request.Form.Set("branch", test.branch) } + repo := &common.Repo{FullName: "foo/bar"} ctx.Set("datastore", store) ctx.Set("repo", repo) + commit := &common.Commit{State: test.state} + store.On("CommitLast", repo, test.branch).Return(commit, test.err).Once() GetBadge(ctx) + g.Assert(rw.Code).Equal(200) g.Assert(rw.Body.Bytes()).Equal(test.badge) g.Assert(rw.HeaderMap.Get("Content-Type")).Equal("image/svg+xml") @@ -68,8 +77,10 @@ func TestBadges(t *testing.T) { ctx.Set("datastore", store) ctx.Set("repo", repo) - build := &common.Build{State: test.state} - store.On("BuildLast", repo.FullName).Return(build, test.err).Once() + commits := []*common.Commit{ + &common.Commit{State: test.state}, + } + store.On("CommitList", repo, mock.AnythingOfType("int"), mock.AnythingOfType("int")).Return(commits, test.err).Once() GetCC(ctx) // in an error scenario (ie no build exists) we should @@ -83,6 +94,7 @@ func TestBadges(t *testing.T) { // it matches the expected values. cc := &ccmenu.CCProjects{} xml.Unmarshal(rw.Body.Bytes(), cc) + g.Assert(rw.Code).Equal(200) g.Assert(cc.Project.Activity).Equal(test.activity) g.Assert(cc.Project.LastBuildStatus).Equal(test.status) g.Assert(rw.HeaderMap.Get("Content-Type")).Equal("application/xml; charset=utf-8")