feat(http): add MacroService client

Co-authored-by: Chris Goller <goller@gmail.com>
Co-authored-by: Chris Henn <chris@chrishenn.net>
pull/10616/head
Christopher Henn 2018-09-18 08:30:52 -07:00 committed by Chris Henn
parent a907329075
commit d8598bd584
11 changed files with 431 additions and 72 deletions

View File

@ -91,8 +91,8 @@ func (c *Client) CreateMacro(ctx context.Context, macro *platform.Macro) error {
}) })
} }
// PutMacro puts a macro in the store // ReplaceMacro puts a macro in the store
func (c *Client) PutMacro(ctx context.Context, macro *platform.Macro) error { func (c *Client) ReplaceMacro(ctx context.Context, macro *platform.Macro) error {
return c.db.Update(func(tx *bolt.Tx) error { return c.db.Update(func(tx *bolt.Tx) error {
return c.putMacro(ctx, tx, macro) return c.putMacro(ctx, tx, macro)
}) })

View File

@ -18,7 +18,7 @@ func initMacroService(f platformtesting.MacroFields, t *testing.T) (platform.Mac
ctx := context.TODO() ctx := context.TODO()
for _, macro := range f.Macros { 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) t.Fatalf("failed to populate test macros: %v", err)
} }
} }

View File

@ -17,6 +17,7 @@ type Service struct {
*UserService *UserService
*BucketService *BucketService
*QueryService *QueryService
*MacroService
*DashboardService *DashboardService
} }
@ -50,6 +51,10 @@ func NewService(addr, token string) *Service {
Addr: addr, Addr: addr,
Token: token, Token: token,
}, },
MacroService: &MacroService{
Addr: addr,
Token: token,
},
} }
} }

View File

@ -1,16 +1,22 @@
package http package http
import ( import (
"bytes"
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"path"
"github.com/influxdata/platform" "github.com/influxdata/platform"
kerrors "github.com/influxdata/platform/kit/errors" kerrors "github.com/influxdata/platform/kit/errors"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
) )
const (
macroPath = "/v2/macros"
)
// MacroHandler is the handler for the macro service // MacroHandler is the handler for the macro service
type MacroHandler struct { type MacroHandler struct {
*httprouter.Router *httprouter.Router
@ -28,6 +34,7 @@ func NewMacroHandler() *MacroHandler {
h.HandlerFunc("POST", "/v2/macros", h.handlePostMacro) h.HandlerFunc("POST", "/v2/macros", h.handlePostMacro)
h.HandlerFunc("GET", "/v2/macros/:id", h.handleGetMacro) h.HandlerFunc("GET", "/v2/macros/:id", h.handleGetMacro)
h.HandlerFunc("PATCH", "/v2/macros/:id", h.handlePatchMacro) h.HandlerFunc("PATCH", "/v2/macros/:id", h.handlePatchMacro)
h.HandlerFunc("PUT", "/v2/macros/:id", h.handlePutMacro)
h.HandlerFunc("DELETE", "/v2/macros/:id", h.handleDeleteMacro) h.HandlerFunc("DELETE", "/v2/macros/:id", h.handleDeleteMacro)
return h return h
@ -42,6 +49,14 @@ type getMacrosResponse struct {
Links macrosLinks `json:"links"` 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 { func newGetMacrosResponse(macros []*platform.Macro) getMacrosResponse {
resp := getMacrosResponse{ resp := getMacrosResponse{
Macros: make([]macroResponse, 0, len(macros)), Macros: make([]macroResponse, 0, len(macros)),
@ -229,18 +244,285 @@ func decodePatchMacroRequest(ctx context.Context, r *http.Request) (*patchMacroR
return req, nil 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) { func (h *MacroHandler) handleDeleteMacro(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
id, err := requestMacroID(ctx) id, err := requestMacroID(ctx)
if err != nil { if err != nil {
EncodeError(ctx, err, w) EncodeError(ctx, err, w)
return
} }
err = h.MacroService.DeleteMacro(ctx, id) err = h.MacroService.DeleteMacro(ctx, id)
if err != nil { if err != nil {
EncodeError(ctx, err, w) EncodeError(ctx, err, w)
return
} }
w.WriteHeader(http.StatusNoContent) 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(&macro); 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())
}

View File

@ -8,8 +8,10 @@ import (
"testing" "testing"
"github.com/influxdata/platform" "github.com/influxdata/platform"
"github.com/influxdata/platform/inmem"
kerrors "github.com/influxdata/platform/kit/errors" kerrors "github.com/influxdata/platform/kit/errors"
"github.com/influxdata/platform/mock" "github.com/influxdata/platform/mock"
platformtesting "github.com/influxdata/platform/testing"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
) )
@ -38,7 +40,7 @@ func TestMacroService_handleGetMacros(t *testing.T) {
ID: platform.ID("0"), ID: platform.ID("0"),
Name: "macro-a", Name: "macro-a",
Selected: []string{"b"}, Selected: []string{"b"},
Arguments: platform.MacroArguments{ Arguments: &platform.MacroArguments{
Type: "constant", Type: "constant",
Values: platform.MacroConstantValues{"a", "b"}, Values: platform.MacroConstantValues{"a", "b"},
}, },
@ -47,7 +49,7 @@ func TestMacroService_handleGetMacros(t *testing.T) {
ID: platform.ID("1"), ID: platform.ID("1"),
Name: "macro-b", Name: "macro-b",
Selected: []string{"c"}, Selected: []string{"c"},
Arguments: platform.MacroArguments{ Arguments: &platform.MacroArguments{
Type: "map", Type: "map",
Values: platform.MacroMapValues{"a": "b", "c": "d"}, Values: platform.MacroMapValues{"a": "b", "c": "d"},
}, },
@ -124,7 +126,7 @@ func TestMacroService_handleGetMacro(t *testing.T) {
ID: platform.ID("0"), ID: platform.ID("0"),
Name: "macro-a", Name: "macro-a",
Selected: []string{"b"}, Selected: []string{"b"},
Arguments: platform.MacroArguments{ Arguments: &platform.MacroArguments{
Type: "constant", Type: "constant",
Values: platform.MacroConstantValues{"a", "b"}, Values: platform.MacroConstantValues{"a", "b"},
}, },
@ -344,7 +346,7 @@ func TestMacroService_handlePatchMacro(t *testing.T) {
return &platform.Macro{ return &platform.Macro{
ID: platform.ID("0"), ID: platform.ID("0"),
Name: "new-name", Name: "new-name",
Arguments: platform.MacroArguments{ Arguments: &platform.MacroArguments{
Type: "constant", Type: "constant",
Values: platform.MacroConstantValues{}, 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)
}

View File

@ -48,7 +48,7 @@ func (s *Service) FindMacros(ctx context.Context) ([]*platform.Macro, error) {
// CreateMacro implements the platform.MacroService interface // CreateMacro implements the platform.MacroService interface
func (s *Service) CreateMacro(ctx context.Context, m *platform.Macro) error { func (s *Service) CreateMacro(ctx context.Context, m *platform.Macro) error {
m.ID = s.IDGenerator.ID() m.ID = s.IDGenerator.ID()
return s.PutMacro(ctx, m) return s.ReplaceMacro(ctx, m)
} }
// UpdateMacro implements the platform.MacroService interface // 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 return nil, err
} }
if err := s.PutMacro(ctx, macro); err != nil { if err := s.ReplaceMacro(ctx, macro); err != nil {
return nil, err return nil, err
} }
@ -81,8 +81,8 @@ func (s *Service) DeleteMacro(ctx context.Context, id platform.ID) error {
return nil return nil
} }
// PutMacro stores a Macro in the key value store // ReplaceMacro stores a Macro in the key value store
func (s *Service) PutMacro(ctx context.Context, m *platform.Macro) error { func (s *Service) ReplaceMacro(ctx context.Context, m *platform.Macro) error {
s.macroKV.Store(m.ID.String(), m) s.macroKV.Store(m.ID.String(), m)
return nil return nil
} }

View File

@ -14,7 +14,7 @@ func initMacroService(f platformtesting.MacroFields, t *testing.T) (platform.Mac
ctx := context.TODO() ctx := context.TODO()
for _, macro := range f.Macros { 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") t.Fatalf("failed to populate macros")
} }
} }

View File

@ -20,8 +20,8 @@ type MacroService interface {
// UpdateMacro updates a single macro with a changeset // UpdateMacro updates a single macro with a changeset
UpdateMacro(ctx context.Context, id ID, update *MacroUpdate) (*Macro, error) UpdateMacro(ctx context.Context, id ID, update *MacroUpdate) (*Macro, error)
// UpdateMacro replaces a single macro // ReplaceMacro replaces a single macro
PutMacro(ctx context.Context, macro *Macro) error ReplaceMacro(ctx context.Context, macro *Macro) error
// DeleteMacro removes a macro from the store // DeleteMacro removes a macro from the store
DeleteMacro(ctx context.Context, id ID) error DeleteMacro(ctx context.Context, id ID) error
@ -33,14 +33,14 @@ type Macro struct {
ID ID `json:"id"` ID ID `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Selected []string `json:"selected"` Selected []string `json:"selected"`
Arguments MacroArguments `json:"arguments"` Arguments *MacroArguments `json:"arguments"`
} }
// A MacroUpdate describes a set of changes that can be applied to a Macro // A MacroUpdate describes a set of changes that can be applied to a Macro
type MacroUpdate struct { type MacroUpdate struct {
Name string `json:"name"` Name string `json:"name"`
Selected []string `json:"selected"` Selected []string `json:"selected"`
Arguments MacroArguments `json:"arguments"` Arguments *MacroArguments `json:"arguments"`
} }
// A MacroArguments contains arguments used when expanding a Macro // 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 // Valid returns an error if a Macro changeset is not valid
func (u *MacroUpdate) Valid() error { 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") return fmt.Errorf("no fields supplied in update")
} }
@ -103,7 +103,7 @@ func (u *MacroUpdate) Apply(m *Macro) error {
m.Selected = u.Selected m.Selected = u.Selected
} }
if u.Arguments != (MacroArguments{}) { if u.Arguments != nil {
m.Arguments = u.Arguments m.Arguments = u.Arguments
} }

View File

@ -31,7 +31,7 @@ func TestMacro_UnmarshalJSON(t *testing.T) {
ID: platform.ID("0"), ID: platform.ID("0"),
Name: "howdy", Name: "howdy",
Selected: make([]string, 0), Selected: make([]string, 0),
Arguments: platform.MacroArguments{ Arguments: &platform.MacroArguments{
Type: "constant", Type: "constant",
Values: platform.MacroConstantValues{"a", "b", "c"}, Values: platform.MacroConstantValues{"a", "b", "c"},
}, },
@ -57,7 +57,7 @@ func TestMacro_UnmarshalJSON(t *testing.T) {
ID: platform.ID("0"), ID: platform.ID("0"),
Name: "howdy", Name: "howdy",
Selected: make([]string, 0), Selected: make([]string, 0),
Arguments: platform.MacroArguments{ Arguments: &platform.MacroArguments{
Type: "map", Type: "map",
Values: platform.MacroMapValues{"a": "A", "b": "B"}, Values: platform.MacroMapValues{"a": "A", "b": "B"},
}, },
@ -83,7 +83,7 @@ func TestMacro_UnmarshalJSON(t *testing.T) {
ID: platform.ID("0"), ID: platform.ID("0"),
Name: "howdy", Name: "howdy",
Selected: make([]string, 0), Selected: make([]string, 0),
Arguments: platform.MacroArguments{ Arguments: &platform.MacroArguments{
Type: "query", Type: "query",
Values: platform.MacroQueryValues{ Values: platform.MacroQueryValues{
Query: "howdy", Query: "howdy",

View File

@ -13,7 +13,7 @@ type MacroService struct {
FindMacroByIDF func(context.Context, platform.ID) (*platform.Macro, error) FindMacroByIDF func(context.Context, platform.ID) (*platform.Macro, error)
CreateMacroF func(context.Context, *platform.Macro) error CreateMacroF func(context.Context, *platform.Macro) error
UpdateMacroF func(ctx context.Context, id platform.ID, update *platform.MacroUpdate) (*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 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) return s.CreateMacroF(ctx, macro)
} }
func (s *MacroService) PutMacro(ctx context.Context, macro *platform.Macro) error { func (s *MacroService) ReplaceMacro(ctx context.Context, macro *platform.Macro) error {
return s.PutMacroF(ctx, macro) return s.ReplaceMacroF(ctx, macro)
} }
func (s *MacroService) FindMacros(ctx context.Context) ([]*platform.Macro, error) { func (s *MacroService) FindMacros(ctx context.Context) ([]*platform.Macro, error) {

View File

@ -38,6 +38,39 @@ type MacroFields struct {
IDGenerator platform.IDGenerator 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 // CreateMacro tests platform.MacroService CreateMacro interface method
func CreateMacro(init func(MacroFields, *testing.T) (platform.MacroService, func()), t *testing.T) { func CreateMacro(init func(MacroFields, *testing.T) (platform.MacroService, func()), t *testing.T) {
type args struct { type args struct {
@ -66,9 +99,10 @@ func CreateMacro(init func(MacroFields, *testing.T) (platform.MacroService, func
&platform.Macro{ &platform.Macro{
ID: idFromString(t, idB), ID: idFromString(t, idB),
Name: "existing-macro", Name: "existing-macro",
Arguments: platform.MacroArguments{ Selected: []string{"a"},
Arguments: &platform.MacroArguments{
Type: "constant", Type: "constant",
Values: platform.MacroConstantValues{}, Values: platform.MacroConstantValues{"a"},
}, },
}, },
}, },
@ -76,9 +110,10 @@ func CreateMacro(init func(MacroFields, *testing.T) (platform.MacroService, func
args: args{ args: args{
macro: &platform.Macro{ macro: &platform.Macro{
Name: "my-macro", Name: "my-macro",
Arguments: platform.MacroArguments{ Selected: []string{"a"},
Arguments: &platform.MacroArguments{
Type: "constant", Type: "constant",
Values: platform.MacroConstantValues{}, Values: platform.MacroConstantValues{"a"},
}, },
}, },
}, },
@ -88,17 +123,19 @@ func CreateMacro(init func(MacroFields, *testing.T) (platform.MacroService, func
&platform.Macro{ &platform.Macro{
ID: idFromString(t, idB), ID: idFromString(t, idB),
Name: "existing-macro", Name: "existing-macro",
Arguments: platform.MacroArguments{ Selected: []string{"a"},
Arguments: &platform.MacroArguments{
Type: "constant", Type: "constant",
Values: platform.MacroConstantValues{}, Values: platform.MacroConstantValues{"a"},
}, },
}, },
&platform.Macro{ &platform.Macro{
ID: idFromString(t, idA), ID: idFromString(t, idA),
Name: "my-macro", Name: "my-macro",
Arguments: platform.MacroArguments{ Selected: []string{"a"},
Arguments: &platform.MacroArguments{
Type: "constant", 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{ &platform.Macro{
ID: idFromString(t, idA), ID: idFromString(t, idA),
Name: "existing-macro-a", Name: "existing-macro-a",
Arguments: platform.MacroArguments{ Arguments: &platform.MacroArguments{
Type: "constant", Type: "constant",
Values: platform.MacroConstantValues{}, Values: platform.MacroConstantValues{},
}, },
@ -155,7 +192,7 @@ func FindMacroByID(init func(MacroFields, *testing.T) (platform.MacroService, fu
&platform.Macro{ &platform.Macro{
ID: idFromString(t, idB), ID: idFromString(t, idB),
Name: "existing-macro-b", Name: "existing-macro-b",
Arguments: platform.MacroArguments{ Arguments: &platform.MacroArguments{
Type: "constant", Type: "constant",
Values: platform.MacroConstantValues{}, Values: platform.MacroConstantValues{},
}, },
@ -170,7 +207,7 @@ func FindMacroByID(init func(MacroFields, *testing.T) (platform.MacroService, fu
macro: &platform.Macro{ macro: &platform.Macro{
ID: idFromString(t, idB), ID: idFromString(t, idB),
Name: "existing-macro-b", Name: "existing-macro-b",
Arguments: platform.MacroArguments{ Arguments: &platform.MacroArguments{
Type: "constant", Type: "constant",
Values: platform.MacroConstantValues{}, Values: platform.MacroConstantValues{},
}, },
@ -230,7 +267,7 @@ func UpdateMacro(init func(MacroFields, *testing.T) (platform.MacroService, func
&platform.Macro{ &platform.Macro{
ID: idFromString(t, idA), ID: idFromString(t, idA),
Name: "existing-macro-a", Name: "existing-macro-a",
Arguments: platform.MacroArguments{ Arguments: &platform.MacroArguments{
Type: "constant", Type: "constant",
Values: platform.MacroConstantValues{}, Values: platform.MacroConstantValues{},
}, },
@ -238,7 +275,7 @@ func UpdateMacro(init func(MacroFields, *testing.T) (platform.MacroService, func
&platform.Macro{ &platform.Macro{
ID: idFromString(t, idB), ID: idFromString(t, idB),
Name: "existing-macro-b", Name: "existing-macro-b",
Arguments: platform.MacroArguments{ Arguments: &platform.MacroArguments{
Type: "constant", Type: "constant",
Values: platform.MacroConstantValues{}, Values: platform.MacroConstantValues{},
}, },
@ -257,7 +294,7 @@ func UpdateMacro(init func(MacroFields, *testing.T) (platform.MacroService, func
&platform.Macro{ &platform.Macro{
ID: idFromString(t, idA), ID: idFromString(t, idA),
Name: "existing-macro-a", Name: "existing-macro-a",
Arguments: platform.MacroArguments{ Arguments: &platform.MacroArguments{
Type: "constant", Type: "constant",
Values: platform.MacroConstantValues{}, Values: platform.MacroConstantValues{},
}, },
@ -265,7 +302,7 @@ func UpdateMacro(init func(MacroFields, *testing.T) (platform.MacroService, func
&platform.Macro{ &platform.Macro{
ID: idFromString(t, idB), ID: idFromString(t, idB),
Name: "new-macro-b-name", Name: "new-macro-b-name",
Arguments: platform.MacroArguments{ Arguments: &platform.MacroArguments{
Type: "constant", Type: "constant",
Values: platform.MacroConstantValues{}, Values: platform.MacroConstantValues{},
}, },
@ -280,7 +317,9 @@ func UpdateMacro(init func(MacroFields, *testing.T) (platform.MacroService, func
}, },
args: args{ args: args{
id: idFromString(t, idA), id: idFromString(t, idA),
update: &platform.MacroUpdate{}, update: &platform.MacroUpdate{
Name: "howdy",
},
}, },
wants: wants{ wants: wants{
err: fmt.Errorf("macro with ID %s not found", idA), err: fmt.Errorf("macro with ID %s not found", idA),
@ -290,6 +329,7 @@ func UpdateMacro(init func(MacroFields, *testing.T) (platform.MacroService, func
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, done := init(tt.fields, t) s, done := init(tt.fields, t)
defer done() defer done()
ctx := context.TODO() ctx := context.TODO()
@ -297,9 +337,11 @@ func UpdateMacro(init func(MacroFields, *testing.T) (platform.MacroService, func
macro, err := s.UpdateMacro(ctx, tt.args.id, tt.args.update) macro, err := s.UpdateMacro(ctx, tt.args.id, tt.args.update)
diffErrors(err, tt.wants.err, t) diffErrors(err, tt.wants.err, t)
if macro != nil {
if tt.args.update.Name != "" && macro.Name != tt.args.update.Name { if tt.args.update.Name != "" && macro.Name != tt.args.update.Name {
t.Fatalf("macro name not updated") t.Fatalf("macro name not updated")
} }
}
macros, err := s.FindMacros(ctx) macros, err := s.FindMacros(ctx)
if err != nil { if err != nil {
@ -308,6 +350,7 @@ func UpdateMacro(init func(MacroFields, *testing.T) (platform.MacroService, func
if diff := cmp.Diff(macros, tt.wants.macros, macroCmpOptions...); diff != "" { if diff := cmp.Diff(macros, tt.wants.macros, macroCmpOptions...); diff != "" {
t.Fatalf("found unexpected macros -got/+want\ndiff %s", 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{ &platform.Macro{
ID: idFromString(t, idA), ID: idFromString(t, idA),
Name: "existing-macro", Name: "existing-macro",
Arguments: platform.MacroArguments{ Arguments: &platform.MacroArguments{
Type: "constant", Type: "constant",
Values: platform.MacroConstantValues{}, Values: platform.MacroConstantValues{},
}, },
@ -356,7 +399,7 @@ func DeleteMacro(init func(MacroFields, *testing.T) (platform.MacroService, func
&platform.Macro{ &platform.Macro{
ID: idFromString(t, idA), ID: idFromString(t, idA),
Name: "existing-macro", Name: "existing-macro",
Arguments: platform.MacroArguments{ Arguments: &platform.MacroArguments{
Type: "constant", Type: "constant",
Values: platform.MacroConstantValues{}, Values: platform.MacroConstantValues{},
}, },
@ -372,7 +415,7 @@ func DeleteMacro(init func(MacroFields, *testing.T) (platform.MacroService, func
&platform.Macro{ &platform.Macro{
ID: idFromString(t, idA), ID: idFromString(t, idA),
Name: "existing-macro", Name: "existing-macro",
Arguments: platform.MacroArguments{ Arguments: &platform.MacroArguments{
Type: "constant", Type: "constant",
Values: platform.MacroConstantValues{}, Values: platform.MacroConstantValues{},
}, },
@ -388,7 +431,7 @@ func DeleteMacro(init func(MacroFields, *testing.T) (platform.MacroService, func
ctx := context.TODO() ctx := context.TODO()
err := s.DeleteMacro(ctx, tt.args.id) err := s.DeleteMacro(ctx, tt.args.id)
defer s.PutMacro(ctx, &platform.Macro{ defer s.ReplaceMacro(ctx, &platform.Macro{
ID: tt.args.id, ID: tt.args.id,
}) })
diffErrors(err, tt.wants.err, t) diffErrors(err, tt.wants.err, t)