Adds select actions by plan function

This commit is contained in:
Deepak Mallubhotla 2020-12-29 19:10:54 -06:00
parent e6158e680f
commit acf793ae41
Signed by: deepak
GPG Key ID: 64BF53A3369104E7
5 changed files with 88 additions and 11 deletions

View File

@ -7,6 +7,7 @@ type Store interface {
SelectPlans() ([]*Plan, error) SelectPlans() ([]*Plan, error)
SelectPlanByID(id int) (*Plan, error) SelectPlanByID(id int) (*Plan, error)
InsertPlan(plan *Plan) (int, error) InsertPlan(plan *Plan) (int, error)
SelectActionsByPlanID(plan *Plan) ([]*Action, error)
} }
// Model represents a current model item. // Model represents a current model item.

View File

@ -31,6 +31,10 @@ func (ms *multiStore) InsertPlan(plan *models.Plan) (int, error) {
return int(plan.PlanID), nil return int(plan.PlanID), nil
} }
func (ms *multiStore) SelectActionsByPlanID(plan *models.Plan) ([]*models.Action, error) {
return ms.actions, 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}
@ -45,30 +49,37 @@ func TestModelActions(t *testing.T) {
assert.Nil(err) assert.Nil(err)
assert.Equal(2, len(actions)) assert.Equal(2, len(actions))
plans, err := m.Plans()
assert.Nil(err)
assert.Equal(1, len(plans))
firstAction, err := m.Action(3) firstAction, err := m.Action(3)
assert.Nil(err) assert.Nil(err)
assert.EqualValues(3, firstAction.ActionID) 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) 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{ ss := &multiStore{
[]*models.Action{}, []*models.Action{a1, a2},
[]*models.Plan{p}, []*models.Plan{p},
} }
m := models.New(ss) 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) planId, err := m.AddPlan(p)
assert.Nil(err) assert.Nil(err)
assert.EqualValues(7, planId) assert.EqualValues(6, planId)
} }

View File

@ -24,3 +24,8 @@ func (m *Model) Plan(id int) (*Plan, error) {
func (m *Model) AddPlan(plan *Plan) (int, error) { func (m *Model) AddPlan(plan *Plan) (int, error) {
return m.InsertPlan(plan) 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)
}

View File

@ -26,6 +26,16 @@ func (store *postgresStore) SelectActions() ([]*models.Action, error) {
return actions, nil 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) { func (store *postgresStore) SelectActionByID(id int) (*models.Action, error) {
action := models.Action{} 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) 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)

View File

@ -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) { func TestSelectActionById(t *testing.T) {
// set up test // set up test
assert := assert.New(t) assert := assert.New(t)