Add mock handler for explorations

Signed-off-by: Will Piers <wpiers0405@gmail.com>
pull/10616/head
Chris Goller 2016-09-12 18:12:31 -05:00
parent 7ac57665b4
commit eb870302ca
4 changed files with 159 additions and 24 deletions

View File

@ -15,26 +15,29 @@ type ExplorationStore struct {
}
func NewExplorationStore(nowFunc func() time.Time) mrfusion.ExplorationStore {
return &ExplorationStore{
e := ExplorationStore{
NowFunc: nowFunc,
db: map[int]mrfusion.Exploration{},
}
e.db[1] = mrfusion.Exploration{
ID: 1,
Name: "Ferdinand Magellan",
UserID: 1,
Data: `"{"panels":{“123":{"id”:"123","queryIds":[“456"]}},"queryConfigs":{"456":{"id”:"456","database":null,"measurement":null,"retentionPolicy":null,"fields":[],"tags":{},"groupBy":{"time":null,"tags":[]},"areTagsAccepted":true,"rawText":null}}}"`,
CreatedAt: nowFunc(),
UpdatedAt: nowFunc(),
}
return &e
}
var DefaultExplorationStore ExplorationStore = ExplorationStore{
NowFunc: time.Now,
}
var DefaultExplorationStore mrfusion.ExplorationStore = NewExplorationStore(time.Now)
func (m *ExplorationStore) Query(ctx context.Context, userID int) ([]mrfusion.Exploration, error) {
return []mrfusion.Exploration{
{
ID: 1,
Name: "John Smith",
UserID: userID,
Data: "",
CreatedAt: m.NowFunc(),
UpdatedAt: m.NowFunc(),
},
}, nil
res := []mrfusion.Exploration{}
for _, v := range m.db {
res = append(res, v)
}
return res, nil
}
func (m *ExplorationStore) Add(ctx context.Context, e mrfusion.Exploration) error {

View File

@ -9,6 +9,7 @@ import (
middleware "github.com/go-openapi/runtime/middleware"
"golang.org/x/net/context"
"github.com/influxdata/mrfusion/restapi/handlers"
"github.com/influxdata/mrfusion/restapi/operations"
)
@ -34,6 +35,8 @@ func configureAPI(api *operations.MrFusionAPI) http.Handler {
api.JSONProducer = runtime.JSONProducer()
mockHandler := handlers.NewMockHandler()
api.DeleteDashboardsIDHandler = operations.DeleteDashboardsIDHandlerFunc(func(ctx context.Context, params operations.DeleteDashboardsIDParams) middleware.Responder {
return middleware.NotImplemented("operation .DeleteDashboardsID has not yet been implemented")
})
@ -43,8 +46,8 @@ func configureAPI(api *operations.MrFusionAPI) http.Handler {
api.DeleteSourcesIDRolesRoleIDHandler = operations.DeleteSourcesIDRolesRoleIDHandlerFunc(func(ctx context.Context, params operations.DeleteSourcesIDRolesRoleIDParams) middleware.Responder {
return middleware.NotImplemented("operation .DeleteSourcesIDRolesRoleID has not yet been implemented")
})
api.DeleteSourcesIDUserUserIDExplorationsExplorationIDHandler = operations.DeleteSourcesIDUserUserIDExplorationsExplorationIDHandlerFunc(func(ctx context.Context, params operations.DeleteSourcesIDUserUserIDExplorationsExplorationIDParams) middleware.Responder {
return middleware.NotImplemented("operation .DeleteSourcesIDUserUserIDExplorationsExplorationID has not yet been implemented")
api.DeleteSourcesIDUsersUserIDExplorationsExplorationIDHandler = operations.DeleteSourcesIDUsersUserIDExplorationsExplorationIDHandlerFunc(func(ctx context.Context, params operations.DeleteSourcesIDUsersUserIDExplorationsExplorationIDParams) middleware.Responder {
return middleware.NotImplemented("operation .DeleteSourcesIDUsersUserIDExplorationsExplorationID has not yet been implemented")
})
api.DeleteSourcesIDUsersUserIDHandler = operations.DeleteSourcesIDUsersUserIDHandlerFunc(func(ctx context.Context, params operations.DeleteSourcesIDUsersUserIDParams) middleware.Responder {
return middleware.NotImplemented("operation .DeleteSourcesIDUsersUserID has not yet been implemented")
@ -73,26 +76,25 @@ func configureAPI(api *operations.MrFusionAPI) http.Handler {
api.GetSourcesIDRolesRoleIDHandler = operations.GetSourcesIDRolesRoleIDHandlerFunc(func(ctx context.Context, params operations.GetSourcesIDRolesRoleIDParams) middleware.Responder {
return middleware.NotImplemented("operation .GetSourcesIDRolesRoleID has not yet been implemented")
})
api.GetSourcesIDUserUserIDExplorationsExplorationIDHandler = operations.GetSourcesIDUserUserIDExplorationsExplorationIDHandlerFunc(func(ctx context.Context, params operations.GetSourcesIDUserUserIDExplorationsExplorationIDParams) middleware.Responder {
return middleware.NotImplemented("operation .GetSourcesIDUserUserIDExplorationsExplorationID has not yet been implemented")
})
api.GetSourcesIDUsersUserIDExplorationsExplorationIDHandler = operations.GetSourcesIDUsersUserIDExplorationsExplorationIDHandlerFunc(mockHandler.Exploration)
api.GetSourcesIDUsersHandler = operations.GetSourcesIDUsersHandlerFunc(func(ctx context.Context, params operations.GetSourcesIDUsersParams) middleware.Responder {
return middleware.NotImplemented("operation .GetSourcesIDUsers has not yet been implemented")
})
api.GetSourcesIDUsersUserIDHandler = operations.GetSourcesIDUsersUserIDHandlerFunc(func(ctx context.Context, params operations.GetSourcesIDUsersUserIDParams) middleware.Responder {
return middleware.NotImplemented("operation .GetSourcesIDUsersUserID has not yet been implemented")
})
api.GetSourcesIDUsersUserIDExplorationsHandler = operations.GetSourcesIDUsersUserIDExplorationsHandlerFunc(func(ctx context.Context, params operations.GetSourcesIDUsersUserIDExplorationsParams) middleware.Responder {
return middleware.NotImplemented("operation .GetSourcesIDUsersUserIDExplorations has not yet been implemented")
})
api.GetSourcesIDUsersUserIDExplorationsHandler = operations.GetSourcesIDUsersUserIDExplorationsHandlerFunc(mockHandler.Explorations)
api.PatchSourcesIDHandler = operations.PatchSourcesIDHandlerFunc(func(ctx context.Context, params operations.PatchSourcesIDParams) middleware.Responder {
return middleware.NotImplemented("operation .PatchSourcesID has not yet been implemented")
})
api.PatchSourcesIDRolesRoleIDHandler = operations.PatchSourcesIDRolesRoleIDHandlerFunc(func(ctx context.Context, params operations.PatchSourcesIDRolesRoleIDParams) middleware.Responder {
return middleware.NotImplemented("operation .PatchSourcesIDRolesRoleID has not yet been implemented")
})
api.PatchSourcesIDUserUserIDExplorationsExplorationIDHandler = operations.PatchSourcesIDUserUserIDExplorationsExplorationIDHandlerFunc(func(ctx context.Context, params operations.PatchSourcesIDUserUserIDExplorationsExplorationIDParams) middleware.Responder {
return middleware.NotImplemented("operation .PatchSourcesIDUserUserIDExplorationsExplorationID has not yet been implemented")
api.PatchSourcesIDUsersUserIDExplorationsExplorationIDHandler = operations.PatchSourcesIDUsersUserIDExplorationsExplorationIDHandlerFunc(func(ctx context.Context, params operations.PatchSourcesIDUsersUserIDExplorationsExplorationIDParams) middleware.Responder {
return middleware.NotImplemented("operation .PatchSourcesIDUsersUserIDExplorationsExplorationID has not yet been implemented")
})
api.PatchSourcesIDUsersUserIDHandler = operations.PatchSourcesIDUsersUserIDHandlerFunc(func(ctx context.Context, params operations.PatchSourcesIDUsersUserIDParams) middleware.Responder {
return middleware.NotImplemented("operation .PatchSourcesIDUsersUserID has not yet been implemented")

View File

@ -0,0 +1,119 @@
package handlers
import (
"strconv"
"github.com/go-openapi/runtime/middleware"
"github.com/go-openapi/strfmt"
"github.com/influxdata/mrfusion"
"github.com/influxdata/mrfusion/mock"
"github.com/influxdata/mrfusion/models"
op "github.com/influxdata/mrfusion/restapi/operations"
"golang.org/x/net/context"
)
type MockHandler struct {
Store mrfusion.ExplorationStore
Response mrfusion.Response
}
func NewMockHandler() MockHandler {
return MockHandler{
mock.DefaultExplorationStore,
mock.SampleResponse,
}
}
func (m *MockHandler) Explorations(ctx context.Context, params op.GetSourcesIDUsersUserIDExplorationsParams) middleware.Responder {
id, err := strconv.Atoi(params.UserID)
if err != nil {
return op.NewGetSourcesIDUsersUserIDExplorationsDefault(500)
}
exs, err := m.Store.Query(ctx, id)
if err != nil {
return op.NewGetSourcesIDUsersUserIDExplorationsNotFound()
}
res := &models.Explorations{}
for _, e := range exs {
res.Explorations = append(res.Explorations, &models.Exploration{
Data: e.Data,
Name: e.Name,
UpdatedAt: strfmt.DateTime(e.UpdatedAt),
CreatedAt: strfmt.DateTime(e.CreatedAt),
},
)
}
return op.NewGetSourcesIDUsersUserIDExplorationsOK().WithPayload(res)
}
func (m *MockHandler) Exploration(ctx context.Context, params op.GetSourcesIDUsersUserIDExplorationsExplorationIDParams) middleware.Responder {
eID, err := strconv.Atoi(params.ExplorationID)
if err != nil {
return op.NewGetSourcesIDUsersUserIDExplorationsExplorationIDDefault(500)
}
e, err := m.Store.Get(ctx, eID)
if err != nil {
return op.NewGetSourcesIDUsersUserIDExplorationsExplorationIDNotFound()
}
res := &models.Exploration{
Data: e.Data,
Name: e.Name,
UpdatedAt: strfmt.DateTime(e.UpdatedAt),
CreatedAt: strfmt.DateTime(e.CreatedAt),
}
return op.NewGetSourcesIDUsersUserIDExplorationsExplorationIDOK().WithPayload(res)
}
func (m *MockHandler) UpdateExploration(ctx context.Context, params op.PatchSourcesIDUsersUserIDExplorationsExplorationIDParams) middleware.Responder {
eID, err := strconv.Atoi(params.ExplorationID)
if err != nil {
return op.NewPatchSourcesIDUsersUserIDExplorationsExplorationIDDefault(500)
}
e, err := m.Store.Get(ctx, eID)
if err != nil {
return op.NewPatchSourcesIDUsersUserIDExplorationsExplorationIDNotFound()
}
if params.Exploration != nil {
e.Data = params.Exploration.Data.(string)
e.Name = params.Exploration.Name
m.Store.Update(ctx, e)
}
return op.NewPatchSourcesIDUsersUserIDExplorationsExplorationIDNoContent()
}
func (m *MockHandler) NewExploration(ctx context.Context, params op.PostSourcesIDUsersUserIDExplorationsParams) middleware.Responder {
id, err := strconv.Atoi(params.UserID)
if err != nil {
return op.NewPostSourcesIDUsersUserIDExplorationsDefault(500)
}
exs, err := m.Store.Query(ctx, id)
if err != nil {
return op.NewPostSourcesIDUsersUserIDExplorationsNotFound()
}
eID := len(exs)
if params.Exploration != nil {
e := mrfusion.Exploration{
Data: params.Exploration.Data.(string),
Name: params.Exploration.Name,
ID: eID,
}
m.Store.Add(ctx, e)
}
return op.NewPostSourcesIDUsersUserIDExplorationsCreated()
}
func (m *MockHandler) DeleteExploration(ctx context.Context, params op.DeleteSourcesIDUsersUserIDExplorationsExplorationIDParams) middleware.Responder {
ID, err := strconv.Atoi(params.ExplorationID)
if err != nil {
return op.NewDeleteSourcesIDUsersUserIDExplorationsExplorationIDDefault(500)
}
if err := m.Store.Delete(ctx, mrfusion.Exploration{ID: ID}); err != nil {
return op.NewDeleteSourcesIDUsersUserIDExplorationsExplorationIDNotFound()
}
return op.NewDeleteSourcesIDUsersUserIDExplorationsExplorationIDNoContent()
}

11
restapi/handlers/proxy.go Normal file
View File

@ -0,0 +1,11 @@
package handlers
import (
"github.com/go-openapi/runtime/middleware"
op "github.com/influxdata/mrfusion/restapi/operations"
"golang.org/x/net/context"
)
func MockProxy(ctx context.Context, params op.PostSourcesIDProxyParams) middleware.Responder {
return nil
}