218 lines
5.2 KiB
Go
218 lines
5.2 KiB
Go
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)
|
|
}
|
|
|
|
}
|