github.com/iron-io/functions@v0.0.0-20180820112432-d59d7d1c40b2/api/datastore/mock.go (about)

     1  package datastore
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/iron-io/functions/api/datastore/internal/datastoreutil"
     7  	"github.com/iron-io/functions/api/models"
     8  )
     9  
    10  type mock struct {
    11  	Apps   []*models.App
    12  	Routes []*models.Route
    13  	data   map[string][]byte
    14  }
    15  
    16  func NewMock() models.Datastore {
    17  	return NewMockInit(nil, nil)
    18  }
    19  
    20  func NewMockInit(apps []*models.App, routes []*models.Route) models.Datastore {
    21  	if apps == nil {
    22  		apps = []*models.App{}
    23  	}
    24  	if routes == nil {
    25  		routes = []*models.Route{}
    26  	}
    27  	return datastoreutil.NewValidator(&mock{apps, routes, make(map[string][]byte)})
    28  }
    29  
    30  func (m *mock) GetApp(ctx context.Context, appName string) (app *models.App, err error) {
    31  	for _, a := range m.Apps {
    32  		if a.Name == appName {
    33  			return a, nil
    34  		}
    35  	}
    36  
    37  	return nil, models.ErrAppsNotFound
    38  }
    39  
    40  func (m *mock) GetApps(ctx context.Context, appFilter *models.AppFilter) ([]*models.App, error) {
    41  	return m.Apps, nil
    42  }
    43  
    44  func (m *mock) InsertApp(ctx context.Context, app *models.App) (*models.App, error) {
    45  	if a, _ := m.GetApp(ctx, app.Name); a != nil {
    46  		return nil, models.ErrAppsAlreadyExists
    47  	}
    48  	m.Apps = append(m.Apps, app)
    49  	return app, nil
    50  }
    51  
    52  func (m *mock) UpdateApp(ctx context.Context, app *models.App) (*models.App, error) {
    53  	a, err := m.GetApp(ctx, app.Name)
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  	a.UpdateConfig(app.Config)
    58  
    59  	return a.Clone(), nil
    60  }
    61  
    62  func (m *mock) RemoveApp(ctx context.Context, appName string) error {
    63  	for i, a := range m.Apps {
    64  		if a.Name == appName {
    65  			m.Apps = append(m.Apps[:i], m.Apps[i+1:]...)
    66  			return nil
    67  		}
    68  	}
    69  	return models.ErrAppsNotFound
    70  }
    71  
    72  func (m *mock) GetRoute(ctx context.Context, appName, routePath string) (*models.Route, error) {
    73  	for _, r := range m.Routes {
    74  		if r.AppName == appName && r.Path == routePath {
    75  			return r, nil
    76  		}
    77  	}
    78  	return nil, models.ErrRoutesNotFound
    79  }
    80  
    81  func (m *mock) GetRoutes(ctx context.Context, routeFilter *models.RouteFilter) (routes []*models.Route, err error) {
    82  	for _, r := range m.Routes {
    83  		routes = append(routes, r)
    84  	}
    85  	return
    86  }
    87  
    88  func (m *mock) GetRoutesByApp(ctx context.Context, appName string, routeFilter *models.RouteFilter) (routes []*models.Route, err error) {
    89  	for _, r := range m.Routes {
    90  		if r.AppName == appName && (routeFilter.Path == "" || r.Path == routeFilter.Path) && (routeFilter.AppName == "" || r.AppName == routeFilter.AppName) {
    91  			routes = append(routes, r)
    92  		}
    93  	}
    94  	return
    95  }
    96  
    97  func (m *mock) InsertRoute(ctx context.Context, route *models.Route) (*models.Route, error) {
    98  	if _, err := m.GetApp(ctx, route.AppName); err != nil {
    99  		return nil, err
   100  	}
   101  
   102  	if r, _ := m.GetRoute(ctx, route.AppName, route.Path); r != nil {
   103  		return nil, models.ErrRoutesAlreadyExists
   104  	}
   105  	m.Routes = append(m.Routes, route)
   106  	return route, nil
   107  }
   108  
   109  func (m *mock) UpdateRoute(ctx context.Context, route *models.Route) (*models.Route, error) {
   110  	r, err := m.GetRoute(ctx, route.AppName, route.Path)
   111  	if err != nil {
   112  		return nil, err
   113  	}
   114  	r.Update(route)
   115  	return r.Clone(), nil
   116  }
   117  
   118  func (m *mock) RemoveRoute(ctx context.Context, appName, routePath string) error {
   119  	for i, r := range m.Routes {
   120  		if r.AppName == appName && r.Path == routePath {
   121  			m.Routes = append(m.Routes[:i], m.Routes[i+1:]...)
   122  			return nil
   123  		}
   124  	}
   125  	return models.ErrRoutesNotFound
   126  }
   127  
   128  func (m *mock) Put(ctx context.Context, key, value []byte) error {
   129  	if len(value) == 0 {
   130  		delete(m.data, string(key))
   131  	} else {
   132  		m.data[string(key)] = value
   133  	}
   134  	return nil
   135  }
   136  
   137  func (m *mock) Get(ctx context.Context, key []byte) ([]byte, error) {
   138  	return m.data[string(key)], nil
   139  }