From d8604dc3ccbae66f172a8da126a68e36fb868e94 Mon Sep 17 00:00:00 2001 From: Deepak Date: Tue, 12 Jan 2021 18:16:47 -0600 Subject: [PATCH] Adds inmemorystore to help refactor tests --- store/inmemory.go | 106 +++++++++++++++++++++++++++++++++++++++++ store/inmemory_test.go | 104 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 210 insertions(+) create mode 100644 store/inmemory.go create mode 100644 store/inmemory_test.go diff --git a/store/inmemory.go b/store/inmemory.go new file mode 100644 index 0000000..bfaac57 --- /dev/null +++ b/store/inmemory.go @@ -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 +} diff --git a/store/inmemory_test.go b/store/inmemory_test.go new file mode 100644 index 0000000..3617e76 --- /dev/null +++ b/store/inmemory_test.go @@ -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) +}