From 8eff1115c59b7f00cbe03c975032df98458887f1 Mon Sep 17 00:00:00 2001 From: Deepak Date: Sun, 17 Jan 2021 15:10:24 -0600 Subject: [PATCH] Fixes failing tests, but incompletely --- models/err_model_test.go | 53 +++---------------------------------- models/errors_test.go | 2 +- models/models.go | 6 ++--- models/models_test.go | 12 +++++---- models/plan.go | 12 ++++----- models/user_test.go | 2 +- routes/plans.go | 10 ++++--- routes/plans_test.go | 6 ++--- routes/post_action_test.go | 6 ++--- routes/post_plan_test.go | 22 ++++++++------- store/errorStore.go | 14 +++++++--- store/errorStore_test.go | 8 +++--- store/inmemory.go | 17 ++++++++---- store/inmemory_test.go | 12 ++++----- store/postgres.go | 13 ++++----- store/postgres_plan_test.go | 36 ++++++++++++++++--------- 16 files changed, 108 insertions(+), 123 deletions(-) diff --git a/models/err_model_test.go b/models/err_model_test.go index db1f899..3928c6b 100644 --- a/models/err_model_test.go +++ b/models/err_model_test.go @@ -2,57 +2,10 @@ package models_test import ( "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 { - e := &errorStore{error: err} - return models.New(e) + str := store.GetErrorStoreForError(err, true) + return models.New(str) } diff --git a/models/errors_test.go b/models/errors_test.go index 20b0749..99ed40f 100644 --- a/models/errors_test.go +++ b/models/errors_test.go @@ -35,7 +35,7 @@ func TestErrorModelWrapping(t *testing.T) { assert := assert.New(t) m := getErrorModel(sql.ErrNoRows) - _, err := m.Plan(0) + _, err := m.Plan(0, 0) assert.True(models.IsNotFoundError(err)) _, err = m.Action(0) assert.True(models.IsNotFoundError(err)) diff --git a/models/models.go b/models/models.go index 586141f..02c4e4f 100644 --- a/models/models.go +++ b/models/models.go @@ -11,9 +11,9 @@ type Store interface { SelectActionByID(id int) (*Action, error) InsertAction(action *Action) (int, error) UpdateAction(action *Action) error - SelectPlans() ([]*Plan, error) - SelectPlanByID(id int) (*Plan, error) - InsertPlan(plan *Plan) (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) SelectUserByUsername(username string) (*User, error) InsertUser(user *User) (int, error) diff --git a/models/models_test.go b/models/models_test.go index acb9502..a82acc8 100644 --- a/models/models_test.go +++ b/models/models_test.go @@ -11,11 +11,12 @@ func TestModelActions(t *testing.T) { assert := assert.New(t) a1 := &models.Action{ActionID: 3} a2 := &models.Action{ActionID: 4} + userID := 3 p := &models.Plan{PlanID: 6} str, _ := store.GetInMemoryStore() str.InsertAction(a1) - str.InsertPlan(p) + str.InsertPlan(p, userID) m := models.New(str) actions, err := m.Actions() @@ -36,21 +37,22 @@ func TestModelActions(t *testing.T) { func TestModelPlanMethods(t *testing.T) { assert := assert.New(t) + userID := 3 a1 := &models.Action{ActionID: 3, PlanID: 1} a2 := &models.Action{ActionID: 4} p := &models.Plan{} str, _ := store.GetInMemoryStore() - str.InsertPlan(p) + str.InsertPlan(p, userID) str.InsertAction(a1) str.InsertAction(a2) m := models.New(str) - plans, err := m.Plans() + plans, err := m.Plans(userID) assert.Nil(err) assert.Equal(1, len(plans)) - firstPlan, err := m.Plan(1) + firstPlan, err := m.Plan(1, userID) assert.Nil(err) assert.EqualValues(1, firstPlan.PlanID) @@ -58,7 +60,7 @@ func TestModelPlanMethods(t *testing.T) { assert.Nil(err) assert.Equal(1, len(actions)) - planId, err := m.AddPlan(&models.Plan{}) + planId, err := m.AddPlan(&models.Plan{}, userID) assert.Nil(err) assert.EqualValues(2, planId) } diff --git a/models/plan.go b/models/plan.go index b6bb432..f661b2d 100644 --- a/models/plan.go +++ b/models/plan.go @@ -12,19 +12,19 @@ type Plan struct { } // Plans returns all plans in the model. -func (m *Model) Plans() ([]*Plan, error) { - return m.SelectPlans() +func (m *Model) Plans(userID int) ([]*Plan, error) { + return m.SelectPlans(userID) } // Plan returns a single plan from the store by plan_id. -func (m *Model) Plan(id int) (*Plan, error) { - plan, err := m.SelectPlanByID(id) +func (m *Model) Plan(id int, userID int) (*Plan, error) { + plan, err := m.SelectPlanByID(id, userID) return plan, wrapNotFound(err) } // 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) { - return m.InsertPlan(plan) +func (m *Model) AddPlan(plan *Plan, userID int) (int, error) { + return m.InsertPlan(plan, userID) } // GetActions returns the actions associated with a particular plan. diff --git a/models/user_test.go b/models/user_test.go index 05ca5db..a161293 100644 --- a/models/user_test.go +++ b/models/user_test.go @@ -19,7 +19,7 @@ func TestModelUsers(t *testing.T) { // password := password user1 := &models.User{Username: username, DisplayName: "Ted Est", Password: []byte("$2y$05$6SVV35GX4cB4PDPhRaDD/exsL.HV8QtMMr60YL6dLyqtX4l58q.cy")} str, _ := store.GetInMemoryStore() - str.InsertPlan(p) + str.InsertPlan(p, 3) str.InsertAction(a1) str.InsertAction(a2) str.InsertUser(user1) diff --git a/routes/plans.go b/routes/plans.go index ff1c64d..5e0262e 100644 --- a/routes/plans.go +++ b/routes/plans.go @@ -20,7 +20,8 @@ func NewPlanRouter(m *models.Model) http.Handler { func getAllPlansFunc(m *models.Model) http.HandlerFunc { 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 { serverError(w, err) return @@ -39,7 +40,8 @@ func getPlanByIDFunc(m *models.Model) http.HandlerFunc { notFoundHandler(w, r) return } - plan, err := m.Plan(id) + // todo get real user id + plan, err := m.Plan(id, 3) if err != nil { if models.IsNotFoundError(err) { 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. plan := &models.Plan{PlanDate: p.PlanDate, UserID: p.UserID} - id, err := m.AddPlan(plan) + id, err := m.AddPlan(plan, 3) if err != nil { serverError(w, err) return } - plan, err = m.Plan(id) + plan, err = m.Plan(id, 3) if err != nil { serverError(w, err) return diff --git a/routes/plans_test.go b/routes/plans_test.go index 51043be..3ac622a 100644 --- a/routes/plans_test.go +++ b/routes/plans_test.go @@ -39,7 +39,7 @@ func TestOnePlan(t *testing.T) { planDate, _ := time.Parse("2006-01-02", "2021-01-01") p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: 3} m := getEmptyModel() - m.AddPlan(p) + m.AddPlan(p, 3) router := routes.NewPlanRouter(m) req, _ := http.NewRequest("GET", "/", nil) @@ -112,7 +112,7 @@ func TestOnePlanByID(t *testing.T) { planDate, _ := time.Parse("2006-01-02", "2021-01-01") p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: 3} m := getEmptyModel() - m.AddPlan(p) + m.AddPlan(p, 3) router := routes.NewPlanRouter(m) req, _ := http.NewRequest("GET", "/1", nil) @@ -164,7 +164,7 @@ func TestEmptyPlanErrorWriterByID(t *testing.T) { p := &models.Plan{PlanID: 1, PlanDate: &planDate} m := getEmptyModel() - m.AddPlan(p) + m.AddPlan(p, 3) router := routes.NewPlanRouter(m) req, _ := http.NewRequest("GET", "/1", nil) diff --git a/routes/post_action_test.go b/routes/post_action_test.go index 49261c9..0e87b07 100644 --- a/routes/post_action_test.go +++ b/routes/post_action_test.go @@ -67,7 +67,7 @@ func TestExtraFieldActionPostJSON(t *testing.T) { planDate, _ := time.Parse("2006-01-02", "2021-01-01") p := &models.Plan{PlanID: 6, PlanDate: &planDate} m := getEmptyModel() - m.AddPlan(p) + m.AddPlan(p, 3) router := routes.NewActionRouter(m) data := []byte(`{ "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") p := &models.Plan{PlanID: 6, PlanDate: &planDate} m := getEmptyModel() - m.AddPlan(p) + m.AddPlan(p, 3) router := routes.NewActionRouter(m) data := []byte(``) 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") p := &models.Plan{PlanID: 6, PlanDate: &planDate} m := getEmptyModel() - m.AddPlan(p) + m.AddPlan(p, 3) router := routes.NewActionRouter(m) data := []byte(`{ "plan_id": 5 diff --git a/routes/post_plan_test.go b/routes/post_plan_test.go index 9ad7902..3320b21 100644 --- a/routes/post_plan_test.go +++ b/routes/post_plan_test.go @@ -17,9 +17,10 @@ func TestCreatePlanRoute(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} + userID := 3 + p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: int64(userID)} m := getEmptyModel() - m.AddPlan(p) + m.AddPlan(p, userID) router := routes.NewPlanRouter(m) data, _ := json.Marshal(p) req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data)) @@ -50,9 +51,10 @@ func TestPureJSON(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, UserID: 3} + userID := 3 + p := &models.Plan{PlanID: 1, PlanDate: &planDate, UserID: int64(userID)} m := getEmptyModel() - m.AddPlan(p) + m.AddPlan(p, userID) router := routes.NewPlanRouter(m) data := []byte(`{ "plan_date": "2021-01-01T00:00:00Z", @@ -87,9 +89,10 @@ func TestExtraFieldJSON(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 + p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: int64(userID)} m := getEmptyModel() - m.AddPlan(p) + m.AddPlan(p, userID) router := routes.NewPlanRouter(m) data := []byte(`{ "plan_date": "2021-01-01T00:00:00Z", @@ -114,9 +117,10 @@ func TestEmptyBody(t *testing.T) { // set up assert := assert.New(t) planDate, _ := time.Parse("2006-01-02", "2021-01-01") + userID := 3 p := &models.Plan{PlanID: 6, PlanDate: &planDate} m := getEmptyModel() - m.AddPlan(p) + m.AddPlan(p, userID) router := routes.NewPlanRouter(m) data := []byte(``) 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") p := &models.Plan{PlanID: 6, PlanDate: &planDate} m := getEmptyModel() - m.AddPlan(p) + m.AddPlan(p, 3) router := routes.NewPlanRouter(m) data := []byte(`{ "plan_date": "2021-01-01T00:00:00Z", @@ -222,7 +226,7 @@ func TestErrorWriterCreatePlan(t *testing.T) { p := &models.Plan{PlanID: 6, PlanDate: &planDate} m := getEmptyModel() - m.AddPlan(p) + m.AddPlan(p, 3) router := routes.NewPlanRouter(m) data, _ := json.Marshal(p) req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data)) diff --git a/store/errorStore.go b/store/errorStore.go index 1b306f4..8cadfcf 100644 --- a/store/errorStore.go +++ b/store/errorStore.go @@ -27,15 +27,15 @@ func (e *errorStore) UpdateAction(action *models.Action) error { return nil } -func (e *errorStore) SelectPlans() ([]*models.Plan, error) { +func (e *errorStore) SelectPlans(userID int) ([]*models.Plan, 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 } -func (e *errorStore) InsertPlan(plan *models.Plan) (int, error) { +func (e *errorStore) InsertPlan(plan *models.Plan, userID int) (int, error) { if e.errorOnInsert { return 0, e.error } @@ -66,8 +66,14 @@ type errorStore struct { 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 { e := &errorStore{error: fmt.Errorf(errorMsg), errorOnInsert: errorOnInsert} 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 +} diff --git a/store/errorStore_test.go b/store/errorStore_test.go index 2ed5b46..6102421 100644 --- a/store/errorStore_test.go +++ b/store/errorStore_test.go @@ -41,15 +41,15 @@ func TestErrorPlanMethods(t *testing.T) { str := store.GetErrorStore("sntahoeu", true) str2 := store.GetErrorStore("sntahoeu", false) - _, err := str.SelectPlans() + _, err := str.SelectPlans(3) assert.NotNil(err) - _, err = str.InsertPlan(&models.Plan{}) + _, err = str.InsertPlan(&models.Plan{}, 3) assert.NotNil(err) - _, err = str2.InsertPlan(&models.Plan{}) + _, err = str2.InsertPlan(&models.Plan{}, 3) assert.Nil(err) - _, err = str.SelectPlanByID(5) + _, err = str.SelectPlanByID(5, 3) assert.NotNil(err) } diff --git a/store/inmemory.go b/store/inmemory.go index bfaac57..26c49bf 100644 --- a/store/inmemory.go +++ b/store/inmemory.go @@ -64,22 +64,29 @@ func (store *inMemoryStore) UpdateAction(action *models.Action) error { } -func (store *inMemoryStore) SelectPlans() ([]*models.Plan, error) { - return store.plans, nil +func (store *inMemoryStore) SelectPlans(userID int) ([]*models.Plan, error) { + 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 { - if id == int(plan.PlanID) { + if id == int(plan.PlanID) && (userID == int(plan.UserID)) { return plan, nil } } 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 plan.PlanID = int64(id) + plan.UserID = int64(userID) store.plans = append(store.plans, plan) return id, nil } diff --git a/store/inmemory_test.go b/store/inmemory_test.go index 3617e76..7b3bf51 100644 --- a/store/inmemory_test.go +++ b/store/inmemory_test.go @@ -55,24 +55,24 @@ func TestInMemoryActionMethods(t *testing.T) { func TestInMemoryPlanMethods(t *testing.T) { assert := assert.New(t) str, _ := store.GetInMemoryStore() - + userID := 1 p := &models.Plan{} - plans, err := str.SelectPlans() + plans, err := str.SelectPlans(userID) assert.Nil(err) assert.EqualValues(0, len(plans)) - id, err := str.InsertPlan(p) - plans, err = str.SelectPlans() + id, err := str.InsertPlan(p, userID) + plans, err = str.SelectPlans(userID) assert.Nil(err) assert.EqualValues(1, len(plans)) - retrievedPlan, err := str.SelectPlanByID(id) + retrievedPlan, err := str.SelectPlanByID(id, userID) assert.Nil(err) assert.Equal(retrievedPlan, p) - _, err = str.SelectPlanByID(135135) + _, err = str.SelectPlanByID(135135, userID) assert.NotNil(err) } diff --git a/store/postgres.go b/store/postgres.go index fc1f697..667b4d7 100644 --- a/store/postgres.go +++ b/store/postgres.go @@ -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) - err := store.db.Select(&plans, "SELECT plan_id, plan_date, user_id FROM plans") + err := store.db.Select(&plans, queryString, userID) if err != nil { return nil, err } 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{} - 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 { return nil, err } 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") tx := store.db.MustBegin() var id int - err := tx.Get(&id, queryString, plan.PlanDate, plan.UserID) + err := tx.Get(&id, queryString, plan.PlanDate, userID) if err != nil { tx.Rollback() return -1, err diff --git a/store/postgres_plan_test.go b/store/postgres_plan_test.go index a02b1d2..fdf1ed8 100644 --- a/store/postgres_plan_test.go +++ b/store/postgres_plan_test.go @@ -19,9 +19,11 @@ func TestSelectPlans(t *testing.T) { 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$").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.Equal(1, len(plans)) plan := plans[0] @@ -44,9 +46,11 @@ func TestSelectPlanByID(t *testing.T) { 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$").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.EqualValues(idToUse, plan.PlanID) assert.Equal(currentTime, *plan.PlanDate) @@ -64,8 +68,9 @@ func TestInsertPlan(t *testing.T) { str, mock := getDbMock(t) planDate, _ := time.Parse("2006-01-02", "2021-01-01") userID := 2 + badUserID := 7 - plan := &models.Plan{PlanDate: &planDate, UserID: int64(userID)} + plan := &models.Plan{PlanDate: &planDate, UserID: int64(badUserID)} idToUse := 8 @@ -78,7 +83,7 @@ func TestInsertPlan(t *testing.T) { mock.ExpectCommit() // function under test - insertedId, err := str.InsertPlan(plan) + insertedId, err := str.InsertPlan(plan, userID) // check results assert.Nil(err) assert.EqualValues(idToUse, insertedId) @@ -95,7 +100,8 @@ func TestInsertPlanErr(t *testing.T) { str, mock := getDbMock(t) planDate, _ := time.Parse("2006-01-02", "2021-01-01") userID := 2 - plan := &models.Plan{PlanDate: &planDate, UserID: 2} + badUserID := 7 + plan := &models.Plan{PlanDate: &planDate, UserID: int64(badUserID)} mock.ExpectBegin() 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() // function under test - _, err := str.InsertPlan(plan) + _, err := str.InsertPlan(plan, userID) // check results assert.NotNil(err) if err := mock.ExpectationsWereMet(); err != nil { @@ -132,7 +138,7 @@ func TestInsertPlanCommitErr(t *testing.T) { mock.ExpectCommit().WillReturnError(fmt.Errorf("another error example")) // function under test - _, err := str.InsertPlan(plan) + _, err := str.InsertPlan(plan, userID) // check results assert.NotNil(err) if err := mock.ExpectationsWereMet(); err != nil { @@ -148,9 +154,11 @@ func TestErrPlanByID(t *testing.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.Nil(plan) @@ -164,9 +172,11 @@ func TestErrPlans(t *testing.T) { assert := assert.New(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 - plans, err := str.SelectPlans() + plans, err := str.SelectPlans(8) // test results assert.Nil(plans) assert.NotNil(err)