Adds healthy check to model

This commit is contained in:
Deepak Mallubhotla 2020-12-31 18:05:06 -06:00
parent 2bda056ca7
commit 0e16bb5361
Signed by: deepak
GPG Key ID: 64BF53A3369104E7
5 changed files with 50 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package models
// Store represents the backing store.
type Store interface {
ConnectionLive() error
SelectActions() ([]*Action, error)
SelectActionByID(id int) (*Action, error)
SelectPlans() ([]*Plan, error)
@ -19,3 +20,9 @@ type Model struct {
func New(store Store) *Model {
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()
}

View File

@ -35,6 +35,10 @@ func (ms *multiStore) SelectActionsByPlanID(plan *models.Plan) ([]*models.Action
return ms.actions, nil
}
func (ms *multiStore) ConnectionLive() error {
return nil
}
func TestModelActions(t *testing.T) {
assert := assert.New(t)
a1 := &models.Action{ActionID: 3}
@ -83,3 +87,16 @@ func TestModelPlanMethods(t *testing.T) {
assert.Nil(err)
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)
}

View File

@ -34,6 +34,10 @@ func (ms *multiStore) SelectActionsByPlanID(plan *models.Plan) ([]*models.Action
return ms.actions, nil
}
func (ms *multiStore) ConnectionLive() error {
return nil
}
func getEmptyModel() *models.Model {
ss := &multiStore{
[]*models.Action{},
@ -75,6 +79,10 @@ func (e *errorStore) SelectActionsByPlanID(plan *models.Plan) ([]*models.Action,
return nil, e.error
}
func (e *errorStore) ConnectionLive() error {
return e.error
}
type errorStore struct {
error error
}

View File

@ -78,3 +78,7 @@ func (store *postgresStore) InsertPlan(plan *models.Plan) (int, error) {
}
return id, nil
}
func (store *postgresStore) ConnectionLive() error {
return store.db.Ping()
}

View File

@ -12,7 +12,7 @@ import (
)
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 {
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)
}
}
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)
}