gogmagog/store/postgres_test.go
Deepak 50bbcdc71d
All checks were successful
gitea-deepak/gogmagog/pipeline/head This commit looks good
adds full action fields to struct
2020-12-29 15:05:00 -06:00

129 lines
3.6 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")
completeTime, _ := time.Parse("2006-01-02", "2021-01-05")
idToUse := 1
estChunks := 5
compChunks := 7
desc := "Howdy, partner."
str, mock := getDbMock(t)
rows := sqlmock.NewRows([]string{
"action_id",
"action_description",
"estimated_chunks",
"completed_chunks",
"completed_on",
"created_at",
"updated_at",
"plan_id"}).
AddRow(idToUse, desc, estChunks, compChunks, completeTime, createTime, updateTime, idToUse).
AddRow(idToUse+1, desc, estChunks, compChunks, completeTime, createTime, updateTime, idToUse)
mock.ExpectQuery("^SELECT action_id, action_description, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id 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(estChunks, action.EstimatedChunks)
assert.Equal(compChunks, action.CompletedChunks)
assert.Equal(completeTime, action.CompletedOn)
assert.Equal(createTime, action.CreatedAt)
assert.Equal(updateTime, action.UpdatedAt)
assert.Equal(idToUse, action.PlanID)
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, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id 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)
}
}