All checks were successful
gitea-deepak/gogmagog/pipeline/head This commit looks good
265 lines
6.9 KiB
Go
265 lines
6.9 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 TestSelectPlans(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
planDesc := "testing"
|
|
idToUse := 1
|
|
userIDToUse := 2
|
|
|
|
str, mock := getDbMock(t)
|
|
|
|
rows := sqlmock.NewRows([]string{"plan_id", "plan_description", "user_id"}).AddRow(idToUse, planDesc, userIDToUse)
|
|
mock.ExpectQuery(`^SELECT plan_id, plan_description, user_id FROM plans WHERE user_id = \$1`).
|
|
WithArgs(userIDToUse).
|
|
WillReturnRows(rows)
|
|
|
|
plans, err := str.SelectPlans(userIDToUse)
|
|
assert.Nil(err)
|
|
assert.Equal(1, len(plans))
|
|
plan := plans[0]
|
|
assert.EqualValues(idToUse, plan.PlanID)
|
|
assert.Equal(planDesc, plan.PlanDescription)
|
|
assert.EqualValues(userIDToUse, plan.UserID)
|
|
|
|
if err := mock.ExpectationsWereMet(); err != nil {
|
|
t.Errorf("unfulfilled expectations: %s", err)
|
|
}
|
|
}
|
|
|
|
func TestSelectPlanByID(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
planDesc := "tsaoeu"
|
|
idToUse := 1
|
|
userIDToUse := 2
|
|
|
|
str, mock := getDbMock(t)
|
|
|
|
rows := sqlmock.NewRows([]string{"plan_id", "plan_description", "user_id"}).AddRow(idToUse, planDesc, userIDToUse)
|
|
mock.ExpectQuery(`^SELECT plan_id, plan_description, user_id FROM plans WHERE plan_id = \$1 AND user_id = \$2$`).
|
|
WithArgs(idToUse, userIDToUse).
|
|
WillReturnRows(rows)
|
|
|
|
plan, err := str.SelectPlanByID(idToUse, userIDToUse)
|
|
assert.Nil(err)
|
|
assert.EqualValues(idToUse, plan.PlanID)
|
|
assert.Equal(planDesc, plan.PlanDescription)
|
|
assert.EqualValues(userIDToUse, plan.UserID)
|
|
|
|
if err := mock.ExpectationsWereMet(); err != nil {
|
|
t.Errorf("unfulfilled expectations: %s", err)
|
|
}
|
|
}
|
|
|
|
func TestInsertPlan(t *testing.T) {
|
|
// setup
|
|
assert := assert.New(t)
|
|
|
|
str, mock := getDbMock(t)
|
|
planDescription := "2021-01-01"
|
|
userID := 2
|
|
badUserID := 7
|
|
|
|
plan := &models.Plan{PlanDescription: planDescription, UserID: int64(badUserID)}
|
|
|
|
idToUse := 8
|
|
|
|
rows := sqlmock.NewRows([]string{"plan_id"}).AddRow(8)
|
|
|
|
mock.ExpectBegin()
|
|
mock.ExpectQuery(`^INSERT INTO plans \(plan_description, user_id\) VALUES \(\$1, \$2\) RETURNING plan_id$`).
|
|
WithArgs(planDescription, userID).
|
|
WillReturnRows(rows)
|
|
mock.ExpectCommit()
|
|
|
|
// function under test
|
|
insertedId, err := str.InsertPlan(plan, userID)
|
|
// check results
|
|
assert.Nil(err)
|
|
assert.EqualValues(idToUse, insertedId)
|
|
if err := mock.ExpectationsWereMet(); err != nil {
|
|
t.Errorf("unfulfilled expectations: %s", err)
|
|
}
|
|
|
|
}
|
|
|
|
func TestInsertPlanErr(t *testing.T) {
|
|
// setup
|
|
assert := assert.New(t)
|
|
|
|
str, mock := getDbMock(t)
|
|
planDescription := "2021-01-01"
|
|
userID := 2
|
|
badUserID := 7
|
|
plan := &models.Plan{PlanDescription: planDescription, UserID: int64(badUserID)}
|
|
|
|
mock.ExpectBegin()
|
|
mock.ExpectQuery(`^INSERT INTO plans \(plan_description, user_id\) VALUES \(\$1, \$2\) RETURNING plan_id$`).
|
|
WithArgs(planDescription, userID).
|
|
WillReturnError(fmt.Errorf("example error"))
|
|
mock.ExpectRollback()
|
|
|
|
// function under test
|
|
_, err := str.InsertPlan(plan, userID)
|
|
// check results
|
|
assert.NotNil(err)
|
|
if err := mock.ExpectationsWereMet(); err != nil {
|
|
t.Errorf("unfulfilled expectations: %s", err)
|
|
}
|
|
|
|
}
|
|
|
|
func TestInsertPlanCommitErr(t *testing.T) {
|
|
// setup
|
|
assert := assert.New(t)
|
|
|
|
str, mock := getDbMock(t)
|
|
planDescription := "2021-01-01"
|
|
userID := 2
|
|
plan := &models.Plan{PlanDescription: planDescription, UserID: int64(userID)}
|
|
idToUse := 8
|
|
|
|
rows := sqlmock.NewRows([]string{"plan_id"}).AddRow(idToUse)
|
|
|
|
mock.ExpectBegin()
|
|
mock.ExpectQuery(`^INSERT INTO plans \(plan_description, user_id\) VALUES \(\$1, \$2\) RETURNING plan_id$`).
|
|
WithArgs(planDescription, userID).
|
|
WillReturnRows(rows)
|
|
mock.ExpectCommit().WillReturnError(fmt.Errorf("another error example"))
|
|
|
|
// function under test
|
|
_, err := str.InsertPlan(plan, userID)
|
|
// check results
|
|
assert.NotNil(err)
|
|
if err := mock.ExpectationsWereMet(); err != nil {
|
|
t.Errorf("unfulfilled expectations: %s", err)
|
|
}
|
|
|
|
}
|
|
|
|
func TestErrPlanByID(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
idToUse := 1
|
|
|
|
str, mock := getDbMock(t)
|
|
|
|
mock.ExpectQuery(`^SELECT plan_id, plan_description, 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, 8)
|
|
assert.NotNil(err)
|
|
assert.Nil(plan)
|
|
|
|
if err := mock.ExpectationsWereMet(); err != nil {
|
|
t.Errorf("unfulfilled expectations: %s", err)
|
|
}
|
|
}
|
|
|
|
func TestErrPlans(t *testing.T) {
|
|
// set up tests
|
|
assert := assert.New(t)
|
|
str, mock := getDbMock(t)
|
|
|
|
mock.ExpectQuery(`^SELECT plan_id, plan_description, user_id FROM plans WHERE user_id = \$1$`).
|
|
WithArgs(8).
|
|
WillReturnError(fmt.Errorf("example error"))
|
|
// function under test
|
|
plans, err := str.SelectPlans(8)
|
|
// test results
|
|
assert.Nil(plans)
|
|
assert.NotNil(err)
|
|
if err := mock.ExpectationsWereMet(); err != nil {
|
|
t.Errorf("unfulfilled expectations: %s", err)
|
|
}
|
|
}
|
|
|
|
func TestUpdatePlan(t *testing.T) {
|
|
// setup
|
|
assert := assert.New(t)
|
|
|
|
str, mock := getDbMock(t)
|
|
planDescription := "2021-01-01"
|
|
userID := 2
|
|
idToUse := 8
|
|
|
|
plan := &models.Plan{PlanDescription: planDescription, UserID: int64(userID), PlanID: int64(idToUse)}
|
|
|
|
mock.ExpectBegin()
|
|
mock.ExpectExec(`^UPDATE plans SET plan_description = \$1 WHERE plan_id = \$2 AND user_id = \$3$`).
|
|
WithArgs(planDescription, idToUse, userID).
|
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
|
mock.ExpectCommit()
|
|
|
|
// function under test
|
|
err := str.UpdatePlan(plan, userID)
|
|
// check results
|
|
assert.Nil(err)
|
|
if err := mock.ExpectationsWereMet(); err != nil {
|
|
t.Errorf("unfulfilled expectations: %s", err)
|
|
}
|
|
}
|
|
func TestUpdatePlanErr(t *testing.T) {
|
|
// setup
|
|
assert := assert.New(t)
|
|
|
|
str, mock := getDbMock(t)
|
|
planDescription := "2021-01-01"
|
|
userID := 2
|
|
idToUse := 8
|
|
|
|
plan := &models.Plan{PlanDescription: planDescription, UserID: int64(userID), PlanID: int64(idToUse)}
|
|
|
|
mock.ExpectBegin()
|
|
mock.ExpectExec(`^UPDATE plans SET plan_description = \$1 WHERE plan_id = \$2 AND user_id = \$3$`).
|
|
WithArgs(planDescription, idToUse, userID).
|
|
WillReturnError(fmt.Errorf("example error"))
|
|
mock.ExpectRollback()
|
|
|
|
// function under test
|
|
err := str.UpdatePlan(plan, userID)
|
|
// check results
|
|
assert.NotNil(err)
|
|
if err := mock.ExpectationsWereMet(); err != nil {
|
|
t.Errorf("unfulfilled expectations: %s", err)
|
|
}
|
|
|
|
}
|
|
|
|
func TestUpdatePlanCommitErr(t *testing.T) {
|
|
// setup
|
|
assert := assert.New(t)
|
|
|
|
str, mock := getDbMock(t)
|
|
planDescription := "2021-01-01"
|
|
userID := 2
|
|
idToUse := 8
|
|
|
|
plan := &models.Plan{PlanDescription: planDescription, UserID: int64(userID), PlanID: int64(idToUse)}
|
|
|
|
mock.ExpectBegin()
|
|
mock.ExpectExec(`^UPDATE plans SET plan_description = \$1 WHERE plan_id = \$2 AND user_id = \$3$`).
|
|
WithArgs(planDescription, idToUse, userID).
|
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
|
mock.ExpectCommit().WillReturnError(fmt.Errorf("another error example"))
|
|
|
|
// function under test
|
|
err := str.UpdatePlan(plan, userID)
|
|
// check results
|
|
assert.NotNil(err)
|
|
if err := mock.ExpectationsWereMet(); err != nil {
|
|
t.Errorf("unfulfilled expectations: %s", err)
|
|
}
|
|
|
|
}
|