From eb8838ab75ae1f43e04cdc784473f78770535d62 Mon Sep 17 00:00:00 2001 From: Deepak Date: Sun, 31 Jan 2021 13:57:54 -0600 Subject: [PATCH] Add store/postgres tests for current plan --- store/postgres.go | 2 +- store/postgres_current_plan_test.go | 217 ++++++++++++++++++++++++++++ 2 files changed, 218 insertions(+), 1 deletion(-) create mode 100644 store/postgres_current_plan_test.go diff --git a/store/postgres.go b/store/postgres.go index e075df8..334b7f9 100644 --- a/store/postgres.go +++ b/store/postgres.go @@ -189,7 +189,7 @@ func (store *postgresStore) InsertCurrentPlan(currentPlan *models.CurrentPlan, u queryString := store.db.Rebind("INSERT INTO user_current_plan (user_id, plan_id) VALUES (?, ?) RETURNING user_id") tx := store.db.MustBegin() var id int - err := tx.Get(&id, queryString, currentPlan.PlanID, userID) + err := tx.Get(&id, queryString, userID, currentPlan.PlanID) if err != nil { tx.Rollback() return err diff --git a/store/postgres_current_plan_test.go b/store/postgres_current_plan_test.go new file mode 100644 index 0000000..77737ed --- /dev/null +++ b/store/postgres_current_plan_test.go @@ -0,0 +1,217 @@ +package store_test + +import ( + "fmt" + "gitea.deepak.science/deepak/gogmagog/models" + "github.com/DATA-DOG/go-sqlmock" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestSelectCurrentPlan(t *testing.T) { + assert := assert.New(t) + + planIDToUse := 1 + userIDToUse := 2 + + str, mock := getDbMock(t) + + rows := sqlmock.NewRows([]string{"plan_id", "user_id"}).AddRow(planIDToUse, userIDToUse) + mock.ExpectQuery(`^SELECT user_id, plan_id FROM user_current_plan WHERE user_id = \$1`). + WithArgs(userIDToUse). + WillReturnRows(rows) + + currPlan, err := str.SelectCurrentPlan(userIDToUse) + assert.Nil(err) + assert.EqualValues(planIDToUse, currPlan.PlanID) + + if err := mock.ExpectationsWereMet(); err != nil { + t.Errorf("unfulfilled expectations: %s", err) + } +} + +func TestSelectCurrentPlanErr(t *testing.T) { + assert := assert.New(t) + + userIDToUse := 2 + + str, mock := getDbMock(t) + + mock.ExpectQuery(`^SELECT user_id, plan_id FROM user_current_plan WHERE user_id = \$1`). + WithArgs(userIDToUse). + WillReturnError(fmt.Errorf("example error")) + + currPlan, err := str.SelectCurrentPlan(userIDToUse) + assert.NotNil(err) + assert.Nil(currPlan) + + if err := mock.ExpectationsWereMet(); err != nil { + t.Errorf("unfulfilled expectations: %s", err) + } +} + +func TestInsertCurrentPlan(t *testing.T) { + // setup + assert := assert.New(t) + + str, mock := getDbMock(t) + planID := 1 + userID := 2 + badUserID := 7 + + cp := &models.CurrentPlan{PlanID: int64(planID), UserID: int64(badUserID)} + + rows := sqlmock.NewRows([]string{"userID"}).AddRow(userID) + + mock.ExpectBegin() + mock.ExpectQuery(`^INSERT INTO user_current_plan \(user_id, plan_id\) VALUES \(\$1, \$2\) RETURNING user_id$`). + WithArgs(userID, planID). + WillReturnRows(rows) + mock.ExpectCommit() + + // function under test + err := str.InsertCurrentPlan(cp, userID) + // check results + assert.Nil(err) + if err := mock.ExpectationsWereMet(); err != nil { + t.Errorf("unfulfilled expectations: %s", err) + } + +} + +func TestInsertCurrentPlanErr(t *testing.T) { + // setup + assert := assert.New(t) + + str, mock := getDbMock(t) + userID := 2 + badUserID := 7 + planID := 1 + cp := &models.CurrentPlan{PlanID: int64(planID), UserID: int64(badUserID)} + + mock.ExpectBegin() + mock.ExpectQuery(`^INSERT INTO user_current_plan \(user_id, plan_id\) VALUES \(\$1, \$2\) RETURNING user_id$`). + WithArgs(userID, planID). + WillReturnError(fmt.Errorf("example error")) + mock.ExpectRollback() + + // function under test + err := str.InsertCurrentPlan(cp, userID) + // check results + assert.NotNil(err) + if err := mock.ExpectationsWereMet(); err != nil { + t.Errorf("unfulfilled expectations: %s", err) + } + +} + +func TestInsertCurrentPlanCommitErr(t *testing.T) { + // setup + assert := assert.New(t) + + str, mock := getDbMock(t) + planID := 1 + userID := 2 + cp := &models.CurrentPlan{PlanID: int64(planID), UserID: int64(userID)} + + rows := sqlmock.NewRows([]string{"user_id"}).AddRow(userID) + + mock.ExpectBegin() + mock.ExpectQuery(`^INSERT INTO user_current_plan \(user_id, plan_id\) VALUES \(\$1, \$2\) RETURNING user_id$`). + WithArgs(userID, planID). + WillReturnRows(rows) + mock.ExpectCommit().WillReturnError(fmt.Errorf("another error example")) + + // function under test + err := str.InsertCurrentPlan(cp, userID) + // check results + assert.NotNil(err) + if err := mock.ExpectationsWereMet(); err != nil { + t.Errorf("unfulfilled expectations: %s", err) + } + +} + +func TestUpdateCurrentPlan(t *testing.T) { + // setup + assert := assert.New(t) + + str, mock := getDbMock(t) + + userIDToUse := 1 + planIDToUse := 2 + + mock.ExpectBegin() + mock.ExpectExec(` + UPDATE user_current_plan SET + plan_id = \$1 + WHERE user_id = \$2`). + WithArgs(planIDToUse, userIDToUse). + WillReturnResult(sqlmock.NewResult(1, 1)) + mock.ExpectCommit() + + // function under test + err := str.UpdateCurrentPlan(&models.CurrentPlan{PlanID: int64(planIDToUse)}, userIDToUse) + // check results + assert.Nil(err) + if err := mock.ExpectationsWereMet(); err != nil { + t.Errorf("unfulfilled expectations: %s", err) + } + +} + +func TestUpdateCurrentPlanErr(t *testing.T) { + // setup + assert := assert.New(t) + + str, mock := getDbMock(t) + + userIDToUse := 1 + planIDToUse := 2 + + mock.ExpectBegin() + mock.ExpectExec(` + UPDATE user_current_plan SET + plan_id = \$1 + WHERE user_id = \$2`). + WithArgs(planIDToUse, userIDToUse). + WillReturnError(fmt.Errorf("example error")) + mock.ExpectRollback() + + // function under test + err := str.UpdateCurrentPlan(&models.CurrentPlan{PlanID: int64(planIDToUse)}, userIDToUse) + // check results + assert.NotNil(err) + if err := mock.ExpectationsWereMet(); err != nil { + t.Errorf("unfulfilled expectations: %s", err) + } + +} + +func TestUpdateCurrentPlanCommitErr(t *testing.T) { + // setup + assert := assert.New(t) + + str, mock := getDbMock(t) + + userIDToUse := 1 + planIDToUse := 2 + + mock.ExpectBegin() + mock.ExpectExec(` + UPDATE user_current_plan SET + plan_id = \$1 + WHERE user_id = \$2`). + WithArgs(planIDToUse, userIDToUse). + WillReturnResult(sqlmock.NewResult(1, 1)) + mock.ExpectCommit().WillReturnError(fmt.Errorf("another error example")) + + // function under test + err := str.UpdateCurrentPlan(&models.CurrentPlan{PlanID: int64(planIDToUse)}, userIDToUse) + // check results + assert.NotNil(err) + if err := mock.ExpectationsWereMet(); err != nil { + t.Errorf("unfulfilled expectations: %s", err) + } + +}