added more unit tests, changed input for template data to byte array & fixed another issue with insert sql script not autoincrement
This commit is contained in:
parent
2d84d48c3a
commit
b63facdc62
9 changed files with 77 additions and 29 deletions
|
@ -22,8 +22,6 @@ import (
|
|||
var (
|
||||
errTemplateNameInvalid = errors.New("No Template Name Provided")
|
||||
errTemplateDataInvalid = errors.New("No Template Data Provided")
|
||||
//errTemplateCreatedInvalid = errors.New("Invalid Template Created Value")
|
||||
//errTemplateUpdatedInvalid = errors.New("Invalid Template Updated Value")
|
||||
)
|
||||
|
||||
type (
|
||||
|
@ -36,7 +34,7 @@ type (
|
|||
Template struct {
|
||||
Id int64 `json:"id,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Data string `json:"data,omitempty"`
|
||||
Data []byte `json:"data,omitempty"`
|
||||
Created int64 `json:"created,omitempty"`
|
||||
Updated int64 `json:"updated,omitempty"`
|
||||
}
|
||||
|
@ -70,10 +68,6 @@ func (s *Template) Validate() error {
|
|||
return errTemplateNameInvalid
|
||||
case len(s.Data) == 0:
|
||||
return errTemplateDataInvalid
|
||||
//case s.Created == 0:
|
||||
// return errTemplateCreatedInvalid
|
||||
//case s.Updated == 0:
|
||||
// return errTemplateUpdatedInvalid
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -15,9 +15,7 @@ import (
|
|||
|
||||
type templateInput struct {
|
||||
Name string `json:"name"`
|
||||
Data string `json:"data"`
|
||||
Created int64 `json:"created"`
|
||||
Updated int64 `json:"updated"`
|
||||
Data []byte `json:"data"`
|
||||
}
|
||||
|
||||
// HandleCreate returns an http.HandlerFunc that processes http
|
||||
|
@ -34,8 +32,6 @@ func HandleCreate(secrets core.TemplateStore) http.HandlerFunc {
|
|||
t := &core.Template{
|
||||
Name: in.Name,
|
||||
Data: in.Data,
|
||||
Created: in.Created,
|
||||
Updated: in.Updated,
|
||||
}
|
||||
|
||||
err = t.Validate()
|
||||
|
|
|
@ -51,7 +51,7 @@ func TestHandleCreate_ValidationErrorName(t *testing.T) {
|
|||
|
||||
c := new(chi.Context)
|
||||
in := new(bytes.Buffer)
|
||||
json.NewEncoder(in).Encode(&core.Template{Name: "", Data: "my_data"})
|
||||
json.NewEncoder(in).Encode(&core.Template{Name: "", Data: []byte("my_data")})
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
r := httptest.NewRequest("GET", "/", in)
|
||||
|
@ -77,7 +77,7 @@ func TestHandleCreate_ValidationErrorData(t *testing.T) {
|
|||
|
||||
c := new(chi.Context)
|
||||
in := new(bytes.Buffer)
|
||||
json.NewEncoder(in).Encode(&core.Template{Name: "my_template", Data: ""})
|
||||
json.NewEncoder(in).Encode(&core.Template{Name: "my_template", Data: nil})
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
r := httptest.NewRequest("GET", "/", in)
|
||||
|
|
|
@ -6,12 +6,24 @@
|
|||
|
||||
package template
|
||||
|
||||
import "github.com/drone/drone/core"
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/drone/drone/core"
|
||||
"github.com/drone/drone/handler/api/errors"
|
||||
"github.com/drone/drone/mock"
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
dummyTemplate = &core.Template{
|
||||
Name: "my_template",
|
||||
Data: "my_data",
|
||||
Data: []byte("my_data"),
|
||||
Created: 1,
|
||||
Updated: 2,
|
||||
}
|
||||
|
@ -19,3 +31,51 @@ var (
|
|||
dummyTemplate,
|
||||
}
|
||||
)
|
||||
|
||||
func TestHandleList(t *testing.T) {
|
||||
controller := gomock.NewController(t)
|
||||
defer controller.Finish()
|
||||
|
||||
templates := mock.NewMockTemplateStore(controller)
|
||||
templates.EXPECT().ListAll(gomock.Any()).Return(dummyTemplateList, nil)
|
||||
|
||||
c := new(chi.Context)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
r := httptest.NewRequest("GET", "/", nil)
|
||||
r = r.WithContext(
|
||||
context.WithValue(context.Background(), chi.RouteCtxKey, c),
|
||||
)
|
||||
|
||||
HandleList(templates).ServeHTTP(w, r)
|
||||
if got, want := w.Code, http.StatusOK; want != got {
|
||||
t.Errorf("Want response code %d, got %d", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleList_TemplateListErr(t *testing.T) {
|
||||
controller := gomock.NewController(t)
|
||||
defer controller.Finish()
|
||||
|
||||
templates := mock.NewMockTemplateStore(controller)
|
||||
templates.EXPECT().ListAll(gomock.Any()).Return(nil, errors.ErrNotFound)
|
||||
|
||||
c := new(chi.Context)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
r := httptest.NewRequest("GET", "/", nil)
|
||||
r = r.WithContext(
|
||||
context.WithValue(context.Background(), chi.RouteCtxKey, c),
|
||||
)
|
||||
|
||||
HandleList(templates).ServeHTTP(w, r)
|
||||
if got, want := w.Code, http.StatusNotFound; want != got {
|
||||
t.Errorf("Want response code %d, got %d", want, got)
|
||||
}
|
||||
|
||||
got, want := new(errors.Error), errors.ErrNotFound
|
||||
json.NewDecoder(w.Body).Decode(got)
|
||||
if diff := cmp.Diff(got, want); len(diff) != 0 {
|
||||
t.Errorf(diff)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ import (
|
|||
)
|
||||
|
||||
type templateUpdate struct {
|
||||
Data *string `json:"data"`
|
||||
Data *[]byte `json:"data"`
|
||||
Updated *int64 `json:"Updated"`
|
||||
}
|
||||
|
||||
|
|
|
@ -121,7 +121,7 @@ func TestHandleUpdate_UpdateError(t *testing.T) {
|
|||
c.URLParams.Add("name", "my_template")
|
||||
|
||||
in := new(bytes.Buffer)
|
||||
json.NewEncoder(in).Encode(&core.Template{Data: "my_data"})
|
||||
json.NewEncoder(in).Encode(&core.Template{Data: []byte("my_data")})
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
r := httptest.NewRequest("GET", "/", in)
|
||||
|
|
|
@ -8,7 +8,6 @@ package template
|
|||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/drone/drone/core"
|
||||
"github.com/drone/drone/store/shared/db"
|
||||
)
|
||||
|
@ -168,14 +167,12 @@ ORDER BY template_name
|
|||
|
||||
const stmtInsert = `
|
||||
INSERT INTO template (
|
||||
template_id
|
||||
,template_name
|
||||
template_name
|
||||
,template_data
|
||||
,template_created
|
||||
,template_updated
|
||||
) VALUES (
|
||||
:template_id
|
||||
,:template_name
|
||||
:template_name
|
||||
,:template_data
|
||||
,:template_created
|
||||
,:template_updated
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
package template
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"database/sql"
|
||||
"github.com/drone/drone/core"
|
||||
|
@ -36,7 +37,7 @@ func testTemplateCreate(store *templateStore) func(t *testing.T) {
|
|||
item := &core.Template{
|
||||
Id: 1,
|
||||
Name: "my_template",
|
||||
Data: "some_template_data",
|
||||
Data: []byte("some_template_data"),
|
||||
Created: 1,
|
||||
Updated: 2,
|
||||
}
|
||||
|
@ -83,7 +84,7 @@ func testTemplate(item *core.Template) func(t *testing.T) {
|
|||
if got, want := item.Name, "my_template"; got != want {
|
||||
t.Errorf("Want template name %q, got %q", want, got)
|
||||
}
|
||||
if got, want := item.Data, "some_template_data"; got != want {
|
||||
if got, want := item.Data, []byte("some_template_data"); bytes.Compare(got, want) != 0 {
|
||||
t.Errorf("Want template data %q, got %q", want, got)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue