Fixes failing tests, but incompletely
This commit is contained in:
parent
4708a2b8ff
commit
8eff1115c5
@ -2,57 +2,10 @@ package models_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"gitea.deepak.science/deepak/gogmagog/models"
|
"gitea.deepak.science/deepak/gogmagog/models"
|
||||||
|
"gitea.deepak.science/deepak/gogmagog/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (e *errorStore) SelectActions() ([]*models.Action, error) {
|
|
||||||
return nil, e.error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *errorStore) SelectActionByID(id int) (*models.Action, error) {
|
|
||||||
return nil, e.error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *errorStore) InsertAction(action *models.Action) (int, error) {
|
|
||||||
return 0, e.error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *errorStore) UpdateAction(action *models.Action) error {
|
|
||||||
return e.error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *errorStore) SelectPlans() ([]*models.Plan, error) {
|
|
||||||
return nil, e.error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *errorStore) SelectPlanByID(id int) (*models.Plan, error) {
|
|
||||||
return nil, e.error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *errorStore) InsertPlan(plan *models.Plan) (int, error) {
|
|
||||||
return 0, e.error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *errorStore) SelectActionsByPlanID(plan *models.Plan) ([]*models.Action, error) {
|
|
||||||
return nil, e.error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *errorStore) SelectUserByUsername(username string) (*models.User, error) {
|
|
||||||
return nil, e.error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *errorStore) InsertUser(user *models.User) (int, error) {
|
|
||||||
return 0, e.error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *errorStore) ConnectionLive() error {
|
|
||||||
return e.error
|
|
||||||
}
|
|
||||||
|
|
||||||
type errorStore struct {
|
|
||||||
error error
|
|
||||||
}
|
|
||||||
|
|
||||||
func getErrorModel(err error) *models.Model {
|
func getErrorModel(err error) *models.Model {
|
||||||
e := &errorStore{error: err}
|
str := store.GetErrorStoreForError(err, true)
|
||||||
return models.New(e)
|
return models.New(str)
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ func TestErrorModelWrapping(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
m := getErrorModel(sql.ErrNoRows)
|
m := getErrorModel(sql.ErrNoRows)
|
||||||
|
|
||||||
_, err := m.Plan(0)
|
_, err := m.Plan(0, 0)
|
||||||
assert.True(models.IsNotFoundError(err))
|
assert.True(models.IsNotFoundError(err))
|
||||||
_, err = m.Action(0)
|
_, err = m.Action(0)
|
||||||
assert.True(models.IsNotFoundError(err))
|
assert.True(models.IsNotFoundError(err))
|
||||||
|
@ -11,9 +11,9 @@ type Store interface {
|
|||||||
SelectActionByID(id int) (*Action, error)
|
SelectActionByID(id int) (*Action, error)
|
||||||
InsertAction(action *Action) (int, error)
|
InsertAction(action *Action) (int, error)
|
||||||
UpdateAction(action *Action) error
|
UpdateAction(action *Action) error
|
||||||
SelectPlans() ([]*Plan, error)
|
SelectPlans(userID int) ([]*Plan, error)
|
||||||
SelectPlanByID(id int) (*Plan, error)
|
SelectPlanByID(id int, userID int) (*Plan, error)
|
||||||
InsertPlan(plan *Plan) (int, error)
|
InsertPlan(plan *Plan, userID int) (int, error)
|
||||||
SelectActionsByPlanID(plan *Plan) ([]*Action, error)
|
SelectActionsByPlanID(plan *Plan) ([]*Action, error)
|
||||||
SelectUserByUsername(username string) (*User, error)
|
SelectUserByUsername(username string) (*User, error)
|
||||||
InsertUser(user *User) (int, error)
|
InsertUser(user *User) (int, error)
|
||||||
|
@ -11,11 +11,12 @@ func TestModelActions(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
a1 := &models.Action{ActionID: 3}
|
a1 := &models.Action{ActionID: 3}
|
||||||
a2 := &models.Action{ActionID: 4}
|
a2 := &models.Action{ActionID: 4}
|
||||||
|
userID := 3
|
||||||
p := &models.Plan{PlanID: 6}
|
p := &models.Plan{PlanID: 6}
|
||||||
|
|
||||||
str, _ := store.GetInMemoryStore()
|
str, _ := store.GetInMemoryStore()
|
||||||
str.InsertAction(a1)
|
str.InsertAction(a1)
|
||||||
str.InsertPlan(p)
|
str.InsertPlan(p, userID)
|
||||||
m := models.New(str)
|
m := models.New(str)
|
||||||
|
|
||||||
actions, err := m.Actions()
|
actions, err := m.Actions()
|
||||||
@ -36,21 +37,22 @@ func TestModelActions(t *testing.T) {
|
|||||||
|
|
||||||
func TestModelPlanMethods(t *testing.T) {
|
func TestModelPlanMethods(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
userID := 3
|
||||||
a1 := &models.Action{ActionID: 3, PlanID: 1}
|
a1 := &models.Action{ActionID: 3, PlanID: 1}
|
||||||
a2 := &models.Action{ActionID: 4}
|
a2 := &models.Action{ActionID: 4}
|
||||||
p := &models.Plan{}
|
p := &models.Plan{}
|
||||||
|
|
||||||
str, _ := store.GetInMemoryStore()
|
str, _ := store.GetInMemoryStore()
|
||||||
str.InsertPlan(p)
|
str.InsertPlan(p, userID)
|
||||||
str.InsertAction(a1)
|
str.InsertAction(a1)
|
||||||
str.InsertAction(a2)
|
str.InsertAction(a2)
|
||||||
m := models.New(str)
|
m := models.New(str)
|
||||||
|
|
||||||
plans, err := m.Plans()
|
plans, err := m.Plans(userID)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
assert.Equal(1, len(plans))
|
assert.Equal(1, len(plans))
|
||||||
|
|
||||||
firstPlan, err := m.Plan(1)
|
firstPlan, err := m.Plan(1, userID)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
assert.EqualValues(1, firstPlan.PlanID)
|
assert.EqualValues(1, firstPlan.PlanID)
|
||||||
|
|
||||||
@ -58,7 +60,7 @@ func TestModelPlanMethods(t *testing.T) {
|
|||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
assert.Equal(1, len(actions))
|
assert.Equal(1, len(actions))
|
||||||
|
|
||||||
planId, err := m.AddPlan(&models.Plan{})
|
planId, err := m.AddPlan(&models.Plan{}, userID)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
assert.EqualValues(2, planId)
|
assert.EqualValues(2, planId)
|
||||||
}
|
}
|
||||||
|
@ -12,19 +12,19 @@ type Plan struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Plans returns all plans in the model.
|
// Plans returns all plans in the model.
|
||||||
func (m *Model) Plans() ([]*Plan, error) {
|
func (m *Model) Plans(userID int) ([]*Plan, error) {
|
||||||
return m.SelectPlans()
|
return m.SelectPlans(userID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Plan returns a single plan from the store by plan_id.
|
// Plan returns a single plan from the store by plan_id.
|
||||||
func (m *Model) Plan(id int) (*Plan, error) {
|
func (m *Model) Plan(id int, userID int) (*Plan, error) {
|
||||||
plan, err := m.SelectPlanByID(id)
|
plan, err := m.SelectPlanByID(id, userID)
|
||||||
return plan, wrapNotFound(err)
|
return plan, wrapNotFound(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddPlan inserts a given plan into the store, returning the generated PlanID. The provided PlanID is ignored.
|
// AddPlan inserts a given plan into the store, returning the generated PlanID. The provided PlanID is ignored.
|
||||||
func (m *Model) AddPlan(plan *Plan) (int, error) {
|
func (m *Model) AddPlan(plan *Plan, userID int) (int, error) {
|
||||||
return m.InsertPlan(plan)
|
return m.InsertPlan(plan, userID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetActions returns the actions associated with a particular plan.
|
// GetActions returns the actions associated with a particular plan.
|
||||||
|
@ -19,7 +19,7 @@ func TestModelUsers(t *testing.T) {
|
|||||||
// password := password
|
// password := password
|
||||||
user1 := &models.User{Username: username, DisplayName: "Ted Est", Password: []byte("$2y$05$6SVV35GX4cB4PDPhRaDD/exsL.HV8QtMMr60YL6dLyqtX4l58q.cy")}
|
user1 := &models.User{Username: username, DisplayName: "Ted Est", Password: []byte("$2y$05$6SVV35GX4cB4PDPhRaDD/exsL.HV8QtMMr60YL6dLyqtX4l58q.cy")}
|
||||||
str, _ := store.GetInMemoryStore()
|
str, _ := store.GetInMemoryStore()
|
||||||
str.InsertPlan(p)
|
str.InsertPlan(p, 3)
|
||||||
str.InsertAction(a1)
|
str.InsertAction(a1)
|
||||||
str.InsertAction(a2)
|
str.InsertAction(a2)
|
||||||
str.InsertUser(user1)
|
str.InsertUser(user1)
|
||||||
|
@ -20,7 +20,8 @@ func NewPlanRouter(m *models.Model) http.Handler {
|
|||||||
|
|
||||||
func getAllPlansFunc(m *models.Model) http.HandlerFunc {
|
func getAllPlansFunc(m *models.Model) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
plans, err := m.Plans()
|
// todo enough to get build
|
||||||
|
plans, err := m.Plans(3)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
serverError(w, err)
|
serverError(w, err)
|
||||||
return
|
return
|
||||||
@ -39,7 +40,8 @@ func getPlanByIDFunc(m *models.Model) http.HandlerFunc {
|
|||||||
notFoundHandler(w, r)
|
notFoundHandler(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
plan, err := m.Plan(id)
|
// todo get real user id
|
||||||
|
plan, err := m.Plan(id, 3)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if models.IsNotFoundError(err) {
|
if models.IsNotFoundError(err) {
|
||||||
notFoundHandler(w, r)
|
notFoundHandler(w, r)
|
||||||
@ -81,12 +83,12 @@ func postPlanFunc(m *models.Model) http.HandlerFunc {
|
|||||||
|
|
||||||
// Map the fields we allow to be set to the plan to be created.
|
// Map the fields we allow to be set to the plan to be created.
|
||||||
plan := &models.Plan{PlanDate: p.PlanDate, UserID: p.UserID}
|
plan := &models.Plan{PlanDate: p.PlanDate, UserID: p.UserID}
|
||||||
id, err := m.AddPlan(plan)
|
id, err := m.AddPlan(plan, 3)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
serverError(w, err)
|
serverError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
plan, err = m.Plan(id)
|
plan, err = m.Plan(id, 3)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
serverError(w, err)
|
serverError(w, err)
|
||||||
return
|
return
|
||||||
|
@ -39,7 +39,7 @@ func TestOnePlan(t *testing.T) {
|
|||||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: 3}
|
p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: 3}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddPlan(p)
|
m.AddPlan(p, 3)
|
||||||
router := routes.NewPlanRouter(m)
|
router := routes.NewPlanRouter(m)
|
||||||
req, _ := http.NewRequest("GET", "/", nil)
|
req, _ := http.NewRequest("GET", "/", nil)
|
||||||
|
|
||||||
@ -112,7 +112,7 @@ func TestOnePlanByID(t *testing.T) {
|
|||||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: 3}
|
p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: 3}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddPlan(p)
|
m.AddPlan(p, 3)
|
||||||
router := routes.NewPlanRouter(m)
|
router := routes.NewPlanRouter(m)
|
||||||
req, _ := http.NewRequest("GET", "/1", nil)
|
req, _ := http.NewRequest("GET", "/1", nil)
|
||||||
|
|
||||||
@ -164,7 +164,7 @@ func TestEmptyPlanErrorWriterByID(t *testing.T) {
|
|||||||
|
|
||||||
p := &models.Plan{PlanID: 1, PlanDate: &planDate}
|
p := &models.Plan{PlanID: 1, PlanDate: &planDate}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddPlan(p)
|
m.AddPlan(p, 3)
|
||||||
|
|
||||||
router := routes.NewPlanRouter(m)
|
router := routes.NewPlanRouter(m)
|
||||||
req, _ := http.NewRequest("GET", "/1", nil)
|
req, _ := http.NewRequest("GET", "/1", nil)
|
||||||
|
@ -67,7 +67,7 @@ func TestExtraFieldActionPostJSON(t *testing.T) {
|
|||||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddPlan(p)
|
m.AddPlan(p, 3)
|
||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
data := []byte(`{
|
data := []byte(`{
|
||||||
"completed_on": "2021-01-01T00:00:00Z",
|
"completed_on": "2021-01-01T00:00:00Z",
|
||||||
@ -94,7 +94,7 @@ func TestEmptyBodyActionPost(t *testing.T) {
|
|||||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddPlan(p)
|
m.AddPlan(p, 3)
|
||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
data := []byte(``)
|
data := []byte(``)
|
||||||
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
|
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
|
||||||
@ -118,7 +118,7 @@ func TestTwoBodyActionPost(t *testing.T) {
|
|||||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddPlan(p)
|
m.AddPlan(p, 3)
|
||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
data := []byte(`{
|
data := []byte(`{
|
||||||
"plan_id": 5
|
"plan_id": 5
|
||||||
|
@ -17,9 +17,10 @@ func TestCreatePlanRoute(t *testing.T) {
|
|||||||
// set up
|
// set up
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: 3}
|
userID := 3
|
||||||
|
p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: int64(userID)}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddPlan(p)
|
m.AddPlan(p, userID)
|
||||||
router := routes.NewPlanRouter(m)
|
router := routes.NewPlanRouter(m)
|
||||||
data, _ := json.Marshal(p)
|
data, _ := json.Marshal(p)
|
||||||
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
|
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
|
||||||
@ -50,9 +51,10 @@ func TestPureJSON(t *testing.T) {
|
|||||||
// set up
|
// set up
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||||
p := &models.Plan{PlanID: 1, PlanDate: &planDate, UserID: 3}
|
userID := 3
|
||||||
|
p := &models.Plan{PlanID: 1, PlanDate: &planDate, UserID: int64(userID)}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddPlan(p)
|
m.AddPlan(p, userID)
|
||||||
router := routes.NewPlanRouter(m)
|
router := routes.NewPlanRouter(m)
|
||||||
data := []byte(`{
|
data := []byte(`{
|
||||||
"plan_date": "2021-01-01T00:00:00Z",
|
"plan_date": "2021-01-01T00:00:00Z",
|
||||||
@ -87,9 +89,10 @@ func TestExtraFieldJSON(t *testing.T) {
|
|||||||
// set up
|
// set up
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
userID := 3
|
||||||
|
p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: int64(userID)}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddPlan(p)
|
m.AddPlan(p, userID)
|
||||||
router := routes.NewPlanRouter(m)
|
router := routes.NewPlanRouter(m)
|
||||||
data := []byte(`{
|
data := []byte(`{
|
||||||
"plan_date": "2021-01-01T00:00:00Z",
|
"plan_date": "2021-01-01T00:00:00Z",
|
||||||
@ -114,9 +117,10 @@ func TestEmptyBody(t *testing.T) {
|
|||||||
// set up
|
// set up
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||||
|
userID := 3
|
||||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddPlan(p)
|
m.AddPlan(p, userID)
|
||||||
router := routes.NewPlanRouter(m)
|
router := routes.NewPlanRouter(m)
|
||||||
data := []byte(``)
|
data := []byte(``)
|
||||||
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
|
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
|
||||||
@ -140,7 +144,7 @@ func TestTwoBody(t *testing.T) {
|
|||||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddPlan(p)
|
m.AddPlan(p, 3)
|
||||||
router := routes.NewPlanRouter(m)
|
router := routes.NewPlanRouter(m)
|
||||||
data := []byte(`{
|
data := []byte(`{
|
||||||
"plan_date": "2021-01-01T00:00:00Z",
|
"plan_date": "2021-01-01T00:00:00Z",
|
||||||
@ -222,7 +226,7 @@ func TestErrorWriterCreatePlan(t *testing.T) {
|
|||||||
|
|
||||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddPlan(p)
|
m.AddPlan(p, 3)
|
||||||
router := routes.NewPlanRouter(m)
|
router := routes.NewPlanRouter(m)
|
||||||
data, _ := json.Marshal(p)
|
data, _ := json.Marshal(p)
|
||||||
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
|
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
|
||||||
|
@ -27,15 +27,15 @@ func (e *errorStore) UpdateAction(action *models.Action) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *errorStore) SelectPlans() ([]*models.Plan, error) {
|
func (e *errorStore) SelectPlans(userID int) ([]*models.Plan, error) {
|
||||||
return nil, e.error
|
return nil, e.error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *errorStore) SelectPlanByID(id int) (*models.Plan, error) {
|
func (e *errorStore) SelectPlanByID(id int, userID int) (*models.Plan, error) {
|
||||||
return nil, e.error
|
return nil, e.error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *errorStore) InsertPlan(plan *models.Plan) (int, error) {
|
func (e *errorStore) InsertPlan(plan *models.Plan, userID int) (int, error) {
|
||||||
if e.errorOnInsert {
|
if e.errorOnInsert {
|
||||||
return 0, e.error
|
return 0, e.error
|
||||||
}
|
}
|
||||||
@ -66,8 +66,14 @@ type errorStore struct {
|
|||||||
errorOnInsert bool
|
errorOnInsert bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetErrorStore returns a models.Store that always errors. This is useful for testirng purposes.
|
// GetErrorStore returns a models.Store that always errors. This is useful for testing purposes.
|
||||||
func GetErrorStore(errorMsg string, errorOnInsert bool) models.Store {
|
func GetErrorStore(errorMsg string, errorOnInsert bool) models.Store {
|
||||||
e := &errorStore{error: fmt.Errorf(errorMsg), errorOnInsert: errorOnInsert}
|
e := &errorStore{error: fmt.Errorf(errorMsg), errorOnInsert: errorOnInsert}
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetErrorStoreForError returns a models.Store that always errors with the provided error.
|
||||||
|
func GetErrorStoreForError(err error, errorOnInsert bool) models.Store {
|
||||||
|
e := &errorStore{error: err, errorOnInsert: errorOnInsert}
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
@ -41,15 +41,15 @@ func TestErrorPlanMethods(t *testing.T) {
|
|||||||
str := store.GetErrorStore("sntahoeu", true)
|
str := store.GetErrorStore("sntahoeu", true)
|
||||||
str2 := store.GetErrorStore("sntahoeu", false)
|
str2 := store.GetErrorStore("sntahoeu", false)
|
||||||
|
|
||||||
_, err := str.SelectPlans()
|
_, err := str.SelectPlans(3)
|
||||||
assert.NotNil(err)
|
assert.NotNil(err)
|
||||||
|
|
||||||
_, err = str.InsertPlan(&models.Plan{})
|
_, err = str.InsertPlan(&models.Plan{}, 3)
|
||||||
assert.NotNil(err)
|
assert.NotNil(err)
|
||||||
_, err = str2.InsertPlan(&models.Plan{})
|
_, err = str2.InsertPlan(&models.Plan{}, 3)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
|
|
||||||
_, err = str.SelectPlanByID(5)
|
_, err = str.SelectPlanByID(5, 3)
|
||||||
assert.NotNil(err)
|
assert.NotNil(err)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -64,22 +64,29 @@ func (store *inMemoryStore) UpdateAction(action *models.Action) error {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *inMemoryStore) SelectPlans() ([]*models.Plan, error) {
|
func (store *inMemoryStore) SelectPlans(userID int) ([]*models.Plan, error) {
|
||||||
return store.plans, nil
|
ret := make([]*models.Plan, 0)
|
||||||
|
for _, plan := range store.plans {
|
||||||
|
if int(plan.UserID) == userID {
|
||||||
|
ret = append(ret, plan)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *inMemoryStore) SelectPlanByID(id int) (*models.Plan, error) {
|
func (store *inMemoryStore) SelectPlanByID(id int, userID int) (*models.Plan, error) {
|
||||||
for _, plan := range store.plans {
|
for _, plan := range store.plans {
|
||||||
if id == int(plan.PlanID) {
|
if id == int(plan.PlanID) && (userID == int(plan.UserID)) {
|
||||||
return plan, nil
|
return plan, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil, sql.ErrNoRows
|
return nil, sql.ErrNoRows
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *inMemoryStore) InsertPlan(plan *models.Plan) (int, error) {
|
func (store *inMemoryStore) InsertPlan(plan *models.Plan, userID int) (int, error) {
|
||||||
id := len(store.plans) + 1
|
id := len(store.plans) + 1
|
||||||
plan.PlanID = int64(id)
|
plan.PlanID = int64(id)
|
||||||
|
plan.UserID = int64(userID)
|
||||||
store.plans = append(store.plans, plan)
|
store.plans = append(store.plans, plan)
|
||||||
return id, nil
|
return id, nil
|
||||||
}
|
}
|
||||||
|
@ -55,24 +55,24 @@ func TestInMemoryActionMethods(t *testing.T) {
|
|||||||
func TestInMemoryPlanMethods(t *testing.T) {
|
func TestInMemoryPlanMethods(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
str, _ := store.GetInMemoryStore()
|
str, _ := store.GetInMemoryStore()
|
||||||
|
userID := 1
|
||||||
p := &models.Plan{}
|
p := &models.Plan{}
|
||||||
plans, err := str.SelectPlans()
|
plans, err := str.SelectPlans(userID)
|
||||||
|
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
assert.EqualValues(0, len(plans))
|
assert.EqualValues(0, len(plans))
|
||||||
|
|
||||||
id, err := str.InsertPlan(p)
|
id, err := str.InsertPlan(p, userID)
|
||||||
plans, err = str.SelectPlans()
|
plans, err = str.SelectPlans(userID)
|
||||||
|
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
assert.EqualValues(1, len(plans))
|
assert.EqualValues(1, len(plans))
|
||||||
|
|
||||||
retrievedPlan, err := str.SelectPlanByID(id)
|
retrievedPlan, err := str.SelectPlanByID(id, userID)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
assert.Equal(retrievedPlan, p)
|
assert.Equal(retrievedPlan, p)
|
||||||
|
|
||||||
_, err = str.SelectPlanByID(135135)
|
_, err = str.SelectPlanByID(135135, userID)
|
||||||
assert.NotNil(err)
|
assert.NotNil(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,29 +97,30 @@ func (store *postgresStore) UpdateAction(action *models.Action) error {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *postgresStore) SelectPlans() ([]*models.Plan, error) {
|
func (store *postgresStore) SelectPlans(userID int) ([]*models.Plan, error) {
|
||||||
|
queryString := store.db.Rebind("SELECT plan_id, plan_date, user_id FROM plans WHERE user_id = ?")
|
||||||
plans := make([]*models.Plan, 0)
|
plans := make([]*models.Plan, 0)
|
||||||
err := store.db.Select(&plans, "SELECT plan_id, plan_date, user_id FROM plans")
|
err := store.db.Select(&plans, queryString, userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return plans, nil
|
return plans, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *postgresStore) SelectPlanByID(id int) (*models.Plan, error) {
|
func (store *postgresStore) SelectPlanByID(id int, userID int) (*models.Plan, error) {
|
||||||
plan := models.Plan{}
|
plan := models.Plan{}
|
||||||
err := store.db.Get(&plan, store.db.Rebind("SELECT plan_id, plan_date, user_id FROM plans WHERE plan_id = ?"), id)
|
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)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &plan, nil
|
return &plan, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *postgresStore) InsertPlan(plan *models.Plan) (int, error) {
|
func (store *postgresStore) InsertPlan(plan *models.Plan, userID int) (int, error) {
|
||||||
queryString := store.db.Rebind("INSERT INTO plans (plan_date, user_id) VALUES (?, ?) RETURNING plan_id")
|
queryString := store.db.Rebind("INSERT INTO plans (plan_date, user_id) VALUES (?, ?) RETURNING plan_id")
|
||||||
tx := store.db.MustBegin()
|
tx := store.db.MustBegin()
|
||||||
var id int
|
var id int
|
||||||
err := tx.Get(&id, queryString, plan.PlanDate, plan.UserID)
|
err := tx.Get(&id, queryString, plan.PlanDate, userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return -1, err
|
return -1, err
|
||||||
|
@ -19,9 +19,11 @@ func TestSelectPlans(t *testing.T) {
|
|||||||
str, mock := getDbMock(t)
|
str, mock := getDbMock(t)
|
||||||
|
|
||||||
rows := sqlmock.NewRows([]string{"plan_id", "plan_date", "user_id"}).AddRow(idToUse, currentTime, userIDToUse)
|
rows := sqlmock.NewRows([]string{"plan_id", "plan_date", "user_id"}).AddRow(idToUse, currentTime, userIDToUse)
|
||||||
mock.ExpectQuery("^SELECT plan_id, plan_date, user_id FROM plans$").WillReturnRows(rows)
|
mock.ExpectQuery(`^SELECT plan_id, plan_date, user_id FROM plans WHERE user_id = \$1`).
|
||||||
|
WithArgs(userIDToUse).
|
||||||
|
WillReturnRows(rows)
|
||||||
|
|
||||||
plans, err := str.SelectPlans()
|
plans, err := str.SelectPlans(userIDToUse)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
assert.Equal(1, len(plans))
|
assert.Equal(1, len(plans))
|
||||||
plan := plans[0]
|
plan := plans[0]
|
||||||
@ -44,9 +46,11 @@ func TestSelectPlanByID(t *testing.T) {
|
|||||||
str, mock := getDbMock(t)
|
str, mock := getDbMock(t)
|
||||||
|
|
||||||
rows := sqlmock.NewRows([]string{"plan_id", "plan_date", "user_id"}).AddRow(idToUse, currentTime, userIDToUse)
|
rows := sqlmock.NewRows([]string{"plan_id", "plan_date", "user_id"}).AddRow(idToUse, currentTime, userIDToUse)
|
||||||
mock.ExpectQuery("^SELECT plan_id, plan_date, user_id FROM plans WHERE plan_id = \\$1$").WithArgs(idToUse).WillReturnRows(rows)
|
mock.ExpectQuery(`^SELECT plan_id, plan_date, user_id FROM plans WHERE plan_id = \$1 AND user_id = \$2$`).
|
||||||
|
WithArgs(idToUse, userIDToUse).
|
||||||
|
WillReturnRows(rows)
|
||||||
|
|
||||||
plan, err := str.SelectPlanByID(idToUse)
|
plan, err := str.SelectPlanByID(idToUse, userIDToUse)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
assert.EqualValues(idToUse, plan.PlanID)
|
assert.EqualValues(idToUse, plan.PlanID)
|
||||||
assert.Equal(currentTime, *plan.PlanDate)
|
assert.Equal(currentTime, *plan.PlanDate)
|
||||||
@ -64,8 +68,9 @@ func TestInsertPlan(t *testing.T) {
|
|||||||
str, mock := getDbMock(t)
|
str, mock := getDbMock(t)
|
||||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||||
userID := 2
|
userID := 2
|
||||||
|
badUserID := 7
|
||||||
|
|
||||||
plan := &models.Plan{PlanDate: &planDate, UserID: int64(userID)}
|
plan := &models.Plan{PlanDate: &planDate, UserID: int64(badUserID)}
|
||||||
|
|
||||||
idToUse := 8
|
idToUse := 8
|
||||||
|
|
||||||
@ -78,7 +83,7 @@ func TestInsertPlan(t *testing.T) {
|
|||||||
mock.ExpectCommit()
|
mock.ExpectCommit()
|
||||||
|
|
||||||
// function under test
|
// function under test
|
||||||
insertedId, err := str.InsertPlan(plan)
|
insertedId, err := str.InsertPlan(plan, userID)
|
||||||
// check results
|
// check results
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
assert.EqualValues(idToUse, insertedId)
|
assert.EqualValues(idToUse, insertedId)
|
||||||
@ -95,7 +100,8 @@ func TestInsertPlanErr(t *testing.T) {
|
|||||||
str, mock := getDbMock(t)
|
str, mock := getDbMock(t)
|
||||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||||
userID := 2
|
userID := 2
|
||||||
plan := &models.Plan{PlanDate: &planDate, UserID: 2}
|
badUserID := 7
|
||||||
|
plan := &models.Plan{PlanDate: &planDate, UserID: int64(badUserID)}
|
||||||
|
|
||||||
mock.ExpectBegin()
|
mock.ExpectBegin()
|
||||||
mock.ExpectQuery(`^INSERT INTO plans \(plan_date, user_id\) VALUES \(\$1, \$2\) RETURNING plan_id$`).
|
mock.ExpectQuery(`^INSERT INTO plans \(plan_date, user_id\) VALUES \(\$1, \$2\) RETURNING plan_id$`).
|
||||||
@ -104,7 +110,7 @@ func TestInsertPlanErr(t *testing.T) {
|
|||||||
mock.ExpectRollback()
|
mock.ExpectRollback()
|
||||||
|
|
||||||
// function under test
|
// function under test
|
||||||
_, err := str.InsertPlan(plan)
|
_, err := str.InsertPlan(plan, userID)
|
||||||
// check results
|
// check results
|
||||||
assert.NotNil(err)
|
assert.NotNil(err)
|
||||||
if err := mock.ExpectationsWereMet(); err != nil {
|
if err := mock.ExpectationsWereMet(); err != nil {
|
||||||
@ -132,7 +138,7 @@ func TestInsertPlanCommitErr(t *testing.T) {
|
|||||||
mock.ExpectCommit().WillReturnError(fmt.Errorf("another error example"))
|
mock.ExpectCommit().WillReturnError(fmt.Errorf("another error example"))
|
||||||
|
|
||||||
// function under test
|
// function under test
|
||||||
_, err := str.InsertPlan(plan)
|
_, err := str.InsertPlan(plan, userID)
|
||||||
// check results
|
// check results
|
||||||
assert.NotNil(err)
|
assert.NotNil(err)
|
||||||
if err := mock.ExpectationsWereMet(); err != nil {
|
if err := mock.ExpectationsWereMet(); err != nil {
|
||||||
@ -148,9 +154,11 @@ func TestErrPlanByID(t *testing.T) {
|
|||||||
|
|
||||||
str, mock := getDbMock(t)
|
str, mock := getDbMock(t)
|
||||||
|
|
||||||
mock.ExpectQuery(`^SELECT plan_id, plan_date, user_id FROM plans WHERE plan_id = \$1$`).WithArgs(idToUse).WillReturnError(fmt.Errorf("example error"))
|
mock.ExpectQuery(`^SELECT plan_id, plan_date, user_id FROM plans WHERE plan_id = \$1 AND user_id = \$2$`).
|
||||||
|
WithArgs(idToUse, 8).
|
||||||
|
WillReturnError(fmt.Errorf("example error"))
|
||||||
|
|
||||||
plan, err := str.SelectPlanByID(idToUse)
|
plan, err := str.SelectPlanByID(idToUse, 8)
|
||||||
assert.NotNil(err)
|
assert.NotNil(err)
|
||||||
assert.Nil(plan)
|
assert.Nil(plan)
|
||||||
|
|
||||||
@ -164,9 +172,11 @@ func TestErrPlans(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
str, mock := getDbMock(t)
|
str, mock := getDbMock(t)
|
||||||
|
|
||||||
mock.ExpectQuery(`^SELECT plan_id, plan_date, user_id FROM plans$`).WillReturnError(fmt.Errorf("example error"))
|
mock.ExpectQuery(`^SELECT plan_id, plan_date, user_id FROM plans WHERE user_id = \$1$`).
|
||||||
|
WithArgs(8).
|
||||||
|
WillReturnError(fmt.Errorf("example error"))
|
||||||
// function under test
|
// function under test
|
||||||
plans, err := str.SelectPlans()
|
plans, err := str.SelectPlans(8)
|
||||||
// test results
|
// test results
|
||||||
assert.Nil(plans)
|
assert.Nil(plans)
|
||||||
assert.NotNil(err)
|
assert.NotNil(err)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user