Adds test for action post
This commit is contained in:
parent
325aa46cf2
commit
4b4070df98
@ -12,8 +12,8 @@ type Action struct {
|
||||
CompletedChunks int `json:"completed_chunks"`
|
||||
CompletedOn *time.Time `json:"completed_on,omitempty"`
|
||||
PlanID int `json:"plan_id"`
|
||||
CreatedAt *time.Time `json:"created_at"`
|
||||
UpdatedAt *time.Time `json:"updated_at"`
|
||||
CreatedAt *time.Time `json:"created_at,omitempty"`
|
||||
UpdatedAt *time.Time `json:"updated_at,omitempty"`
|
||||
}
|
||||
|
||||
// Actions returns all actions from the model.
|
||||
|
211
routes/post_action_test.go
Normal file
211
routes/post_action_test.go
Normal file
@ -0,0 +1,211 @@
|
||||
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 TestPureJSONPostAction(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("POST", "/actions", 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.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_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 TestExtraFieldActionPostJSON(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("POST", "/actions", 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 TestEmptyBodyActionPost(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("POST", "/actions", 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 TestTwoBodyActionPost(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_date": "2021-01-01T00:00:00Z",
|
||||
"plan_id": 5
|
||||
}, {
|
||||
"plan_date": "2021-01-01T00:00:00Z",
|
||||
"plan_id": 6
|
||||
}`)
|
||||
req, _ := http.NewRequest("POST", "/actions", 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 TestErrorCreateAction(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("POST", "/actions", 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 TestErrorOnRetrieveCreateAction(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("POST", "/actions", 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 TestErrorWriterCreateAction(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("POST", "/actions", 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)
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user