Add select by action id
This commit is contained in:
parent
50bbcdc71d
commit
ad47895597
@ -20,3 +20,8 @@ type Action struct {
|
|||||||
func (m *Model) Actions() ([]*Action, error) {
|
func (m *Model) Actions() ([]*Action, error) {
|
||||||
return m.SelectActions()
|
return m.SelectActions()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Action returns a single action from its ID
|
||||||
|
func (m *Model) Action(id int) (*Action, error) {
|
||||||
|
return m.SelectActionByID(id)
|
||||||
|
}
|
||||||
|
@ -4,6 +4,7 @@ package models
|
|||||||
type Store interface {
|
type Store interface {
|
||||||
SelectActions() ([]*Action, error)
|
SelectActions() ([]*Action, error)
|
||||||
SelectPlans() ([]*Plan, error)
|
SelectPlans() ([]*Plan, error)
|
||||||
|
SelectActionByID(id int) (*Action, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Model represents a current model item.
|
// Model represents a current model item.
|
||||||
|
@ -6,10 +6,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
type store interface {
|
|
||||||
SelectActions() ([]*models.Action, error)
|
|
||||||
SelectPlans() ([]*models.Plan, error)
|
|
||||||
}
|
|
||||||
type multiStore struct {
|
type multiStore struct {
|
||||||
actions []*models.Action
|
actions []*models.Action
|
||||||
plans []*models.Plan
|
plans []*models.Plan
|
||||||
@ -19,6 +16,10 @@ func (ms *multiStore) SelectActions() ([]*models.Action, error) {
|
|||||||
return ms.actions, nil
|
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) {
|
func (ms *multiStore) SelectPlans() ([]*models.Plan, error) {
|
||||||
return ms.plans, nil
|
return ms.plans, nil
|
||||||
}
|
}
|
||||||
@ -40,4 +41,8 @@ func TestModelActions(t *testing.T) {
|
|||||||
plans, err := m.Plans()
|
plans, err := m.Plans()
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
assert.Equal(1, len(plans))
|
assert.Equal(1, len(plans))
|
||||||
|
|
||||||
|
firstAction, err := m.Action(3)
|
||||||
|
assert.Nil(err)
|
||||||
|
assert.EqualValues(3, firstAction.ActionID)
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,15 @@ func (store *postgresStore) SelectActions() ([]*models.Action, error) {
|
|||||||
return actions, nil
|
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) {
|
func (store *postgresStore) SelectPlans() ([]*models.Plan, error) {
|
||||||
plans := make([]*models.Plan, 0)
|
plans := make([]*models.Plan, 0)
|
||||||
err := store.db.Select(&plans, "SELECT plan_id, plan_date FROM plans")
|
err := store.db.Select(&plans, "SELECT plan_id, plan_date FROM plans")
|
||||||
|
@ -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) {
|
func TestErrPlans(t *testing.T) {
|
||||||
// set up tests
|
// set up tests
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
@ -126,3 +175,19 @@ func TestErrActions(t *testing.T) {
|
|||||||
t.Errorf("unfulfilled expectations: %s", err)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user