Compare commits
11 Commits
d37c53c60b
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
c3be0b8e54
|
|||
|
8ca7858069
|
|||
|
5e030a5bc3
|
|||
|
b47abbfc0b
|
|||
|
4905d18222
|
|||
|
eb8838ab75
|
|||
|
6ad0112683
|
|||
|
b9cea2347c
|
|||
|
e420bf303a
|
|||
|
92ddd9e0fe
|
|||
|
8c17aa9d6d
|
23
models/current_plan.go
Normal file
23
models/current_plan.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package models
|
||||
|
||||
// CurrentPlan represents the primary plan ID for a particular user ID.
|
||||
type CurrentPlan struct {
|
||||
UserID int64 `json:"user_id"`
|
||||
PlanID int64 `json:"plan_id"`
|
||||
}
|
||||
|
||||
// CurrentPlan returns the primary plan for a provided user ID in the given model.
|
||||
func (m *Model) CurrentPlan(userID int) (*CurrentPlan, error) {
|
||||
pp, err := m.SelectCurrentPlan(userID)
|
||||
return pp, wrapNotFound(err)
|
||||
}
|
||||
|
||||
// AddCurrentPlan inserts a given primary plan into the store, returning nothing.
|
||||
func (m *Model) AddCurrentPlan(pp *CurrentPlan, userID int) error {
|
||||
return m.InsertCurrentPlan(pp, userID)
|
||||
}
|
||||
|
||||
// SaveCurrentPlan saves and updates a primary plan.
|
||||
func (m *Model) SaveCurrentPlan(pp *CurrentPlan, userID int) error {
|
||||
return m.UpdateCurrentPlan(pp, userID)
|
||||
}
|
||||
45
models/current_plan_test.go
Normal file
45
models/current_plan_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package models_test
|
||||
|
||||
import (
|
||||
"gitea.deepak.science/deepak/gogmagog/models"
|
||||
"gitea.deepak.science/deepak/gogmagog/store"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestModelCurrentPlan(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
a1 := &models.Action{ActionID: 3}
|
||||
userID := 3
|
||||
p := &models.Plan{PlanID: 6}
|
||||
|
||||
str, _ := store.GetInMemoryStore()
|
||||
str.InsertAction(a1, userID)
|
||||
str.InsertPlan(p, userID)
|
||||
str.InsertPlan(p, userID)
|
||||
m := models.New(str)
|
||||
|
||||
_, err := m.CurrentPlan(userID)
|
||||
assert.NotNil(err)
|
||||
assert.True(models.IsNotFoundError(err))
|
||||
|
||||
err = m.AddCurrentPlan(&models.CurrentPlan{PlanID: 1}, userID)
|
||||
assert.Nil(err)
|
||||
|
||||
pp, err := m.CurrentPlan(userID)
|
||||
assert.Nil(err)
|
||||
assert.EqualValues(1, pp.PlanID)
|
||||
assert.EqualValues(userID, pp.UserID)
|
||||
|
||||
err = m.AddCurrentPlan(&models.CurrentPlan{PlanID: 2}, userID)
|
||||
assert.NotNil(err)
|
||||
|
||||
err = m.SaveCurrentPlan(&models.CurrentPlan{PlanID: 2}, userID)
|
||||
assert.Nil(err)
|
||||
|
||||
pp, err = m.CurrentPlan(userID)
|
||||
assert.Nil(err)
|
||||
assert.EqualValues(2, pp.PlanID)
|
||||
assert.EqualValues(userID, pp.UserID)
|
||||
|
||||
}
|
||||
@@ -14,9 +14,13 @@ 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)
|
||||
SelectCurrentPlan(userID int) (*CurrentPlan, error)
|
||||
InsertCurrentPlan(currentPlan *CurrentPlan, userID int) error
|
||||
UpdateCurrentPlan(currentPlan *CurrentPlan, userID int) error
|
||||
}
|
||||
|
||||
// Model represents a current model item.
|
||||
|
||||
@@ -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
|
||||
}`)
|
||||
|
||||
160
routes/current_plan.go
Normal file
160
routes/current_plan.go
Normal file
@@ -0,0 +1,160 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"gitea.deepak.science/deepak/gogmagog/models"
|
||||
"gitea.deepak.science/deepak/gogmagog/tokens"
|
||||
"github.com/go-chi/chi"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// NewCurrentPlanRouter returns a new primary plan router
|
||||
func NewCurrentPlanRouter(m *models.Model) http.Handler {
|
||||
router := chi.NewRouter()
|
||||
router.Get("/", getCurrentPlanFunc(m))
|
||||
router.Post("/", postCurrentPlanFunc(m))
|
||||
router.Put("/", putCurrentPlanFunc(m))
|
||||
return router
|
||||
}
|
||||
|
||||
func getCurrentPlanFunc(m *models.Model) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
userID, userErr := tokens.GetUserID(r.Context())
|
||||
if userErr != nil {
|
||||
unauthorizedHandler(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
pp, err := m.CurrentPlan(userID)
|
||||
if err != nil {
|
||||
if models.IsNotFoundError(err) {
|
||||
notFoundHandler(w, r)
|
||||
return
|
||||
}
|
||||
serverError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(w).Encode(pp); err != nil {
|
||||
serverError(w, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type createCurrentPlanResponse struct {
|
||||
CreatedCurrentPlan *models.CurrentPlan `json:"created_current_plan"`
|
||||
}
|
||||
|
||||
func postCurrentPlanFunc(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
|
||||
}
|
||||
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 1024)
|
||||
dec := json.NewDecoder(r.Body)
|
||||
dec.DisallowUnknownFields()
|
||||
var pp models.CurrentPlan
|
||||
err = dec.Decode(&pp)
|
||||
if err != nil {
|
||||
badRequestError(w, err)
|
||||
return
|
||||
}
|
||||
err = dec.Decode(&struct{}{})
|
||||
if err != io.EOF {
|
||||
badRequestError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
newPP := &models.CurrentPlan{
|
||||
PlanID: pp.PlanID,
|
||||
UserID: int64(userID),
|
||||
}
|
||||
err = m.AddCurrentPlan(newPP, userID)
|
||||
if err != nil {
|
||||
serverError(w, err)
|
||||
return
|
||||
}
|
||||
finishedPP, err := m.CurrentPlan(userID)
|
||||
if err != nil {
|
||||
serverError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
response := &createCurrentPlanResponse{
|
||||
CreatedCurrentPlan: finishedPP,
|
||||
}
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||
serverError(w, err)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
type updateCurrentPlanResponse struct {
|
||||
UpdatedCurrentPlan *models.CurrentPlan `json:"updated_current_plan"`
|
||||
}
|
||||
|
||||
func putCurrentPlanFunc(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
|
||||
}
|
||||
|
||||
_, err = m.CurrentPlan(userID)
|
||||
if models.IsNotFoundError(err) {
|
||||
notFoundHandler(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 1024)
|
||||
dec := json.NewDecoder(r.Body)
|
||||
dec.DisallowUnknownFields()
|
||||
var pp models.CurrentPlan
|
||||
err = dec.Decode(&pp)
|
||||
if err != nil {
|
||||
badRequestError(w, err)
|
||||
return
|
||||
}
|
||||
err = dec.Decode(&struct{}{})
|
||||
if err != io.EOF {
|
||||
badRequestError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
newPP := &models.CurrentPlan{
|
||||
PlanID: pp.PlanID,
|
||||
UserID: int64(userID),
|
||||
}
|
||||
err = m.SaveCurrentPlan(newPP, userID)
|
||||
if err != nil {
|
||||
serverError(w, err)
|
||||
return
|
||||
}
|
||||
newPP, err = m.CurrentPlan(userID)
|
||||
if err != nil {
|
||||
serverError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
response := &updateCurrentPlanResponse{
|
||||
UpdatedCurrentPlan: newPP,
|
||||
}
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||
serverError(w, err)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
228
routes/current_plan_post_test.go
Normal file
228
routes/current_plan_post_test.go
Normal file
@@ -0,0 +1,228 @@
|
||||
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"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPostSinglePlan(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getEmptyModel()
|
||||
router := routes.NewCurrentPlanRouter(m)
|
||||
|
||||
plan := &models.Plan{}
|
||||
m.AddPlan(plan, 3)
|
||||
|
||||
pp := &models.CurrentPlan{PlanID: 1}
|
||||
data, _ := json.Marshal(pp)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", 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)
|
||||
}
|
||||
|
||||
func TestPostCurrentPlanUnauth(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getEmptyModel()
|
||||
|
||||
plan := &models.Plan{}
|
||||
m.AddPlan(plan, 3)
|
||||
|
||||
pp := &models.CurrentPlan{PlanID: 1}
|
||||
data, _ := json.Marshal(pp)
|
||||
req, _ := http.NewRequestWithContext(context.Background(), "POST", "/", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
router := routes.NewCurrentPlanRouter(m)
|
||||
|
||||
// function under test
|
||||
router.ServeHTTP(rr, req)
|
||||
|
||||
// check results
|
||||
status := rr.Code
|
||||
assert.Equal(http.StatusUnauthorized, status)
|
||||
}
|
||||
|
||||
func TestExtraFieldJSONCurrentPlan(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getEmptyModel()
|
||||
|
||||
plan := &models.Plan{}
|
||||
m.AddPlan(plan, 3)
|
||||
|
||||
data := []byte(`{
|
||||
"plan_id": 5,
|
||||
"sabotage": "omg"
|
||||
}`)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
router := routes.NewCurrentPlanRouter(m)
|
||||
|
||||
// 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 TestEmptyBodyJSONCurrentPlan(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getEmptyModel()
|
||||
|
||||
plan := &models.Plan{}
|
||||
m.AddPlan(plan, 3)
|
||||
|
||||
data := []byte(``)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
router := routes.NewCurrentPlanRouter(m)
|
||||
|
||||
// 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 TestTwoBodyCurrentPlan(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getEmptyModel()
|
||||
|
||||
plan := &models.Plan{}
|
||||
m.AddPlan(plan, 3)
|
||||
|
||||
data := []byte(`{
|
||||
"plan_id": 5
|
||||
}, {
|
||||
"plan_id": 7
|
||||
}`)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
router := routes.NewCurrentPlanRouter(m)
|
||||
|
||||
// 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 TestErrorCreateCurrentPlan(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getErrorModel("error model")
|
||||
|
||||
plan := &models.Plan{}
|
||||
m.AddPlan(plan, 3)
|
||||
|
||||
data := []byte(`{
|
||||
"plan_id": 5
|
||||
}`)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
router := routes.NewCurrentPlanRouter(m)
|
||||
|
||||
// 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 TestErrorOnRetrieveCreateCurrentPlan(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getErrorOnGetModel("error model")
|
||||
|
||||
plan := &models.Plan{}
|
||||
m.AddPlan(plan, 3)
|
||||
|
||||
data := []byte(`{
|
||||
"plan_id": 5
|
||||
}`)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
router := routes.NewCurrentPlanRouter(m)
|
||||
|
||||
// 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 TestErrorWriterCreateCurrentPlan(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getEmptyModel()
|
||||
|
||||
plan := &models.Plan{}
|
||||
m.AddPlan(plan, 3)
|
||||
|
||||
data := []byte(`{
|
||||
"plan_id": 5
|
||||
}`)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := NewBadWriter()
|
||||
router := routes.NewCurrentPlanRouter(m)
|
||||
|
||||
// function under test
|
||||
router.ServeHTTP(rr, req)
|
||||
|
||||
// check results
|
||||
status := rr.Code
|
||||
assert.Equal(http.StatusInternalServerError, status)
|
||||
}
|
||||
269
routes/current_plan_put_test.go
Normal file
269
routes/current_plan_put_test.go
Normal file
@@ -0,0 +1,269 @@
|
||||
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"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPutSinglePlanNotFound(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getEmptyModel()
|
||||
router := routes.NewCurrentPlanRouter(m)
|
||||
|
||||
plan := &models.Plan{}
|
||||
m.AddPlan(plan, 3)
|
||||
m.AddPlan(plan, 3)
|
||||
|
||||
pp := &models.CurrentPlan{PlanID: 1}
|
||||
data, _ := json.Marshal(pp)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/", 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)
|
||||
}
|
||||
|
||||
func TestPutSinglePlanNonDup(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getEmptyModel()
|
||||
router := routes.NewCurrentPlanRouter(m)
|
||||
|
||||
plan := &models.Plan{}
|
||||
m.AddPlan(plan, 3)
|
||||
m.AddPlan(plan, 3)
|
||||
m.AddCurrentPlan(&models.CurrentPlan{PlanID: 1}, 3)
|
||||
|
||||
pp := &models.CurrentPlan{PlanID: 2}
|
||||
data, _ := json.Marshal(pp)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/", 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)
|
||||
}
|
||||
|
||||
func TestPutCurrentPlanUnauth(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getEmptyModel()
|
||||
|
||||
plan := &models.Plan{}
|
||||
m.AddPlan(plan, 3)
|
||||
m.AddPlan(plan, 3)
|
||||
m.AddCurrentPlan(&models.CurrentPlan{PlanID: 1}, 3)
|
||||
|
||||
pp := &models.CurrentPlan{PlanID: 1}
|
||||
data, _ := json.Marshal(pp)
|
||||
req, _ := http.NewRequestWithContext(context.Background(), "PUT", "/", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
router := routes.NewCurrentPlanRouter(m)
|
||||
|
||||
// function under test
|
||||
router.ServeHTTP(rr, req)
|
||||
|
||||
// check results
|
||||
status := rr.Code
|
||||
assert.Equal(http.StatusUnauthorized, status)
|
||||
}
|
||||
|
||||
func TestExtraFieldJSONPutCurrentPlan(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getEmptyModel()
|
||||
|
||||
plan := &models.Plan{}
|
||||
m.AddPlan(plan, 3)
|
||||
m.AddPlan(plan, 3)
|
||||
m.AddCurrentPlan(&models.CurrentPlan{PlanID: 1}, 3)
|
||||
|
||||
data := []byte(`{
|
||||
"plan_id": 5,
|
||||
"sabotage": "omg"
|
||||
}`)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
router := routes.NewCurrentPlanRouter(m)
|
||||
|
||||
// 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 TestEmptyBodyJSONPutCurrentPlan(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getEmptyModel()
|
||||
|
||||
plan := &models.Plan{}
|
||||
m.AddPlan(plan, 3)
|
||||
m.AddPlan(plan, 3)
|
||||
m.AddCurrentPlan(&models.CurrentPlan{PlanID: 1}, 3)
|
||||
|
||||
data := []byte(``)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
router := routes.NewCurrentPlanRouter(m)
|
||||
|
||||
// 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 TestTwoBodyPutCurrentPlan(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getEmptyModel()
|
||||
|
||||
plan := &models.Plan{}
|
||||
m.AddPlan(plan, 3)
|
||||
m.AddPlan(plan, 3)
|
||||
m.AddCurrentPlan(&models.CurrentPlan{PlanID: 1}, 3)
|
||||
|
||||
data := []byte(`{
|
||||
"plan_id": 5
|
||||
}, {
|
||||
"plan_id": 7
|
||||
}`)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
router := routes.NewCurrentPlanRouter(m)
|
||||
|
||||
// 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 TestErrorPutCurrentPlan(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getErrorModel("error model")
|
||||
|
||||
plan := &models.Plan{}
|
||||
m.AddPlan(plan, 3)
|
||||
m.AddPlan(plan, 3)
|
||||
m.AddCurrentPlan(&models.CurrentPlan{PlanID: 1}, 3)
|
||||
|
||||
data := []byte(`{
|
||||
"plan_id": 5
|
||||
}`)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
router := routes.NewCurrentPlanRouter(m)
|
||||
|
||||
// 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 TestErrorOnRetrievePutCurrentPlan(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getErrorOnGetModel("error model")
|
||||
|
||||
plan := &models.Plan{}
|
||||
m.AddPlan(plan, 3)
|
||||
m.AddPlan(plan, 3)
|
||||
m.AddCurrentPlan(&models.CurrentPlan{PlanID: 1}, 3)
|
||||
|
||||
data := []byte(`{
|
||||
"plan_id": 5
|
||||
}`)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
router := routes.NewCurrentPlanRouter(m)
|
||||
|
||||
// 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 TestErrorWriterPutCurrentPlan(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getEmptyModel()
|
||||
|
||||
plan := &models.Plan{}
|
||||
m.AddPlan(plan, 3)
|
||||
m.AddPlan(plan, 3)
|
||||
m.AddCurrentPlan(&models.CurrentPlan{PlanID: 1}, 3)
|
||||
|
||||
data := []byte(`{
|
||||
"plan_id": 5
|
||||
}`)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := NewBadWriter()
|
||||
router := routes.NewCurrentPlanRouter(m)
|
||||
|
||||
// function under test
|
||||
router.ServeHTTP(rr, req)
|
||||
|
||||
// check results
|
||||
status := rr.Code
|
||||
assert.Equal(http.StatusInternalServerError, status)
|
||||
}
|
||||
117
routes/current_plan_test.go
Normal file
117
routes/current_plan_test.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package routes_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"gitea.deepak.science/deepak/gogmagog/models"
|
||||
"gitea.deepak.science/deepak/gogmagog/routes"
|
||||
// "gitea.deepak.science/deepak/gogmagog/tokens"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEmptyCurrentPlan(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getEmptyModel()
|
||||
router := routes.NewCurrentPlanRouter(m)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/", nil)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
// function under test
|
||||
router.ServeHTTP(rr, req)
|
||||
|
||||
// check results
|
||||
status := rr.Code
|
||||
assert.Equal(http.StatusNotFound, status)
|
||||
}
|
||||
|
||||
func TestEmptyCurrentPlanUnauth(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getEmptyModel()
|
||||
router := routes.NewCurrentPlanRouter(m)
|
||||
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/", nil)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
// function under test
|
||||
router.ServeHTTP(rr, req)
|
||||
|
||||
// check results
|
||||
status := rr.Code
|
||||
assert.Equal(http.StatusUnauthorized, status)
|
||||
}
|
||||
|
||||
func TestErrorCurrentPlan(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
|
||||
m := getErrorModel("Model always errors")
|
||||
|
||||
router := routes.NewCurrentPlanRouter(m)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/", nil)
|
||||
|
||||
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 TestEmptyCurrentPlanErrorWriter(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
|
||||
m := getEmptyModel()
|
||||
m.AddCurrentPlan(&models.CurrentPlan{PlanID: 3}, 3)
|
||||
|
||||
router := routes.NewCurrentPlanRouter(m)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/", nil)
|
||||
|
||||
rr := NewBadWriter()
|
||||
|
||||
// function under test
|
||||
router.ServeHTTP(rr, req)
|
||||
|
||||
// check results
|
||||
status := rr.Code
|
||||
assert.Equal(http.StatusInternalServerError, status)
|
||||
|
||||
}
|
||||
|
||||
func TestSingleCurrentPlan(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getEmptyModel()
|
||||
m.AddCurrentPlan(&models.CurrentPlan{PlanID: 3}, 3)
|
||||
|
||||
router := routes.NewCurrentPlanRouter(m)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/", nil)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
// function under test
|
||||
router.ServeHTTP(rr, req)
|
||||
|
||||
// check results
|
||||
status := rr.Code
|
||||
assert.Equal(http.StatusOK, status)
|
||||
// We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp.
|
||||
expected := `{
|
||||
"plan_id": 3,
|
||||
"user_id": 3
|
||||
}`
|
||||
assert.JSONEq(expected, rr.Body.String())
|
||||
contentType := rr.Header().Get("Content-Type")
|
||||
assert.Equal("application/json", contentType)
|
||||
}
|
||||
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)
|
||||
|
||||
@@ -18,6 +18,7 @@ func NewRouter(m *models.Model, tok tokens.Toker) http.Handler {
|
||||
r.Mount("/actions", NewActionRouter(m))
|
||||
r.Mount("/plans", NewPlanRouter(m))
|
||||
r.Mount("/me", NewCurrentUserRouter(m))
|
||||
r.Mount("/currentPlan", NewCurrentPlanRouter(m))
|
||||
})
|
||||
router.Mount("/auth", NewAuthRouter(m, tok))
|
||||
router.Mount("/health", newHealthRouter(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
|
||||
}
|
||||
@@ -57,6 +64,24 @@ func (e *errorStore) InsertUser(user *models.User) (int, error) {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (e *errorStore) SelectCurrentPlan(userID int) (*models.CurrentPlan, error) {
|
||||
return nil, e.error
|
||||
}
|
||||
|
||||
func (e *errorStore) InsertCurrentPlan(currentPlan *models.CurrentPlan, userID int) error {
|
||||
if e.errorOnInsert {
|
||||
return e.error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *errorStore) UpdateCurrentPlan(currentPlan *models.CurrentPlan, userID int) error {
|
||||
if e.errorOnInsert {
|
||||
return e.error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *errorStore) ConnectionLive() error {
|
||||
return 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)
|
||||
|
||||
@@ -82,3 +88,25 @@ func TestErrorUserMethods(t *testing.T) {
|
||||
_, err = str.SelectUserByUsername("snth")
|
||||
assert.NotNil(err)
|
||||
}
|
||||
|
||||
func TestErrorCurrentPlanMethods(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
str := store.GetErrorStore("error", true)
|
||||
str2 := store.GetErrorStore("error", false)
|
||||
|
||||
cp := &models.CurrentPlan{}
|
||||
|
||||
_, err := str.SelectCurrentPlan(1)
|
||||
assert.NotNil(err)
|
||||
|
||||
err = str.InsertCurrentPlan(cp, 1)
|
||||
assert.NotNil(err)
|
||||
err = str2.InsertCurrentPlan(cp, 1)
|
||||
assert.Nil(err)
|
||||
|
||||
replace := &models.CurrentPlan{}
|
||||
err = str.UpdateCurrentPlan(replace, 1)
|
||||
assert.NotNil(err)
|
||||
err = str2.UpdateCurrentPlan(replace, 1)
|
||||
assert.Nil(err)
|
||||
}
|
||||
|
||||
@@ -2,13 +2,15 @@ package store
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"gitea.deepak.science/deepak/gogmagog/models"
|
||||
)
|
||||
|
||||
type inMemoryStore struct {
|
||||
actions []*models.Action
|
||||
plans []*models.Plan
|
||||
users []*models.User
|
||||
actions []*models.Action
|
||||
plans []*models.Plan
|
||||
users []*models.User
|
||||
currentPlans []*models.CurrentPlan
|
||||
}
|
||||
|
||||
// GetInMemoryStore provides a purely in memory store, for testing purposes only, with no persistence.
|
||||
@@ -98,6 +100,48 @@ 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) {
|
||||
return currentPlan, nil
|
||||
}
|
||||
}
|
||||
return nil, sql.ErrNoRows
|
||||
}
|
||||
|
||||
func (store *inMemoryStore) InsertCurrentPlan(currentPlan *models.CurrentPlan, userID int) error {
|
||||
_, err := store.SelectCurrentPlan(userID)
|
||||
if err == nil {
|
||||
return fmt.Errorf("Can't insert primary plan")
|
||||
}
|
||||
// actually impossible, but at this point it must be a not found error.
|
||||
// if err != sql.ErrNoRows {
|
||||
// return err
|
||||
// }
|
||||
|
||||
store.currentPlans = append(store.currentPlans, &models.CurrentPlan{PlanID: int64(currentPlan.PlanID), UserID: int64(userID)})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *inMemoryStore) UpdateCurrentPlan(currentPlan *models.CurrentPlan, userID int) error {
|
||||
current, err := store.SelectCurrentPlan(userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
current.PlanID = currentPlan.PlanID
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *inMemoryStore) ConnectionLive() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
@@ -105,3 +115,37 @@ func TestInMemoryUserMethods(t *testing.T) {
|
||||
_, err = str.SelectUserByUsername("bad username")
|
||||
assert.NotNil(err)
|
||||
}
|
||||
|
||||
func TestInMemoryCurrentPlanMethods(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
str, _ := store.GetInMemoryStore()
|
||||
|
||||
userID := 10
|
||||
|
||||
cp1 := &models.CurrentPlan{PlanID: 1}
|
||||
cp2 := &models.CurrentPlan{PlanID: 2}
|
||||
|
||||
err := str.UpdateCurrentPlan(cp1, userID)
|
||||
assert.NotNil(err)
|
||||
|
||||
err = str.InsertCurrentPlan(cp1, userID)
|
||||
assert.Nil(err)
|
||||
|
||||
receivedCp, err := str.SelectCurrentPlan(userID)
|
||||
assert.Nil(err)
|
||||
assert.EqualValues(1, receivedCp.PlanID)
|
||||
|
||||
_, err = str.SelectCurrentPlan(userID + 1)
|
||||
assert.NotNil(err)
|
||||
|
||||
str.InsertCurrentPlan(cp2, userID)
|
||||
assert.NotNil(err)
|
||||
|
||||
err = str.UpdateCurrentPlan(cp2, userID)
|
||||
assert.Nil(err)
|
||||
|
||||
receivedCp, err = str.SelectCurrentPlan(userID)
|
||||
assert.Nil(err)
|
||||
assert.EqualValues(2, receivedCp.PlanID)
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
DROP TABLE IF EXISTS actions;
|
||||
DROP TABLE IF EXISTS user_current_plan;
|
||||
DROP TABLE IF EXISTS plans;
|
||||
DROP TABLE IF EXISTS users;
|
||||
|
||||
|
||||
@@ -9,10 +9,17 @@ 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
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
UNIQUE (user_id, plan_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS user_current_plan(
|
||||
user_id int PRIMARY KEY,
|
||||
plan_id int,
|
||||
FOREIGN KEY (user_id, plan_id) REFERENCES plans(user_id, plan_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS actions(
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
@@ -174,3 +198,51 @@ func (store *postgresStore) InsertUser(user *models.User) (int, error) {
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (store *postgresStore) SelectCurrentPlan(userID int) (*models.CurrentPlan, error) {
|
||||
pp := models.CurrentPlan{}
|
||||
queryString := store.db.Rebind(`SELECT user_id, plan_id FROM user_current_plan WHERE user_id = ?`)
|
||||
err := store.db.Get(&pp, queryString, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pp, nil
|
||||
}
|
||||
|
||||
func (store *postgresStore) InsertCurrentPlan(currentPlan *models.CurrentPlan, userID int) error {
|
||||
queryString := store.db.Rebind("INSERT INTO user_current_plan (user_id, plan_id) VALUES (?, ?) RETURNING user_id")
|
||||
tx := store.db.MustBegin()
|
||||
var id int
|
||||
err := tx.Get(&id, queryString, userID, currentPlan.PlanID)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *postgresStore) UpdateCurrentPlan(currentPlan *models.CurrentPlan, userID int) error {
|
||||
query := `UPDATE user_current_plan SET
|
||||
plan_id = :plan_id
|
||||
WHERE user_id = :user_id`
|
||||
tx := store.db.MustBegin()
|
||||
ppToUse := &models.CurrentPlan{
|
||||
PlanID: currentPlan.PlanID,
|
||||
UserID: int64(userID),
|
||||
}
|
||||
_, err := store.db.NamedExec(query, ppToUse)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
217
store/postgres_current_plan_test.go
Normal file
217
store/postgres_current_plan_test.go
Normal file
@@ -0,0 +1,217 @@
|
||||
package store_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gitea.deepak.science/deepak/gogmagog/models"
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSelectCurrentPlan(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
planIDToUse := 1
|
||||
userIDToUse := 2
|
||||
|
||||
str, mock := getDbMock(t)
|
||||
|
||||
rows := sqlmock.NewRows([]string{"plan_id", "user_id"}).AddRow(planIDToUse, userIDToUse)
|
||||
mock.ExpectQuery(`^SELECT user_id, plan_id FROM user_current_plan WHERE user_id = \$1`).
|
||||
WithArgs(userIDToUse).
|
||||
WillReturnRows(rows)
|
||||
|
||||
currPlan, err := str.SelectCurrentPlan(userIDToUse)
|
||||
assert.Nil(err)
|
||||
assert.EqualValues(planIDToUse, currPlan.PlanID)
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unfulfilled expectations: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelectCurrentPlanErr(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
userIDToUse := 2
|
||||
|
||||
str, mock := getDbMock(t)
|
||||
|
||||
mock.ExpectQuery(`^SELECT user_id, plan_id FROM user_current_plan WHERE user_id = \$1`).
|
||||
WithArgs(userIDToUse).
|
||||
WillReturnError(fmt.Errorf("example error"))
|
||||
|
||||
currPlan, err := str.SelectCurrentPlan(userIDToUse)
|
||||
assert.NotNil(err)
|
||||
assert.Nil(currPlan)
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unfulfilled expectations: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertCurrentPlan(t *testing.T) {
|
||||
// setup
|
||||
assert := assert.New(t)
|
||||
|
||||
str, mock := getDbMock(t)
|
||||
planID := 1
|
||||
userID := 2
|
||||
badUserID := 7
|
||||
|
||||
cp := &models.CurrentPlan{PlanID: int64(planID), UserID: int64(badUserID)}
|
||||
|
||||
rows := sqlmock.NewRows([]string{"userID"}).AddRow(userID)
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectQuery(`^INSERT INTO user_current_plan \(user_id, plan_id\) VALUES \(\$1, \$2\) RETURNING user_id$`).
|
||||
WithArgs(userID, planID).
|
||||
WillReturnRows(rows)
|
||||
mock.ExpectCommit()
|
||||
|
||||
// function under test
|
||||
err := str.InsertCurrentPlan(cp, userID)
|
||||
// check results
|
||||
assert.Nil(err)
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unfulfilled expectations: %s", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestInsertCurrentPlanErr(t *testing.T) {
|
||||
// setup
|
||||
assert := assert.New(t)
|
||||
|
||||
str, mock := getDbMock(t)
|
||||
userID := 2
|
||||
badUserID := 7
|
||||
planID := 1
|
||||
cp := &models.CurrentPlan{PlanID: int64(planID), UserID: int64(badUserID)}
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectQuery(`^INSERT INTO user_current_plan \(user_id, plan_id\) VALUES \(\$1, \$2\) RETURNING user_id$`).
|
||||
WithArgs(userID, planID).
|
||||
WillReturnError(fmt.Errorf("example error"))
|
||||
mock.ExpectRollback()
|
||||
|
||||
// function under test
|
||||
err := str.InsertCurrentPlan(cp, userID)
|
||||
// check results
|
||||
assert.NotNil(err)
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unfulfilled expectations: %s", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestInsertCurrentPlanCommitErr(t *testing.T) {
|
||||
// setup
|
||||
assert := assert.New(t)
|
||||
|
||||
str, mock := getDbMock(t)
|
||||
planID := 1
|
||||
userID := 2
|
||||
cp := &models.CurrentPlan{PlanID: int64(planID), UserID: int64(userID)}
|
||||
|
||||
rows := sqlmock.NewRows([]string{"user_id"}).AddRow(userID)
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectQuery(`^INSERT INTO user_current_plan \(user_id, plan_id\) VALUES \(\$1, \$2\) RETURNING user_id$`).
|
||||
WithArgs(userID, planID).
|
||||
WillReturnRows(rows)
|
||||
mock.ExpectCommit().WillReturnError(fmt.Errorf("another error example"))
|
||||
|
||||
// function under test
|
||||
err := str.InsertCurrentPlan(cp, userID)
|
||||
// check results
|
||||
assert.NotNil(err)
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unfulfilled expectations: %s", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestUpdateCurrentPlan(t *testing.T) {
|
||||
// setup
|
||||
assert := assert.New(t)
|
||||
|
||||
str, mock := getDbMock(t)
|
||||
|
||||
userIDToUse := 1
|
||||
planIDToUse := 2
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec(`
|
||||
UPDATE user_current_plan SET
|
||||
plan_id = \$1
|
||||
WHERE user_id = \$2`).
|
||||
WithArgs(planIDToUse, userIDToUse).
|
||||
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit()
|
||||
|
||||
// function under test
|
||||
err := str.UpdateCurrentPlan(&models.CurrentPlan{PlanID: int64(planIDToUse)}, userIDToUse)
|
||||
// check results
|
||||
assert.Nil(err)
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unfulfilled expectations: %s", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestUpdateCurrentPlanErr(t *testing.T) {
|
||||
// setup
|
||||
assert := assert.New(t)
|
||||
|
||||
str, mock := getDbMock(t)
|
||||
|
||||
userIDToUse := 1
|
||||
planIDToUse := 2
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec(`
|
||||
UPDATE user_current_plan SET
|
||||
plan_id = \$1
|
||||
WHERE user_id = \$2`).
|
||||
WithArgs(planIDToUse, userIDToUse).
|
||||
WillReturnError(fmt.Errorf("example error"))
|
||||
mock.ExpectRollback()
|
||||
|
||||
// function under test
|
||||
err := str.UpdateCurrentPlan(&models.CurrentPlan{PlanID: int64(planIDToUse)}, userIDToUse)
|
||||
// check results
|
||||
assert.NotNil(err)
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unfulfilled expectations: %s", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestUpdateCurrentPlanCommitErr(t *testing.T) {
|
||||
// setup
|
||||
assert := assert.New(t)
|
||||
|
||||
str, mock := getDbMock(t)
|
||||
|
||||
userIDToUse := 1
|
||||
planIDToUse := 2
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec(`
|
||||
UPDATE user_current_plan SET
|
||||
plan_id = \$1
|
||||
WHERE user_id = \$2`).
|
||||
WithArgs(planIDToUse, userIDToUse).
|
||||
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit().WillReturnError(fmt.Errorf("another error example"))
|
||||
|
||||
// function under test
|
||||
err := str.UpdateCurrentPlan(&models.CurrentPlan{PlanID: int64(planIDToUse)}, userIDToUse)
|
||||
// check results
|
||||
assert.NotNil(err)
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unfulfilled expectations: %s", err)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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