added select plan by id
All checks were successful
gitea-deepak/gogmagog/pipeline/head This commit looks good

This commit is contained in:
Deepak Mallubhotla 2020-12-29 16:26:30 -06:00
parent ba18d011bd
commit 0c6f686ce5
Signed by: deepak
GPG Key ID: 64BF53A3369104E7
5 changed files with 63 additions and 1 deletions

View File

@ -3,8 +3,9 @@ package models
// Store represents the backing store.
type Store interface {
SelectActions() ([]*Action, error)
SelectPlans() ([]*Plan, error)
SelectActionByID(id int) (*Action, error)
SelectPlans() ([]*Plan, error)
SelectPlanByID(id int) (*Plan, error)
}
// Model represents a current model item.

View File

@ -23,6 +23,10 @@ func (ms *multiStore) SelectPlans() ([]*models.Plan, error) {
return ms.plans, nil
}
func (ms *multiStore) SelectPlanByID(id int) (*models.Plan, error) {
return ms.plans[0], nil
}
func TestModelActions(t *testing.T) {
assert := assert.New(t)
a1 := &models.Action{ActionID: 3}
@ -44,4 +48,8 @@ func TestModelActions(t *testing.T) {
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)
}

View File

@ -14,3 +14,8 @@ type Plan struct {
func (m *Model) Plans() ([]*Plan, error) {
return m.SelectPlans()
}
// Plan returns a single plan from the store by plan_id.
func (m *Model) Plan(id int) (*Plan, error) {
return m.SelectPlanByID(id)
}

View File

@ -44,3 +44,12 @@ func (store *postgresStore) SelectPlans() ([]*models.Plan, error) {
}
return plans, nil
}
func (store *postgresStore) SelectPlanByID(id int) (*models.Plan, error) {
plan := models.Plan{}
err := store.db.Get(&plan, store.db.Rebind("SELECT plan_id, plan_date FROM plans WHERE plan_id = ?"), id)
if err != nil {
return nil, err
}
return &plan, nil
}

View File

@ -47,6 +47,45 @@ func TestSelectPlans(t *testing.T) {
}
}
func TestSelectPlanByID(t *testing.T) {
assert := assert.New(t)
currentTime := time.Now()
idToUse := 1
str, mock := getDbMock(t)
rows := sqlmock.NewRows([]string{"plan_id", "plan_date"}).AddRow(idToUse, currentTime)
mock.ExpectQuery("^SELECT plan_id, plan_date FROM plans WHERE plan_id = \\$1$").WithArgs(idToUse).WillReturnRows(rows)
plan, err := str.SelectPlanByID(idToUse)
assert.Nil(err)
assert.EqualValues(idToUse, plan.PlanID)
assert.Equal(currentTime, plan.PlanDate)
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("unfulfilled expectations: %s", err)
}
}
func TestErrPlanByID(t *testing.T) {
assert := assert.New(t)
idToUse := 1
str, mock := getDbMock(t)
mock.ExpectQuery("^SELECT plan_id, plan_date FROM plans WHERE plan_id = \\$1$").WithArgs(idToUse).WillReturnError(fmt.Errorf("example error"))
plan, err := str.SelectPlanByID(idToUse)
assert.NotNil(err)
assert.Nil(plan)
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("unfulfilled expectations: %s", err)
}
}
func TestSelectActions(t *testing.T) {
// set up test
assert := assert.New(t)