Merge pull request 'put_actions' (#2) from put_actions into master
All checks were successful
gitea-deepak/gogmagog/pipeline/head This commit looks good

Reviewed-on: #2
This commit is contained in:
Deepak Mallubhotla 2021-01-10 17:35:20 +00:00
commit 7e7558dd5e
9 changed files with 447 additions and 0 deletions

View File

@ -31,3 +31,8 @@ func (m *Model) Action(id int) (*Action, error) {
func (m *Model) AddAction(action *Action) (int, error) {
return m.InsertAction(action)
}
// SaveAction saves and updates an action.
func (m *Model) SaveAction(action *Action) error {
return m.UpdateAction(action)
}

View File

@ -16,6 +16,10 @@ func (e *errorStore) InsertAction(action *models.Action) (int, error) {
return 0, e.error
}
func (e *errorStore) UpdateAction(action *models.Action) error {
return e.error
}
func (e *errorStore) SelectPlans() ([]*models.Plan, error) {
return nil, e.error
}

View File

@ -10,6 +10,7 @@ type Store interface {
SelectActions() ([]*Action, error)
SelectActionByID(id int) (*Action, error)
InsertAction(action *Action) (int, error)
UpdateAction(action *Action) error
SelectPlans() ([]*Plan, error)
SelectPlanByID(id int) (*Plan, error)
InsertPlan(plan *Plan) (int, error)

View File

@ -23,6 +23,10 @@ func (ms *multiStore) InsertAction(action *models.Action) (int, error) {
return int(action.ActionID), nil
}
func (ms *multiStore) UpdateAction(action *models.Action) error {
return nil
}
func (ms *multiStore) SelectPlans() ([]*models.Plan, error) {
return ms.plans, nil
}
@ -65,6 +69,8 @@ func TestModelActions(t *testing.T) {
assert.Nil(err)
assert.EqualValues(3, actionID)
err = m.SaveAction(a1)
assert.Nil(err)
}
func TestModelPlanMethods(t *testing.T) {

View File

@ -14,6 +14,7 @@ func newActionRouter(m *models.Model) http.Handler {
router.Get("/", getActionsFunc(m))
router.Post("/", postActionFunc(m))
router.Get("/{actionid}", getActionByIDFunc(m))
router.Put("/{actionid}", putActionFunc(m))
return router
}
@ -124,3 +125,62 @@ func postActionFunc(m *models.Model) http.HandlerFunc {
}
}
type updateActionResponse struct {
UpdatedAction *models.Action `json:"updated_action"`
ID int64 `json:"id"`
}
func putActionFunc(m *models.Model) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(chi.URLParam(r, "actionid"))
if err != nil {
notFoundHandler(w, r)
return
}
r.Body = http.MaxBytesReader(w, r.Body, 1024)
dec := json.NewDecoder(r.Body)
dec.DisallowUnknownFields()
var a models.Action
err = dec.Decode(&a)
if err != nil {
badRequestError(w, err)
return
}
err = dec.Decode(&struct{}{})
if err != io.EOF {
badRequestError(w, err)
return
}
action := &models.Action{
ActionDescription: a.ActionDescription,
EstimatedChunks: a.EstimatedChunks,
CompletedChunks: a.CompletedChunks,
CompletedOn: a.CompletedOn,
PlanID: a.PlanID,
ActionID: int64(id),
}
err = m.SaveAction(action)
if err != nil {
serverError(w, err)
return
}
action, err = m.Action(id)
if err != nil {
serverError(w, err)
return
}
response := &updateActionResponse{
UpdatedAction: action,
ID: int64(id),
}
w.WriteHeader(http.StatusOK)
w.Header().Add("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(response); err != nil {
serverError(w, err)
}
}
}

236
routes/put_action_test.go Normal file
View File

@ -0,0 +1,236 @@
package routes_test
import (
"bytes"
"encoding/json"
"gitea.deepak.science/deepak/gogmagog/models"
"gitea.deepak.science/deepak/gogmagog/routes"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestPureJSONPutAction(t *testing.T) {
// set up
assert := assert.New(t)
compOn, _ := time.Parse("2006-01-02", "2021-01-01")
a := &models.Action{
PlanID: 5,
CompletedOn: &compOn,
EstimatedChunks: 3,
CompletedChunks: 2,
ActionDescription: "here's an action",
}
m := getModel([]*models.Plan{}, []*models.Action{a})
router := routes.NewRouter(m)
data := []byte(`{
"action_description": "here's an action",
"estimated_chunks": 3,
"completed_chunks": 2,
"completed_on": "2021-01-01T00:00:00Z",
"plan_id": 5
}`)
req, _ := http.NewRequest("PUT", "/actions/0", bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
// function under test
router.ServeHTTP(rr, req)
// check results
status := rr.Code
assert.Equal(http.StatusOK, status)
// We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp.
expected := `{
"updated_action": {
"action_description": "here's an action",
"estimated_chunks": 3,
"completed_chunks": 2,
"completed_on": "2021-01-01T00:00:00Z",
"plan_id": 5,
"action_id": 0
},
"id": 0
}`
assert.JSONEq(expected, rr.Body.String())
contentType := rr.Header().Get("Content-Type")
assert.Equal("application/json", contentType)
}
func TestExtraFieldActionPutJSON(t *testing.T) {
// set up
assert := assert.New(t)
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
m := getModel([]*models.Plan{p}, []*models.Action{})
router := routes.NewRouter(m)
data := []byte(`{
"completed_on": "2021-01-01T00:00:00Z",
"plan_id": 5,
"sabotage": "omg"
}`)
req, _ := http.NewRequest("PUT", "/actions/1", bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
// function under test
router.ServeHTTP(rr, req)
// check results
status := rr.Code
assert.Equal(http.StatusBadRequest, status)
// We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp.
expected := `Bad Request`
assert.Equal(expected, strings.TrimSpace(rr.Body.String()))
}
func TestEmptyBodyActionPut(t *testing.T) {
// set up
assert := assert.New(t)
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
m := getModel([]*models.Plan{p}, []*models.Action{})
router := routes.NewRouter(m)
data := []byte(``)
req, _ := http.NewRequest("PUT", "/actions/1", bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
// function under test
router.ServeHTTP(rr, req)
// check results
status := rr.Code
assert.Equal(http.StatusBadRequest, status)
// We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp.
expected := `Bad Request`
assert.Equal(expected, strings.TrimSpace(rr.Body.String()))
}
func TestTwoBodyActionPut(t *testing.T) {
// set up
assert := assert.New(t)
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
m := getModel([]*models.Plan{p}, []*models.Action{})
router := routes.NewRouter(m)
data := []byte(`{
"plan_id": 5
}, {
"plan_id": 6
}`)
req, _ := http.NewRequest("PUT", "/actions/1", bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
// function under test
router.ServeHTTP(rr, req)
// check results
status := rr.Code
assert.Equal(http.StatusBadRequest, status)
// We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp.
expected := `Bad Request`
assert.Equal(expected, strings.TrimSpace(rr.Body.String()))
}
func TestBadActionIDPut(t *testing.T) {
// set up
assert := assert.New(t)
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
m := getModel([]*models.Plan{p}, []*models.Action{})
router := routes.NewRouter(m)
data := []byte(`{
"plan_id": 5
}, {
"plan_id": 6
}`)
req, _ := http.NewRequest("PUT", "/actions/text", bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
// function under test
router.ServeHTTP(rr, req)
// check results
status := rr.Code
assert.Equal(http.StatusNotFound, status)
// We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp.
expected := `Not Found`
assert.Equal(expected, strings.TrimSpace(rr.Body.String()))
}
func TestErrorUpdateAction(t *testing.T) {
// set up
assert := assert.New(t)
m := getErrorModel("Model always errors")
router := routes.NewRouter(m)
a := &models.Action{PlanID: 6}
data, _ := json.Marshal(a)
req, _ := http.NewRequest("PUT", "/actions/1", bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
// function under test
router.ServeHTTP(rr, req)
// check results
status := rr.Code
assert.Equal(http.StatusInternalServerError, status)
// We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp.
expected := `Internal Server Error`
assert.Equal(expected, strings.TrimSpace(rr.Body.String()))
}
func TestErrorOnRetrieveUpdateAction(t *testing.T) {
// set up
assert := assert.New(t)
m := getErrorOnGetModel("Model always errors")
router := routes.NewRouter(m)
a := &models.Action{PlanID: 6}
data, _ := json.Marshal(a)
req, _ := http.NewRequest("PUT", "/actions/1", bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
// function under test
router.ServeHTTP(rr, req)
// check results
status := rr.Code
assert.Equal(http.StatusInternalServerError, status)
// We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp.
expected := `Internal Server Error`
assert.Equal(expected, strings.TrimSpace(rr.Body.String()))
}
func TestErrorWriterUpdateAction(t *testing.T) {
// set up
assert := assert.New(t)
a := &models.Action{PlanID: 6}
m := getModel([]*models.Plan{}, []*models.Action{a})
router := routes.NewRouter(m)
data, _ := json.Marshal(a)
req, _ := http.NewRequest("PUT", "/actions/1", bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")
rr := NewBadWriter()
// function under test
router.ServeHTTP(rr, req)
// check results
status := rr.Code
assert.Equal(http.StatusInternalServerError, status)
}

View File

@ -34,6 +34,10 @@ func (ms *multiStore) InsertAction(action *models.Action) (int, error) {
return int(action.ActionID), nil
}
func (ms *multiStore) UpdateAction(action *models.Action) error {
return nil
}
func (ms *multiStore) SelectPlans() ([]*models.Plan, error) {
return ms.plans, nil
}
@ -87,6 +91,10 @@ func (e *errorStore) InsertAction(action *models.Action) (int, error) {
return 0, e.error
}
func (e *errorStore) UpdateAction(action *models.Action) error {
return e.error
}
func (e *errorStore) SelectPlans() ([]*models.Plan, error) {
return nil, e.error
}
@ -123,10 +131,15 @@ func (e *onlyCreateStore) SelectActions() ([]*models.Action, error) {
func (e *onlyCreateStore) SelectActionByID(id int) (*models.Action, error) {
return nil, e.error
}
func (e *onlyCreateStore) InsertAction(action *models.Action) (int, error) {
return int(action.ActionID), nil
}
func (e *onlyCreateStore) UpdateAction(action *models.Action) error {
return nil
}
func (e *onlyCreateStore) SelectPlans() ([]*models.Plan, error) {
return nil, e.error
}

View File

@ -75,6 +75,28 @@ func (store *postgresStore) InsertAction(action *models.Action) (int, error) {
return id, nil
}
func (store *postgresStore) UpdateAction(action *models.Action) error {
query := `UPDATE actions SET
action_description = :action_description,
estimated_chunks = :estimated_chunks,
completed_chunks = :completed_chunks,
completed_on = :completed_on,
plan_id = :plan_id
WHERE action_id = :action_id`
tx := store.db.MustBegin()
_, err := store.db.NamedExec(query, action)
if err != nil {
tx.Rollback()
return err
}
err = tx.Commit()
if err != nil {
return err
}
return 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")

View File

@ -496,3 +496,103 @@ func TestInsertActionCommitErr(t *testing.T) {
}
}
func TestUpdateAction(t *testing.T) {
// setup
assert := assert.New(t)
str, mock := getDbMock(t)
completedOn, _ := time.Parse("2006-01-02", "2021-01-01")
action := &models.Action{
CompletedOn: &completedOn,
EstimatedChunks: 3,
CompletedChunks: 6,
PlanID: 5,
ActionDescription: "testing",
ActionID: 2,
}
mock.ExpectBegin()
mock.ExpectExec(`
UPDATE actions SET
action_description = \$1,
estimated_chunks = \$2,
completed_chunks = \$3,
completed_on = \$4,
plan_id = \$5
WHERE action_id = \$6`).
WithArgs("testing", 3, 6, completedOn, 5, 2).
WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
// function under test
err := str.UpdateAction(action)
// check results
assert.Nil(err)
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("unfulfilled expectations: %s", err)
}
}
func TestUpdateActionErr(t *testing.T) {
// setup
assert := assert.New(t)
str, mock := getDbMock(t)
completedOn, _ := time.Parse("2006-01-02", "2021-01-01")
action := &models.Action{
CompletedOn: &completedOn,
EstimatedChunks: 3,
CompletedChunks: 6,
PlanID: 5,
ActionDescription: "testing",
ActionID: 2,
}
mock.ExpectBegin()
mock.ExpectExec("UPDATE actions").
WithArgs("testing", 3, 6, completedOn, 5, 2).
WillReturnError(fmt.Errorf("example error"))
mock.ExpectRollback()
// function under test
err := str.UpdateAction(action)
// check results
assert.NotNil(err)
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("unfulfilled expectations: %s", err)
}
}
func TestUpdateActionCommitErr(t *testing.T) {
// setup
assert := assert.New(t)
str, mock := getDbMock(t)
completedOn, _ := time.Parse("2006-01-02", "2021-01-01")
action := &models.Action{
CompletedOn: &completedOn,
EstimatedChunks: 3,
CompletedChunks: 6,
PlanID: 5,
ActionDescription: "testing",
ActionID: 2,
}
mock.ExpectBegin()
mock.ExpectExec("UPDATE actions").
WithArgs("testing", 3, 6, completedOn, 5, 2).
WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit().WillReturnError(fmt.Errorf("another error example"))
// function under test
err := str.UpdateAction(action)
// check results
assert.NotNil(err)
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("unfulfilled expectations: %s", err)
}
}