All checks were successful
gitea-deepak/gogmagog/pipeline/head This commit looks good
114 lines
3.0 KiB
Go
114 lines
3.0 KiB
Go
package store_test
|
|
|
|
import (
|
|
"fmt"
|
|
"gitea.deepak.science/deepak/gogmagog/models"
|
|
"gitea.deepak.science/deepak/gogmagog/store"
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
"github.com/jmoiron/sqlx"
|
|
"github.com/stretchr/testify/assert"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func getDbMock(t *testing.T) (models.Store, sqlmock.Sqlmock) {
|
|
db, mock, err := sqlmock.New()
|
|
if err != nil {
|
|
t.Fatalf("got an error creating a stub db. How?: %s", err)
|
|
}
|
|
str, err := store.GetPostgresStore(sqlx.NewDb(db, "pgx"))
|
|
if err != nil {
|
|
t.Errorf("Error creating postgres store: %s", err)
|
|
}
|
|
|
|
return str, mock
|
|
}
|
|
|
|
func TestSelectPlans(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
currentTime := time.Now()
|
|
idToUse := 1
|
|
|
|
str, mock := getDbMock(t)
|
|
|
|
rows := sqlmock.NewRows([]string{"plan_id", "plan_date"}).AddRow(idToUse, currentTime)
|
|
mock.ExpectQuery("^SELECT plan_id, plan_date FROM plans$").WillReturnRows(rows)
|
|
|
|
plans, err := str.SelectPlans()
|
|
assert.Nil(err)
|
|
assert.Equal(1, len(plans))
|
|
plan := plans[0]
|
|
assert.EqualValues(idToUse, plan.PlanID)
|
|
assert.Equal(currentTime, plan.PlanDate)
|
|
|
|
if err := mock.ExpectationsWereMet(); err != nil {
|
|
t.Errorf("unfulfilled expectations: %s", err)
|
|
}
|
|
}
|
|
|
|
func TestSelectActions(t *testing.T) {
|
|
// set up test
|
|
assert := assert.New(t)
|
|
|
|
createTime, _ := time.Parse("2006-01-02", "2020-12-31")
|
|
updateTime, _ := time.Parse("2006-01-02", "2021-01-01")
|
|
idToUse := 1
|
|
desc := "Howdy, partner."
|
|
|
|
str, mock := getDbMock(t)
|
|
|
|
rows := sqlmock.NewRows([]string{"action_id", "action_description", "created_at", "updated_at"}).
|
|
AddRow(idToUse, desc, createTime, updateTime).
|
|
AddRow(idToUse+1, desc, createTime, updateTime)
|
|
mock.ExpectQuery("^SELECT action_id, action_description, created_at, updated_at FROM actions$").WillReturnRows(rows)
|
|
|
|
// function under test
|
|
actions, err := str.SelectActions()
|
|
|
|
// test results
|
|
assert.Nil(err)
|
|
assert.Equal(2, len(actions))
|
|
action := actions[0]
|
|
assert.EqualValues(idToUse, action.ActionID)
|
|
assert.Equal(desc, action.ActionDescription)
|
|
assert.Equal(createTime, action.CreatedAt)
|
|
assert.Equal(updateTime, action.UpdatedAt)
|
|
|
|
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_date FROM plans$").WillReturnError(fmt.Errorf("example error"))
|
|
// function under test
|
|
plans, err := str.SelectPlans()
|
|
// test results
|
|
assert.Nil(plans)
|
|
assert.NotNil(err)
|
|
if err := mock.ExpectationsWereMet(); err != nil {
|
|
t.Errorf("unfulfilled expectations: %s", err)
|
|
}
|
|
}
|
|
|
|
func TestErrActions(t *testing.T) {
|
|
// set up tests
|
|
assert := assert.New(t)
|
|
str, mock := getDbMock(t)
|
|
|
|
mock.ExpectQuery("^SELECT action_id, action_description, created_at, updated_at FROM actions$").WillReturnError(fmt.Errorf("example error"))
|
|
// function under test
|
|
actions, err := str.SelectActions()
|
|
// test results
|
|
assert.Nil(actions)
|
|
assert.NotNil(err)
|
|
if err := mock.ExpectationsWereMet(); err != nil {
|
|
t.Errorf("unfulfilled expectations: %s", err)
|
|
}
|
|
}
|