Compare commits
3 Commits
b47abbfc0b
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
c3be0b8e54
|
|||
|
8ca7858069
|
|||
|
5e030a5bc3
|
@@ -14,6 +14,7 @@ type Store interface {
|
||||
SelectPlans(userID int) ([]*Plan, error)
|
||||
SelectPlanByID(id int, userID int) (*Plan, error)
|
||||
InsertPlan(plan *Plan, userID int) (int, error)
|
||||
UpdatePlan(plan *Plan, userID int) error
|
||||
SelectActionsByPlanID(plan *Plan, userID int) ([]*Action, error)
|
||||
SelectUserByUsername(username string) (*User, error)
|
||||
InsertUser(user *User) (int, error)
|
||||
|
||||
@@ -56,6 +56,12 @@ func TestModelPlanMethods(t *testing.T) {
|
||||
assert.Nil(err)
|
||||
assert.EqualValues(1, firstPlan.PlanID)
|
||||
|
||||
p2 := &models.Plan{PlanDescription: "testing", PlanID: 1}
|
||||
m.SavePlan(p2, userID)
|
||||
p2, err = m.Plan(1, userID)
|
||||
assert.Nil(err)
|
||||
assert.Equal("testing", p2.PlanDescription)
|
||||
|
||||
actions, err := m.GetActions(firstPlan, userID)
|
||||
assert.Nil(err)
|
||||
assert.Equal(1, len(actions))
|
||||
|
||||
@@ -1,14 +1,10 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Plan represents a single day's agenda of actions.
|
||||
type Plan struct {
|
||||
PlanID int64 `json:"plan_id"`
|
||||
PlanDate *time.Time `json:"plan_date"`
|
||||
UserID int64 `json:"user_id"`
|
||||
PlanID int64 `json:"plan_id"`
|
||||
PlanDescription string `json:"plan_description"`
|
||||
UserID int64 `json:"user_id"`
|
||||
}
|
||||
|
||||
// Plans returns all plans in the model.
|
||||
@@ -27,6 +23,11 @@ func (m *Model) AddPlan(plan *Plan, userID int) (int, error) {
|
||||
return m.InsertPlan(plan, userID)
|
||||
}
|
||||
|
||||
// SavePlan saves and updates a plan.
|
||||
func (m *Model) SavePlan(plan *Plan, userID int) error {
|
||||
return m.UpdatePlan(plan, userID)
|
||||
}
|
||||
|
||||
// GetActions returns the actions associated with a particular plan.
|
||||
func (m *Model) GetActions(plan *Plan, userID int) ([]*Action, error) {
|
||||
return m.SelectActionsByPlanID(plan, userID)
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestEmptyActionEmptyContext(t *testing.T) {
|
||||
@@ -34,8 +33,8 @@ func TestEmptyActionEmptyContext(t *testing.T) {
|
||||
func TestOneActionEmptyContext(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, UserID: 3}
|
||||
planDescription := "2006-01-02"
|
||||
p := &models.Plan{PlanID: 6, PlanDescription: planDescription, UserID: 3}
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p, 3)
|
||||
router := routes.NewActionRouter(m)
|
||||
@@ -55,8 +54,8 @@ func TestOneActionEmptyContext(t *testing.T) {
|
||||
func TestOneActionByIDEmptyContext(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, UserID: 3}
|
||||
planDescription := "2021-01-01"
|
||||
p := &models.Plan{PlanID: 6, PlanDescription: planDescription, UserID: 3}
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p, 3)
|
||||
router := routes.NewActionRouter(m)
|
||||
@@ -79,7 +78,7 @@ func TestPureJSONActionEmptyContext(t *testing.T) {
|
||||
m := getEmptyModel()
|
||||
router := routes.NewActionRouter(m)
|
||||
data := []byte(`{
|
||||
"plan_date": "2021-01-01T00:00:00Z",
|
||||
"plan_description": "2021-01-01T00:00:00Z",
|
||||
"plan_id": 1,
|
||||
"user_id": 3
|
||||
}`)
|
||||
|
||||
218
routes/plan_put_test.go
Normal file
218
routes/plan_put_test.go
Normal file
@@ -0,0 +1,218 @@
|
||||
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"
|
||||
)
|
||||
|
||||
func TestPureJSONPutPlan(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
p := &models.Plan{
|
||||
PlanID: 1,
|
||||
PlanDescription: "hn",
|
||||
}
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p, 3)
|
||||
router := routes.NewPlanRouter(m)
|
||||
data := []byte(`{
|
||||
"plan_id": 1,
|
||||
"plan_description": "testing"
|
||||
}`)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/1", 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.StatusOK, status)
|
||||
expected := `{
|
||||
"updated_plan": {
|
||||
"plan_description": "testing",
|
||||
"plan_id": 1,
|
||||
"user_id": 3
|
||||
},
|
||||
"id": 1
|
||||
}`
|
||||
assert.JSONEq(expected, rr.Body.String())
|
||||
contentType := rr.Header().Get("Content-Type")
|
||||
assert.Equal("application/json", contentType)
|
||||
}
|
||||
|
||||
func TestExtraFieldPlanPutJSON(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getEmptyModel()
|
||||
p := &models.Plan{
|
||||
PlanID: 1,
|
||||
PlanDescription: "hn",
|
||||
}
|
||||
m.AddPlan(p, 3)
|
||||
router := routes.NewPlanRouter(m)
|
||||
data := []byte(`{
|
||||
"plan_id": 1,
|
||||
"plan_description": "testing",
|
||||
"sabotage": "omg"
|
||||
}`)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/1", 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)
|
||||
expected := `Bad Request`
|
||||
assert.Equal(expected, strings.TrimSpace(rr.Body.String()))
|
||||
}
|
||||
|
||||
func TestEmptyBodyPlanPut(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getEmptyModel()
|
||||
router := routes.NewPlanRouter(m)
|
||||
data := []byte(``)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/1", 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)
|
||||
expected := `Bad Request`
|
||||
assert.Equal(expected, strings.TrimSpace(rr.Body.String()))
|
||||
}
|
||||
|
||||
func TestTwoBodyPlanPut(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getEmptyModel()
|
||||
router := routes.NewPlanRouter(m)
|
||||
data := []byte(`{
|
||||
"plan_id": 5
|
||||
}, {
|
||||
"plan_id": 6
|
||||
}`)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/1", 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)
|
||||
expected := `Bad Request`
|
||||
assert.Equal(expected, strings.TrimSpace(rr.Body.String()))
|
||||
}
|
||||
|
||||
func TestBadPlanIDPut(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getEmptyModel()
|
||||
router := routes.NewPlanRouter(m)
|
||||
data := []byte(`{
|
||||
"plan_id": 5
|
||||
}, {
|
||||
"plan_id": 6
|
||||
}`)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/text", 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.StatusNotFound, status)
|
||||
expected := `Not Found`
|
||||
assert.Equal(expected, strings.TrimSpace(rr.Body.String()))
|
||||
}
|
||||
|
||||
func TestErrorUpdatePlan(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
|
||||
m := getErrorModel("Model always errors")
|
||||
|
||||
router := routes.NewPlanRouter(m)
|
||||
p := &models.Plan{PlanID: 6}
|
||||
data, _ := json.Marshal(p)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/1", 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)
|
||||
expected := `Internal Server Error`
|
||||
assert.Equal(expected, strings.TrimSpace(rr.Body.String()))
|
||||
}
|
||||
|
||||
func TestErrorOnRetrieveUpdatePlan(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
|
||||
m := getErrorOnGetModel("Model always errors")
|
||||
|
||||
router := routes.NewPlanRouter(m)
|
||||
p := &models.Plan{PlanID: 6}
|
||||
data, _ := json.Marshal(p)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/1", 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)
|
||||
expected := `Internal Server Error`
|
||||
assert.Equal(expected, strings.TrimSpace(rr.Body.String()))
|
||||
}
|
||||
|
||||
func TestErrorWriterUpdatePlan(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
|
||||
p := &models.Plan{PlanID: 6}
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p, 3)
|
||||
|
||||
router := routes.NewPlanRouter(m)
|
||||
data, _ := json.Marshal(p)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/1", 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)
|
||||
|
||||
}
|
||||
@@ -16,6 +16,7 @@ func NewPlanRouter(m *models.Model) http.Handler {
|
||||
router.Get("/", getAllPlansFunc(m))
|
||||
router.Post("/", postPlanFunc(m))
|
||||
router.Get("/{planid}", getPlanByIDFunc(m))
|
||||
router.Put("/{planid}", putPlanFunc(m))
|
||||
return router
|
||||
}
|
||||
|
||||
@@ -99,7 +100,7 @@ func postPlanFunc(m *models.Model) http.HandlerFunc {
|
||||
}
|
||||
|
||||
// Map the fields we allow to be set to the plan to be created.
|
||||
plan := &models.Plan{PlanDate: p.PlanDate, UserID: p.UserID}
|
||||
plan := &models.Plan{PlanDescription: p.PlanDescription, UserID: p.UserID}
|
||||
id, err := m.AddPlan(plan, userID)
|
||||
if err != nil {
|
||||
serverError(w, err)
|
||||
@@ -123,3 +124,65 @@ func postPlanFunc(m *models.Model) http.HandlerFunc {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
type updatePlanResponse struct {
|
||||
UpdatedPlan *models.Plan `json:"updated_plan"`
|
||||
ID int64 `json:"id"`
|
||||
}
|
||||
|
||||
func putPlanFunc(m *models.Model) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
userID, err := tokens.GetUserID(r.Context())
|
||||
if err != nil {
|
||||
unauthorizedHandler(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
id, err := strconv.Atoi(chi.URLParam(r, "planid"))
|
||||
if err != nil {
|
||||
notFoundHandler(w, r)
|
||||
return
|
||||
}
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 1024)
|
||||
dec := json.NewDecoder(r.Body)
|
||||
dec.DisallowUnknownFields()
|
||||
var p models.Plan
|
||||
err = dec.Decode(&p)
|
||||
if err != nil {
|
||||
badRequestError(w, err)
|
||||
return
|
||||
}
|
||||
err = dec.Decode(&struct{}{})
|
||||
if err != io.EOF {
|
||||
badRequestError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
plan := &models.Plan{
|
||||
PlanDescription: p.PlanDescription,
|
||||
PlanID: int64(id),
|
||||
}
|
||||
err = m.SavePlan(plan, userID)
|
||||
if err != nil {
|
||||
serverError(w, err)
|
||||
return
|
||||
}
|
||||
plan, err = m.Plan(id, userID)
|
||||
if err != nil {
|
||||
serverError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
response := &updatePlanResponse{
|
||||
UpdatedPlan: plan,
|
||||
ID: int64(id),
|
||||
}
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||
serverError(w, err)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var sampleContext = tokens.GetContextForUserValues(3, "testing")
|
||||
@@ -39,8 +38,8 @@ func TestEmptyPlans(t *testing.T) {
|
||||
func TestOnePlan(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, UserID: 3}
|
||||
planDescription := "2021-01-01"
|
||||
p := &models.Plan{PlanID: 6, PlanDescription: planDescription, UserID: 3}
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p, 3)
|
||||
router := routes.NewPlanRouter(m)
|
||||
@@ -58,7 +57,7 @@ func TestOnePlan(t *testing.T) {
|
||||
expected := `[
|
||||
{
|
||||
"plan_id": 1,
|
||||
"plan_date": "2021-01-01T00:00:00Z",
|
||||
"plan_description": "2021-01-01",
|
||||
"user_id": 3
|
||||
}
|
||||
]`
|
||||
@@ -84,7 +83,6 @@ func TestErrorPlan(t *testing.T) {
|
||||
// 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()))
|
||||
}
|
||||
@@ -112,8 +110,8 @@ func TestEmptyPlanErrorWriter(t *testing.T) {
|
||||
func TestOnePlanByID(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, UserID: 3}
|
||||
planDescription := "2021-01-01"
|
||||
p := &models.Plan{PlanID: 6, PlanDescription: planDescription, UserID: 3}
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p, 3)
|
||||
router := routes.NewPlanRouter(m)
|
||||
@@ -127,10 +125,10 @@ func TestOnePlanByID(t *testing.T) {
|
||||
// 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 := `{
|
||||
"plan_id": 1,
|
||||
"plan_date": "2021-01-01T00:00:00Z",
|
||||
"plan_description": "2021-01-01",
|
||||
"user_id": 3
|
||||
}`
|
||||
assert.JSONEq(expected, rr.Body.String())
|
||||
@@ -163,9 +161,8 @@ func TestErrorPlanByID(t *testing.T) {
|
||||
func TestEmptyPlanErrorWriterByID(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
|
||||
p := &models.Plan{PlanID: 1, PlanDate: &planDate}
|
||||
planDescription := "2021-01-01"
|
||||
p := &models.Plan{PlanID: 1, PlanDescription: planDescription}
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p, 3)
|
||||
|
||||
|
||||
@@ -3,13 +3,13 @@ package routes_test
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"gitea.deepak.science/deepak/gogmagog/models"
|
||||
"gitea.deepak.science/deepak/gogmagog/routes"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestEmptyPlanEmptyContext(t *testing.T) {
|
||||
@@ -33,8 +33,8 @@ func TestEmptyPlanEmptyContext(t *testing.T) {
|
||||
func TestOnePlanEmptyContext(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, UserID: 3}
|
||||
planDescription := "2021-01-01"
|
||||
p := &models.Plan{PlanID: 6, PlanDescription: planDescription, UserID: 3}
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p, 3)
|
||||
router := routes.NewPlanRouter(m)
|
||||
@@ -54,8 +54,8 @@ func TestOnePlanEmptyContext(t *testing.T) {
|
||||
func TestOnePlanByIDEmptyContext(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, UserID: 3}
|
||||
planDescription := "2021-01-01"
|
||||
p := &models.Plan{PlanID: 6, PlanDescription: planDescription, UserID: 3}
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p, 3)
|
||||
router := routes.NewPlanRouter(m)
|
||||
@@ -78,7 +78,7 @@ func TestPureJSONEmptyContext(t *testing.T) {
|
||||
m := getEmptyModel()
|
||||
router := routes.NewPlanRouter(m)
|
||||
data := []byte(`{
|
||||
"plan_date": "2021-01-01T00:00:00Z",
|
||||
"plan_description": "2021-01-01T00:00:00Z",
|
||||
"plan_id": 1,
|
||||
"user_id": 3
|
||||
}`)
|
||||
@@ -93,3 +93,27 @@ func TestPureJSONEmptyContext(t *testing.T) {
|
||||
status := rr.Code
|
||||
assert.Equal(http.StatusUnauthorized, status)
|
||||
}
|
||||
|
||||
func TestPutPlanEmptyContext(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
|
||||
p := &models.Plan{PlanID: 6, PlanDescription: "sth"}
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p, 3)
|
||||
|
||||
router := routes.NewPlanRouter(m)
|
||||
data, _ := json.Marshal(p)
|
||||
req, _ := http.NewRequestWithContext(context.Background(), "PUT", "/1", 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.StatusUnauthorized, status)
|
||||
|
||||
}
|
||||
|
||||
@@ -65,8 +65,8 @@ func TestPureJSONPostAction(t *testing.T) {
|
||||
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}
|
||||
planDescription := "2021-01-01"
|
||||
p := &models.Plan{PlanID: 6, PlanDescription: planDescription}
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p, 3)
|
||||
router := routes.NewActionRouter(m)
|
||||
@@ -92,8 +92,8 @@ func TestExtraFieldActionPostJSON(t *testing.T) {
|
||||
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}
|
||||
planDescription := "2021-01-01"
|
||||
p := &models.Plan{PlanID: 6, PlanDescription: planDescription}
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p, 3)
|
||||
router := routes.NewActionRouter(m)
|
||||
@@ -116,8 +116,8 @@ func TestEmptyBodyActionPost(t *testing.T) {
|
||||
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}
|
||||
planDescription := "2021-01-01"
|
||||
p := &models.Plan{PlanID: 6, PlanDescription: planDescription}
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p, 3)
|
||||
router := routes.NewActionRouter(m)
|
||||
|
||||
@@ -10,15 +10,14 @@ import (
|
||||
"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")
|
||||
planDescription := "2021-01-01"
|
||||
userID := 3
|
||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: int64(userID)}
|
||||
p := &models.Plan{PlanID: 6, PlanDescription: planDescription, UserID: int64(userID)}
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p, userID)
|
||||
router := routes.NewPlanRouter(m)
|
||||
@@ -37,7 +36,7 @@ func TestCreatePlanRoute(t *testing.T) {
|
||||
expected := `{
|
||||
"created_plan": {
|
||||
"plan_id": 2,
|
||||
"plan_date": "2021-01-01T00:00:00Z",
|
||||
"plan_description": "2021-01-01",
|
||||
"user_id": 3
|
||||
},
|
||||
"id": 2
|
||||
@@ -50,14 +49,14 @@ func TestCreatePlanRoute(t *testing.T) {
|
||||
func TestPureJSON(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
planDescription := "2021-01-01"
|
||||
userID := 3
|
||||
p := &models.Plan{PlanID: 1, PlanDate: &planDate, UserID: int64(userID)}
|
||||
p := &models.Plan{PlanID: 1, PlanDescription: planDescription, UserID: int64(userID)}
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p, userID)
|
||||
router := routes.NewPlanRouter(m)
|
||||
data := []byte(`{
|
||||
"plan_date": "2021-01-01T00:00:00Z",
|
||||
"plan_description": "2021-01-01",
|
||||
"plan_id": 1,
|
||||
"user_id": 3
|
||||
}`)
|
||||
@@ -76,7 +75,7 @@ func TestPureJSON(t *testing.T) {
|
||||
"created_plan": {
|
||||
"plan_id": 2,
|
||||
"user_id": 3,
|
||||
"plan_date": "2021-01-01T00:00:00Z"
|
||||
"plan_description": "2021-01-01"
|
||||
},
|
||||
"id": 2
|
||||
}`
|
||||
@@ -88,14 +87,14 @@ func TestPureJSON(t *testing.T) {
|
||||
func TestExtraFieldJSON(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
planDescription := "2021-01-01"
|
||||
userID := 3
|
||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: int64(userID)}
|
||||
p := &models.Plan{PlanID: 6, PlanDescription: planDescription, UserID: int64(userID)}
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p, userID)
|
||||
router := routes.NewPlanRouter(m)
|
||||
data := []byte(`{
|
||||
"plan_date": "2021-01-01T00:00:00Z",
|
||||
"plan_description": "2021-01-01",
|
||||
"plan_id": 5,
|
||||
"plan_sabotage": "omg"
|
||||
}`)
|
||||
@@ -116,9 +115,9 @@ func TestExtraFieldJSON(t *testing.T) {
|
||||
func TestEmptyBody(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
planDescription := "2021-01-01"
|
||||
userID := 3
|
||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
||||
p := &models.Plan{PlanID: 6, PlanDescription: planDescription}
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p, userID)
|
||||
router := routes.NewPlanRouter(m)
|
||||
@@ -141,16 +140,16 @@ func TestEmptyBody(t *testing.T) {
|
||||
func TestTwoBody(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}
|
||||
planDescription := "2021-01-01"
|
||||
p := &models.Plan{PlanID: 6, PlanDescription: planDescription}
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p, 3)
|
||||
router := routes.NewPlanRouter(m)
|
||||
data := []byte(`{
|
||||
"plan_date": "2021-01-01T00:00:00Z",
|
||||
"plan_description": "2021-01-01",
|
||||
"plan_id": 5
|
||||
}, {
|
||||
"plan_date": "2021-01-01T00:00:00Z",
|
||||
"plan_description": "2021-01-01",
|
||||
"plan_id": 6
|
||||
}`)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
||||
@@ -175,8 +174,8 @@ func TestErrorCreatePlan(t *testing.T) {
|
||||
m := getErrorModel("Model always errors")
|
||||
|
||||
router := routes.NewPlanRouter(m)
|
||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
||||
planDescription := "2021-01-01"
|
||||
p := &models.Plan{PlanID: 6, PlanDescription: planDescription}
|
||||
data, _ := json.Marshal(p)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
@@ -201,8 +200,8 @@ func TestErrorOnRetrieveCreatePlan(t *testing.T) {
|
||||
m := getErrorOnGetModel("Model always errors")
|
||||
|
||||
router := routes.NewPlanRouter(m)
|
||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
||||
planDescription := "2021-01-01"
|
||||
p := &models.Plan{PlanID: 6, PlanDescription: planDescription}
|
||||
data, _ := json.Marshal(p)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
@@ -222,9 +221,9 @@ func TestErrorOnRetrieveCreatePlan(t *testing.T) {
|
||||
func TestErrorWriterCreatePlan(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
planDescription := "2021-01-01"
|
||||
|
||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
||||
p := &models.Plan{PlanID: 6, PlanDescription: planDescription}
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p, 3)
|
||||
router := routes.NewPlanRouter(m)
|
||||
|
||||
@@ -42,6 +42,13 @@ func (e *errorStore) InsertPlan(plan *models.Plan, userID int) (int, error) {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (e *errorStore) UpdatePlan(plan *models.Plan, userID int) error {
|
||||
if e.errorOnInsert {
|
||||
return e.error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *errorStore) SelectActionsByPlanID(plan *models.Plan, userID int) ([]*models.Action, error) {
|
||||
return nil, e.error
|
||||
}
|
||||
|
||||
@@ -54,6 +54,12 @@ func TestErrorPlanMethods(t *testing.T) {
|
||||
_, err = str2.InsertPlan(&models.Plan{}, 3)
|
||||
assert.Nil(err)
|
||||
|
||||
replacementPlan := &models.Plan{}
|
||||
err = str.UpdatePlan(replacementPlan, 3)
|
||||
assert.NotNil(err)
|
||||
err = str2.UpdatePlan(replacementPlan, 3)
|
||||
assert.Nil(err)
|
||||
|
||||
_, err = str.SelectPlanByID(5, 3)
|
||||
assert.NotNil(err)
|
||||
|
||||
|
||||
@@ -100,6 +100,16 @@ func (store *inMemoryStore) InsertPlan(plan *models.Plan, userID int) (int, erro
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (store *inMemoryStore) UpdatePlan(plan *models.Plan, userID int) error {
|
||||
currPlan, err := store.SelectPlanByID(int(plan.PlanID), userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
currPlan.PlanDescription = plan.PlanDescription
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *inMemoryStore) SelectCurrentPlan(userID int) (*models.CurrentPlan, error) {
|
||||
for _, currentPlan := range store.currentPlans {
|
||||
if userID == int(currentPlan.UserID) {
|
||||
|
||||
@@ -77,6 +77,16 @@ func TestInMemoryPlanMethods(t *testing.T) {
|
||||
|
||||
_, err = str.SelectPlanByID(135135, userID)
|
||||
assert.NotNil(err)
|
||||
|
||||
sampleDescription := "snth"
|
||||
replacePlan := &models.Plan{PlanID: 1, PlanDescription: sampleDescription}
|
||||
err = str.UpdatePlan(replacePlan, userID)
|
||||
assert.Nil(err)
|
||||
assert.Equal(sampleDescription, p.PlanDescription)
|
||||
|
||||
replacePlan = &models.Plan{PlanID: 1235122, PlanDescription: sampleDescription}
|
||||
err = str.UpdatePlan(replacePlan, userID)
|
||||
assert.NotNil(err)
|
||||
}
|
||||
|
||||
func TestLive(t *testing.T) {
|
||||
|
||||
@@ -9,7 +9,7 @@ CREATE TABLE IF NOT EXISTS users(
|
||||
|
||||
CREATE TABLE IF NOT EXISTS plans(
|
||||
plan_id serial PRIMARY KEY,
|
||||
plan_date DATE NOT NULL,
|
||||
plan_description VARCHAR (500) NOT NULL,
|
||||
user_id int REFERENCES users(user_id),
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
|
||||
@@ -112,7 +112,7 @@ func (store *postgresStore) UpdateAction(action *models.Action, userID int) erro
|
||||
}
|
||||
|
||||
func (store *postgresStore) SelectPlans(userID int) ([]*models.Plan, error) {
|
||||
queryString := store.db.Rebind("SELECT plan_id, plan_date, user_id FROM plans WHERE user_id = ?")
|
||||
queryString := store.db.Rebind("SELECT plan_id, plan_description, user_id FROM plans WHERE user_id = ?")
|
||||
plans := make([]*models.Plan, 0)
|
||||
err := store.db.Select(&plans, queryString, userID)
|
||||
if err != nil {
|
||||
@@ -123,7 +123,7 @@ func (store *postgresStore) SelectPlans(userID int) ([]*models.Plan, error) {
|
||||
|
||||
func (store *postgresStore) SelectPlanByID(id int, userID int) (*models.Plan, error) {
|
||||
plan := models.Plan{}
|
||||
err := store.db.Get(&plan, store.db.Rebind("SELECT plan_id, plan_date, user_id FROM plans WHERE plan_id = ? AND user_id = ?"), id, userID)
|
||||
err := store.db.Get(&plan, store.db.Rebind("SELECT plan_id, plan_description, user_id FROM plans WHERE plan_id = ? AND user_id = ?"), id, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -131,10 +131,10 @@ func (store *postgresStore) SelectPlanByID(id int, userID int) (*models.Plan, er
|
||||
}
|
||||
|
||||
func (store *postgresStore) InsertPlan(plan *models.Plan, userID int) (int, error) {
|
||||
queryString := store.db.Rebind("INSERT INTO plans (plan_date, user_id) VALUES (?, ?) RETURNING plan_id")
|
||||
queryString := store.db.Rebind("INSERT INTO plans (plan_description, user_id) VALUES (?, ?) RETURNING plan_id")
|
||||
tx := store.db.MustBegin()
|
||||
var id int
|
||||
err := tx.Get(&id, queryString, plan.PlanDate, userID)
|
||||
err := tx.Get(&id, queryString, plan.PlanDescription, userID)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return -1, err
|
||||
@@ -146,6 +146,30 @@ func (store *postgresStore) InsertPlan(plan *models.Plan, userID int) (int, erro
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (store *postgresStore) UpdatePlan(plan *models.Plan, userID int) error {
|
||||
query := `UPDATE plans SET
|
||||
plan_description = :plan_description
|
||||
WHERE plan_id = :plan_id
|
||||
AND user_id = :user_id`
|
||||
tx := store.db.MustBegin()
|
||||
planToUse := &models.Plan{
|
||||
PlanDescription: plan.PlanDescription,
|
||||
PlanID: plan.PlanID,
|
||||
UserID: int64(userID),
|
||||
}
|
||||
_, err := store.db.NamedExec(query, planToUse)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (store *postgresStore) ConnectionLive() error {
|
||||
return store.db.Ping()
|
||||
}
|
||||
|
||||
@@ -6,20 +6,19 @@ import (
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestSelectPlans(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
currentTime := time.Now()
|
||||
planDesc := "testing"
|
||||
idToUse := 1
|
||||
userIDToUse := 2
|
||||
|
||||
str, mock := getDbMock(t)
|
||||
|
||||
rows := sqlmock.NewRows([]string{"plan_id", "plan_date", "user_id"}).AddRow(idToUse, currentTime, userIDToUse)
|
||||
mock.ExpectQuery(`^SELECT plan_id, plan_date, user_id FROM plans WHERE user_id = \$1`).
|
||||
rows := sqlmock.NewRows([]string{"plan_id", "plan_description", "user_id"}).AddRow(idToUse, planDesc, userIDToUse)
|
||||
mock.ExpectQuery(`^SELECT plan_id, plan_description, user_id FROM plans WHERE user_id = \$1`).
|
||||
WithArgs(userIDToUse).
|
||||
WillReturnRows(rows)
|
||||
|
||||
@@ -28,7 +27,7 @@ func TestSelectPlans(t *testing.T) {
|
||||
assert.Equal(1, len(plans))
|
||||
plan := plans[0]
|
||||
assert.EqualValues(idToUse, plan.PlanID)
|
||||
assert.Equal(currentTime, *plan.PlanDate)
|
||||
assert.Equal(planDesc, plan.PlanDescription)
|
||||
assert.EqualValues(userIDToUse, plan.UserID)
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
@@ -39,21 +38,21 @@ func TestSelectPlans(t *testing.T) {
|
||||
func TestSelectPlanByID(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
currentTime := time.Now()
|
||||
planDesc := "tsaoeu"
|
||||
idToUse := 1
|
||||
userIDToUse := 2
|
||||
|
||||
str, mock := getDbMock(t)
|
||||
|
||||
rows := sqlmock.NewRows([]string{"plan_id", "plan_date", "user_id"}).AddRow(idToUse, currentTime, userIDToUse)
|
||||
mock.ExpectQuery(`^SELECT plan_id, plan_date, user_id FROM plans WHERE plan_id = \$1 AND user_id = \$2$`).
|
||||
rows := sqlmock.NewRows([]string{"plan_id", "plan_description", "user_id"}).AddRow(idToUse, planDesc, userIDToUse)
|
||||
mock.ExpectQuery(`^SELECT plan_id, plan_description, user_id FROM plans WHERE plan_id = \$1 AND user_id = \$2$`).
|
||||
WithArgs(idToUse, userIDToUse).
|
||||
WillReturnRows(rows)
|
||||
|
||||
plan, err := str.SelectPlanByID(idToUse, userIDToUse)
|
||||
assert.Nil(err)
|
||||
assert.EqualValues(idToUse, plan.PlanID)
|
||||
assert.Equal(currentTime, *plan.PlanDate)
|
||||
assert.Equal(planDesc, plan.PlanDescription)
|
||||
assert.EqualValues(userIDToUse, plan.UserID)
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
@@ -66,19 +65,19 @@ func TestInsertPlan(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
str, mock := getDbMock(t)
|
||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
planDescription := "2021-01-01"
|
||||
userID := 2
|
||||
badUserID := 7
|
||||
|
||||
plan := &models.Plan{PlanDate: &planDate, UserID: int64(badUserID)}
|
||||
plan := &models.Plan{PlanDescription: planDescription, UserID: int64(badUserID)}
|
||||
|
||||
idToUse := 8
|
||||
|
||||
rows := sqlmock.NewRows([]string{"plan_id"}).AddRow(8)
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectQuery(`^INSERT INTO plans \(plan_date, user_id\) VALUES \(\$1, \$2\) RETURNING plan_id$`).
|
||||
WithArgs(planDate, userID).
|
||||
mock.ExpectQuery(`^INSERT INTO plans \(plan_description, user_id\) VALUES \(\$1, \$2\) RETURNING plan_id$`).
|
||||
WithArgs(planDescription, userID).
|
||||
WillReturnRows(rows)
|
||||
mock.ExpectCommit()
|
||||
|
||||
@@ -98,14 +97,14 @@ func TestInsertPlanErr(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
str, mock := getDbMock(t)
|
||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
planDescription := "2021-01-01"
|
||||
userID := 2
|
||||
badUserID := 7
|
||||
plan := &models.Plan{PlanDate: &planDate, UserID: int64(badUserID)}
|
||||
plan := &models.Plan{PlanDescription: planDescription, UserID: int64(badUserID)}
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectQuery(`^INSERT INTO plans \(plan_date, user_id\) VALUES \(\$1, \$2\) RETURNING plan_id$`).
|
||||
WithArgs(planDate, userID).
|
||||
mock.ExpectQuery(`^INSERT INTO plans \(plan_description, user_id\) VALUES \(\$1, \$2\) RETURNING plan_id$`).
|
||||
WithArgs(planDescription, userID).
|
||||
WillReturnError(fmt.Errorf("example error"))
|
||||
mock.ExpectRollback()
|
||||
|
||||
@@ -124,16 +123,16 @@ func TestInsertPlanCommitErr(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
str, mock := getDbMock(t)
|
||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
planDescription := "2021-01-01"
|
||||
userID := 2
|
||||
plan := &models.Plan{PlanDate: &planDate, UserID: int64(userID)}
|
||||
plan := &models.Plan{PlanDescription: planDescription, UserID: int64(userID)}
|
||||
idToUse := 8
|
||||
|
||||
rows := sqlmock.NewRows([]string{"plan_id"}).AddRow(idToUse)
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectQuery(`^INSERT INTO plans \(plan_date, user_id\) VALUES \(\$1, \$2\) RETURNING plan_id$`).
|
||||
WithArgs(planDate, userID).
|
||||
mock.ExpectQuery(`^INSERT INTO plans \(plan_description, user_id\) VALUES \(\$1, \$2\) RETURNING plan_id$`).
|
||||
WithArgs(planDescription, userID).
|
||||
WillReturnRows(rows)
|
||||
mock.ExpectCommit().WillReturnError(fmt.Errorf("another error example"))
|
||||
|
||||
@@ -154,7 +153,7 @@ func TestErrPlanByID(t *testing.T) {
|
||||
|
||||
str, mock := getDbMock(t)
|
||||
|
||||
mock.ExpectQuery(`^SELECT plan_id, plan_date, user_id FROM plans WHERE plan_id = \$1 AND user_id = \$2$`).
|
||||
mock.ExpectQuery(`^SELECT plan_id, plan_description, user_id FROM plans WHERE plan_id = \$1 AND user_id = \$2$`).
|
||||
WithArgs(idToUse, 8).
|
||||
WillReturnError(fmt.Errorf("example error"))
|
||||
|
||||
@@ -172,7 +171,7 @@ func TestErrPlans(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
str, mock := getDbMock(t)
|
||||
|
||||
mock.ExpectQuery(`^SELECT plan_id, plan_date, user_id FROM plans WHERE user_id = \$1$`).
|
||||
mock.ExpectQuery(`^SELECT plan_id, plan_description, user_id FROM plans WHERE user_id = \$1$`).
|
||||
WithArgs(8).
|
||||
WillReturnError(fmt.Errorf("example error"))
|
||||
// function under test
|
||||
@@ -184,3 +183,82 @@ func TestErrPlans(t *testing.T) {
|
||||
t.Errorf("unfulfilled expectations: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdatePlan(t *testing.T) {
|
||||
// setup
|
||||
assert := assert.New(t)
|
||||
|
||||
str, mock := getDbMock(t)
|
||||
planDescription := "2021-01-01"
|
||||
userID := 2
|
||||
idToUse := 8
|
||||
|
||||
plan := &models.Plan{PlanDescription: planDescription, UserID: int64(userID), PlanID: int64(idToUse)}
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec(`^UPDATE plans SET plan_description = \$1 WHERE plan_id = \$2 AND user_id = \$3$`).
|
||||
WithArgs(planDescription, idToUse, userID).
|
||||
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit()
|
||||
|
||||
// function under test
|
||||
err := str.UpdatePlan(plan, userID)
|
||||
// check results
|
||||
assert.Nil(err)
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unfulfilled expectations: %s", err)
|
||||
}
|
||||
}
|
||||
func TestUpdatePlanErr(t *testing.T) {
|
||||
// setup
|
||||
assert := assert.New(t)
|
||||
|
||||
str, mock := getDbMock(t)
|
||||
planDescription := "2021-01-01"
|
||||
userID := 2
|
||||
idToUse := 8
|
||||
|
||||
plan := &models.Plan{PlanDescription: planDescription, UserID: int64(userID), PlanID: int64(idToUse)}
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec(`^UPDATE plans SET plan_description = \$1 WHERE plan_id = \$2 AND user_id = \$3$`).
|
||||
WithArgs(planDescription, idToUse, userID).
|
||||
WillReturnError(fmt.Errorf("example error"))
|
||||
mock.ExpectRollback()
|
||||
|
||||
// function under test
|
||||
err := str.UpdatePlan(plan, userID)
|
||||
// check results
|
||||
assert.NotNil(err)
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unfulfilled expectations: %s", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestUpdatePlanCommitErr(t *testing.T) {
|
||||
// setup
|
||||
assert := assert.New(t)
|
||||
|
||||
str, mock := getDbMock(t)
|
||||
planDescription := "2021-01-01"
|
||||
userID := 2
|
||||
idToUse := 8
|
||||
|
||||
plan := &models.Plan{PlanDescription: planDescription, UserID: int64(userID), PlanID: int64(idToUse)}
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec(`^UPDATE plans SET plan_description = \$1 WHERE plan_id = \$2 AND user_id = \$3$`).
|
||||
WithArgs(planDescription, idToUse, userID).
|
||||
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit().WillReturnError(fmt.Errorf("another error example"))
|
||||
|
||||
// function under test
|
||||
err := str.UpdatePlan(plan, userID)
|
||||
// check results
|
||||
assert.NotNil(err)
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unfulfilled expectations: %s", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user