Fixes failing tests, but incompletely

This commit is contained in:
Deepak Mallubhotla 2021-01-17 15:10:24 -06:00
parent 4708a2b8ff
commit 8eff1115c5
Signed by: deepak
GPG Key ID: 64BF53A3369104E7
16 changed files with 108 additions and 123 deletions

View File

@ -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)
}

View File

@ -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))

View File

@ -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)

View File

@ -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)
}

View File

@ -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.

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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))

View File

@ -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
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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

View File

@ -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)