diff --git a/models/models.go b/models/models.go index 6669f01..065243e 100644 --- a/models/models.go +++ b/models/models.go @@ -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() +} diff --git a/models/models_test.go b/models/models_test.go index cfdf76d..a7d64e7 100644 --- a/models/models_test.go +++ b/models/models_test.go @@ -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) +} diff --git a/routes/route_model_test.go b/routes/route_model_test.go index 2317184..3742924 100644 --- a/routes/route_model_test.go +++ b/routes/route_model_test.go @@ -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 } diff --git a/store/postgres.go b/store/postgres.go index 084b9f7..a148f6c 100644 --- a/store/postgres.go +++ b/store/postgres.go @@ -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() +} diff --git a/store/postgres_test.go b/store/postgres_test.go index af13ff8..1718d03 100644 --- a/store/postgres_test.go +++ b/store/postgres_test.go @@ -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) +}