Merge pull request 'actions_post' (#1) from actions_post into master
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
Reviewed-on: #1
This commit is contained in:
commit
dfb5ead740
@ -12,8 +12,8 @@ type Action struct {
|
|||||||
CompletedChunks int `json:"completed_chunks"`
|
CompletedChunks int `json:"completed_chunks"`
|
||||||
CompletedOn *time.Time `json:"completed_on,omitempty"`
|
CompletedOn *time.Time `json:"completed_on,omitempty"`
|
||||||
PlanID int `json:"plan_id"`
|
PlanID int `json:"plan_id"`
|
||||||
CreatedAt *time.Time `json:"created_at"`
|
CreatedAt *time.Time `json:"created_at,omitempty"`
|
||||||
UpdatedAt *time.Time `json:"updated_at"`
|
UpdatedAt *time.Time `json:"updated_at,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actions returns all actions from the model.
|
// Actions returns all actions from the model.
|
||||||
@ -26,3 +26,8 @@ func (m *Model) Action(id int) (*Action, error) {
|
|||||||
act, err := m.SelectActionByID(id)
|
act, err := m.SelectActionByID(id)
|
||||||
return act, wrapNotFound(err)
|
return act, wrapNotFound(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddAction inserts a given action into the store, returning the generated ActionID. The provided ActionID is ignored.
|
||||||
|
func (m *Model) AddAction(action *Action) (int, error) {
|
||||||
|
return m.InsertAction(action)
|
||||||
|
}
|
||||||
|
@ -12,6 +12,10 @@ func (e *errorStore) SelectActionByID(id int) (*models.Action, error) {
|
|||||||
return nil, e.error
|
return nil, e.error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *errorStore) InsertAction(action *models.Action) (int, error) {
|
||||||
|
return 0, e.error
|
||||||
|
}
|
||||||
|
|
||||||
func (e *errorStore) SelectPlans() ([]*models.Plan, error) {
|
func (e *errorStore) SelectPlans() ([]*models.Plan, error) {
|
||||||
return nil, e.error
|
return nil, e.error
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ type Store interface {
|
|||||||
ConnectionLive() error
|
ConnectionLive() error
|
||||||
SelectActions() ([]*Action, error)
|
SelectActions() ([]*Action, error)
|
||||||
SelectActionByID(id int) (*Action, error)
|
SelectActionByID(id int) (*Action, error)
|
||||||
|
InsertAction(action *Action) (int, error)
|
||||||
SelectPlans() ([]*Plan, error)
|
SelectPlans() ([]*Plan, error)
|
||||||
SelectPlanByID(id int) (*Plan, error)
|
SelectPlanByID(id int) (*Plan, error)
|
||||||
InsertPlan(plan *Plan) (int, error)
|
InsertPlan(plan *Plan) (int, error)
|
||||||
|
@ -19,6 +19,10 @@ func (ms *multiStore) SelectActionByID(id int) (*models.Action, error) {
|
|||||||
return ms.actions[0], nil
|
return ms.actions[0], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ms *multiStore) InsertAction(action *models.Action) (int, error) {
|
||||||
|
return int(action.ActionID), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (ms *multiStore) SelectPlans() ([]*models.Plan, error) {
|
func (ms *multiStore) SelectPlans() ([]*models.Plan, error) {
|
||||||
return ms.plans, nil
|
return ms.plans, nil
|
||||||
}
|
}
|
||||||
@ -57,6 +61,10 @@ func TestModelActions(t *testing.T) {
|
|||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
assert.EqualValues(3, firstAction.ActionID)
|
assert.EqualValues(3, firstAction.ActionID)
|
||||||
|
|
||||||
|
actionID, err := m.AddAction(a1)
|
||||||
|
assert.Nil(err)
|
||||||
|
assert.EqualValues(3, actionID)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestModelPlanMethods(t *testing.T) {
|
func TestModelPlanMethods(t *testing.T) {
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"gitea.deepak.science/deepak/gogmagog/models"
|
"gitea.deepak.science/deepak/gogmagog/models"
|
||||||
"github.com/go-chi/chi"
|
"github.com/go-chi/chi"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
@ -11,6 +12,7 @@ import (
|
|||||||
func newActionRouter(m *models.Model) http.Handler {
|
func newActionRouter(m *models.Model) http.Handler {
|
||||||
router := chi.NewRouter()
|
router := chi.NewRouter()
|
||||||
router.Get("/", getActionsFunc(m))
|
router.Get("/", getActionsFunc(m))
|
||||||
|
router.Post("/", postActionFunc(m))
|
||||||
router.Get("/{actionid}", getActionByIDFunc(m))
|
router.Get("/{actionid}", getActionByIDFunc(m))
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
@ -68,3 +70,57 @@ func getActionByIDFunc(m *models.Model) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type createActionResponse struct {
|
||||||
|
CreatedAction *models.Action `json:"created_action"`
|
||||||
|
ID int64 `json:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func postActionFunc(m *models.Model) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
id, err := m.AddAction(action)
|
||||||
|
if err != nil {
|
||||||
|
serverError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
action, err = m.Action(id)
|
||||||
|
if err != nil {
|
||||||
|
serverError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
response := &createActionResponse{
|
||||||
|
CreatedAction: action,
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
209
routes/post_action_test.go
Normal file
209
routes/post_action_test.go
Normal file
@ -0,0 +1,209 @@
|
|||||||
|
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_id": 5
|
||||||
|
}, {
|
||||||
|
"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)
|
||||||
|
|
||||||
|
}
|
@ -30,6 +30,10 @@ func (ms *multiStore) SelectActionByID(id int) (*models.Action, error) {
|
|||||||
return ms.actions[0], nil
|
return ms.actions[0], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ms *multiStore) InsertAction(action *models.Action) (int, error) {
|
||||||
|
return int(action.ActionID), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (ms *multiStore) SelectPlans() ([]*models.Plan, error) {
|
func (ms *multiStore) SelectPlans() ([]*models.Plan, error) {
|
||||||
return ms.plans, nil
|
return ms.plans, nil
|
||||||
}
|
}
|
||||||
@ -79,6 +83,10 @@ func (e *errorStore) SelectActionByID(id int) (*models.Action, error) {
|
|||||||
return nil, e.error
|
return nil, e.error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *errorStore) InsertAction(action *models.Action) (int, error) {
|
||||||
|
return 0, e.error
|
||||||
|
}
|
||||||
|
|
||||||
func (e *errorStore) SelectPlans() ([]*models.Plan, error) {
|
func (e *errorStore) SelectPlans() ([]*models.Plan, error) {
|
||||||
return nil, e.error
|
return nil, e.error
|
||||||
}
|
}
|
||||||
@ -115,6 +123,9 @@ func (e *onlyCreateStore) SelectActions() ([]*models.Action, error) {
|
|||||||
func (e *onlyCreateStore) SelectActionByID(id int) (*models.Action, error) {
|
func (e *onlyCreateStore) SelectActionByID(id int) (*models.Action, error) {
|
||||||
return nil, e.error
|
return nil, e.error
|
||||||
}
|
}
|
||||||
|
func (e *onlyCreateStore) InsertAction(action *models.Action) (int, error) {
|
||||||
|
return int(action.ActionID), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (e *onlyCreateStore) SelectPlans() ([]*models.Plan, error) {
|
func (e *onlyCreateStore) SelectPlans() ([]*models.Plan, error) {
|
||||||
return nil, e.error
|
return nil, e.error
|
||||||
|
@ -45,6 +45,36 @@ func (store *postgresStore) SelectActionByID(id int) (*models.Action, error) {
|
|||||||
return &action, nil
|
return &action, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (store *postgresStore) InsertAction(action *models.Action) (int, error) {
|
||||||
|
queryString := store.db.Rebind(
|
||||||
|
`INSERT INTO actions (action_description,
|
||||||
|
estimated_chunks,
|
||||||
|
completed_chunks,
|
||||||
|
completed_on,
|
||||||
|
plan_id) VALUES (?, ?, ?, ?, ?) RETURNING action_id`,
|
||||||
|
)
|
||||||
|
tx := store.db.MustBegin()
|
||||||
|
var id int
|
||||||
|
err := tx.Get(
|
||||||
|
&id,
|
||||||
|
queryString,
|
||||||
|
action.ActionDescription,
|
||||||
|
action.EstimatedChunks,
|
||||||
|
action.CompletedChunks,
|
||||||
|
action.CompletedOn,
|
||||||
|
action.PlanID,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return -1, err
|
||||||
|
}
|
||||||
|
err = tx.Commit()
|
||||||
|
if err != nil {
|
||||||
|
return -1, err
|
||||||
|
}
|
||||||
|
return id, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (store *postgresStore) SelectPlans() ([]*models.Plan, error) {
|
func (store *postgresStore) SelectPlans() ([]*models.Plan, error) {
|
||||||
plans := make([]*models.Plan, 0)
|
plans := make([]*models.Plan, 0)
|
||||||
err := store.db.Select(&plans, "SELECT plan_id, plan_date FROM plans")
|
err := store.db.Select(&plans, "SELECT plan_id, plan_date FROM plans")
|
||||||
|
@ -398,3 +398,101 @@ func TestConnectionLive(t *testing.T) {
|
|||||||
// results
|
// results
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInsertAction(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",
|
||||||
|
}
|
||||||
|
|
||||||
|
idToUse := 8
|
||||||
|
|
||||||
|
rows := sqlmock.NewRows([]string{"action_id"}).AddRow(8)
|
||||||
|
|
||||||
|
mock.ExpectBegin()
|
||||||
|
mock.ExpectQuery("^INSERT INTO actions \\(action_description, estimated_chunks, completed_chunks, completed_on, plan_id\\) VALUES \\(\\$1, \\$2, \\$3, \\$4, \\$5\\) RETURNING action_id$").
|
||||||
|
WithArgs("testing", 3, 6, completedOn, 5).
|
||||||
|
WillReturnRows(rows)
|
||||||
|
mock.ExpectCommit()
|
||||||
|
|
||||||
|
// function under test
|
||||||
|
insertedId, err := str.InsertAction(action)
|
||||||
|
// check results
|
||||||
|
assert.Nil(err)
|
||||||
|
assert.EqualValues(idToUse, insertedId)
|
||||||
|
if err := mock.ExpectationsWereMet(); err != nil {
|
||||||
|
t.Errorf("unfulfilled expectations: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInsertActionErr(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",
|
||||||
|
}
|
||||||
|
|
||||||
|
mock.ExpectBegin()
|
||||||
|
mock.ExpectQuery("^INSERT INTO actions \\(action_description, estimated_chunks, completed_chunks, completed_on, plan_id\\) VALUES \\(\\$1, \\$2, \\$3, \\$4, \\$5\\) RETURNING action_id$").
|
||||||
|
WithArgs("testing", 3, 6, completedOn, 5).
|
||||||
|
WillReturnError(fmt.Errorf("example error"))
|
||||||
|
mock.ExpectRollback()
|
||||||
|
|
||||||
|
// function under test
|
||||||
|
_, err := str.InsertAction(action)
|
||||||
|
// check results
|
||||||
|
assert.NotNil(err)
|
||||||
|
if err := mock.ExpectationsWereMet(); err != nil {
|
||||||
|
t.Errorf("unfulfilled expectations: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInsertActionCommitErr(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",
|
||||||
|
}
|
||||||
|
idToUse := 8
|
||||||
|
|
||||||
|
rows := sqlmock.NewRows([]string{"plan_id"}).AddRow(idToUse)
|
||||||
|
|
||||||
|
mock.ExpectBegin()
|
||||||
|
mock.ExpectQuery("^INSERT INTO actions \\(action_description, estimated_chunks, completed_chunks, completed_on, plan_id\\) VALUES \\(\\$1, \\$2, \\$3, \\$4, \\$5\\) RETURNING action_id$").
|
||||||
|
WithArgs("testing", 3, 6, completedOn, 5).
|
||||||
|
WillReturnRows(rows)
|
||||||
|
mock.ExpectCommit().WillReturnError(fmt.Errorf("another error example"))
|
||||||
|
|
||||||
|
// function under test
|
||||||
|
_, err := str.InsertAction(action)
|
||||||
|
// check results
|
||||||
|
assert.NotNil(err)
|
||||||
|
if err := mock.ExpectationsWereMet(); err != nil {
|
||||||
|
t.Errorf("unfulfilled expectations: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user