Adds healthy check to model
This commit is contained in:
parent
2bda056ca7
commit
0e16bb5361
@ -2,6 +2,7 @@ package models
|
|||||||
|
|
||||||
// Store represents the backing store.
|
// Store represents the backing store.
|
||||||
type Store interface {
|
type Store interface {
|
||||||
|
ConnectionLive() error
|
||||||
SelectActions() ([]*Action, error)
|
SelectActions() ([]*Action, error)
|
||||||
SelectActionByID(id int) (*Action, error)
|
SelectActionByID(id int) (*Action, error)
|
||||||
SelectPlans() ([]*Plan, error)
|
SelectPlans() ([]*Plan, error)
|
||||||
@ -19,3 +20,9 @@ type Model struct {
|
|||||||
func New(store Store) *Model {
|
func New(store Store) *Model {
|
||||||
return &Model{Store: store}
|
return &Model{Store: store}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Healthy returns an error if the connection is healthy.
|
||||||
|
// Wrapper over db.Ping()
|
||||||
|
func (m *Model) Healthy() error {
|
||||||
|
return m.ConnectionLive()
|
||||||
|
}
|
||||||
|
@ -35,6 +35,10 @@ func (ms *multiStore) SelectActionsByPlanID(plan *models.Plan) ([]*models.Action
|
|||||||
return ms.actions, nil
|
return ms.actions, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ms *multiStore) ConnectionLive() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func TestModelActions(t *testing.T) {
|
func TestModelActions(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
a1 := &models.Action{ActionID: 3}
|
a1 := &models.Action{ActionID: 3}
|
||||||
@ -83,3 +87,16 @@ func TestModelPlanMethods(t *testing.T) {
|
|||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
assert.EqualValues(6, planId)
|
assert.EqualValues(6, planId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestModelHealthy(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
ss := &multiStore{
|
||||||
|
[]*models.Action{},
|
||||||
|
[]*models.Plan{},
|
||||||
|
}
|
||||||
|
m := models.New(ss)
|
||||||
|
|
||||||
|
err := m.Healthy()
|
||||||
|
assert.Nil(err)
|
||||||
|
}
|
||||||
|
@ -34,6 +34,10 @@ func (ms *multiStore) SelectActionsByPlanID(plan *models.Plan) ([]*models.Action
|
|||||||
return ms.actions, nil
|
return ms.actions, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ms *multiStore) ConnectionLive() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func getEmptyModel() *models.Model {
|
func getEmptyModel() *models.Model {
|
||||||
ss := &multiStore{
|
ss := &multiStore{
|
||||||
[]*models.Action{},
|
[]*models.Action{},
|
||||||
@ -75,6 +79,10 @@ func (e *errorStore) SelectActionsByPlanID(plan *models.Plan) ([]*models.Action,
|
|||||||
return nil, e.error
|
return nil, e.error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *errorStore) ConnectionLive() error {
|
||||||
|
return e.error
|
||||||
|
}
|
||||||
|
|
||||||
type errorStore struct {
|
type errorStore struct {
|
||||||
error error
|
error error
|
||||||
}
|
}
|
||||||
|
@ -78,3 +78,7 @@ func (store *postgresStore) InsertPlan(plan *models.Plan) (int, error) {
|
|||||||
}
|
}
|
||||||
return id, nil
|
return id, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (store *postgresStore) ConnectionLive() error {
|
||||||
|
return store.db.Ping()
|
||||||
|
}
|
||||||
|
@ -12,7 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func getDbMock(t *testing.T) (models.Store, sqlmock.Sqlmock) {
|
func getDbMock(t *testing.T) (models.Store, sqlmock.Sqlmock) {
|
||||||
db, mock, err := sqlmock.New()
|
db, mock, err := sqlmock.New(sqlmock.MonitorPingsOption(true))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("got an error creating a stub db. How?: %s", err)
|
t.Fatalf("got an error creating a stub db. How?: %s", err)
|
||||||
}
|
}
|
||||||
@ -385,3 +385,16 @@ func TestErrActionByID(t *testing.T) {
|
|||||||
t.Errorf("unfulfilled expectations: %s", err)
|
t.Errorf("unfulfilled expectations: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConnectionLive(t *testing.T) {
|
||||||
|
// setup
|
||||||
|
assert := assert.New(t)
|
||||||
|
str, mock := getDbMock(t)
|
||||||
|
|
||||||
|
mock.ExpectPing()
|
||||||
|
|
||||||
|
// perform func under tests
|
||||||
|
err := str.ConnectionLive()
|
||||||
|
// results
|
||||||
|
assert.Nil(err)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user