Adds user id to actions
This commit is contained in:
parent
12515c2a1e
commit
2465b0a73a
@ -8,6 +8,7 @@ import (
|
||||
type Action struct {
|
||||
ActionID int64 `json:"action_id"`
|
||||
ActionDescription string `json:"action_description"`
|
||||
UserID int64 `json:"user_id"`
|
||||
EstimatedChunks int `json:"estimated_chunks"`
|
||||
CompletedChunks int `json:"completed_chunks"`
|
||||
CompletedOn *time.Time `json:"completed_on,omitempty"`
|
||||
@ -17,22 +18,22 @@ type Action struct {
|
||||
}
|
||||
|
||||
// Actions returns all actions from the model.
|
||||
func (m *Model) Actions() ([]*Action, error) {
|
||||
return m.SelectActions()
|
||||
func (m *Model) Actions(userID int) ([]*Action, error) {
|
||||
return m.SelectActions(userID)
|
||||
}
|
||||
|
||||
// Action returns a single action from its ID
|
||||
func (m *Model) Action(id int) (*Action, error) {
|
||||
act, err := m.SelectActionByID(id)
|
||||
func (m *Model) Action(id int, userID int) (*Action, error) {
|
||||
act, err := m.SelectActionByID(id, userID)
|
||||
return act, wrapNotFound(err)
|
||||
}
|
||||
|
||||
// AddAction inserts a given action into the store, returning the generated ActionID. The provided ActionID is ignored.
|
||||
func (m *Model) AddAction(action *Action) (int, error) {
|
||||
return m.InsertAction(action)
|
||||
func (m *Model) AddAction(action *Action, userID int) (int, error) {
|
||||
return m.InsertAction(action, userID)
|
||||
}
|
||||
|
||||
// SaveAction saves and updates an action.
|
||||
func (m *Model) SaveAction(action *Action) error {
|
||||
return m.UpdateAction(action)
|
||||
func (m *Model) SaveAction(action *Action, userID int) error {
|
||||
return m.UpdateAction(action, userID)
|
||||
}
|
||||
|
@ -37,7 +37,7 @@ func TestErrorModelWrapping(t *testing.T) {
|
||||
|
||||
_, err := m.Plan(0, 0)
|
||||
assert.True(models.IsNotFoundError(err))
|
||||
_, err = m.Action(0)
|
||||
_, err = m.Action(0, 0)
|
||||
assert.True(models.IsNotFoundError(err))
|
||||
}
|
||||
func TestErrorModelInvalidLogin(t *testing.T) {
|
||||
|
@ -7,14 +7,14 @@ import (
|
||||
// Store represents the backing store.
|
||||
type Store interface {
|
||||
ConnectionLive() error
|
||||
SelectActions() ([]*Action, error)
|
||||
SelectActionByID(id int) (*Action, error)
|
||||
InsertAction(action *Action) (int, error)
|
||||
UpdateAction(action *Action) error
|
||||
SelectActions(userID int) ([]*Action, error)
|
||||
SelectActionByID(id int, userID int) (*Action, error)
|
||||
InsertAction(action *Action, userID int) (int, error)
|
||||
UpdateAction(action *Action, userID int) error
|
||||
SelectPlans(userID int) ([]*Plan, error)
|
||||
SelectPlanByID(id int, userID int) (*Plan, error)
|
||||
InsertPlan(plan *Plan, userID int) (int, error)
|
||||
SelectActionsByPlanID(plan *Plan) ([]*Action, error)
|
||||
SelectActionsByPlanID(plan *Plan, userID int) ([]*Action, error)
|
||||
SelectUserByUsername(username string) (*User, error)
|
||||
InsertUser(user *User) (int, error)
|
||||
}
|
||||
|
@ -15,23 +15,23 @@ func TestModelActions(t *testing.T) {
|
||||
p := &models.Plan{PlanID: 6}
|
||||
|
||||
str, _ := store.GetInMemoryStore()
|
||||
str.InsertAction(a1)
|
||||
str.InsertAction(a1, userID)
|
||||
str.InsertPlan(p, userID)
|
||||
m := models.New(str)
|
||||
|
||||
actions, err := m.Actions()
|
||||
actions, err := m.Actions(userID)
|
||||
assert.Nil(err)
|
||||
assert.Equal(1, len(actions))
|
||||
|
||||
firstAction, err := m.Action(1)
|
||||
firstAction, err := m.Action(1, userID)
|
||||
assert.Nil(err)
|
||||
assert.EqualValues(1, firstAction.ActionID)
|
||||
|
||||
actionID, err := m.AddAction(a2)
|
||||
actionID, err := m.AddAction(a2, userID)
|
||||
assert.Nil(err)
|
||||
assert.EqualValues(2, actionID)
|
||||
|
||||
err = m.SaveAction(a1)
|
||||
err = m.SaveAction(a1, userID)
|
||||
assert.Nil(err)
|
||||
}
|
||||
|
||||
@ -44,8 +44,8 @@ func TestModelPlanMethods(t *testing.T) {
|
||||
|
||||
str, _ := store.GetInMemoryStore()
|
||||
str.InsertPlan(p, userID)
|
||||
str.InsertAction(a1)
|
||||
str.InsertAction(a2)
|
||||
str.InsertAction(a1, userID)
|
||||
str.InsertAction(a2, userID)
|
||||
m := models.New(str)
|
||||
|
||||
plans, err := m.Plans(userID)
|
||||
@ -56,7 +56,7 @@ func TestModelPlanMethods(t *testing.T) {
|
||||
assert.Nil(err)
|
||||
assert.EqualValues(1, firstPlan.PlanID)
|
||||
|
||||
actions, err := m.GetActions(firstPlan)
|
||||
actions, err := m.GetActions(firstPlan, userID)
|
||||
assert.Nil(err)
|
||||
assert.Equal(1, len(actions))
|
||||
|
||||
|
@ -28,6 +28,6 @@ func (m *Model) AddPlan(plan *Plan, userID int) (int, error) {
|
||||
}
|
||||
|
||||
// GetActions returns the actions associated with a particular plan.
|
||||
func (m *Model) GetActions(plan *Plan) ([]*Action, error) {
|
||||
return m.SelectActionsByPlanID(plan)
|
||||
func (m *Model) GetActions(plan *Plan, userID int) ([]*Action, error) {
|
||||
return m.SelectActionsByPlanID(plan, userID)
|
||||
}
|
||||
|
@ -20,8 +20,8 @@ func TestModelUsers(t *testing.T) {
|
||||
user1 := &models.User{Username: username, DisplayName: "Ted Est", Password: []byte("$2y$05$6SVV35GX4cB4PDPhRaDD/exsL.HV8QtMMr60YL6dLyqtX4l58q.cy")}
|
||||
str, _ := store.GetInMemoryStore()
|
||||
str.InsertPlan(p, 3)
|
||||
str.InsertAction(a1)
|
||||
str.InsertAction(a2)
|
||||
str.InsertAction(a1, 3)
|
||||
str.InsertAction(a2, 3)
|
||||
str.InsertUser(user1)
|
||||
m := models.New(str)
|
||||
|
||||
|
@ -3,6 +3,7 @@ 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"
|
||||
@ -21,13 +22,19 @@ func NewActionRouter(m *models.Model) http.Handler {
|
||||
|
||||
func getActionsFunc(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
|
||||
}
|
||||
|
||||
var (
|
||||
actions []*models.Action
|
||||
err error
|
||||
)
|
||||
planIDString := r.URL.Query().Get("plan_id")
|
||||
if planIDString == "" {
|
||||
actions, err = m.Actions()
|
||||
actions, err = m.Actions(userID)
|
||||
} else {
|
||||
planID, convErr := strconv.ParseInt(planIDString, 10, 64)
|
||||
if convErr != nil {
|
||||
@ -35,7 +42,7 @@ func getActionsFunc(m *models.Model) http.HandlerFunc {
|
||||
err = nil
|
||||
} else {
|
||||
plan := &models.Plan{PlanID: planID}
|
||||
actions, err = m.GetActions(plan)
|
||||
actions, err = m.GetActions(plan, userID)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
@ -51,12 +58,20 @@ func getActionsFunc(m *models.Model) http.HandlerFunc {
|
||||
|
||||
func getActionByIDFunc(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
|
||||
}
|
||||
|
||||
id, err := strconv.Atoi(chi.URLParam(r, "actionid"))
|
||||
if err != nil {
|
||||
notFoundHandler(w, r)
|
||||
return
|
||||
}
|
||||
action, err := m.Action(id)
|
||||
|
||||
action, err := m.Action(id, userID)
|
||||
if err != nil {
|
||||
if models.IsNotFoundError(err) {
|
||||
notFoundHandler(w, r)
|
||||
@ -81,11 +96,17 @@ type createActionResponse struct {
|
||||
func postActionFunc(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 a models.Action
|
||||
err := dec.Decode(&a)
|
||||
err = dec.Decode(&a)
|
||||
if err != nil {
|
||||
badRequestError(w, err)
|
||||
return
|
||||
@ -103,12 +124,12 @@ func postActionFunc(m *models.Model) http.HandlerFunc {
|
||||
CompletedOn: a.CompletedOn,
|
||||
PlanID: a.PlanID,
|
||||
}
|
||||
id, err := m.AddAction(action)
|
||||
id, err := m.AddAction(action, userID)
|
||||
if err != nil {
|
||||
serverError(w, err)
|
||||
return
|
||||
}
|
||||
action, err = m.Action(id)
|
||||
action, err = m.Action(id, userID)
|
||||
if err != nil {
|
||||
serverError(w, err)
|
||||
return
|
||||
@ -134,6 +155,13 @@ type updateActionResponse struct {
|
||||
|
||||
func putActionFunc(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, "actionid"))
|
||||
if err != nil {
|
||||
notFoundHandler(w, r)
|
||||
@ -162,12 +190,12 @@ func putActionFunc(m *models.Model) http.HandlerFunc {
|
||||
PlanID: a.PlanID,
|
||||
ActionID: int64(id),
|
||||
}
|
||||
err = m.SaveAction(action)
|
||||
err = m.SaveAction(action, userID)
|
||||
if err != nil {
|
||||
serverError(w, err)
|
||||
return
|
||||
}
|
||||
action, err = m.Action(id)
|
||||
action, err = m.Action(id, userID)
|
||||
if err != nil {
|
||||
serverError(w, err)
|
||||
return
|
||||
|
@ -16,7 +16,7 @@ func TestEmptyActions(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
m := getEmptyModel()
|
||||
router := routes.NewActionRouter(m)
|
||||
req, _ := http.NewRequest("GET", "/", nil)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/", nil)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
@ -41,10 +41,10 @@ func TestOneAction(t *testing.T) {
|
||||
completedDate, _ := time.Parse("2006-01-02", "2021-01-03")
|
||||
a1 := &models.Action{ActionID: 3, ActionDescription: "testing", CompletedChunks: 1, CompletedOn: &completedDate, CreatedAt: &createdDate, UpdatedAt: &updatedDate, EstimatedChunks: 3, PlanID: 0}
|
||||
m := getEmptyModel()
|
||||
m.AddAction(a1)
|
||||
m.AddAction(a1, 3)
|
||||
|
||||
router := routes.NewActionRouter(m)
|
||||
req, _ := http.NewRequest("GET", "/", nil)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/", nil)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
@ -59,6 +59,7 @@ func TestOneAction(t *testing.T) {
|
||||
{
|
||||
"action_id": 1,
|
||||
"action_description": "testing",
|
||||
"user_id": 3,
|
||||
"estimated_chunks": 3,
|
||||
"completed_chunks": 1,
|
||||
"completed_on": "2021-01-03T00:00:00Z",
|
||||
@ -79,7 +80,7 @@ func TestErrorAction(t *testing.T) {
|
||||
m := getErrorModel("Model always errors")
|
||||
|
||||
router := routes.NewActionRouter(m)
|
||||
req, _ := http.NewRequest("GET", "/", nil)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/", nil)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
@ -101,7 +102,7 @@ func TestEmptyActionErrorWriter(t *testing.T) {
|
||||
m := getEmptyModel()
|
||||
|
||||
router := routes.NewActionRouter(m)
|
||||
req, _ := http.NewRequest("GET", "/", nil)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/", nil)
|
||||
|
||||
rr := NewBadWriter()
|
||||
|
||||
@ -121,9 +122,9 @@ func TestOneActionByID(t *testing.T) {
|
||||
updatedDate, _ := time.Parse("2006-01-02", "2021-01-02")
|
||||
a := &models.Action{ActionID: 6, ActionDescription: "howdy", CompletedOn: nil, CreatedAt: &createdDate, UpdatedAt: &updatedDate, CompletedChunks: 0, EstimatedChunks: 54, PlanID: 3}
|
||||
m := getEmptyModel()
|
||||
m.InsertAction(a)
|
||||
m.InsertAction(a, 3)
|
||||
router := routes.NewActionRouter(m)
|
||||
req, _ := http.NewRequest("GET", "/1", nil)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/1", nil)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
@ -137,6 +138,7 @@ func TestOneActionByID(t *testing.T) {
|
||||
expected := `{
|
||||
"action_id": 1,
|
||||
"action_description": "howdy",
|
||||
"user_id": 3,
|
||||
"estimated_chunks": 54,
|
||||
"completed_chunks": 0,
|
||||
"updated_at": "2021-01-02T00:00:00Z",
|
||||
@ -155,7 +157,7 @@ func TestErrorActionByID(t *testing.T) {
|
||||
m := getErrorModel("Model always errors")
|
||||
|
||||
router := routes.NewActionRouter(m)
|
||||
req, _ := http.NewRequest("GET", "/5", nil)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/5", nil)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
@ -176,10 +178,10 @@ func TestEmptyActionErrorWriterByID(t *testing.T) {
|
||||
|
||||
a := &models.Action{ActionID: 6}
|
||||
m := getEmptyModel()
|
||||
m.AddAction(a)
|
||||
m.AddAction(a, 3)
|
||||
|
||||
router := routes.NewActionRouter(m)
|
||||
req, _ := http.NewRequest("GET", "/1", nil)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/1", nil)
|
||||
|
||||
rr := NewBadWriter()
|
||||
|
||||
@ -199,7 +201,7 @@ func TestNotFoundActionByIDText(t *testing.T) {
|
||||
m := getEmptyModel()
|
||||
|
||||
router := routes.NewActionRouter(m)
|
||||
req, _ := http.NewRequest("GET", "/wo", nil)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/wo", nil)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
@ -218,7 +220,7 @@ func TestNotFoundActionByIDEmpty(t *testing.T) {
|
||||
m := getEmptyModel()
|
||||
|
||||
router := routes.NewActionRouter(m)
|
||||
req, _ := http.NewRequest("GET", "/1", nil)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/1", nil)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
@ -238,9 +240,9 @@ func TestActionsByPlanID(t *testing.T) {
|
||||
updatedDate, _ := time.Parse("2006-01-02", "2021-01-02")
|
||||
a := &models.Action{ActionID: 1, ActionDescription: "howdy", CompletedOn: nil, CreatedAt: &createdDate, UpdatedAt: &updatedDate, CompletedChunks: 0, EstimatedChunks: 54, PlanID: 6}
|
||||
m := getEmptyModel()
|
||||
m.AddAction(a)
|
||||
m.AddAction(a, 3)
|
||||
router := routes.NewActionRouter(m)
|
||||
req, _ := http.NewRequest("GET", "/?plan_id=6", nil)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/?plan_id=6", nil)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
@ -255,6 +257,7 @@ func TestActionsByPlanID(t *testing.T) {
|
||||
{
|
||||
"action_id": 1,
|
||||
"action_description": "howdy",
|
||||
"user_id": 3,
|
||||
"estimated_chunks": 54,
|
||||
"completed_chunks": 0,
|
||||
"updated_at": "2021-01-02T00:00:00Z",
|
||||
@ -274,9 +277,9 @@ func TestActionsByPlanIDInvalidID(t *testing.T) {
|
||||
updatedDate, _ := time.Parse("2006-01-02", "2021-01-02")
|
||||
a := &models.Action{ActionID: 6, ActionDescription: "howdy", CompletedOn: nil, CreatedAt: &createdDate, UpdatedAt: &updatedDate, CompletedChunks: 0, EstimatedChunks: 54, PlanID: 3}
|
||||
m := getEmptyModel()
|
||||
m.AddAction(a)
|
||||
m.AddAction(a, 3)
|
||||
router := routes.NewActionRouter(m)
|
||||
req, _ := http.NewRequest("GET", "/?plan_id=aoeu", nil)
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/?plan_id=aoeu", nil)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
|
@ -25,7 +25,7 @@ func TestPureJSONPostAction(t *testing.T) {
|
||||
ActionDescription: "here's an action",
|
||||
}
|
||||
m := getEmptyModel()
|
||||
m.AddAction(a)
|
||||
m.AddAction(a, 3)
|
||||
router := routes.NewActionRouter(m)
|
||||
data := []byte(`{
|
||||
"action_description": "here's an action",
|
||||
@ -34,7 +34,7 @@ func TestPureJSONPostAction(t *testing.T) {
|
||||
"completed_on": "2021-01-01T00:00:00Z",
|
||||
"plan_id": 5
|
||||
}`)
|
||||
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@ -52,7 +52,8 @@ func TestPureJSONPostAction(t *testing.T) {
|
||||
"completed_chunks": 2,
|
||||
"completed_on": "2021-01-01T00:00:00Z",
|
||||
"plan_id": 5,
|
||||
"action_id": 2
|
||||
"action_id": 2,
|
||||
"user_id": 3
|
||||
},
|
||||
"id": 2
|
||||
}`
|
||||
@ -74,7 +75,7 @@ func TestExtraFieldActionPostJSON(t *testing.T) {
|
||||
"plan_id": 5,
|
||||
"sabotage": "omg"
|
||||
}`)
|
||||
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@ -97,7 +98,7 @@ func TestEmptyBodyActionPost(t *testing.T) {
|
||||
m.AddPlan(p, 3)
|
||||
router := routes.NewActionRouter(m)
|
||||
data := []byte(``)
|
||||
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@ -125,7 +126,7 @@ func TestTwoBodyActionPost(t *testing.T) {
|
||||
}, {
|
||||
"plan_id": 6
|
||||
}`)
|
||||
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@ -149,7 +150,7 @@ func TestErrorCreateAction(t *testing.T) {
|
||||
router := routes.NewActionRouter(m)
|
||||
a := &models.Action{PlanID: 6}
|
||||
data, _ := json.Marshal(a)
|
||||
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@ -174,7 +175,7 @@ func TestErrorOnRetrieveCreateAction(t *testing.T) {
|
||||
router := routes.NewActionRouter(m)
|
||||
a := &models.Action{PlanID: 6}
|
||||
data, _ := json.Marshal(a)
|
||||
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
@ -195,11 +196,11 @@ func TestErrorWriterCreateAction(t *testing.T) {
|
||||
|
||||
a := &models.Action{PlanID: 6}
|
||||
m := getEmptyModel()
|
||||
m.AddAction(a)
|
||||
m.AddAction(a, 3)
|
||||
|
||||
router := routes.NewActionRouter(m)
|
||||
data, _ := json.Marshal(a)
|
||||
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := NewBadWriter()
|
||||
|
@ -25,7 +25,7 @@ func TestPureJSONPutAction(t *testing.T) {
|
||||
ActionDescription: "hn",
|
||||
}
|
||||
m := getEmptyModel()
|
||||
m.AddAction(a)
|
||||
m.AddAction(a, 3)
|
||||
router := routes.NewActionRouter(m)
|
||||
data := []byte(`{
|
||||
"action_description": "here's an action",
|
||||
@ -34,7 +34,7 @@ func TestPureJSONPutAction(t *testing.T) {
|
||||
"completed_on": "2021-01-01T00:00:00Z",
|
||||
"plan_id": 5
|
||||
}`)
|
||||
req, _ := http.NewRequest("PUT", "/1", bytes.NewBuffer(data))
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/1", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@ -52,7 +52,8 @@ func TestPureJSONPutAction(t *testing.T) {
|
||||
"completed_chunks": 2,
|
||||
"completed_on": "2021-01-01T00:00:00Z",
|
||||
"plan_id": 5,
|
||||
"action_id": 1
|
||||
"action_id": 1,
|
||||
"user_id": 3
|
||||
},
|
||||
"id": 1
|
||||
}`
|
||||
@ -71,7 +72,7 @@ func TestExtraFieldActionPutJSON(t *testing.T) {
|
||||
"plan_id": 5,
|
||||
"sabotage": "omg"
|
||||
}`)
|
||||
req, _ := http.NewRequest("PUT", "/1", bytes.NewBuffer(data))
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/1", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@ -91,7 +92,7 @@ func TestEmptyBodyActionPut(t *testing.T) {
|
||||
m := getEmptyModel()
|
||||
router := routes.NewActionRouter(m)
|
||||
data := []byte(``)
|
||||
req, _ := http.NewRequest("PUT", "/1", bytes.NewBuffer(data))
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/1", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@ -116,7 +117,7 @@ func TestTwoBodyActionPut(t *testing.T) {
|
||||
}, {
|
||||
"plan_id": 6
|
||||
}`)
|
||||
req, _ := http.NewRequest("PUT", "/1", bytes.NewBuffer(data))
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/1", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@ -141,7 +142,7 @@ func TestBadActionIDPut(t *testing.T) {
|
||||
}, {
|
||||
"plan_id": 6
|
||||
}`)
|
||||
req, _ := http.NewRequest("PUT", "/text", bytes.NewBuffer(data))
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/text", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@ -165,7 +166,7 @@ func TestErrorUpdateAction(t *testing.T) {
|
||||
router := routes.NewActionRouter(m)
|
||||
a := &models.Action{PlanID: 6}
|
||||
data, _ := json.Marshal(a)
|
||||
req, _ := http.NewRequest("PUT", "/1", bytes.NewBuffer(data))
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/1", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@ -190,7 +191,7 @@ func TestErrorOnRetrieveUpdateAction(t *testing.T) {
|
||||
router := routes.NewActionRouter(m)
|
||||
a := &models.Action{PlanID: 6}
|
||||
data, _ := json.Marshal(a)
|
||||
req, _ := http.NewRequest("PUT", "/1", bytes.NewBuffer(data))
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/1", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
@ -211,11 +212,11 @@ func TestErrorWriterUpdateAction(t *testing.T) {
|
||||
|
||||
a := &models.Action{PlanID: 6}
|
||||
m := getEmptyModel()
|
||||
m.AddAction(a)
|
||||
m.AddAction(a, 3)
|
||||
|
||||
router := routes.NewActionRouter(m)
|
||||
data, _ := json.Marshal(a)
|
||||
req, _ := http.NewRequest("PUT", "/1", bytes.NewBuffer(data))
|
||||
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/1", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := NewBadWriter()
|
||||
|
@ -5,22 +5,22 @@ import (
|
||||
"gitea.deepak.science/deepak/gogmagog/models"
|
||||
)
|
||||
|
||||
func (e *errorStore) SelectActions() ([]*models.Action, error) {
|
||||
func (e *errorStore) SelectActions(userID int) ([]*models.Action, error) {
|
||||
return nil, e.error
|
||||
}
|
||||
|
||||
func (e *errorStore) SelectActionByID(id int) (*models.Action, error) {
|
||||
func (e *errorStore) SelectActionByID(id int, userID int) (*models.Action, error) {
|
||||
return nil, e.error
|
||||
}
|
||||
|
||||
func (e *errorStore) InsertAction(action *models.Action) (int, error) {
|
||||
func (e *errorStore) InsertAction(action *models.Action, userID int) (int, error) {
|
||||
if e.errorOnInsert {
|
||||
return 0, e.error
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (e *errorStore) UpdateAction(action *models.Action) error {
|
||||
func (e *errorStore) UpdateAction(action *models.Action, userID int) error {
|
||||
if e.errorOnInsert {
|
||||
return e.error
|
||||
}
|
||||
@ -42,7 +42,7 @@ func (e *errorStore) InsertPlan(plan *models.Plan, userID int) (int, error) {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (e *errorStore) SelectActionsByPlanID(plan *models.Plan) ([]*models.Action, error) {
|
||||
func (e *errorStore) SelectActionsByPlanID(plan *models.Plan, userID int) ([]*models.Action, error) {
|
||||
return nil, e.error
|
||||
}
|
||||
|
||||
|
@ -10,32 +10,33 @@ import (
|
||||
|
||||
func TestErrorActionMethods(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
userID := 2
|
||||
str := store.GetErrorStore("error message sample", true)
|
||||
str2 := store.GetErrorStore("error message sample", false)
|
||||
str3 := store.GetErrorStoreForError(fmt.Errorf("test error"), false)
|
||||
|
||||
_, err := str.InsertAction(&models.Action{})
|
||||
_, err := str.InsertAction(&models.Action{}, userID)
|
||||
assert.NotNil(err)
|
||||
_, err = str2.InsertAction(&models.Action{})
|
||||
_, err = str2.InsertAction(&models.Action{}, userID)
|
||||
assert.Nil(err)
|
||||
_, err = str3.InsertAction(&models.Action{})
|
||||
_, err = str3.InsertAction(&models.Action{}, userID)
|
||||
assert.Nil(err)
|
||||
|
||||
_, err = str.SelectActionByID(8)
|
||||
_, err = str.SelectActionByID(8, userID)
|
||||
assert.NotNil(err)
|
||||
_, err = str2.SelectActionByID(8)
|
||||
_, err = str2.SelectActionByID(8, userID)
|
||||
assert.NotNil(err)
|
||||
|
||||
_, err = str.SelectActions()
|
||||
_, err = str.SelectActions(userID)
|
||||
assert.NotNil(err)
|
||||
|
||||
_, err = str.SelectActionsByPlanID(&models.Plan{})
|
||||
_, err = str.SelectActionsByPlanID(&models.Plan{}, userID)
|
||||
assert.NotNil(err)
|
||||
|
||||
replacementAction := &models.Action{}
|
||||
err = str.UpdateAction(replacementAction)
|
||||
err = str.UpdateAction(replacementAction, userID)
|
||||
assert.NotNil(err)
|
||||
err = str2.UpdateAction(replacementAction)
|
||||
err = str2.UpdateAction(replacementAction, userID)
|
||||
assert.Nil(err)
|
||||
|
||||
}
|
||||
|
@ -20,38 +20,45 @@ func GetInMemoryStore() (models.Store, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (store *inMemoryStore) SelectActions() ([]*models.Action, error) {
|
||||
return store.actions, nil
|
||||
}
|
||||
|
||||
func (store *inMemoryStore) SelectActionsByPlanID(plan *models.Plan) ([]*models.Action, error) {
|
||||
func (store *inMemoryStore) SelectActions(userID int) ([]*models.Action, error) {
|
||||
ret := make([]*models.Action, 0)
|
||||
for _, action := range store.actions {
|
||||
if int(plan.PlanID) == int(action.PlanID) {
|
||||
if int(action.UserID) == userID {
|
||||
ret = append(ret, action)
|
||||
}
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (store *inMemoryStore) SelectActionByID(id int) (*models.Action, error) {
|
||||
func (store *inMemoryStore) SelectActionsByPlanID(plan *models.Plan, userID int) ([]*models.Action, error) {
|
||||
ret := make([]*models.Action, 0)
|
||||
for _, action := range store.actions {
|
||||
if id == int(action.ActionID) {
|
||||
if (int(plan.PlanID) == int(action.PlanID)) && (int(action.UserID) == userID) {
|
||||
ret = append(ret, action)
|
||||
}
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (store *inMemoryStore) SelectActionByID(id int, userID int) (*models.Action, error) {
|
||||
for _, action := range store.actions {
|
||||
if id == int(action.ActionID) && (int(action.UserID) == userID) {
|
||||
return action, nil
|
||||
}
|
||||
}
|
||||
return nil, sql.ErrNoRows
|
||||
}
|
||||
|
||||
func (store *inMemoryStore) InsertAction(action *models.Action) (int, error) {
|
||||
func (store *inMemoryStore) InsertAction(action *models.Action, userID int) (int, error) {
|
||||
id := len(store.actions) + 1
|
||||
action.ActionID = int64(id)
|
||||
action.UserID = int64(userID)
|
||||
store.actions = append(store.actions, action)
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (store *inMemoryStore) UpdateAction(action *models.Action) error {
|
||||
currentAction, err := store.SelectActionByID(int(action.ActionID))
|
||||
func (store *inMemoryStore) UpdateAction(action *models.Action, userID int) error {
|
||||
currentAction, err := store.SelectActionByID(int(action.ActionID), userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -12,42 +12,45 @@ func TestInMemoryActionMethods(t *testing.T) {
|
||||
str, _ := store.GetInMemoryStore()
|
||||
|
||||
sampleplanid := 8
|
||||
userID := 10
|
||||
|
||||
act := &models.Action{}
|
||||
a2 := &models.Action{PlanID: sampleplanid}
|
||||
|
||||
id, _ := str.InsertAction(act)
|
||||
id, _ := str.InsertAction(act, userID)
|
||||
assert.EqualValues(1, id)
|
||||
|
||||
receivedAction, err := str.SelectActionByID(id)
|
||||
receivedAction, err := str.SelectActionByID(id, userID)
|
||||
assert.Nil(err)
|
||||
assert.EqualValues(act, receivedAction)
|
||||
_, err = str.SelectActionByID(id, userID+1)
|
||||
assert.NotNil(err)
|
||||
|
||||
allactions, err := str.SelectActions()
|
||||
allactions, err := str.SelectActions(userID)
|
||||
assert.Nil(err)
|
||||
assert.EqualValues(1, len(allactions))
|
||||
|
||||
str.InsertAction(a2)
|
||||
allactions, err = str.SelectActions()
|
||||
str.InsertAction(a2, userID)
|
||||
allactions, err = str.SelectActions(userID)
|
||||
assert.Nil(err)
|
||||
assert.EqualValues(2, len(allactions))
|
||||
|
||||
planactions, err := str.SelectActionsByPlanID(&models.Plan{PlanID: int64(sampleplanid)})
|
||||
planactions, err := str.SelectActionsByPlanID(&models.Plan{PlanID: int64(sampleplanid)}, userID)
|
||||
assert.Nil(err)
|
||||
assert.EqualValues(1, len(planactions))
|
||||
assert.Equal(a2, planactions[0])
|
||||
|
||||
_, err = str.SelectActionByID(151)
|
||||
_, err = str.SelectActionByID(151, userID)
|
||||
assert.NotNil(err)
|
||||
|
||||
sampleDescription := "snth"
|
||||
replacementAction := &models.Action{ActionID: 1, ActionDescription: sampleDescription}
|
||||
err = str.UpdateAction(replacementAction)
|
||||
err = str.UpdateAction(replacementAction, userID)
|
||||
assert.Nil(err)
|
||||
assert.Equal(sampleDescription, act.ActionDescription)
|
||||
|
||||
replacementAction = &models.Action{ActionID: 1235122, ActionDescription: sampleDescription}
|
||||
err = str.UpdateAction(replacementAction)
|
||||
err = str.UpdateAction(replacementAction, userID)
|
||||
assert.NotNil(err)
|
||||
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ CREATE TABLE IF NOT EXISTS plans(
|
||||
CREATE TABLE IF NOT EXISTS actions(
|
||||
action_id serial PRIMARY KEY,
|
||||
action_description VARCHAR (500) NOT NULL,
|
||||
user_id int REFERENCES users(user_id),
|
||||
estimated_chunks SMALLINT,
|
||||
completed_chunks SMALLINT,
|
||||
completed_on TIMESTAMP WITH TIME ZONE,
|
||||
|
@ -17,41 +17,44 @@ func GetPostgresStore(db *sqlx.DB) (models.Store, error) {
|
||||
return &postgresStore{db: db}, nil
|
||||
}
|
||||
|
||||
func (store *postgresStore) SelectActions() ([]*models.Action, error) {
|
||||
func (store *postgresStore) SelectActions(userID int) ([]*models.Action, error) {
|
||||
queryString := store.db.Rebind("SELECT action_id, action_description, user_id, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE user_id = ?")
|
||||
actions := make([]*models.Action, 0)
|
||||
err := store.db.Select(&actions, "SELECT action_id, action_description, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions")
|
||||
err := store.db.Select(&actions, queryString, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return actions, nil
|
||||
}
|
||||
|
||||
func (store *postgresStore) SelectActionsByPlanID(plan *models.Plan) ([]*models.Action, error) {
|
||||
queryString := store.db.Rebind("SELECT action_id, action_description, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE plan_id = ?")
|
||||
func (store *postgresStore) SelectActionsByPlanID(plan *models.Plan, userID int) ([]*models.Action, error) {
|
||||
queryString := store.db.Rebind("SELECT action_id, action_description, user_id, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE plan_id = ? AND user_id = ?")
|
||||
actions := make([]*models.Action, 0)
|
||||
err := store.db.Select(&actions, queryString, plan.PlanID)
|
||||
err := store.db.Select(&actions, queryString, plan.PlanID, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return actions, nil
|
||||
}
|
||||
|
||||
func (store *postgresStore) SelectActionByID(id int) (*models.Action, error) {
|
||||
func (store *postgresStore) SelectActionByID(id int, userID int) (*models.Action, error) {
|
||||
queryString := store.db.Rebind("SELECT action_id, action_description, user_id, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE action_id = ? AND user_id = ?")
|
||||
action := models.Action{}
|
||||
err := store.db.Get(&action, store.db.Rebind("SELECT action_id, action_description, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE action_id = ?"), id)
|
||||
err := store.db.Get(&action, queryString, id, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &action, nil
|
||||
}
|
||||
|
||||
func (store *postgresStore) InsertAction(action *models.Action) (int, error) {
|
||||
func (store *postgresStore) InsertAction(action *models.Action, userID int) (int, error) {
|
||||
queryString := store.db.Rebind(
|
||||
`INSERT INTO actions (action_description,
|
||||
user_id,
|
||||
estimated_chunks,
|
||||
completed_chunks,
|
||||
completed_on,
|
||||
plan_id) VALUES (?, ?, ?, ?, ?) RETURNING action_id`,
|
||||
plan_id) VALUES (?, ?, ?, ?, ?, ?) RETURNING action_id`,
|
||||
)
|
||||
tx := store.db.MustBegin()
|
||||
var id int
|
||||
@ -59,6 +62,7 @@ func (store *postgresStore) InsertAction(action *models.Action) (int, error) {
|
||||
&id,
|
||||
queryString,
|
||||
action.ActionDescription,
|
||||
userID,
|
||||
action.EstimatedChunks,
|
||||
action.CompletedChunks,
|
||||
action.CompletedOn,
|
||||
@ -75,16 +79,26 @@ func (store *postgresStore) InsertAction(action *models.Action) (int, error) {
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (store *postgresStore) UpdateAction(action *models.Action) error {
|
||||
func (store *postgresStore) UpdateAction(action *models.Action, userID int) error {
|
||||
query := `UPDATE actions SET
|
||||
action_description = :action_description,
|
||||
estimated_chunks = :estimated_chunks,
|
||||
completed_chunks = :completed_chunks,
|
||||
completed_on = :completed_on,
|
||||
plan_id = :plan_id
|
||||
WHERE action_id = :action_id`
|
||||
WHERE action_id = :action_id
|
||||
AND user_id = :user_id`
|
||||
tx := store.db.MustBegin()
|
||||
_, err := store.db.NamedExec(query, action)
|
||||
actionToUse := &models.Action{
|
||||
ActionDescription: action.ActionDescription,
|
||||
EstimatedChunks: action.EstimatedChunks,
|
||||
CompletedChunks: action.CompletedChunks,
|
||||
CompletedOn: action.CompletedOn,
|
||||
PlanID: action.PlanID,
|
||||
ActionID: action.ActionID,
|
||||
UserID: int64(userID),
|
||||
}
|
||||
_, err := store.db.NamedExec(query, actionToUse)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
|
@ -34,6 +34,7 @@ func TestSelectActions(t *testing.T) {
|
||||
idToUse := 1
|
||||
estChunks := 5
|
||||
compChunks := 7
|
||||
userIDToUse := 13
|
||||
desc := "Howdy, partner."
|
||||
|
||||
str, mock := getDbMock(t)
|
||||
@ -46,13 +47,16 @@ func TestSelectActions(t *testing.T) {
|
||||
"completed_on",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"plan_id"}).
|
||||
AddRow(idToUse, desc, estChunks, compChunks, completeTime, createTime, updateTime, idToUse).
|
||||
AddRow(idToUse+1, desc, estChunks, compChunks, completeTime, createTime, updateTime, idToUse)
|
||||
mock.ExpectQuery("^SELECT action_id, action_description, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions$").WillReturnRows(rows)
|
||||
"plan_id",
|
||||
"user_id"}).
|
||||
AddRow(idToUse, desc, estChunks, compChunks, completeTime, createTime, updateTime, idToUse, userIDToUse).
|
||||
AddRow(idToUse+1, desc, estChunks, compChunks, completeTime, createTime, updateTime, idToUse, userIDToUse)
|
||||
mock.ExpectQuery(`^SELECT action_id, action_description, user_id, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE user_id = \$1$`).
|
||||
WithArgs(userIDToUse).
|
||||
WillReturnRows(rows)
|
||||
|
||||
// function under test
|
||||
actions, err := str.SelectActions()
|
||||
actions, err := str.SelectActions(userIDToUse)
|
||||
|
||||
// test results
|
||||
assert.Nil(err)
|
||||
@ -66,6 +70,7 @@ func TestSelectActions(t *testing.T) {
|
||||
assert.Equal(createTime, *action.CreatedAt)
|
||||
assert.Equal(updateTime, *action.UpdatedAt)
|
||||
assert.Equal(idToUse, action.PlanID)
|
||||
assert.EqualValues(userIDToUse, action.UserID)
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unfulfilled expectations: %s", err)
|
||||
@ -82,6 +87,7 @@ func TestSelectActionsByPlanID(t *testing.T) {
|
||||
idToUse := 1
|
||||
estChunks := 5
|
||||
compChunks := 7
|
||||
userIDToUse := 13
|
||||
desc := "Howdy, partner."
|
||||
|
||||
str, mock := getDbMock(t)
|
||||
@ -89,20 +95,21 @@ func TestSelectActionsByPlanID(t *testing.T) {
|
||||
rows := sqlmock.NewRows([]string{
|
||||
"action_id",
|
||||
"action_description",
|
||||
"user_id",
|
||||
"estimated_chunks",
|
||||
"completed_chunks",
|
||||
"completed_on",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"plan_id"}).
|
||||
AddRow(idToUse, desc, estChunks, compChunks, completeTime, createTime, updateTime, idToUse).
|
||||
AddRow(idToUse+1, desc, estChunks, compChunks, completeTime, createTime, updateTime, idToUse)
|
||||
mock.ExpectQuery("^SELECT action_id, action_description, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE plan_id = \\$1$").
|
||||
WithArgs(idToUse).
|
||||
AddRow(idToUse, desc, userIDToUse, estChunks, compChunks, completeTime, createTime, updateTime, idToUse).
|
||||
AddRow(idToUse+1, desc, userIDToUse, estChunks, compChunks, completeTime, createTime, updateTime, idToUse)
|
||||
mock.ExpectQuery(`^SELECT action_id, action_description, user_id, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE plan_id = \$1 AND user_id = \$2$`).
|
||||
WithArgs(idToUse, userIDToUse).
|
||||
WillReturnRows(rows)
|
||||
|
||||
// function under test
|
||||
actions, err := str.SelectActionsByPlanID(&models.Plan{PlanID: int64(idToUse)})
|
||||
actions, err := str.SelectActionsByPlanID(&models.Plan{PlanID: int64(idToUse)}, userIDToUse)
|
||||
|
||||
// test results
|
||||
assert.Nil(err)
|
||||
@ -116,6 +123,7 @@ func TestSelectActionsByPlanID(t *testing.T) {
|
||||
assert.Equal(createTime, *action.CreatedAt)
|
||||
assert.Equal(updateTime, *action.UpdatedAt)
|
||||
assert.Equal(idToUse, action.PlanID)
|
||||
assert.EqualValues(userIDToUse, action.UserID)
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unfulfilled expectations: %s", err)
|
||||
@ -127,15 +135,16 @@ func TestSelectActionsByPlanIDErr(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
idToUse := 1
|
||||
userIDToUse := 13
|
||||
|
||||
str, mock := getDbMock(t)
|
||||
|
||||
mock.ExpectQuery("^SELECT action_id, action_description, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE plan_id = \\$1$").
|
||||
WithArgs(idToUse).
|
||||
mock.ExpectQuery(`^SELECT action_id, action_description, user_id, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE plan_id = \$1 AND user_id = \$2$`).
|
||||
WithArgs(idToUse, userIDToUse).
|
||||
WillReturnError(fmt.Errorf("example error"))
|
||||
|
||||
// function under test
|
||||
actions, err := str.SelectActionsByPlanID(&models.Plan{PlanID: int64(idToUse)})
|
||||
actions, err := str.SelectActionsByPlanID(&models.Plan{PlanID: int64(idToUse)}, userIDToUse)
|
||||
|
||||
// test results
|
||||
assert.NotNil(err)
|
||||
@ -156,6 +165,7 @@ func TestSelectActionById(t *testing.T) {
|
||||
idToUse := 1
|
||||
estChunks := 5
|
||||
compChunks := 7
|
||||
userIDToUse := 13
|
||||
desc := "Howdy, partner."
|
||||
|
||||
str, mock := getDbMock(t)
|
||||
@ -163,20 +173,21 @@ func TestSelectActionById(t *testing.T) {
|
||||
rows := sqlmock.NewRows([]string{
|
||||
"action_id",
|
||||
"action_description",
|
||||
"user_id",
|
||||
"estimated_chunks",
|
||||
"completed_chunks",
|
||||
"completed_on",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"plan_id"}).
|
||||
AddRow(idToUse, desc, estChunks, compChunks, completeTime, createTime, updateTime, idToUse)
|
||||
AddRow(idToUse, desc, userIDToUse, estChunks, compChunks, completeTime, createTime, updateTime, idToUse)
|
||||
|
||||
mock.ExpectQuery("^SELECT action_id, action_description, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE action_id = \\$1").
|
||||
WithArgs(1).
|
||||
mock.ExpectQuery(`^SELECT action_id, action_description, user_id, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE action_id = \$1 AND user_id = \$2`).
|
||||
WithArgs(1, userIDToUse).
|
||||
WillReturnRows(rows)
|
||||
|
||||
// function under test
|
||||
action, err := str.SelectActionByID(1)
|
||||
action, err := str.SelectActionByID(1, userIDToUse)
|
||||
|
||||
// test results
|
||||
assert.Nil(err)
|
||||
@ -189,6 +200,7 @@ func TestSelectActionById(t *testing.T) {
|
||||
assert.Equal(createTime, *action.CreatedAt)
|
||||
assert.Equal(updateTime, *action.UpdatedAt)
|
||||
assert.Equal(idToUse, action.PlanID)
|
||||
assert.EqualValues(userIDToUse, action.UserID)
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unfulfilled expectations: %s", err)
|
||||
@ -200,9 +212,9 @@ func TestErrActions(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
str, mock := getDbMock(t)
|
||||
|
||||
mock.ExpectQuery("^SELECT action_id, action_description, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions$").WillReturnError(fmt.Errorf("example error"))
|
||||
mock.ExpectQuery(`^SELECT action_id, action_description, user_id, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE user_id = \$1$`).WithArgs(1).WillReturnError(fmt.Errorf("example error"))
|
||||
// function under test
|
||||
actions, err := str.SelectActions()
|
||||
actions, err := str.SelectActions(1)
|
||||
// test results
|
||||
assert.Nil(actions)
|
||||
assert.NotNil(err)
|
||||
@ -216,9 +228,11 @@ func TestErrActionByID(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
str, mock := getDbMock(t)
|
||||
|
||||
mock.ExpectQuery("^SELECT action_id, action_description, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE action_id = \\$1").WillReturnError(fmt.Errorf("example error"))
|
||||
userIDToUse := 3
|
||||
|
||||
mock.ExpectQuery(`^SELECT action_id, action_description, user_id, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE action_id = \$1 AND user_id = \$2`).WithArgs(1, userIDToUse).WillReturnError(fmt.Errorf("example error"))
|
||||
// function under test
|
||||
action, err := str.SelectActionByID(1)
|
||||
action, err := str.SelectActionByID(1, userIDToUse)
|
||||
// test results
|
||||
assert.Nil(action)
|
||||
assert.NotNil(err)
|
||||
@ -244,6 +258,8 @@ func TestInsertAction(t *testing.T) {
|
||||
// setup
|
||||
assert := assert.New(t)
|
||||
|
||||
userIDToUse := 7
|
||||
|
||||
str, mock := getDbMock(t)
|
||||
completedOn, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
action := &models.Action{
|
||||
@ -259,13 +275,13 @@ func TestInsertAction(t *testing.T) {
|
||||
rows := sqlmock.NewRows([]string{"action_id"}).AddRow(8)
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectQuery("^INSERT INTO actions \\(action_description, estimated_chunks, completed_chunks, completed_on, plan_id\\) VALUES \\(\\$1, \\$2, \\$3, \\$4, \\$5\\) RETURNING action_id$").
|
||||
WithArgs("testing", 3, 6, completedOn, 5).
|
||||
mock.ExpectQuery(`^INSERT INTO actions \(action_description, user_id, estimated_chunks, completed_chunks, completed_on, plan_id\) VALUES \(\$1, \$2, \$3, \$4, \$5, \$6\) RETURNING action_id$`).
|
||||
WithArgs("testing", userIDToUse, 3, 6, completedOn, 5).
|
||||
WillReturnRows(rows)
|
||||
mock.ExpectCommit()
|
||||
|
||||
// function under test
|
||||
insertedId, err := str.InsertAction(action)
|
||||
insertedId, err := str.InsertAction(action, userIDToUse)
|
||||
// check results
|
||||
assert.Nil(err)
|
||||
assert.EqualValues(idToUse, insertedId)
|
||||
@ -290,13 +306,13 @@ func TestInsertActionErr(t *testing.T) {
|
||||
}
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectQuery("^INSERT INTO actions \\(action_description, estimated_chunks, completed_chunks, completed_on, plan_id\\) VALUES \\(\\$1, \\$2, \\$3, \\$4, \\$5\\) RETURNING action_id$").
|
||||
WithArgs("testing", 3, 6, completedOn, 5).
|
||||
mock.ExpectQuery(`^INSERT INTO actions \(action_description, user_id, estimated_chunks, completed_chunks, completed_on, plan_id\) VALUES \(\$1, \$2, \$3, \$4, \$5, \$6\) RETURNING action_id$`).
|
||||
WithArgs("testing", 7, 3, 6, completedOn, 5).
|
||||
WillReturnError(fmt.Errorf("example error"))
|
||||
mock.ExpectRollback()
|
||||
|
||||
// function under test
|
||||
_, err := str.InsertAction(action)
|
||||
_, err := str.InsertAction(action, 7)
|
||||
// check results
|
||||
assert.NotNil(err)
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
@ -319,17 +335,18 @@ func TestInsertActionCommitErr(t *testing.T) {
|
||||
ActionDescription: "testing",
|
||||
}
|
||||
idToUse := 8
|
||||
userIDToUse := 11
|
||||
|
||||
rows := sqlmock.NewRows([]string{"plan_id"}).AddRow(idToUse)
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectQuery("^INSERT INTO actions \\(action_description, estimated_chunks, completed_chunks, completed_on, plan_id\\) VALUES \\(\\$1, \\$2, \\$3, \\$4, \\$5\\) RETURNING action_id$").
|
||||
WithArgs("testing", 3, 6, completedOn, 5).
|
||||
mock.ExpectQuery(`^INSERT INTO actions \(action_description, user_id, estimated_chunks, completed_chunks, completed_on, plan_id\) VALUES \(\$1, \$2, \$3, \$4, \$5, \$6\) RETURNING action_id$`).
|
||||
WithArgs("testing", userIDToUse, 3, 6, completedOn, 5).
|
||||
WillReturnRows(rows)
|
||||
mock.ExpectCommit().WillReturnError(fmt.Errorf("another error example"))
|
||||
|
||||
// function under test
|
||||
_, err := str.InsertAction(action)
|
||||
_, err := str.InsertAction(action, userIDToUse)
|
||||
// check results
|
||||
assert.NotNil(err)
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
@ -352,6 +369,7 @@ func TestUpdateAction(t *testing.T) {
|
||||
ActionDescription: "testing",
|
||||
ActionID: 2,
|
||||
}
|
||||
userIDToUse := 31
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec(`
|
||||
@ -361,13 +379,14 @@ func TestUpdateAction(t *testing.T) {
|
||||
completed_chunks = \$3,
|
||||
completed_on = \$4,
|
||||
plan_id = \$5
|
||||
WHERE action_id = \$6`).
|
||||
WithArgs("testing", 3, 6, completedOn, 5, 2).
|
||||
WHERE action_id = \$6
|
||||
AND user_id = \$7`).
|
||||
WithArgs("testing", 3, 6, completedOn, 5, 2, userIDToUse).
|
||||
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit()
|
||||
|
||||
// function under test
|
||||
err := str.UpdateAction(action)
|
||||
err := str.UpdateAction(action, userIDToUse)
|
||||
// check results
|
||||
assert.Nil(err)
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
@ -393,12 +412,12 @@ func TestUpdateActionErr(t *testing.T) {
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("UPDATE actions").
|
||||
WithArgs("testing", 3, 6, completedOn, 5, 2).
|
||||
WithArgs("testing", 3, 6, completedOn, 5, 2, 31).
|
||||
WillReturnError(fmt.Errorf("example error"))
|
||||
mock.ExpectRollback()
|
||||
|
||||
// function under test
|
||||
err := str.UpdateAction(action)
|
||||
err := str.UpdateAction(action, 31)
|
||||
// check results
|
||||
assert.NotNil(err)
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
@ -424,12 +443,12 @@ func TestUpdateActionCommitErr(t *testing.T) {
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("UPDATE actions").
|
||||
WithArgs("testing", 3, 6, completedOn, 5, 2).
|
||||
WithArgs("testing", 3, 6, completedOn, 5, 2, 31).
|
||||
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit().WillReturnError(fmt.Errorf("another error example"))
|
||||
|
||||
// function under test
|
||||
err := str.UpdateAction(action)
|
||||
err := str.UpdateAction(action, 31)
|
||||
// check results
|
||||
assert.NotNil(err)
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user