Add store/postgres tests for current plan

This commit is contained in:
Deepak Mallubhotla 2021-01-31 13:57:54 -06:00
parent 6ad0112683
commit eb8838ab75
Signed by: deepak
GPG Key ID: 64BF53A3369104E7
2 changed files with 218 additions and 1 deletions

View File

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

View File

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