Adds post endpoint for creating plans
All checks were successful
gitea-deepak/gogmagog/pipeline/head This commit looks good

This commit is contained in:
Deepak Mallubhotla 2021-01-03 20:28:46 -06:00
parent f8b1949c28
commit 27cdde132d
Signed by: deepak
GPG Key ID: 64BF53A3369104E7
3 changed files with 146 additions and 0 deletions

View File

@ -81,6 +81,7 @@ func postPlanFunc(m *models.Model) http.HandlerFunc {
CreatedPlan: plan,
ID: int64(id),
}
w.WriteHeader(http.StatusCreated)
w.Header().Add("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(response); err != nil {
serverError(w, err)

108
routes/post_plan_test.go Normal file
View File

@ -0,0 +1,108 @@
package routes_test
import (
"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 TestCreatePlanRoute(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)
req, _ := http.NewRequest("POST", "/plans", nil)
rr := httptest.NewRecorder()
// function under test
router.ServeHTTP(rr, req)
// check results
status := rr.Code
assert.Equal(http.StatusCreated, status)
// We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp.
expected := `{
"created_plan": {
"plan_id": 6,
"plan_date": "2021-01-01T00:00:00Z"
},
"id": 0
}`
assert.JSONEq(expected, rr.Body.String())
contentType := rr.Header().Get("Content-Type")
assert.Equal("application/json", contentType)
}
func TestErrorCreatePlan(t *testing.T) {
// set up
assert := assert.New(t)
m := getErrorModel("Model always errors")
router := routes.NewRouter(m)
req, _ := http.NewRequest("POST", "/plans", nil)
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 TestErrorOnRetrieveCreatePlan(t *testing.T) {
// set up
assert := assert.New(t)
m := getErrorOnGetModel("Model always errors")
router := routes.NewRouter(m)
req, _ := http.NewRequest("POST", "/plans", nil)
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 TestErrorWriterCreatePlan(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)
req, _ := http.NewRequest("POST", "/plans", nil)
rr := NewBadWriter()
// function under test
router.ServeHTTP(rr, req)
// check results
status := rr.Code
assert.Equal(http.StatusInternalServerError, status)
}

View File

@ -107,3 +107,40 @@ func getErrorModel(errorMsg string) *models.Model {
e := &errorStore{error: fmt.Errorf(errorMsg)}
return models.New(e)
}
func (e *onlyCreateStore) SelectActions() ([]*models.Action, error) {
return nil, e.error
}
func (e *onlyCreateStore) SelectActionByID(id int) (*models.Action, error) {
return nil, e.error
}
func (e *onlyCreateStore) SelectPlans() ([]*models.Plan, error) {
return nil, e.error
}
func (e *onlyCreateStore) SelectPlanByID(id int) (*models.Plan, error) {
return nil, e.error
}
func (e *onlyCreateStore) InsertPlan(plan *models.Plan) (int, error) {
return int(plan.PlanID), nil
}
func (e *onlyCreateStore) SelectActionsByPlanID(plan *models.Plan) ([]*models.Action, error) {
return nil, e.error
}
func (e *onlyCreateStore) ConnectionLive() error {
return e.error
}
type onlyCreateStore struct {
error error
}
func getErrorOnGetModel(errorMsg string) *models.Model {
e := &onlyCreateStore{error: fmt.Errorf(errorMsg)}
return models.New(e)
}