Compare commits
2 Commits
9b276bd644
...
27cdde132d
| Author | SHA1 | Date | |
|---|---|---|---|
|
27cdde132d
|
|||
|
f8b1949c28
|
@@ -6,11 +6,13 @@ import (
|
||||
"github.com/go-chi/chi"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func newPlanRouter(m *models.Model) http.Handler {
|
||||
router := chi.NewRouter()
|
||||
router.Get("/", getAllPlansFunc(m))
|
||||
router.Post("/", postPlanFunc(m))
|
||||
router.Get("/{planid}", getPlanByIDFunc(m))
|
||||
return router
|
||||
}
|
||||
@@ -52,3 +54,38 @@ func getPlanByIDFunc(m *models.Model) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type createPlanResponse struct {
|
||||
CreatedPlan *models.Plan `json:"created_plan"`
|
||||
ID int64 `json:"id"`
|
||||
}
|
||||
|
||||
func postPlanFunc(m *models.Model) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
time := time.Now()
|
||||
plan := &models.Plan{PlanDate: &time}
|
||||
|
||||
id, err := m.AddPlan(plan)
|
||||
if err != nil {
|
||||
serverError(w, err)
|
||||
return
|
||||
}
|
||||
plan, err = m.Plan(id)
|
||||
if err != nil {
|
||||
serverError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
response := &createPlanResponse{
|
||||
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
108
routes/post_plan_test.go
Normal 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)
|
||||
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user