mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Datastore validator (#565)
* add datastore validator; adapt mock and tests * adapt bolt datastore to common validator * adapt postgres datastore to validator * adapt redis datastore to common validator
This commit is contained in:
committed by
Travis Reeder
parent
5803886a06
commit
06171800e2
@@ -1,31 +1,33 @@
|
||||
package datastore
|
||||
|
||||
import (
|
||||
"github.com/iron-io/functions/api/models"
|
||||
|
||||
"context"
|
||||
|
||||
"github.com/iron-io/functions/api/datastore/internal/datastoreutil"
|
||||
"github.com/iron-io/functions/api/models"
|
||||
)
|
||||
|
||||
type Mock struct {
|
||||
type mock struct {
|
||||
Apps []*models.App
|
||||
Routes []*models.Route
|
||||
data map[string][]byte
|
||||
}
|
||||
|
||||
func NewMock(apps []*models.App, routes []*models.Route) *Mock {
|
||||
func NewMock() models.Datastore {
|
||||
return NewMockInit(nil, nil)
|
||||
}
|
||||
|
||||
func NewMockInit(apps []*models.App, routes []*models.Route) models.Datastore {
|
||||
if apps == nil {
|
||||
apps = []*models.App{}
|
||||
}
|
||||
if routes == nil {
|
||||
routes = []*models.Route{}
|
||||
}
|
||||
return &Mock{apps, routes, make(map[string][]byte)}
|
||||
return datastoreutil.NewValidator(&mock{apps, routes, make(map[string][]byte)})
|
||||
}
|
||||
|
||||
func (m *Mock) GetApp(ctx context.Context, appName string) (app *models.App, err error) {
|
||||
if appName == "" {
|
||||
return nil, models.ErrDatastoreEmptyAppName
|
||||
}
|
||||
func (m *mock) GetApp(ctx context.Context, appName string) (app *models.App, err error) {
|
||||
for _, a := range m.Apps {
|
||||
if a.Name == appName {
|
||||
return a, nil
|
||||
@@ -35,18 +37,11 @@ func (m *Mock) GetApp(ctx context.Context, appName string) (app *models.App, err
|
||||
return nil, models.ErrAppsNotFound
|
||||
}
|
||||
|
||||
func (m *Mock) GetApps(ctx context.Context, appFilter *models.AppFilter) ([]*models.App, error) {
|
||||
func (m *mock) GetApps(ctx context.Context, appFilter *models.AppFilter) ([]*models.App, error) {
|
||||
return m.Apps, nil
|
||||
}
|
||||
|
||||
func (m *Mock) InsertApp(ctx context.Context, app *models.App) (*models.App, error) {
|
||||
if app == nil {
|
||||
return nil, models.ErrDatastoreEmptyApp
|
||||
}
|
||||
if app.Name == "" {
|
||||
return nil, models.ErrDatastoreEmptyAppName
|
||||
}
|
||||
|
||||
func (m *mock) InsertApp(ctx context.Context, app *models.App) (*models.App, error) {
|
||||
if a, _ := m.GetApp(ctx, app.Name); a != nil {
|
||||
return nil, models.ErrAppsAlreadyExists
|
||||
}
|
||||
@@ -54,7 +49,7 @@ func (m *Mock) InsertApp(ctx context.Context, app *models.App) (*models.App, err
|
||||
return app, nil
|
||||
}
|
||||
|
||||
func (m *Mock) UpdateApp(ctx context.Context, app *models.App) (*models.App, error) {
|
||||
func (m *mock) UpdateApp(ctx context.Context, app *models.App) (*models.App, error) {
|
||||
a, err := m.GetApp(ctx, app.Name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -64,10 +59,7 @@ func (m *Mock) UpdateApp(ctx context.Context, app *models.App) (*models.App, err
|
||||
return a.Clone(), nil
|
||||
}
|
||||
|
||||
func (m *Mock) RemoveApp(ctx context.Context, appName string) error {
|
||||
if appName == "" {
|
||||
return models.ErrDatastoreEmptyAppName
|
||||
}
|
||||
func (m *mock) RemoveApp(ctx context.Context, appName string) error {
|
||||
for i, a := range m.Apps {
|
||||
if a.Name == appName {
|
||||
m.Apps = append(m.Apps[:i], m.Apps[i+1:]...)
|
||||
@@ -77,13 +69,7 @@ func (m *Mock) RemoveApp(ctx context.Context, appName string) error {
|
||||
return models.ErrAppsNotFound
|
||||
}
|
||||
|
||||
func (m *Mock) GetRoute(ctx context.Context, appName, routePath string) (*models.Route, error) {
|
||||
if appName == "" {
|
||||
return nil, models.ErrDatastoreEmptyAppName
|
||||
}
|
||||
if routePath == "" {
|
||||
return nil, models.ErrDatastoreEmptyRoutePath
|
||||
}
|
||||
func (m *mock) GetRoute(ctx context.Context, appName, routePath string) (*models.Route, error) {
|
||||
for _, r := range m.Routes {
|
||||
if r.AppName == appName && r.Path == routePath {
|
||||
return r, nil
|
||||
@@ -92,14 +78,14 @@ func (m *Mock) GetRoute(ctx context.Context, appName, routePath string) (*models
|
||||
return nil, models.ErrRoutesNotFound
|
||||
}
|
||||
|
||||
func (m *Mock) GetRoutes(ctx context.Context, routeFilter *models.RouteFilter) (routes []*models.Route, err error) {
|
||||
func (m *mock) GetRoutes(ctx context.Context, routeFilter *models.RouteFilter) (routes []*models.Route, err error) {
|
||||
for _, r := range m.Routes {
|
||||
routes = append(routes, r)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (m *Mock) GetRoutesByApp(ctx context.Context, appName string, routeFilter *models.RouteFilter) (routes []*models.Route, err error) {
|
||||
func (m *mock) GetRoutesByApp(ctx context.Context, appName string, routeFilter *models.RouteFilter) (routes []*models.Route, err error) {
|
||||
for _, r := range m.Routes {
|
||||
if r.AppName == appName && (routeFilter.Path == "" || r.Path == routeFilter.Path) && (routeFilter.AppName == "" || r.AppName == routeFilter.AppName) {
|
||||
routes = append(routes, r)
|
||||
@@ -108,19 +94,7 @@ func (m *Mock) GetRoutesByApp(ctx context.Context, appName string, routeFilter *
|
||||
return
|
||||
}
|
||||
|
||||
func (m *Mock) InsertRoute(ctx context.Context, route *models.Route) (*models.Route, error) {
|
||||
if route == nil {
|
||||
return nil, models.ErrDatastoreEmptyRoute
|
||||
}
|
||||
|
||||
if route.AppName == "" {
|
||||
return nil, models.ErrDatastoreEmptyAppName
|
||||
}
|
||||
|
||||
if route.Path == "" {
|
||||
return nil, models.ErrDatastoreEmptyRoutePath
|
||||
}
|
||||
|
||||
func (m *mock) InsertRoute(ctx context.Context, route *models.Route) (*models.Route, error) {
|
||||
if _, err := m.GetApp(ctx, route.AppName); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -132,7 +106,7 @@ func (m *Mock) InsertRoute(ctx context.Context, route *models.Route) (*models.Ro
|
||||
return route, nil
|
||||
}
|
||||
|
||||
func (m *Mock) UpdateRoute(ctx context.Context, route *models.Route) (*models.Route, error) {
|
||||
func (m *mock) UpdateRoute(ctx context.Context, route *models.Route) (*models.Route, error) {
|
||||
r, err := m.GetRoute(ctx, route.AppName, route.Path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -141,13 +115,7 @@ func (m *Mock) UpdateRoute(ctx context.Context, route *models.Route) (*models.Ro
|
||||
return r.Clone(), nil
|
||||
}
|
||||
|
||||
func (m *Mock) RemoveRoute(ctx context.Context, appName, routePath string) error {
|
||||
if appName == "" {
|
||||
return models.ErrDatastoreEmptyAppName
|
||||
}
|
||||
if routePath == "" {
|
||||
return models.ErrDatastoreEmptyRoutePath
|
||||
}
|
||||
func (m *mock) RemoveRoute(ctx context.Context, appName, routePath string) error {
|
||||
for i, r := range m.Routes {
|
||||
if r.AppName == appName && r.Path == routePath {
|
||||
m.Routes = append(m.Routes[:i], m.Routes[i+1:]...)
|
||||
@@ -157,10 +125,7 @@ func (m *Mock) RemoveRoute(ctx context.Context, appName, routePath string) error
|
||||
return models.ErrRoutesNotFound
|
||||
}
|
||||
|
||||
func (m *Mock) Put(ctx context.Context, key, value []byte) error {
|
||||
if len(key) == 0 {
|
||||
return models.ErrDatastoreEmptyKey
|
||||
}
|
||||
func (m *mock) Put(ctx context.Context, key, value []byte) error {
|
||||
if len(value) == 0 {
|
||||
delete(m.data, string(key))
|
||||
} else {
|
||||
@@ -169,9 +134,6 @@ func (m *Mock) Put(ctx context.Context, key, value []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Mock) Get(ctx context.Context, key []byte) ([]byte, error) {
|
||||
if len(key) == 0 {
|
||||
return nil, models.ErrDatastoreEmptyKey
|
||||
}
|
||||
func (m *mock) Get(ctx context.Context, key []byte) ([]byte, error) {
|
||||
return m.data[string(key)], nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user