feat(http): add MacroService client
Co-authored-by: Chris Goller <goller@gmail.com> Co-authored-by: Chris Henn <chris@chrishenn.net>pull/10616/head
parent
a907329075
commit
d8598bd584
|
@ -91,8 +91,8 @@ func (c *Client) CreateMacro(ctx context.Context, macro *platform.Macro) error {
|
|||
})
|
||||
}
|
||||
|
||||
// PutMacro puts a macro in the store
|
||||
func (c *Client) PutMacro(ctx context.Context, macro *platform.Macro) error {
|
||||
// ReplaceMacro puts a macro in the store
|
||||
func (c *Client) ReplaceMacro(ctx context.Context, macro *platform.Macro) error {
|
||||
return c.db.Update(func(tx *bolt.Tx) error {
|
||||
return c.putMacro(ctx, tx, macro)
|
||||
})
|
||||
|
|
|
@ -18,7 +18,7 @@ func initMacroService(f platformtesting.MacroFields, t *testing.T) (platform.Mac
|
|||
ctx := context.TODO()
|
||||
|
||||
for _, macro := range f.Macros {
|
||||
if err := c.PutMacro(ctx, macro); err != nil {
|
||||
if err := c.ReplaceMacro(ctx, macro); err != nil {
|
||||
t.Fatalf("failed to populate test macros: %v", err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ type Service struct {
|
|||
*UserService
|
||||
*BucketService
|
||||
*QueryService
|
||||
*MacroService
|
||||
*DashboardService
|
||||
}
|
||||
|
||||
|
@ -50,6 +51,10 @@ func NewService(addr, token string) *Service {
|
|||
Addr: addr,
|
||||
Token: token,
|
||||
},
|
||||
MacroService: &MacroService{
|
||||
Addr: addr,
|
||||
Token: token,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,16 +1,22 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path"
|
||||
|
||||
"github.com/influxdata/platform"
|
||||
kerrors "github.com/influxdata/platform/kit/errors"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
const (
|
||||
macroPath = "/v2/macros"
|
||||
)
|
||||
|
||||
// MacroHandler is the handler for the macro service
|
||||
type MacroHandler struct {
|
||||
*httprouter.Router
|
||||
|
@ -28,6 +34,7 @@ func NewMacroHandler() *MacroHandler {
|
|||
h.HandlerFunc("POST", "/v2/macros", h.handlePostMacro)
|
||||
h.HandlerFunc("GET", "/v2/macros/:id", h.handleGetMacro)
|
||||
h.HandlerFunc("PATCH", "/v2/macros/:id", h.handlePatchMacro)
|
||||
h.HandlerFunc("PUT", "/v2/macros/:id", h.handlePutMacro)
|
||||
h.HandlerFunc("DELETE", "/v2/macros/:id", h.handleDeleteMacro)
|
||||
|
||||
return h
|
||||
|
@ -42,6 +49,14 @@ type getMacrosResponse struct {
|
|||
Links macrosLinks `json:"links"`
|
||||
}
|
||||
|
||||
func (r getMacrosResponse) ToPlatform() []*platform.Macro {
|
||||
macros := make([]*platform.Macro, len(r.Macros))
|
||||
for i := range r.Macros {
|
||||
macros[i] = r.Macros[i].Macro
|
||||
}
|
||||
return macros
|
||||
}
|
||||
|
||||
func newGetMacrosResponse(macros []*platform.Macro) getMacrosResponse {
|
||||
resp := getMacrosResponse{
|
||||
Macros: make([]macroResponse, 0, len(macros)),
|
||||
|
@ -229,18 +244,285 @@ func decodePatchMacroRequest(ctx context.Context, r *http.Request) (*patchMacroR
|
|||
return req, nil
|
||||
}
|
||||
|
||||
func (h *MacroHandler) handlePutMacro(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
req, err := decodePutMacroRequest(ctx, r)
|
||||
if err != nil {
|
||||
EncodeError(ctx, err, w)
|
||||
return
|
||||
}
|
||||
|
||||
err = h.MacroService.ReplaceMacro(ctx, req.macro)
|
||||
if err != nil {
|
||||
EncodeError(ctx, err, w)
|
||||
return
|
||||
}
|
||||
|
||||
err = encodeResponse(ctx, w, http.StatusOK, newMacroResponse(req.macro))
|
||||
if err != nil {
|
||||
EncodeError(ctx, err, w)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
type putMacroRequest struct {
|
||||
macro *platform.Macro
|
||||
}
|
||||
|
||||
func (r *putMacroRequest) Valid() error {
|
||||
return r.macro.Valid()
|
||||
}
|
||||
|
||||
func decodePutMacroRequest(ctx context.Context, r *http.Request) (*putMacroRequest, error) {
|
||||
m := &platform.Macro{}
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(m)
|
||||
if err != nil {
|
||||
return nil, kerrors.MalformedDataf(err.Error())
|
||||
}
|
||||
|
||||
req := &putMacroRequest{
|
||||
macro: m,
|
||||
}
|
||||
|
||||
if err := req.Valid(); err != nil {
|
||||
return nil, kerrors.InvalidDataf(err.Error())
|
||||
}
|
||||
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func (h *MacroHandler) handleDeleteMacro(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
id, err := requestMacroID(ctx)
|
||||
if err != nil {
|
||||
EncodeError(ctx, err, w)
|
||||
return
|
||||
}
|
||||
|
||||
err = h.MacroService.DeleteMacro(ctx, id)
|
||||
if err != nil {
|
||||
EncodeError(ctx, err, w)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// MacroService is a macro service over HTTP to the influxdb server
|
||||
type MacroService struct {
|
||||
Addr string
|
||||
Token string
|
||||
InsecureSkipVerify bool
|
||||
}
|
||||
|
||||
// FindMacroByID finds a single macro from the store by its ID
|
||||
func (s *MacroService) FindMacroByID(ctx context.Context, id platform.ID) (*platform.Macro, error) {
|
||||
path := macroIDPath(id)
|
||||
url, err := newURL(s.Addr, path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", url.String(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
SetToken(s.Token, req)
|
||||
hc := newClient(url.Scheme, s.InsecureSkipVerify)
|
||||
|
||||
resp, err := hc.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := CheckError(resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var mr macroResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&mr); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
macro := mr.Macro
|
||||
return macro, nil
|
||||
}
|
||||
|
||||
// FindMacros returns all macros in the store
|
||||
func (s *MacroService) FindMacros(ctx context.Context) ([]*platform.Macro, error) {
|
||||
url, err := newURL(s.Addr, macroPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", url.String(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
SetToken(s.Token, req)
|
||||
hc := newClient(url.Scheme, s.InsecureSkipVerify)
|
||||
|
||||
resp, err := hc.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := CheckError(resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var ms getMacrosResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&ms); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
macros := ms.ToPlatform()
|
||||
return macros, nil
|
||||
}
|
||||
|
||||
// CreateMacro creates a new macro and assigns it an platform.ID
|
||||
func (s *MacroService) CreateMacro(ctx context.Context, m *platform.Macro) error {
|
||||
if err := m.Valid(); err != nil {
|
||||
return kerrors.InvalidDataf(err.Error())
|
||||
}
|
||||
|
||||
url, err := newURL(s.Addr, macroPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
octets, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url.String(), bytes.NewReader(octets))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
SetToken(s.Token, req)
|
||||
|
||||
hc := newClient(url.Scheme, s.InsecureSkipVerify)
|
||||
|
||||
resp, err := hc.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := CheckError(resp); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return json.NewDecoder(resp.Body).Decode(m)
|
||||
}
|
||||
|
||||
// UpdateMacro updates a single macro with a changeset
|
||||
func (s *MacroService) UpdateMacro(ctx context.Context, id platform.ID, update *platform.MacroUpdate) (*platform.Macro, error) {
|
||||
u, err := newURL(s.Addr, macroIDPath(id))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
octets, err := json.Marshal(update)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("PATCH", u.String(), bytes.NewReader(octets))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
SetToken(s.Token, req)
|
||||
|
||||
hc := newClient(u.Scheme, s.InsecureSkipVerify)
|
||||
|
||||
resp, err := hc.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := CheckError(resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var m platform.Macro
|
||||
if err := json.NewDecoder(resp.Body).Decode(&m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
return &m, nil
|
||||
}
|
||||
|
||||
// ReplaceMacro replaces a single macro
|
||||
func (s *MacroService) ReplaceMacro(ctx context.Context, macro *platform.Macro) error {
|
||||
u, err := newURL(s.Addr, macroIDPath(macro.ID))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
octets, err := json.Marshal(macro)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("PUT", u.String(), bytes.NewReader(octets))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
SetToken(s.Token, req)
|
||||
|
||||
hc := newClient(u.Scheme, s.InsecureSkipVerify)
|
||||
|
||||
resp, err := hc.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := CheckError(resp); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(resp.Body).Decode(¯o); err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteMacro removes a macro from the store
|
||||
func (s *MacroService) DeleteMacro(ctx context.Context, id platform.ID) error {
|
||||
u, err := newURL(s.Addr, macroIDPath(id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("DELETE", u.String(), nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
SetToken(s.Token, req)
|
||||
|
||||
hc := newClient(u.Scheme, s.InsecureSkipVerify)
|
||||
resp, err := hc.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return CheckError(resp)
|
||||
}
|
||||
|
||||
func macroIDPath(id platform.ID) string {
|
||||
return path.Join(macroPath, id.String())
|
||||
}
|
||||
|
|
|
@ -8,8 +8,10 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/influxdata/platform"
|
||||
"github.com/influxdata/platform/inmem"
|
||||
kerrors "github.com/influxdata/platform/kit/errors"
|
||||
"github.com/influxdata/platform/mock"
|
||||
platformtesting "github.com/influxdata/platform/testing"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
|
@ -38,7 +40,7 @@ func TestMacroService_handleGetMacros(t *testing.T) {
|
|||
ID: platform.ID("0"),
|
||||
Name: "macro-a",
|
||||
Selected: []string{"b"},
|
||||
Arguments: platform.MacroArguments{
|
||||
Arguments: &platform.MacroArguments{
|
||||
Type: "constant",
|
||||
Values: platform.MacroConstantValues{"a", "b"},
|
||||
},
|
||||
|
@ -47,7 +49,7 @@ func TestMacroService_handleGetMacros(t *testing.T) {
|
|||
ID: platform.ID("1"),
|
||||
Name: "macro-b",
|
||||
Selected: []string{"c"},
|
||||
Arguments: platform.MacroArguments{
|
||||
Arguments: &platform.MacroArguments{
|
||||
Type: "map",
|
||||
Values: platform.MacroMapValues{"a": "b", "c": "d"},
|
||||
},
|
||||
|
@ -124,7 +126,7 @@ func TestMacroService_handleGetMacro(t *testing.T) {
|
|||
ID: platform.ID("0"),
|
||||
Name: "macro-a",
|
||||
Selected: []string{"b"},
|
||||
Arguments: platform.MacroArguments{
|
||||
Arguments: &platform.MacroArguments{
|
||||
Type: "constant",
|
||||
Values: platform.MacroConstantValues{"a", "b"},
|
||||
},
|
||||
|
@ -344,7 +346,7 @@ func TestMacroService_handlePatchMacro(t *testing.T) {
|
|||
return &platform.Macro{
|
||||
ID: platform.ID("0"),
|
||||
Name: "new-name",
|
||||
Arguments: platform.MacroArguments{
|
||||
Arguments: &platform.MacroArguments{
|
||||
Type: "constant",
|
||||
Values: platform.MacroConstantValues{},
|
||||
},
|
||||
|
@ -495,3 +497,30 @@ func TestMacroService_handleDeleteMacro(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func initMacroService(f platformtesting.MacroFields, t *testing.T) (platform.MacroService, func()) {
|
||||
t.Helper()
|
||||
svc := inmem.NewService()
|
||||
svc.IDGenerator = f.IDGenerator
|
||||
|
||||
ctx := context.Background()
|
||||
for _, macro := range f.Macros {
|
||||
if err := svc.ReplaceMacro(ctx, macro); err != nil {
|
||||
t.Fatalf("failed to populate macros")
|
||||
}
|
||||
}
|
||||
|
||||
handler := NewMacroHandler()
|
||||
handler.MacroService = svc
|
||||
server := httptest.NewServer(handler)
|
||||
client := MacroService{
|
||||
Addr: server.URL,
|
||||
}
|
||||
done := server.Close
|
||||
|
||||
return &client, done
|
||||
}
|
||||
|
||||
func TestMacroService(t *testing.T) {
|
||||
platformtesting.MacroService(initMacroService, t)
|
||||
}
|
||||
|
|
|
@ -48,7 +48,7 @@ func (s *Service) FindMacros(ctx context.Context) ([]*platform.Macro, error) {
|
|||
// CreateMacro implements the platform.MacroService interface
|
||||
func (s *Service) CreateMacro(ctx context.Context, m *platform.Macro) error {
|
||||
m.ID = s.IDGenerator.ID()
|
||||
return s.PutMacro(ctx, m)
|
||||
return s.ReplaceMacro(ctx, m)
|
||||
}
|
||||
|
||||
// UpdateMacro implements the platform.MacroService interface
|
||||
|
@ -62,7 +62,7 @@ func (s *Service) UpdateMacro(ctx context.Context, id platform.ID, update *platf
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if err := s.PutMacro(ctx, macro); err != nil {
|
||||
if err := s.ReplaceMacro(ctx, macro); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -81,8 +81,8 @@ func (s *Service) DeleteMacro(ctx context.Context, id platform.ID) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// PutMacro stores a Macro in the key value store
|
||||
func (s *Service) PutMacro(ctx context.Context, m *platform.Macro) error {
|
||||
// ReplaceMacro stores a Macro in the key value store
|
||||
func (s *Service) ReplaceMacro(ctx context.Context, m *platform.Macro) error {
|
||||
s.macroKV.Store(m.ID.String(), m)
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ func initMacroService(f platformtesting.MacroFields, t *testing.T) (platform.Mac
|
|||
|
||||
ctx := context.TODO()
|
||||
for _, macro := range f.Macros {
|
||||
if err := s.PutMacro(ctx, macro); err != nil {
|
||||
if err := s.ReplaceMacro(ctx, macro); err != nil {
|
||||
t.Fatalf("failed to populate macros")
|
||||
}
|
||||
}
|
||||
|
|
22
macro.go
22
macro.go
|
@ -20,8 +20,8 @@ type MacroService interface {
|
|||
// UpdateMacro updates a single macro with a changeset
|
||||
UpdateMacro(ctx context.Context, id ID, update *MacroUpdate) (*Macro, error)
|
||||
|
||||
// UpdateMacro replaces a single macro
|
||||
PutMacro(ctx context.Context, macro *Macro) error
|
||||
// ReplaceMacro replaces a single macro
|
||||
ReplaceMacro(ctx context.Context, macro *Macro) error
|
||||
|
||||
// DeleteMacro removes a macro from the store
|
||||
DeleteMacro(ctx context.Context, id ID) error
|
||||
|
@ -30,17 +30,17 @@ type MacroService interface {
|
|||
// A Macro describes a keyword that can be expanded into several possible
|
||||
// values when used in an InfluxQL or Flux query
|
||||
type Macro struct {
|
||||
ID ID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Selected []string `json:"selected"`
|
||||
Arguments MacroArguments `json:"arguments"`
|
||||
ID ID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Selected []string `json:"selected"`
|
||||
Arguments *MacroArguments `json:"arguments"`
|
||||
}
|
||||
|
||||
// A MacroUpdate describes a set of changes that can be applied to a Macro
|
||||
type MacroUpdate struct {
|
||||
Name string `json:"name"`
|
||||
Selected []string `json:"selected"`
|
||||
Arguments MacroArguments `json:"arguments"`
|
||||
Name string `json:"name"`
|
||||
Selected []string `json:"selected"`
|
||||
Arguments *MacroArguments `json:"arguments"`
|
||||
}
|
||||
|
||||
// A MacroArguments contains arguments used when expanding a Macro
|
||||
|
@ -86,7 +86,7 @@ func (m *Macro) Valid() error {
|
|||
|
||||
// Valid returns an error if a Macro changeset is not valid
|
||||
func (u *MacroUpdate) Valid() error {
|
||||
if u.Name == "" && u.Selected == nil && u.Arguments == (MacroArguments{}) {
|
||||
if u.Name == "" && u.Selected == nil && u.Arguments == nil {
|
||||
return fmt.Errorf("no fields supplied in update")
|
||||
}
|
||||
|
||||
|
@ -103,7 +103,7 @@ func (u *MacroUpdate) Apply(m *Macro) error {
|
|||
m.Selected = u.Selected
|
||||
}
|
||||
|
||||
if u.Arguments != (MacroArguments{}) {
|
||||
if u.Arguments != nil {
|
||||
m.Arguments = u.Arguments
|
||||
}
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@ func TestMacro_UnmarshalJSON(t *testing.T) {
|
|||
ID: platform.ID("0"),
|
||||
Name: "howdy",
|
||||
Selected: make([]string, 0),
|
||||
Arguments: platform.MacroArguments{
|
||||
Arguments: &platform.MacroArguments{
|
||||
Type: "constant",
|
||||
Values: platform.MacroConstantValues{"a", "b", "c"},
|
||||
},
|
||||
|
@ -57,7 +57,7 @@ func TestMacro_UnmarshalJSON(t *testing.T) {
|
|||
ID: platform.ID("0"),
|
||||
Name: "howdy",
|
||||
Selected: make([]string, 0),
|
||||
Arguments: platform.MacroArguments{
|
||||
Arguments: &platform.MacroArguments{
|
||||
Type: "map",
|
||||
Values: platform.MacroMapValues{"a": "A", "b": "B"},
|
||||
},
|
||||
|
@ -83,7 +83,7 @@ func TestMacro_UnmarshalJSON(t *testing.T) {
|
|||
ID: platform.ID("0"),
|
||||
Name: "howdy",
|
||||
Selected: make([]string, 0),
|
||||
Arguments: platform.MacroArguments{
|
||||
Arguments: &platform.MacroArguments{
|
||||
Type: "query",
|
||||
Values: platform.MacroQueryValues{
|
||||
Query: "howdy",
|
||||
|
|
|
@ -13,7 +13,7 @@ type MacroService struct {
|
|||
FindMacroByIDF func(context.Context, platform.ID) (*platform.Macro, error)
|
||||
CreateMacroF func(context.Context, *platform.Macro) error
|
||||
UpdateMacroF func(ctx context.Context, id platform.ID, update *platform.MacroUpdate) (*platform.Macro, error)
|
||||
PutMacroF func(context.Context, *platform.Macro) error
|
||||
ReplaceMacroF func(context.Context, *platform.Macro) error
|
||||
DeleteMacroF func(context.Context, platform.ID) error
|
||||
}
|
||||
|
||||
|
@ -21,8 +21,8 @@ func (s *MacroService) CreateMacro(ctx context.Context, macro *platform.Macro) e
|
|||
return s.CreateMacroF(ctx, macro)
|
||||
}
|
||||
|
||||
func (s *MacroService) PutMacro(ctx context.Context, macro *platform.Macro) error {
|
||||
return s.PutMacroF(ctx, macro)
|
||||
func (s *MacroService) ReplaceMacro(ctx context.Context, macro *platform.Macro) error {
|
||||
return s.ReplaceMacroF(ctx, macro)
|
||||
}
|
||||
|
||||
func (s *MacroService) FindMacros(ctx context.Context) ([]*platform.Macro, error) {
|
||||
|
|
129
testing/macro.go
129
testing/macro.go
|
@ -38,6 +38,39 @@ type MacroFields struct {
|
|||
IDGenerator platform.IDGenerator
|
||||
}
|
||||
|
||||
// MacroService tests all the service functions.
|
||||
func MacroService(
|
||||
init func(MacroFields, *testing.T) (platform.MacroService, func()), t *testing.T,
|
||||
) {
|
||||
tests := []struct {
|
||||
name string
|
||||
fn func(init func(MacroFields, *testing.T) (platform.MacroService, func()),
|
||||
t *testing.T)
|
||||
}{
|
||||
{
|
||||
name: "CreateMacro",
|
||||
fn: CreateMacro,
|
||||
},
|
||||
{
|
||||
name: "FindMacroByID",
|
||||
fn: FindMacroByID,
|
||||
},
|
||||
{
|
||||
name: "UpdateMacro",
|
||||
fn: UpdateMacro,
|
||||
},
|
||||
{
|
||||
name: "DeleteMacro",
|
||||
fn: DeleteMacro,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tt.fn(init, t)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// CreateMacro tests platform.MacroService CreateMacro interface method
|
||||
func CreateMacro(init func(MacroFields, *testing.T) (platform.MacroService, func()), t *testing.T) {
|
||||
type args struct {
|
||||
|
@ -64,21 +97,23 @@ func CreateMacro(init func(MacroFields, *testing.T) (platform.MacroService, func
|
|||
},
|
||||
Macros: []*platform.Macro{
|
||||
&platform.Macro{
|
||||
ID: idFromString(t, idB),
|
||||
Name: "existing-macro",
|
||||
Arguments: platform.MacroArguments{
|
||||
ID: idFromString(t, idB),
|
||||
Name: "existing-macro",
|
||||
Selected: []string{"a"},
|
||||
Arguments: &platform.MacroArguments{
|
||||
Type: "constant",
|
||||
Values: platform.MacroConstantValues{},
|
||||
Values: platform.MacroConstantValues{"a"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
args: args{
|
||||
macro: &platform.Macro{
|
||||
Name: "my-macro",
|
||||
Arguments: platform.MacroArguments{
|
||||
Name: "my-macro",
|
||||
Selected: []string{"a"},
|
||||
Arguments: &platform.MacroArguments{
|
||||
Type: "constant",
|
||||
Values: platform.MacroConstantValues{},
|
||||
Values: platform.MacroConstantValues{"a"},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
@ -86,19 +121,21 @@ func CreateMacro(init func(MacroFields, *testing.T) (platform.MacroService, func
|
|||
err: nil,
|
||||
macros: []*platform.Macro{
|
||||
&platform.Macro{
|
||||
ID: idFromString(t, idB),
|
||||
Name: "existing-macro",
|
||||
Arguments: platform.MacroArguments{
|
||||
ID: idFromString(t, idB),
|
||||
Name: "existing-macro",
|
||||
Selected: []string{"a"},
|
||||
Arguments: &platform.MacroArguments{
|
||||
Type: "constant",
|
||||
Values: platform.MacroConstantValues{},
|
||||
Values: platform.MacroConstantValues{"a"},
|
||||
},
|
||||
},
|
||||
&platform.Macro{
|
||||
ID: idFromString(t, idA),
|
||||
Name: "my-macro",
|
||||
Arguments: platform.MacroArguments{
|
||||
ID: idFromString(t, idA),
|
||||
Name: "my-macro",
|
||||
Selected: []string{"a"},
|
||||
Arguments: &platform.MacroArguments{
|
||||
Type: "constant",
|
||||
Values: platform.MacroConstantValues{},
|
||||
Values: platform.MacroConstantValues{"a"},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
@ -147,7 +184,7 @@ func FindMacroByID(init func(MacroFields, *testing.T) (platform.MacroService, fu
|
|||
&platform.Macro{
|
||||
ID: idFromString(t, idA),
|
||||
Name: "existing-macro-a",
|
||||
Arguments: platform.MacroArguments{
|
||||
Arguments: &platform.MacroArguments{
|
||||
Type: "constant",
|
||||
Values: platform.MacroConstantValues{},
|
||||
},
|
||||
|
@ -155,7 +192,7 @@ func FindMacroByID(init func(MacroFields, *testing.T) (platform.MacroService, fu
|
|||
&platform.Macro{
|
||||
ID: idFromString(t, idB),
|
||||
Name: "existing-macro-b",
|
||||
Arguments: platform.MacroArguments{
|
||||
Arguments: &platform.MacroArguments{
|
||||
Type: "constant",
|
||||
Values: platform.MacroConstantValues{},
|
||||
},
|
||||
|
@ -170,7 +207,7 @@ func FindMacroByID(init func(MacroFields, *testing.T) (platform.MacroService, fu
|
|||
macro: &platform.Macro{
|
||||
ID: idFromString(t, idB),
|
||||
Name: "existing-macro-b",
|
||||
Arguments: platform.MacroArguments{
|
||||
Arguments: &platform.MacroArguments{
|
||||
Type: "constant",
|
||||
Values: platform.MacroConstantValues{},
|
||||
},
|
||||
|
@ -230,7 +267,7 @@ func UpdateMacro(init func(MacroFields, *testing.T) (platform.MacroService, func
|
|||
&platform.Macro{
|
||||
ID: idFromString(t, idA),
|
||||
Name: "existing-macro-a",
|
||||
Arguments: platform.MacroArguments{
|
||||
Arguments: &platform.MacroArguments{
|
||||
Type: "constant",
|
||||
Values: platform.MacroConstantValues{},
|
||||
},
|
||||
|
@ -238,7 +275,7 @@ func UpdateMacro(init func(MacroFields, *testing.T) (platform.MacroService, func
|
|||
&platform.Macro{
|
||||
ID: idFromString(t, idB),
|
||||
Name: "existing-macro-b",
|
||||
Arguments: platform.MacroArguments{
|
||||
Arguments: &platform.MacroArguments{
|
||||
Type: "constant",
|
||||
Values: platform.MacroConstantValues{},
|
||||
},
|
||||
|
@ -257,7 +294,7 @@ func UpdateMacro(init func(MacroFields, *testing.T) (platform.MacroService, func
|
|||
&platform.Macro{
|
||||
ID: idFromString(t, idA),
|
||||
Name: "existing-macro-a",
|
||||
Arguments: platform.MacroArguments{
|
||||
Arguments: &platform.MacroArguments{
|
||||
Type: "constant",
|
||||
Values: platform.MacroConstantValues{},
|
||||
},
|
||||
|
@ -265,7 +302,7 @@ func UpdateMacro(init func(MacroFields, *testing.T) (platform.MacroService, func
|
|||
&platform.Macro{
|
||||
ID: idFromString(t, idB),
|
||||
Name: "new-macro-b-name",
|
||||
Arguments: platform.MacroArguments{
|
||||
Arguments: &platform.MacroArguments{
|
||||
Type: "constant",
|
||||
Values: platform.MacroConstantValues{},
|
||||
},
|
||||
|
@ -279,8 +316,10 @@ func UpdateMacro(init func(MacroFields, *testing.T) (platform.MacroService, func
|
|||
Macros: []*platform.Macro{},
|
||||
},
|
||||
args: args{
|
||||
id: idFromString(t, idA),
|
||||
update: &platform.MacroUpdate{},
|
||||
id: idFromString(t, idA),
|
||||
update: &platform.MacroUpdate{
|
||||
Name: "howdy",
|
||||
},
|
||||
},
|
||||
wants: wants{
|
||||
err: fmt.Errorf("macro with ID %s not found", idA),
|
||||
|
@ -290,24 +329,28 @@ func UpdateMacro(init func(MacroFields, *testing.T) (platform.MacroService, func
|
|||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
s, done := init(tt.fields, t)
|
||||
defer done()
|
||||
ctx := context.TODO()
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
s, done := init(tt.fields, t)
|
||||
defer done()
|
||||
ctx := context.TODO()
|
||||
|
||||
macro, err := s.UpdateMacro(ctx, tt.args.id, tt.args.update)
|
||||
diffErrors(err, tt.wants.err, t)
|
||||
macro, err := s.UpdateMacro(ctx, tt.args.id, tt.args.update)
|
||||
diffErrors(err, tt.wants.err, t)
|
||||
|
||||
if tt.args.update.Name != "" && macro.Name != tt.args.update.Name {
|
||||
t.Fatalf("macro name not updated")
|
||||
}
|
||||
if macro != nil {
|
||||
if tt.args.update.Name != "" && macro.Name != tt.args.update.Name {
|
||||
t.Fatalf("macro name not updated")
|
||||
}
|
||||
}
|
||||
|
||||
macros, err := s.FindMacros(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to retrieve macros: %v", err)
|
||||
}
|
||||
if diff := cmp.Diff(macros, tt.wants.macros, macroCmpOptions...); diff != "" {
|
||||
t.Fatalf("found unexpected macros -got/+want\ndiff %s", diff)
|
||||
}
|
||||
macros, err := s.FindMacros(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to retrieve macros: %v", err)
|
||||
}
|
||||
if diff := cmp.Diff(macros, tt.wants.macros, macroCmpOptions...); diff != "" {
|
||||
t.Fatalf("found unexpected macros -got/+want\ndiff %s", diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -334,7 +377,7 @@ func DeleteMacro(init func(MacroFields, *testing.T) (platform.MacroService, func
|
|||
&platform.Macro{
|
||||
ID: idFromString(t, idA),
|
||||
Name: "existing-macro",
|
||||
Arguments: platform.MacroArguments{
|
||||
Arguments: &platform.MacroArguments{
|
||||
Type: "constant",
|
||||
Values: platform.MacroConstantValues{},
|
||||
},
|
||||
|
@ -356,7 +399,7 @@ func DeleteMacro(init func(MacroFields, *testing.T) (platform.MacroService, func
|
|||
&platform.Macro{
|
||||
ID: idFromString(t, idA),
|
||||
Name: "existing-macro",
|
||||
Arguments: platform.MacroArguments{
|
||||
Arguments: &platform.MacroArguments{
|
||||
Type: "constant",
|
||||
Values: platform.MacroConstantValues{},
|
||||
},
|
||||
|
@ -372,7 +415,7 @@ func DeleteMacro(init func(MacroFields, *testing.T) (platform.MacroService, func
|
|||
&platform.Macro{
|
||||
ID: idFromString(t, idA),
|
||||
Name: "existing-macro",
|
||||
Arguments: platform.MacroArguments{
|
||||
Arguments: &platform.MacroArguments{
|
||||
Type: "constant",
|
||||
Values: platform.MacroConstantValues{},
|
||||
},
|
||||
|
@ -388,7 +431,7 @@ func DeleteMacro(init func(MacroFields, *testing.T) (platform.MacroService, func
|
|||
ctx := context.TODO()
|
||||
|
||||
err := s.DeleteMacro(ctx, tt.args.id)
|
||||
defer s.PutMacro(ctx, &platform.Macro{
|
||||
defer s.ReplaceMacro(ctx, &platform.Macro{
|
||||
ID: tt.args.id,
|
||||
})
|
||||
diffErrors(err, tt.wants.err, t)
|
||||
|
|
Loading…
Reference in New Issue