Adds inmemorystore to help refactor tests
All checks were successful
gitea-deepak/gogmagog/pipeline/head This commit looks good

This commit is contained in:
Deepak Mallubhotla 2021-01-12 18:16:47 -06:00
parent 417a7cf982
commit d8604dc3cc
Signed by: deepak
GPG Key ID: 64BF53A3369104E7
2 changed files with 210 additions and 0 deletions

106
store/inmemory.go Normal file
View File

@ -0,0 +1,106 @@
package store
import (
"database/sql"
"gitea.deepak.science/deepak/gogmagog/models"
)
type inMemoryStore struct {
actions []*models.Action
plans []*models.Plan
users []*models.User
}
// GetInMemoryStore provides a purely in memory store, for testing purposes only, with no persistence.
func GetInMemoryStore() (models.Store, error) {
return &inMemoryStore{
actions: make([]*models.Action, 0),
plans: make([]*models.Plan, 0),
users: make([]*models.User, 0),
}, nil
}
func (store *inMemoryStore) SelectActions() ([]*models.Action, error) {
return store.actions, nil
}
func (store *inMemoryStore) SelectActionsByPlanID(plan *models.Plan) ([]*models.Action, error) {
ret := make([]*models.Action, 0)
for _, action := range store.actions {
if int(plan.PlanID) == int(action.PlanID) {
ret = append(ret, action)
}
}
return ret, nil
}
func (store *inMemoryStore) SelectActionByID(id int) (*models.Action, error) {
for _, action := range store.actions {
if id == int(action.ActionID) {
return action, nil
}
}
return nil, sql.ErrNoRows
}
func (store *inMemoryStore) InsertAction(action *models.Action) (int, error) {
id := len(store.actions) + 1
action.ActionID = int64(id)
store.actions = append(store.actions, action)
return id, nil
}
func (store *inMemoryStore) UpdateAction(action *models.Action) error {
currentAction, err := store.SelectActionByID(int(action.ActionID))
if err != nil {
return err
}
currentAction.ActionDescription = action.ActionDescription
currentAction.EstimatedChunks = action.EstimatedChunks
currentAction.CompletedChunks = action.CompletedChunks
currentAction.PlanID = action.PlanID
return nil
}
func (store *inMemoryStore) SelectPlans() ([]*models.Plan, error) {
return store.plans, nil
}
func (store *inMemoryStore) SelectPlanByID(id int) (*models.Plan, error) {
for _, plan := range store.plans {
if id == int(plan.PlanID) {
return plan, nil
}
}
return nil, sql.ErrNoRows
}
func (store *inMemoryStore) InsertPlan(plan *models.Plan) (int, error) {
id := len(store.plans) + 1
plan.PlanID = int64(id)
store.plans = append(store.plans, plan)
return id, nil
}
func (store *inMemoryStore) ConnectionLive() error {
return nil
}
func (store *inMemoryStore) SelectUserByUsername(username string) (*models.User, error) {
for _, user := range store.users {
if username == user.Username {
return user, nil
}
}
return nil, sql.ErrNoRows
}
// inMemoryStore.InsertUser will not enforce unique usernames, which is ok.
func (store *inMemoryStore) InsertUser(user *models.User) (int, error) {
id := len(store.users) + 1
user.UserID = int64(id)
store.users = append(store.users, user)
return id, nil
}

104
store/inmemory_test.go Normal file
View File

@ -0,0 +1,104 @@
package store_test
import (
"gitea.deepak.science/deepak/gogmagog/models"
"gitea.deepak.science/deepak/gogmagog/store"
"github.com/stretchr/testify/assert"
"testing"
)
func TestInMemoryActionMethods(t *testing.T) {
assert := assert.New(t)
str, _ := store.GetInMemoryStore()
sampleplanid := 8
act := &models.Action{}
a2 := &models.Action{PlanID: sampleplanid}
id, _ := str.InsertAction(act)
assert.EqualValues(1, id)
receivedAction, err := str.SelectActionByID(id)
assert.Nil(err)
assert.EqualValues(act, receivedAction)
allactions, err := str.SelectActions()
assert.Nil(err)
assert.EqualValues(1, len(allactions))
str.InsertAction(a2)
allactions, err = str.SelectActions()
assert.Nil(err)
assert.EqualValues(2, len(allactions))
planactions, err := str.SelectActionsByPlanID(&models.Plan{PlanID: int64(sampleplanid)})
assert.Nil(err)
assert.EqualValues(1, len(planactions))
assert.Equal(a2, planactions[0])
_, err = str.SelectActionByID(151)
assert.NotNil(err)
sampleDescription := "snth"
replacementAction := &models.Action{ActionID: 1, ActionDescription: sampleDescription}
err = str.UpdateAction(replacementAction)
assert.Nil(err)
assert.Equal(sampleDescription, act.ActionDescription)
replacementAction = &models.Action{ActionID: 1235122, ActionDescription: sampleDescription}
err = str.UpdateAction(replacementAction)
assert.NotNil(err)
}
func TestInMemoryPlanMethods(t *testing.T) {
assert := assert.New(t)
str, _ := store.GetInMemoryStore()
p := &models.Plan{}
plans, err := str.SelectPlans()
assert.Nil(err)
assert.EqualValues(0, len(plans))
id, err := str.InsertPlan(p)
plans, err = str.SelectPlans()
assert.Nil(err)
assert.EqualValues(1, len(plans))
retrievedPlan, err := str.SelectPlanByID(id)
assert.Nil(err)
assert.Equal(retrievedPlan, p)
_, err = str.SelectPlanByID(135135)
assert.NotNil(err)
}
func TestLive(t *testing.T) {
assert := assert.New(t)
str, _ := store.GetInMemoryStore()
err := str.ConnectionLive()
assert.Nil(err)
}
func TestInMemoryUserMethods(t *testing.T) {
assert := assert.New(t)
str, _ := store.GetInMemoryStore()
uname := "hiimauser"
u := &models.User{Username: uname}
id, err := str.InsertUser(u)
assert.Nil(err)
retrievedUser, err := str.SelectUserByUsername(uname)
assert.Nil(err)
assert.EqualValues(id, retrievedUser.UserID)
_, err = str.SelectUserByUsername("bad username")
assert.NotNil(err)
}