Compare commits
2 Commits
b47abbfc0b
...
8ca7858069
| Author | SHA1 | Date | |
|---|---|---|---|
|
8ca7858069
|
|||
|
5e030a5bc3
|
@@ -14,6 +14,7 @@ type Store interface {
|
|||||||
SelectPlans(userID int) ([]*Plan, error)
|
SelectPlans(userID int) ([]*Plan, error)
|
||||||
SelectPlanByID(id int, userID int) (*Plan, error)
|
SelectPlanByID(id int, userID int) (*Plan, error)
|
||||||
InsertPlan(plan *Plan, userID int) (int, error)
|
InsertPlan(plan *Plan, userID int) (int, error)
|
||||||
|
UpdatePlan(plan *Plan, userID int) error
|
||||||
SelectActionsByPlanID(plan *Plan, userID int) ([]*Action, error)
|
SelectActionsByPlanID(plan *Plan, userID int) ([]*Action, error)
|
||||||
SelectUserByUsername(username string) (*User, error)
|
SelectUserByUsername(username string) (*User, error)
|
||||||
InsertUser(user *User) (int, error)
|
InsertUser(user *User) (int, error)
|
||||||
|
|||||||
@@ -56,6 +56,12 @@ func TestModelPlanMethods(t *testing.T) {
|
|||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
assert.EqualValues(1, firstPlan.PlanID)
|
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)
|
actions, err := m.GetActions(firstPlan, userID)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
assert.Equal(1, len(actions))
|
assert.Equal(1, len(actions))
|
||||||
|
|||||||
@@ -1,14 +1,10 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Plan represents a single day's agenda of actions.
|
// Plan represents a single day's agenda of actions.
|
||||||
type Plan struct {
|
type Plan struct {
|
||||||
PlanID int64 `json:"plan_id"`
|
PlanID int64 `json:"plan_id"`
|
||||||
PlanDate *time.Time `json:"plan_date"`
|
PlanDescription string `json:"plan_description"`
|
||||||
UserID int64 `json:"user_id"`
|
UserID int64 `json:"user_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Plans returns all plans in the model.
|
// 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)
|
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.
|
// GetActions returns the actions associated with a particular plan.
|
||||||
func (m *Model) GetActions(plan *Plan, userID int) ([]*Action, error) {
|
func (m *Model) GetActions(plan *Plan, userID int) ([]*Action, error) {
|
||||||
return m.SelectActionsByPlanID(plan, userID)
|
return m.SelectActionsByPlanID(plan, userID)
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestEmptyActionEmptyContext(t *testing.T) {
|
func TestEmptyActionEmptyContext(t *testing.T) {
|
||||||
@@ -34,8 +33,8 @@ func TestEmptyActionEmptyContext(t *testing.T) {
|
|||||||
func TestOneActionEmptyContext(t *testing.T) {
|
func TestOneActionEmptyContext(t *testing.T) {
|
||||||
// set up
|
// set up
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
planDescription := "2006-01-02"
|
||||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: 3}
|
p := &models.Plan{PlanID: 6, PlanDescription: planDescription, UserID: 3}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddPlan(p, 3)
|
m.AddPlan(p, 3)
|
||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
@@ -55,8 +54,8 @@ func TestOneActionEmptyContext(t *testing.T) {
|
|||||||
func TestOneActionByIDEmptyContext(t *testing.T) {
|
func TestOneActionByIDEmptyContext(t *testing.T) {
|
||||||
// set up
|
// set up
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
planDescription := "2021-01-01"
|
||||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: 3}
|
p := &models.Plan{PlanID: 6, PlanDescription: planDescription, UserID: 3}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddPlan(p, 3)
|
m.AddPlan(p, 3)
|
||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
@@ -79,7 +78,7 @@ func TestPureJSONActionEmptyContext(t *testing.T) {
|
|||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
data := []byte(`{
|
data := []byte(`{
|
||||||
"plan_date": "2021-01-01T00:00:00Z",
|
"plan_description": "2021-01-01T00:00:00Z",
|
||||||
"plan_id": 1,
|
"plan_id": 1,
|
||||||
"user_id": 3
|
"user_id": 3
|
||||||
}`)
|
}`)
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ func postPlanFunc(m *models.Model) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Map the fields we allow to be set to the plan to be created.
|
// 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)
|
id, err := m.AddPlan(plan, userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
serverError(w, err)
|
serverError(w, err)
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import (
|
|||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var sampleContext = tokens.GetContextForUserValues(3, "testing")
|
var sampleContext = tokens.GetContextForUserValues(3, "testing")
|
||||||
@@ -39,8 +38,8 @@ func TestEmptyPlans(t *testing.T) {
|
|||||||
func TestOnePlan(t *testing.T) {
|
func TestOnePlan(t *testing.T) {
|
||||||
// set up
|
// set up
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
planDescription := "2021-01-01"
|
||||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: 3}
|
p := &models.Plan{PlanID: 6, PlanDescription: planDescription, UserID: 3}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddPlan(p, 3)
|
m.AddPlan(p, 3)
|
||||||
router := routes.NewPlanRouter(m)
|
router := routes.NewPlanRouter(m)
|
||||||
@@ -58,7 +57,7 @@ func TestOnePlan(t *testing.T) {
|
|||||||
expected := `[
|
expected := `[
|
||||||
{
|
{
|
||||||
"plan_id": 1,
|
"plan_id": 1,
|
||||||
"plan_date": "2021-01-01T00:00:00Z",
|
"plan_description": "2021-01-01",
|
||||||
"user_id": 3
|
"user_id": 3
|
||||||
}
|
}
|
||||||
]`
|
]`
|
||||||
@@ -84,7 +83,6 @@ func TestErrorPlan(t *testing.T) {
|
|||||||
// check results
|
// check results
|
||||||
status := rr.Code
|
status := rr.Code
|
||||||
assert.Equal(http.StatusInternalServerError, status)
|
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`
|
expected := `Internal Server Error`
|
||||||
assert.Equal(expected, strings.TrimSpace(rr.Body.String()))
|
assert.Equal(expected, strings.TrimSpace(rr.Body.String()))
|
||||||
}
|
}
|
||||||
@@ -112,8 +110,8 @@ func TestEmptyPlanErrorWriter(t *testing.T) {
|
|||||||
func TestOnePlanByID(t *testing.T) {
|
func TestOnePlanByID(t *testing.T) {
|
||||||
// set up
|
// set up
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
planDescription := "2021-01-01"
|
||||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: 3}
|
p := &models.Plan{PlanID: 6, PlanDescription: planDescription, UserID: 3}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddPlan(p, 3)
|
m.AddPlan(p, 3)
|
||||||
router := routes.NewPlanRouter(m)
|
router := routes.NewPlanRouter(m)
|
||||||
@@ -127,10 +125,10 @@ func TestOnePlanByID(t *testing.T) {
|
|||||||
// check results
|
// check results
|
||||||
status := rr.Code
|
status := rr.Code
|
||||||
assert.Equal(http.StatusOK, status)
|
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 := `{
|
expected := `{
|
||||||
"plan_id": 1,
|
"plan_id": 1,
|
||||||
"plan_date": "2021-01-01T00:00:00Z",
|
"plan_description": "2021-01-01",
|
||||||
"user_id": 3
|
"user_id": 3
|
||||||
}`
|
}`
|
||||||
assert.JSONEq(expected, rr.Body.String())
|
assert.JSONEq(expected, rr.Body.String())
|
||||||
@@ -163,9 +161,8 @@ func TestErrorPlanByID(t *testing.T) {
|
|||||||
func TestEmptyPlanErrorWriterByID(t *testing.T) {
|
func TestEmptyPlanErrorWriterByID(t *testing.T) {
|
||||||
// set up
|
// set up
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
planDescription := "2021-01-01"
|
||||||
|
p := &models.Plan{PlanID: 1, PlanDescription: planDescription}
|
||||||
p := &models.Plan{PlanID: 1, PlanDate: &planDate}
|
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddPlan(p, 3)
|
m.AddPlan(p, 3)
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestEmptyPlanEmptyContext(t *testing.T) {
|
func TestEmptyPlanEmptyContext(t *testing.T) {
|
||||||
@@ -33,8 +32,8 @@ func TestEmptyPlanEmptyContext(t *testing.T) {
|
|||||||
func TestOnePlanEmptyContext(t *testing.T) {
|
func TestOnePlanEmptyContext(t *testing.T) {
|
||||||
// set up
|
// set up
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
planDescription := "2021-01-01"
|
||||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: 3}
|
p := &models.Plan{PlanID: 6, PlanDescription: planDescription, UserID: 3}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddPlan(p, 3)
|
m.AddPlan(p, 3)
|
||||||
router := routes.NewPlanRouter(m)
|
router := routes.NewPlanRouter(m)
|
||||||
@@ -54,8 +53,8 @@ func TestOnePlanEmptyContext(t *testing.T) {
|
|||||||
func TestOnePlanByIDEmptyContext(t *testing.T) {
|
func TestOnePlanByIDEmptyContext(t *testing.T) {
|
||||||
// set up
|
// set up
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
planDescription := "2021-01-01"
|
||||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: 3}
|
p := &models.Plan{PlanID: 6, PlanDescription: planDescription, UserID: 3}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddPlan(p, 3)
|
m.AddPlan(p, 3)
|
||||||
router := routes.NewPlanRouter(m)
|
router := routes.NewPlanRouter(m)
|
||||||
@@ -78,7 +77,7 @@ func TestPureJSONEmptyContext(t *testing.T) {
|
|||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
router := routes.NewPlanRouter(m)
|
router := routes.NewPlanRouter(m)
|
||||||
data := []byte(`{
|
data := []byte(`{
|
||||||
"plan_date": "2021-01-01T00:00:00Z",
|
"plan_description": "2021-01-01T00:00:00Z",
|
||||||
"plan_id": 1,
|
"plan_id": 1,
|
||||||
"user_id": 3
|
"user_id": 3
|
||||||
}`)
|
}`)
|
||||||
|
|||||||
@@ -65,8 +65,8 @@ func TestPureJSONPostAction(t *testing.T) {
|
|||||||
func TestExtraFieldActionPostJSON(t *testing.T) {
|
func TestExtraFieldActionPostJSON(t *testing.T) {
|
||||||
// set up
|
// set up
|
||||||
assert := assert.New(t)
|
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 := getEmptyModel()
|
||||||
m.AddPlan(p, 3)
|
m.AddPlan(p, 3)
|
||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
@@ -92,8 +92,8 @@ func TestExtraFieldActionPostJSON(t *testing.T) {
|
|||||||
func TestEmptyBodyActionPost(t *testing.T) {
|
func TestEmptyBodyActionPost(t *testing.T) {
|
||||||
// set up
|
// set up
|
||||||
assert := assert.New(t)
|
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 := getEmptyModel()
|
||||||
m.AddPlan(p, 3)
|
m.AddPlan(p, 3)
|
||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
@@ -116,8 +116,8 @@ func TestEmptyBodyActionPost(t *testing.T) {
|
|||||||
func TestTwoBodyActionPost(t *testing.T) {
|
func TestTwoBodyActionPost(t *testing.T) {
|
||||||
// set up
|
// set up
|
||||||
assert := assert.New(t)
|
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 := getEmptyModel()
|
||||||
m.AddPlan(p, 3)
|
m.AddPlan(p, 3)
|
||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
|
|||||||
@@ -10,15 +10,14 @@ import (
|
|||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCreatePlanRoute(t *testing.T) {
|
func TestCreatePlanRoute(t *testing.T) {
|
||||||
// set up
|
// set up
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
planDescription := "2021-01-01"
|
||||||
userID := 3
|
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 := getEmptyModel()
|
||||||
m.AddPlan(p, userID)
|
m.AddPlan(p, userID)
|
||||||
router := routes.NewPlanRouter(m)
|
router := routes.NewPlanRouter(m)
|
||||||
@@ -37,7 +36,7 @@ func TestCreatePlanRoute(t *testing.T) {
|
|||||||
expected := `{
|
expected := `{
|
||||||
"created_plan": {
|
"created_plan": {
|
||||||
"plan_id": 2,
|
"plan_id": 2,
|
||||||
"plan_date": "2021-01-01T00:00:00Z",
|
"plan_description": "2021-01-01",
|
||||||
"user_id": 3
|
"user_id": 3
|
||||||
},
|
},
|
||||||
"id": 2
|
"id": 2
|
||||||
@@ -50,14 +49,14 @@ func TestCreatePlanRoute(t *testing.T) {
|
|||||||
func TestPureJSON(t *testing.T) {
|
func TestPureJSON(t *testing.T) {
|
||||||
// set up
|
// set up
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
planDescription := "2021-01-01"
|
||||||
userID := 3
|
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 := getEmptyModel()
|
||||||
m.AddPlan(p, userID)
|
m.AddPlan(p, userID)
|
||||||
router := routes.NewPlanRouter(m)
|
router := routes.NewPlanRouter(m)
|
||||||
data := []byte(`{
|
data := []byte(`{
|
||||||
"plan_date": "2021-01-01T00:00:00Z",
|
"plan_description": "2021-01-01",
|
||||||
"plan_id": 1,
|
"plan_id": 1,
|
||||||
"user_id": 3
|
"user_id": 3
|
||||||
}`)
|
}`)
|
||||||
@@ -76,7 +75,7 @@ func TestPureJSON(t *testing.T) {
|
|||||||
"created_plan": {
|
"created_plan": {
|
||||||
"plan_id": 2,
|
"plan_id": 2,
|
||||||
"user_id": 3,
|
"user_id": 3,
|
||||||
"plan_date": "2021-01-01T00:00:00Z"
|
"plan_description": "2021-01-01"
|
||||||
},
|
},
|
||||||
"id": 2
|
"id": 2
|
||||||
}`
|
}`
|
||||||
@@ -88,14 +87,14 @@ func TestPureJSON(t *testing.T) {
|
|||||||
func TestExtraFieldJSON(t *testing.T) {
|
func TestExtraFieldJSON(t *testing.T) {
|
||||||
// set up
|
// set up
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
planDescription := "2021-01-01"
|
||||||
userID := 3
|
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 := getEmptyModel()
|
||||||
m.AddPlan(p, userID)
|
m.AddPlan(p, userID)
|
||||||
router := routes.NewPlanRouter(m)
|
router := routes.NewPlanRouter(m)
|
||||||
data := []byte(`{
|
data := []byte(`{
|
||||||
"plan_date": "2021-01-01T00:00:00Z",
|
"plan_description": "2021-01-01",
|
||||||
"plan_id": 5,
|
"plan_id": 5,
|
||||||
"plan_sabotage": "omg"
|
"plan_sabotage": "omg"
|
||||||
}`)
|
}`)
|
||||||
@@ -116,9 +115,9 @@ func TestExtraFieldJSON(t *testing.T) {
|
|||||||
func TestEmptyBody(t *testing.T) {
|
func TestEmptyBody(t *testing.T) {
|
||||||
// set up
|
// set up
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
planDescription := "2021-01-01"
|
||||||
userID := 3
|
userID := 3
|
||||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
p := &models.Plan{PlanID: 6, PlanDescription: planDescription}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddPlan(p, userID)
|
m.AddPlan(p, userID)
|
||||||
router := routes.NewPlanRouter(m)
|
router := routes.NewPlanRouter(m)
|
||||||
@@ -141,16 +140,16 @@ func TestEmptyBody(t *testing.T) {
|
|||||||
func TestTwoBody(t *testing.T) {
|
func TestTwoBody(t *testing.T) {
|
||||||
// set up
|
// set up
|
||||||
assert := assert.New(t)
|
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 := getEmptyModel()
|
||||||
m.AddPlan(p, 3)
|
m.AddPlan(p, 3)
|
||||||
router := routes.NewPlanRouter(m)
|
router := routes.NewPlanRouter(m)
|
||||||
data := []byte(`{
|
data := []byte(`{
|
||||||
"plan_date": "2021-01-01T00:00:00Z",
|
"plan_description": "2021-01-01",
|
||||||
"plan_id": 5
|
"plan_id": 5
|
||||||
}, {
|
}, {
|
||||||
"plan_date": "2021-01-01T00:00:00Z",
|
"plan_description": "2021-01-01",
|
||||||
"plan_id": 6
|
"plan_id": 6
|
||||||
}`)
|
}`)
|
||||||
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
||||||
@@ -175,8 +174,8 @@ func TestErrorCreatePlan(t *testing.T) {
|
|||||||
m := getErrorModel("Model always errors")
|
m := getErrorModel("Model always errors")
|
||||||
|
|
||||||
router := routes.NewPlanRouter(m)
|
router := routes.NewPlanRouter(m)
|
||||||
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}
|
||||||
data, _ := json.Marshal(p)
|
data, _ := json.Marshal(p)
|
||||||
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
@@ -201,8 +200,8 @@ func TestErrorOnRetrieveCreatePlan(t *testing.T) {
|
|||||||
m := getErrorOnGetModel("Model always errors")
|
m := getErrorOnGetModel("Model always errors")
|
||||||
|
|
||||||
router := routes.NewPlanRouter(m)
|
router := routes.NewPlanRouter(m)
|
||||||
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}
|
||||||
data, _ := json.Marshal(p)
|
data, _ := json.Marshal(p)
|
||||||
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
@@ -222,9 +221,9 @@ func TestErrorOnRetrieveCreatePlan(t *testing.T) {
|
|||||||
func TestErrorWriterCreatePlan(t *testing.T) {
|
func TestErrorWriterCreatePlan(t *testing.T) {
|
||||||
// set up
|
// set up
|
||||||
assert := assert.New(t)
|
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 := getEmptyModel()
|
||||||
m.AddPlan(p, 3)
|
m.AddPlan(p, 3)
|
||||||
router := routes.NewPlanRouter(m)
|
router := routes.NewPlanRouter(m)
|
||||||
|
|||||||
@@ -42,6 +42,13 @@ func (e *errorStore) InsertPlan(plan *models.Plan, userID int) (int, error) {
|
|||||||
return 0, nil
|
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) {
|
func (e *errorStore) SelectActionsByPlanID(plan *models.Plan, userID int) ([]*models.Action, error) {
|
||||||
return nil, e.error
|
return nil, e.error
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,6 +54,12 @@ func TestErrorPlanMethods(t *testing.T) {
|
|||||||
_, err = str2.InsertPlan(&models.Plan{}, 3)
|
_, err = str2.InsertPlan(&models.Plan{}, 3)
|
||||||
assert.Nil(err)
|
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)
|
_, err = str.SelectPlanByID(5, 3)
|
||||||
assert.NotNil(err)
|
assert.NotNil(err)
|
||||||
|
|
||||||
|
|||||||
@@ -100,6 +100,16 @@ func (store *inMemoryStore) InsertPlan(plan *models.Plan, userID int) (int, erro
|
|||||||
return id, nil
|
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) {
|
func (store *inMemoryStore) SelectCurrentPlan(userID int) (*models.CurrentPlan, error) {
|
||||||
for _, currentPlan := range store.currentPlans {
|
for _, currentPlan := range store.currentPlans {
|
||||||
if userID == int(currentPlan.UserID) {
|
if userID == int(currentPlan.UserID) {
|
||||||
|
|||||||
@@ -77,6 +77,16 @@ func TestInMemoryPlanMethods(t *testing.T) {
|
|||||||
|
|
||||||
_, err = str.SelectPlanByID(135135, userID)
|
_, err = str.SelectPlanByID(135135, userID)
|
||||||
assert.NotNil(err)
|
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) {
|
func TestLive(t *testing.T) {
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ CREATE TABLE IF NOT EXISTS users(
|
|||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS plans(
|
CREATE TABLE IF NOT EXISTS plans(
|
||||||
plan_id serial PRIMARY KEY,
|
plan_id serial PRIMARY KEY,
|
||||||
plan_date DATE NOT NULL,
|
plan_description VARCHAR (500) NOT NULL,
|
||||||
user_id int REFERENCES users(user_id),
|
user_id int REFERENCES users(user_id),
|
||||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||||
updated_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) {
|
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)
|
plans := make([]*models.Plan, 0)
|
||||||
err := store.db.Select(&plans, queryString, userID)
|
err := store.db.Select(&plans, queryString, userID)
|
||||||
if err != nil {
|
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) {
|
func (store *postgresStore) SelectPlanByID(id int, userID int) (*models.Plan, error) {
|
||||||
plan := models.Plan{}
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
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) {
|
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()
|
tx := store.db.MustBegin()
|
||||||
var id int
|
var id int
|
||||||
err := tx.Get(&id, queryString, plan.PlanDate, userID)
|
err := tx.Get(&id, queryString, plan.PlanDescription, userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return -1, err
|
return -1, err
|
||||||
@@ -146,6 +146,30 @@ func (store *postgresStore) InsertPlan(plan *models.Plan, userID int) (int, erro
|
|||||||
return id, nil
|
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 {
|
func (store *postgresStore) ConnectionLive() error {
|
||||||
return store.db.Ping()
|
return store.db.Ping()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,20 +6,19 @@ import (
|
|||||||
"github.com/DATA-DOG/go-sqlmock"
|
"github.com/DATA-DOG/go-sqlmock"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSelectPlans(t *testing.T) {
|
func TestSelectPlans(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
currentTime := time.Now()
|
planDesc := "testing"
|
||||||
idToUse := 1
|
idToUse := 1
|
||||||
userIDToUse := 2
|
userIDToUse := 2
|
||||||
|
|
||||||
str, mock := getDbMock(t)
|
str, mock := getDbMock(t)
|
||||||
|
|
||||||
rows := sqlmock.NewRows([]string{"plan_id", "plan_date", "user_id"}).AddRow(idToUse, currentTime, userIDToUse)
|
rows := sqlmock.NewRows([]string{"plan_id", "plan_description", "user_id"}).AddRow(idToUse, planDesc, userIDToUse)
|
||||||
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(userIDToUse).
|
WithArgs(userIDToUse).
|
||||||
WillReturnRows(rows)
|
WillReturnRows(rows)
|
||||||
|
|
||||||
@@ -28,7 +27,7 @@ func TestSelectPlans(t *testing.T) {
|
|||||||
assert.Equal(1, len(plans))
|
assert.Equal(1, len(plans))
|
||||||
plan := plans[0]
|
plan := plans[0]
|
||||||
assert.EqualValues(idToUse, plan.PlanID)
|
assert.EqualValues(idToUse, plan.PlanID)
|
||||||
assert.Equal(currentTime, *plan.PlanDate)
|
assert.Equal(planDesc, plan.PlanDescription)
|
||||||
assert.EqualValues(userIDToUse, plan.UserID)
|
assert.EqualValues(userIDToUse, plan.UserID)
|
||||||
|
|
||||||
if err := mock.ExpectationsWereMet(); err != nil {
|
if err := mock.ExpectationsWereMet(); err != nil {
|
||||||
@@ -39,21 +38,21 @@ func TestSelectPlans(t *testing.T) {
|
|||||||
func TestSelectPlanByID(t *testing.T) {
|
func TestSelectPlanByID(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
currentTime := time.Now()
|
planDesc := "tsaoeu"
|
||||||
idToUse := 1
|
idToUse := 1
|
||||||
userIDToUse := 2
|
userIDToUse := 2
|
||||||
|
|
||||||
str, mock := getDbMock(t)
|
str, mock := getDbMock(t)
|
||||||
|
|
||||||
rows := sqlmock.NewRows([]string{"plan_id", "plan_date", "user_id"}).AddRow(idToUse, currentTime, userIDToUse)
|
rows := sqlmock.NewRows([]string{"plan_id", "plan_description", "user_id"}).AddRow(idToUse, planDesc, userIDToUse)
|
||||||
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, userIDToUse).
|
WithArgs(idToUse, userIDToUse).
|
||||||
WillReturnRows(rows)
|
WillReturnRows(rows)
|
||||||
|
|
||||||
plan, err := str.SelectPlanByID(idToUse, userIDToUse)
|
plan, err := str.SelectPlanByID(idToUse, userIDToUse)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
assert.EqualValues(idToUse, plan.PlanID)
|
assert.EqualValues(idToUse, plan.PlanID)
|
||||||
assert.Equal(currentTime, *plan.PlanDate)
|
assert.Equal(planDesc, plan.PlanDescription)
|
||||||
assert.EqualValues(userIDToUse, plan.UserID)
|
assert.EqualValues(userIDToUse, plan.UserID)
|
||||||
|
|
||||||
if err := mock.ExpectationsWereMet(); err != nil {
|
if err := mock.ExpectationsWereMet(); err != nil {
|
||||||
@@ -66,19 +65,19 @@ func TestInsertPlan(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
str, mock := getDbMock(t)
|
str, mock := getDbMock(t)
|
||||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
planDescription := "2021-01-01"
|
||||||
userID := 2
|
userID := 2
|
||||||
badUserID := 7
|
badUserID := 7
|
||||||
|
|
||||||
plan := &models.Plan{PlanDate: &planDate, UserID: int64(badUserID)}
|
plan := &models.Plan{PlanDescription: planDescription, UserID: int64(badUserID)}
|
||||||
|
|
||||||
idToUse := 8
|
idToUse := 8
|
||||||
|
|
||||||
rows := sqlmock.NewRows([]string{"plan_id"}).AddRow(8)
|
rows := sqlmock.NewRows([]string{"plan_id"}).AddRow(8)
|
||||||
|
|
||||||
mock.ExpectBegin()
|
mock.ExpectBegin()
|
||||||
mock.ExpectQuery(`^INSERT INTO plans \(plan_date, user_id\) VALUES \(\$1, \$2\) RETURNING plan_id$`).
|
mock.ExpectQuery(`^INSERT INTO plans \(plan_description, user_id\) VALUES \(\$1, \$2\) RETURNING plan_id$`).
|
||||||
WithArgs(planDate, userID).
|
WithArgs(planDescription, userID).
|
||||||
WillReturnRows(rows)
|
WillReturnRows(rows)
|
||||||
mock.ExpectCommit()
|
mock.ExpectCommit()
|
||||||
|
|
||||||
@@ -98,14 +97,14 @@ func TestInsertPlanErr(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
str, mock := getDbMock(t)
|
str, mock := getDbMock(t)
|
||||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
planDescription := "2021-01-01"
|
||||||
userID := 2
|
userID := 2
|
||||||
badUserID := 7
|
badUserID := 7
|
||||||
plan := &models.Plan{PlanDate: &planDate, UserID: int64(badUserID)}
|
plan := &models.Plan{PlanDescription: planDescription, UserID: int64(badUserID)}
|
||||||
|
|
||||||
mock.ExpectBegin()
|
mock.ExpectBegin()
|
||||||
mock.ExpectQuery(`^INSERT INTO plans \(plan_date, user_id\) VALUES \(\$1, \$2\) RETURNING plan_id$`).
|
mock.ExpectQuery(`^INSERT INTO plans \(plan_description, user_id\) VALUES \(\$1, \$2\) RETURNING plan_id$`).
|
||||||
WithArgs(planDate, userID).
|
WithArgs(planDescription, userID).
|
||||||
WillReturnError(fmt.Errorf("example error"))
|
WillReturnError(fmt.Errorf("example error"))
|
||||||
mock.ExpectRollback()
|
mock.ExpectRollback()
|
||||||
|
|
||||||
@@ -124,16 +123,16 @@ func TestInsertPlanCommitErr(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
str, mock := getDbMock(t)
|
str, mock := getDbMock(t)
|
||||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
planDescription := "2021-01-01"
|
||||||
userID := 2
|
userID := 2
|
||||||
plan := &models.Plan{PlanDate: &planDate, UserID: int64(userID)}
|
plan := &models.Plan{PlanDescription: planDescription, UserID: int64(userID)}
|
||||||
idToUse := 8
|
idToUse := 8
|
||||||
|
|
||||||
rows := sqlmock.NewRows([]string{"plan_id"}).AddRow(idToUse)
|
rows := sqlmock.NewRows([]string{"plan_id"}).AddRow(idToUse)
|
||||||
|
|
||||||
mock.ExpectBegin()
|
mock.ExpectBegin()
|
||||||
mock.ExpectQuery(`^INSERT INTO plans \(plan_date, user_id\) VALUES \(\$1, \$2\) RETURNING plan_id$`).
|
mock.ExpectQuery(`^INSERT INTO plans \(plan_description, user_id\) VALUES \(\$1, \$2\) RETURNING plan_id$`).
|
||||||
WithArgs(planDate, userID).
|
WithArgs(planDescription, userID).
|
||||||
WillReturnRows(rows)
|
WillReturnRows(rows)
|
||||||
mock.ExpectCommit().WillReturnError(fmt.Errorf("another error example"))
|
mock.ExpectCommit().WillReturnError(fmt.Errorf("another error example"))
|
||||||
|
|
||||||
@@ -154,7 +153,7 @@ func TestErrPlanByID(t *testing.T) {
|
|||||||
|
|
||||||
str, mock := getDbMock(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).
|
WithArgs(idToUse, 8).
|
||||||
WillReturnError(fmt.Errorf("example error"))
|
WillReturnError(fmt.Errorf("example error"))
|
||||||
|
|
||||||
@@ -172,7 +171,7 @@ func TestErrPlans(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
str, mock := getDbMock(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).
|
WithArgs(8).
|
||||||
WillReturnError(fmt.Errorf("example error"))
|
WillReturnError(fmt.Errorf("example error"))
|
||||||
// function under test
|
// function under test
|
||||||
@@ -184,3 +183,82 @@ func TestErrPlans(t *testing.T) {
|
|||||||
t.Errorf("unfulfilled expectations: %s", err)
|
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