mirror of
https://github.com/harness/drone.git
synced 2025-05-19 10:29:55 +08:00
Fixed issue with template store.
This commit is contained in:
parent
00b6d6e436
commit
4b0d9a1340
@ -24,11 +24,11 @@ type templateStore struct {
|
|||||||
db *db.DB
|
db *db.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *templateStore) List(ctx context.Context, id int64) ([]*core.Template, error) {
|
func (s *templateStore) ListAll(ctx context.Context) ([]*core.Template, error) {
|
||||||
var out []*core.Template
|
var out []*core.Template
|
||||||
err := s.db.View(func(queryer db.Queryer, binder db.Binder) error {
|
err := s.db.View(func(queryer db.Queryer, binder db.Binder) error {
|
||||||
params := map[string]interface{}{"template_id": id}
|
params := map[string]interface{}{}
|
||||||
stmt, args, err := binder.BindNamed(queryRepo, params)
|
stmt, args, err := binder.BindNamed(queryAll, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -59,8 +59,8 @@ func (s *templateStore) Find(ctx context.Context, id int64) (*core.Template, err
|
|||||||
return out, err
|
return out, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *templateStore) FindName(ctx context.Context, id int64, name string) (*core.Template, error) {
|
func (s *templateStore) FindName(ctx context.Context, name string) (*core.Template, error) {
|
||||||
out := &core.Template{Name: name, Id: id}
|
out := &core.Template{Name: name}
|
||||||
err := s.db.View(func(queryer db.Queryer, binder db.Binder) error {
|
err := s.db.View(func(queryer db.Queryer, binder db.Binder) error {
|
||||||
params, err := toParams(out)
|
params, err := toParams(out)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -161,11 +161,11 @@ SELECT
|
|||||||
,template_updated
|
,template_updated
|
||||||
`
|
`
|
||||||
|
|
||||||
const queryRepo = queryBase + `
|
const queryAll = queryBase + `
|
||||||
FROM template
|
FROM template
|
||||||
WHERE template_id = :template_id
|
|
||||||
ORDER BY template_name
|
ORDER BY template_name
|
||||||
`
|
`
|
||||||
|
|
||||||
const stmtInsert = `
|
const stmtInsert = `
|
||||||
INSERT INTO template (
|
INSERT INTO template (
|
||||||
template_id
|
template_id
|
||||||
@ -197,7 +197,6 @@ WHERE template_id = :template_id
|
|||||||
const queryName = queryBase + `
|
const queryName = queryBase + `
|
||||||
FROM template
|
FROM template
|
||||||
WHERE template_name = :template_name
|
WHERE template_name = :template_name
|
||||||
AND template_id = :template_id
|
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
`
|
`
|
||||||
|
|
||||||
|
@ -10,7 +10,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"github.com/drone/drone/core"
|
"github.com/drone/drone/core"
|
||||||
"github.com/drone/drone/store/repos"
|
|
||||||
"github.com/drone/drone/store/shared/db/dbtest"
|
"github.com/drone/drone/store/shared/db/dbtest"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@ -28,21 +27,14 @@ func TestTemplate(t *testing.T) {
|
|||||||
dbtest.Disconnect(conn)
|
dbtest.Disconnect(conn)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// seeds the database with a dummy repository.
|
|
||||||
repo := &core.Repository{UID: "1", Slug: "octocat/hello-world"}
|
|
||||||
repos := repos.New(conn)
|
|
||||||
if err := repos.Create(noContext, repo); err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
store := New(conn).(*templateStore)
|
store := New(conn).(*templateStore)
|
||||||
t.Run("Create", testTemplateCreate(store, repos, repo))
|
t.Run("Create", testTemplateCreate(store))
|
||||||
}
|
}
|
||||||
|
|
||||||
func testTemplateCreate(store *templateStore, repos core.RepositoryStore, repo *core.Repository) func(t *testing.T) {
|
func testTemplateCreate(store *templateStore) func(t *testing.T) {
|
||||||
return func(t *testing.T) {
|
return func(t *testing.T) {
|
||||||
item := &core.Template{
|
item := &core.Template{
|
||||||
Id: repo.ID,
|
Id: 1,
|
||||||
Name: "my_template",
|
Name: "my_template",
|
||||||
Data: "some_template_data",
|
Data: "some_template_data",
|
||||||
Created: 1,
|
Created: 1,
|
||||||
@ -57,10 +49,10 @@ func testTemplateCreate(store *templateStore, repos core.RepositoryStore, repo *
|
|||||||
}
|
}
|
||||||
|
|
||||||
t.Run("Find", testTemplateFind(store, item))
|
t.Run("Find", testTemplateFind(store, item))
|
||||||
t.Run("FindName", testTemplateFindName(store, repo))
|
t.Run("FindName", testTemplateFindName(store))
|
||||||
t.Run("List", testTemplateList(store, repo))
|
t.Run("ListAll", testTemplateListAll(store))
|
||||||
t.Run("Update", testTemplateUpdate(store, repo))
|
t.Run("Update", testTemplateUpdate(store))
|
||||||
t.Run("Delete", testTemplateDelete(store, repo))
|
t.Run("Delete", testTemplateDelete(store))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,9 +67,9 @@ func testTemplateFind(store *templateStore, template *core.Template) func(t *tes
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testTemplateFindName(store *templateStore, repo *core.Repository) func(t *testing.T) {
|
func testTemplateFindName(store *templateStore) func(t *testing.T) {
|
||||||
return func(t *testing.T) {
|
return func(t *testing.T) {
|
||||||
item, err := store.FindName(noContext, repo.ID, "my_template")
|
item, err := store.FindName(noContext, "my_template")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
} else {
|
} else {
|
||||||
@ -97,9 +89,9 @@ func testTemplate(item *core.Template) func(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testTemplateList(store *templateStore, repo *core.Repository) func(t *testing.T) {
|
func testTemplateListAll(store *templateStore) func(t *testing.T) {
|
||||||
return func(t *testing.T) {
|
return func(t *testing.T) {
|
||||||
list, err := store.List(noContext, repo.ID)
|
list, err := store.ListAll(noContext)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
return
|
return
|
||||||
@ -112,9 +104,9 @@ func testTemplateList(store *templateStore, repo *core.Repository) func(t *testi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testTemplateUpdate(store *templateStore, repo *core.Repository) func(t *testing.T) {
|
func testTemplateUpdate(store *templateStore) func(t *testing.T) {
|
||||||
return func(t *testing.T) {
|
return func(t *testing.T) {
|
||||||
before, err := store.FindName(noContext, repo.ID, "my_template")
|
before, err := store.FindName(noContext, "my_template")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
return
|
return
|
||||||
@ -135,9 +127,9 @@ func testTemplateUpdate(store *templateStore, repo *core.Repository) func(t *tes
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testTemplateDelete(store *templateStore, repo *core.Repository) func(t *testing.T) {
|
func testTemplateDelete(store *templateStore) func(t *testing.T) {
|
||||||
return func(t *testing.T) {
|
return func(t *testing.T) {
|
||||||
secret, err := store.FindName(noContext, repo.ID, "my_template")
|
secret, err := store.FindName(noContext, "my_template")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user