diff --git a/models/models.go b/models/models.go index 0bd537a..6669f01 100644 --- a/models/models.go +++ b/models/models.go @@ -7,6 +7,7 @@ type Store interface { SelectPlans() ([]*Plan, error) SelectPlanByID(id int) (*Plan, error) InsertPlan(plan *Plan) (int, error) + SelectActionsByPlanID(plan *Plan) ([]*Action, error) } // Model represents a current model item. diff --git a/models/models_test.go b/models/models_test.go index da9d36e..cfdf76d 100644 --- a/models/models_test.go +++ b/models/models_test.go @@ -31,6 +31,10 @@ func (ms *multiStore) InsertPlan(plan *models.Plan) (int, error) { return int(plan.PlanID), nil } +func (ms *multiStore) SelectActionsByPlanID(plan *models.Plan) ([]*models.Action, error) { + return ms.actions, nil +} + func TestModelActions(t *testing.T) { assert := assert.New(t) a1 := &models.Action{ActionID: 3} @@ -45,30 +49,37 @@ func TestModelActions(t *testing.T) { assert.Nil(err) assert.Equal(2, len(actions)) - 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) - firstPlan, err := m.Plan(6) - assert.Nil(err) - assert.EqualValues(6, firstPlan.PlanID) } -func TestModelInsertPlan(t *testing.T) { +func TestModelPlanMethods(t *testing.T) { assert := assert.New(t) - p := &models.Plan{PlanID: 7} + a1 := &models.Action{ActionID: 3} + a2 := &models.Action{ActionID: 4} + p := &models.Plan{PlanID: 6} ss := &multiStore{ - []*models.Action{}, + []*models.Action{a1, a2}, []*models.Plan{p}, } m := models.New(ss) + plans, err := m.Plans() + assert.Nil(err) + assert.Equal(1, len(plans)) + + firstPlan, err := m.Plan(6) + assert.Nil(err) + assert.EqualValues(6, firstPlan.PlanID) + + actions, err := m.GetActions(firstPlan) + assert.Nil(err) + assert.Equal(2, len(actions)) + planId, err := m.AddPlan(p) assert.Nil(err) - assert.EqualValues(7, planId) + assert.EqualValues(6, planId) } diff --git a/models/plan.go b/models/plan.go index b5efef7..e4eb911 100644 --- a/models/plan.go +++ b/models/plan.go @@ -24,3 +24,8 @@ func (m *Model) Plan(id int) (*Plan, error) { func (m *Model) AddPlan(plan *Plan) (int, error) { return m.InsertPlan(plan) } + +// GetActions returns the actions associated with a particular plan. +func (m *Model) GetActions(plan *Plan) ([]*Action, error) { + return m.SelectActionsByPlanID(plan) +} diff --git a/store/postgres.go b/store/postgres.go index d4c126f..084b9f7 100644 --- a/store/postgres.go +++ b/store/postgres.go @@ -26,6 +26,16 @@ func (store *postgresStore) SelectActions() ([]*models.Action, error) { return actions, nil } +func (store *postgresStore) SelectActionsByPlanID(plan *models.Plan) ([]*models.Action, error) { + queryString := store.db.Rebind("SELECT action_id, action_description, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE plan_id = ?") + actions := make([]*models.Action, 0) + err := store.db.Select(&actions, queryString, plan.PlanID) + if err != nil { + return nil, err + } + return actions, nil +} + func (store *postgresStore) SelectActionByID(id int) (*models.Action, error) { action := models.Action{} err := store.db.Get(&action, store.db.Rebind("SELECT action_id, action_description, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE action_id = ?"), id) diff --git a/store/postgres_test.go b/store/postgres_test.go index 7c1c5d2..161eb1a 100644 --- a/store/postgres_test.go +++ b/store/postgres_test.go @@ -187,6 +187,56 @@ func TestSelectActions(t *testing.T) { } } +func TestSelectActionsByPlanID(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). + AddRow(idToUse+1, 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 plan_id = \\$1$"). + WithArgs(idToUse). + WillReturnRows(rows) + + // function under test + actions, err := str.SelectActionsByPlanID(&models.Plan{PlanID: int64(idToUse)}) + + // test results + assert.Nil(err) + assert.Equal(2, len(actions)) + action := actions[0] + 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 TestSelectActionById(t *testing.T) { // set up test assert := assert.New(t)