Adds route for filtering actions by planID
All checks were successful
gitea-deepak/gogmagog/pipeline/head This commit looks good
All checks were successful
gitea-deepak/gogmagog/pipeline/head This commit looks good
This commit is contained in:
parent
70ddd91d6b
commit
9b276bd644
@ -10,14 +10,30 @@ import (
|
||||
|
||||
func newActionRouter(m *models.Model) http.Handler {
|
||||
router := chi.NewRouter()
|
||||
router.Get("/", getAllActionsFunc(m))
|
||||
router.Get("/", getActionsFunc(m))
|
||||
router.Get("/{actionid}", getActionByIDFunc(m))
|
||||
return router
|
||||
}
|
||||
|
||||
func getAllActionsFunc(m *models.Model) http.HandlerFunc {
|
||||
func getActionsFunc(m *models.Model) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
actions, err := m.Actions()
|
||||
var (
|
||||
actions []*models.Action
|
||||
err error
|
||||
)
|
||||
planIDString := r.URL.Query().Get("plan_id")
|
||||
if planIDString == "" {
|
||||
actions, err = m.Actions()
|
||||
} else {
|
||||
planID, convErr := strconv.ParseInt(planIDString, 10, 64)
|
||||
if convErr != nil {
|
||||
actions = []*models.Action{}
|
||||
err = nil
|
||||
} else {
|
||||
plan := &models.Plan{PlanID: planID}
|
||||
actions, err = m.GetActions(plan)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
serverError(w, err)
|
||||
return
|
||||
|
@ -226,3 +226,63 @@ func TestNotFoundActionByIDEmpty(t *testing.T) {
|
||||
assert.Equal(http.StatusNotFound, status)
|
||||
|
||||
}
|
||||
|
||||
func TestActionsByPlanID(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
createdDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
updatedDate, _ := time.Parse("2006-01-02", "2021-01-02")
|
||||
a := &models.Action{ActionID: 6, ActionDescription: "howdy", CompletedOn: nil, CreatedAt: &createdDate, UpdatedAt: &updatedDate, CompletedChunks: 0, EstimatedChunks: 54, PlanID: 3}
|
||||
m := getModel([]*models.Plan{}, []*models.Action{a})
|
||||
router := routes.NewRouter(m)
|
||||
req, _ := http.NewRequest("GET", "/actions?plan_id=6", nil)
|
||||
|
||||
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 := `[
|
||||
{
|
||||
"action_id": 6,
|
||||
"action_description": "howdy",
|
||||
"estimated_chunks": 54,
|
||||
"completed_chunks": 0,
|
||||
"updated_at": "2021-01-02T00:00:00Z",
|
||||
"created_at": "2021-01-01T00:00:00Z",
|
||||
"plan_id": 3
|
||||
}
|
||||
]`
|
||||
assert.JSONEq(expected, rr.Body.String())
|
||||
contentType := rr.Header().Get("Content-Type")
|
||||
assert.Equal("application/json", contentType)
|
||||
}
|
||||
|
||||
func TestActionsByPlanIDInvalidID(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
createdDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
updatedDate, _ := time.Parse("2006-01-02", "2021-01-02")
|
||||
a := &models.Action{ActionID: 6, ActionDescription: "howdy", CompletedOn: nil, CreatedAt: &createdDate, UpdatedAt: &updatedDate, CompletedChunks: 0, EstimatedChunks: 54, PlanID: 3}
|
||||
m := getModel([]*models.Plan{}, []*models.Action{a})
|
||||
router := routes.NewRouter(m)
|
||||
req, _ := http.NewRequest("GET", "/actions?plan_id=aoeu", nil)
|
||||
|
||||
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 := `[]`
|
||||
assert.JSONEq(expected, rr.Body.String())
|
||||
contentType := rr.Header().Get("Content-Type")
|
||||
assert.Equal("application/json", contentType)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user