From ad47895597be4f09c85c0bbf3bcb26ee5def6a3f Mon Sep 17 00:00:00 2001 From: Deepak Date: Tue, 29 Dec 2020 16:08:31 -0600 Subject: [PATCH] Add select by action id --- models/action.go | 5 ++++ models/models.go | 1 + models/models_test.go | 13 ++++++--- store/postgres.go | 9 ++++++ store/postgres_test.go | 65 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 89 insertions(+), 4 deletions(-) diff --git a/models/action.go b/models/action.go index c32cf7e..c2564d0 100644 --- a/models/action.go +++ b/models/action.go @@ -20,3 +20,8 @@ type Action struct { func (m *Model) Actions() ([]*Action, error) { return m.SelectActions() } + +// Action returns a single action from its ID +func (m *Model) Action(id int) (*Action, error) { + return m.SelectActionByID(id) +} diff --git a/models/models.go b/models/models.go index 87d2194..d503f57 100644 --- a/models/models.go +++ b/models/models.go @@ -4,6 +4,7 @@ package models type Store interface { SelectActions() ([]*Action, error) SelectPlans() ([]*Plan, error) + SelectActionByID(id int) (*Action, error) } // Model represents a current model item. diff --git a/models/models_test.go b/models/models_test.go index ee5e1e6..27ab7a6 100644 --- a/models/models_test.go +++ b/models/models_test.go @@ -6,10 +6,7 @@ import ( "testing" ) -type store interface { - SelectActions() ([]*models.Action, error) - SelectPlans() ([]*models.Plan, error) -} + type multiStore struct { actions []*models.Action plans []*models.Plan @@ -19,6 +16,10 @@ func (ms *multiStore) SelectActions() ([]*models.Action, error) { return ms.actions, nil } +func (ms *multiStore) SelectActionByID(id int) (*models.Action, error) { + return ms.actions[0], nil +} + func (ms *multiStore) SelectPlans() ([]*models.Plan, error) { return ms.plans, nil } @@ -40,4 +41,8 @@ func TestModelActions(t *testing.T) { plans, err := m.Plans() assert.Nil(err) assert.Equal(1, len(plans)) + + firstAction, err := m.Action(3) + assert.Nil(err) + assert.EqualValues(3, firstAction.ActionID) } diff --git a/store/postgres.go b/store/postgres.go index 66ef86b..30a96f9 100644 --- a/store/postgres.go +++ b/store/postgres.go @@ -27,6 +27,15 @@ func (store *postgresStore) SelectActions() ([]*models.Action, error) { return actions, nil } +func (store *postgresStore) SelectActionByID(id int) (*models.Action, error) { + action := models.Action{} + err := store.db.Get(&action, "SELECT action_id, action_description, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE action_id = ?", id) + if err != nil { + return nil, err + } + return &action, nil +} + func (store *postgresStore) SelectPlans() ([]*models.Plan, error) { plans := make([]*models.Plan, 0) err := store.db.Select(&plans, "SELECT plan_id, plan_date FROM plans") diff --git a/store/postgres_test.go b/store/postgres_test.go index f67a731..0a099e4 100644 --- a/store/postgres_test.go +++ b/store/postgres_test.go @@ -95,6 +95,55 @@ func TestSelectActions(t *testing.T) { } } +func TestSelectActionById(t *testing.T) { + // set up test + assert := assert.New(t) + + createTime, _ := time.Parse("2006-01-02", "2020-12-31") + updateTime, _ := time.Parse("2006-01-02", "2021-01-01") + completeTime, _ := time.Parse("2006-01-02", "2021-01-05") + idToUse := 1 + estChunks := 5 + compChunks := 7 + desc := "Howdy, partner." + + str, mock := getDbMock(t) + + rows := sqlmock.NewRows([]string{ + "action_id", + "action_description", + "estimated_chunks", + "completed_chunks", + "completed_on", + "created_at", + "updated_at", + "plan_id"}). + AddRow(idToUse, desc, estChunks, compChunks, completeTime, createTime, updateTime, idToUse) + + mock.ExpectQuery("^SELECT action_id, action_description, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE action_id = \\?"). + WithArgs(1). + WillReturnRows(rows) + + // function under test + action, err := str.SelectActionByID(1) + + // test results + assert.Nil(err) + + assert.EqualValues(idToUse, action.ActionID) + assert.Equal(desc, action.ActionDescription) + assert.Equal(estChunks, action.EstimatedChunks) + assert.Equal(compChunks, action.CompletedChunks) + assert.Equal(completeTime, action.CompletedOn) + assert.Equal(createTime, action.CreatedAt) + assert.Equal(updateTime, action.UpdatedAt) + assert.Equal(idToUse, action.PlanID) + + if err := mock.ExpectationsWereMet(); err != nil { + t.Errorf("unfulfilled expectations: %s", err) + } +} + func TestErrPlans(t *testing.T) { // set up tests assert := assert.New(t) @@ -126,3 +175,19 @@ func TestErrActions(t *testing.T) { t.Errorf("unfulfilled expectations: %s", err) } } + +func TestErrActionByID(t *testing.T) { + // set up tests + assert := assert.New(t) + str, mock := getDbMock(t) + + mock.ExpectQuery("^SELECT action_id, action_description, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE action_id = \\?").WillReturnError(fmt.Errorf("example error")) + // function under test + action, err := str.SelectActionByID(1) + // test results + assert.Nil(action) + assert.NotNil(err) + if err := mock.ExpectationsWereMet(); err != nil { + t.Errorf("unfulfilled expectations: %s", err) + } +}