Adds tests for model code
This commit is contained in:
parent
92ddd9e0fe
commit
e420bf303a
45
models/current_plan_test.go
Normal file
45
models/current_plan_test.go
Normal file
@ -0,0 +1,45 @@
|
||||
package models_test
|
||||
|
||||
import (
|
||||
"gitea.deepak.science/deepak/gogmagog/models"
|
||||
"gitea.deepak.science/deepak/gogmagog/store"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestModelCurrentPlan(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
a1 := &models.Action{ActionID: 3}
|
||||
userID := 3
|
||||
p := &models.Plan{PlanID: 6}
|
||||
|
||||
str, _ := store.GetInMemoryStore()
|
||||
str.InsertAction(a1, userID)
|
||||
str.InsertPlan(p, userID)
|
||||
str.InsertPlan(p, userID)
|
||||
m := models.New(str)
|
||||
|
||||
_, err := m.CurrentPlan(userID)
|
||||
assert.NotNil(err)
|
||||
assert.True(models.IsNotFoundError(err))
|
||||
|
||||
err = m.AddCurrentPlan(&models.CurrentPlan{PlanID: 1}, userID)
|
||||
assert.Nil(err)
|
||||
|
||||
pp, err := m.CurrentPlan(userID)
|
||||
assert.Nil(err)
|
||||
assert.EqualValues(1, pp.PlanID)
|
||||
assert.EqualValues(userID, pp.UserID)
|
||||
|
||||
err = m.AddCurrentPlan(&models.CurrentPlan{PlanID: 2}, userID)
|
||||
assert.NotNil(err)
|
||||
|
||||
err = m.SaveCurrentPlan(&models.CurrentPlan{PlanID: 2}, userID)
|
||||
assert.Nil(err)
|
||||
|
||||
pp, err = m.CurrentPlan(userID)
|
||||
assert.Nil(err)
|
||||
assert.EqualValues(2, pp.PlanID)
|
||||
assert.EqualValues(userID, pp.UserID)
|
||||
|
||||
}
|
@ -111,12 +111,12 @@ func (store *inMemoryStore) SelectCurrentPlan(userID int) (*models.CurrentPlan,
|
||||
|
||||
func (store *inMemoryStore) InsertCurrentPlan(currentPlan *models.CurrentPlan, userID int) error {
|
||||
_, err := store.SelectCurrentPlan(userID)
|
||||
if err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
if err == nil {
|
||||
return fmt.Errorf("Can't insert primary plan")
|
||||
}
|
||||
if err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
|
||||
store.currentPlans = append(store.currentPlans, &models.CurrentPlan{PlanID: int64(currentPlan.PlanID), UserID: int64(userID)})
|
||||
return nil
|
||||
|
Loading…
x
Reference in New Issue
Block a user