Removed multistore
All checks were successful
gitea-deepak/gogmagog/pipeline/head This commit looks good
All checks were successful
gitea-deepak/gogmagog/pipeline/head This commit looks good
This commit is contained in:
parent
d8604dc3cc
commit
1c3555d8b7
@ -2,81 +2,33 @@ package models_test
|
||||
|
||||
import (
|
||||
"gitea.deepak.science/deepak/gogmagog/models"
|
||||
"gitea.deepak.science/deepak/gogmagog/store"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type multiStore struct {
|
||||
actions []*models.Action
|
||||
plans []*models.Plan
|
||||
}
|
||||
|
||||
func (ms *multiStore) SelectActions() ([]*models.Action, error) {
|
||||
return ms.actions, nil
|
||||
}
|
||||
|
||||
func (ms *multiStore) SelectActionByID(id int) (*models.Action, error) {
|
||||
return ms.actions[0], nil
|
||||
}
|
||||
|
||||
func (ms *multiStore) InsertAction(action *models.Action) (int, error) {
|
||||
return int(action.ActionID), nil
|
||||
}
|
||||
|
||||
func (ms *multiStore) UpdateAction(action *models.Action) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ms *multiStore) SelectPlans() ([]*models.Plan, error) {
|
||||
return ms.plans, nil
|
||||
}
|
||||
|
||||
func (ms *multiStore) SelectPlanByID(id int) (*models.Plan, error) {
|
||||
return ms.plans[0], nil
|
||||
}
|
||||
|
||||
func (ms *multiStore) InsertPlan(plan *models.Plan) (int, error) {
|
||||
return int(plan.PlanID), nil
|
||||
}
|
||||
|
||||
func (ms *multiStore) SelectActionsByPlanID(plan *models.Plan) ([]*models.Action, error) {
|
||||
return ms.actions, nil
|
||||
}
|
||||
|
||||
func (ms *multiStore) SelectUserByUsername(username string) (*models.User, error) {
|
||||
// password is "password"
|
||||
return &models.User{UserID: int64(1), Username: username, DisplayName: "Ted Est", Password: []byte("$2y$05$6SVV35GX4cB4PDPhRaDD/exsL.HV8QtMMr60YL6dLyqtX4l58q.cy")}, nil
|
||||
}
|
||||
|
||||
func (ms *multiStore) InsertUser(user *models.User) (int, error) {
|
||||
return int(user.UserID), nil
|
||||
}
|
||||
|
||||
func (ms *multiStore) ConnectionLive() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestModelActions(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
a1 := &models.Action{ActionID: 3}
|
||||
a2 := &models.Action{ActionID: 4}
|
||||
p := &models.Plan{PlanID: 6}
|
||||
ss := &multiStore{
|
||||
[]*models.Action{a1, a2},
|
||||
[]*models.Plan{p}}
|
||||
m := models.New(ss)
|
||||
|
||||
str, _ := store.GetInMemoryStore()
|
||||
str.InsertAction(a1)
|
||||
str.InsertPlan(p)
|
||||
m := models.New(str)
|
||||
|
||||
actions, err := m.Actions()
|
||||
assert.Nil(err)
|
||||
assert.Equal(2, len(actions))
|
||||
assert.Equal(1, len(actions))
|
||||
|
||||
firstAction, err := m.Action(3)
|
||||
firstAction, err := m.Action(1)
|
||||
assert.Nil(err)
|
||||
assert.EqualValues(3, firstAction.ActionID)
|
||||
assert.EqualValues(1, firstAction.ActionID)
|
||||
|
||||
actionID, err := m.AddAction(a1)
|
||||
actionID, err := m.AddAction(a2)
|
||||
assert.Nil(err)
|
||||
assert.EqualValues(3, actionID)
|
||||
assert.EqualValues(2, actionID)
|
||||
|
||||
err = m.SaveAction(a1)
|
||||
assert.Nil(err)
|
||||
@ -84,41 +36,38 @@ func TestModelActions(t *testing.T) {
|
||||
|
||||
func TestModelPlanMethods(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
a1 := &models.Action{ActionID: 3}
|
||||
a1 := &models.Action{ActionID: 3, PlanID: 1}
|
||||
a2 := &models.Action{ActionID: 4}
|
||||
p := &models.Plan{PlanID: 6}
|
||||
p := &models.Plan{}
|
||||
|
||||
ss := &multiStore{
|
||||
[]*models.Action{a1, a2},
|
||||
[]*models.Plan{p},
|
||||
}
|
||||
m := models.New(ss)
|
||||
str, _ := store.GetInMemoryStore()
|
||||
str.InsertPlan(p)
|
||||
str.InsertAction(a1)
|
||||
str.InsertAction(a2)
|
||||
m := models.New(str)
|
||||
|
||||
plans, err := m.Plans()
|
||||
assert.Nil(err)
|
||||
assert.Equal(1, len(plans))
|
||||
|
||||
firstPlan, err := m.Plan(6)
|
||||
firstPlan, err := m.Plan(1)
|
||||
assert.Nil(err)
|
||||
assert.EqualValues(6, firstPlan.PlanID)
|
||||
assert.EqualValues(1, firstPlan.PlanID)
|
||||
|
||||
actions, err := m.GetActions(firstPlan)
|
||||
assert.Nil(err)
|
||||
assert.Equal(2, len(actions))
|
||||
assert.Equal(1, len(actions))
|
||||
|
||||
planId, err := m.AddPlan(p)
|
||||
planId, err := m.AddPlan(&models.Plan{})
|
||||
assert.Nil(err)
|
||||
assert.EqualValues(6, planId)
|
||||
assert.EqualValues(2, planId)
|
||||
}
|
||||
|
||||
func TestModelHealthy(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
ss := &multiStore{
|
||||
[]*models.Action{},
|
||||
[]*models.Plan{},
|
||||
}
|
||||
m := models.New(ss)
|
||||
str, _ := store.GetInMemoryStore()
|
||||
m := models.New(str)
|
||||
|
||||
err := m.Healthy()
|
||||
assert.Nil(err)
|
||||
|
@ -3,25 +3,37 @@ package models_test
|
||||
import (
|
||||
"fmt"
|
||||
"gitea.deepak.science/deepak/gogmagog/models"
|
||||
"gitea.deepak.science/deepak/gogmagog/store"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestModelUsers(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
a1 := &models.Action{ActionID: 3}
|
||||
a2 := &models.Action{ActionID: 4}
|
||||
p := &models.Plan{PlanID: 6}
|
||||
ss := &multiStore{
|
||||
[]*models.Action{a1, a2},
|
||||
[]*models.Plan{p}}
|
||||
m := models.New(ss)
|
||||
|
||||
user, err := m.VerifyUserByUsernamePassword("test", "password")
|
||||
username := "test1"
|
||||
// password := password
|
||||
user1 := &models.User{Username: username, DisplayName: "Ted Est", Password: []byte("$2y$05$6SVV35GX4cB4PDPhRaDD/exsL.HV8QtMMr60YL6dLyqtX4l58q.cy")}
|
||||
str, _ := store.GetInMemoryStore()
|
||||
str.InsertPlan(p)
|
||||
str.InsertAction(a1)
|
||||
str.InsertAction(a2)
|
||||
str.InsertUser(user1)
|
||||
m := models.New(str)
|
||||
|
||||
user, err := m.VerifyUserByUsernamePassword("test1", "password")
|
||||
assert.Nil(err)
|
||||
assert.NotNil(user)
|
||||
|
||||
user, err = m.VerifyUserByUsernamePassword("test", "wrong_password")
|
||||
user, err = m.VerifyUserByUsernamePassword("test1", "wrong_password")
|
||||
assert.NotNil(err)
|
||||
assert.Nil(user)
|
||||
|
||||
user, err = m.VerifyUserByUsernamePassword("test2", "password")
|
||||
assert.NotNil(err)
|
||||
assert.Nil(user)
|
||||
}
|
||||
@ -42,13 +54,12 @@ func TestCreateUser(t *testing.T) {
|
||||
pass := "abc"
|
||||
u := &models.CreateUserRequest{Username: username, DisplayName: displayName, Password: pass}
|
||||
|
||||
ss := &multiStore{
|
||||
[]*models.Action{},
|
||||
[]*models.Plan{}}
|
||||
m := models.New(ss)
|
||||
str, _ := store.GetInMemoryStore()
|
||||
m := models.New(str)
|
||||
|
||||
_, err := m.CreateUser(u)
|
||||
id, err := m.CreateUser(u)
|
||||
assert.Nil(err)
|
||||
assert.EqualValues(1, id)
|
||||
}
|
||||
func TestCreateUserFailValidation(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
@ -57,10 +68,8 @@ func TestCreateUserFailValidation(t *testing.T) {
|
||||
pass := "abc"
|
||||
u := &models.CreateUserRequest{Username: username, DisplayName: displayName, Password: pass}
|
||||
|
||||
ss := &multiStore{
|
||||
[]*models.Action{},
|
||||
[]*models.Plan{}}
|
||||
m := models.New(ss)
|
||||
str, _ := store.GetInMemoryStore()
|
||||
m := models.New(str)
|
||||
|
||||
_, err := m.CreateUser(u)
|
||||
assert.NotNil(err)
|
||||
@ -73,10 +82,8 @@ func TestCreateUserFailValidationPassword(t *testing.T) {
|
||||
pass := ""
|
||||
u := &models.CreateUserRequest{Username: username, DisplayName: displayName, Password: pass}
|
||||
|
||||
ss := &multiStore{
|
||||
[]*models.Action{},
|
||||
[]*models.Plan{}}
|
||||
m := models.New(ss)
|
||||
str, _ := store.GetInMemoryStore()
|
||||
m := models.New(str)
|
||||
|
||||
_, err := m.CreateUser(u)
|
||||
assert.NotNil(err)
|
||||
|
@ -40,7 +40,9 @@ func TestOneAction(t *testing.T) {
|
||||
updatedDate, _ := time.Parse("2006-01-02", "2021-01-02")
|
||||
completedDate, _ := time.Parse("2006-01-02", "2021-01-03")
|
||||
a1 := &models.Action{ActionID: 3, ActionDescription: "testing", CompletedChunks: 1, CompletedOn: &completedDate, CreatedAt: &createdDate, UpdatedAt: &updatedDate, EstimatedChunks: 3, PlanID: 0}
|
||||
m := getModel([]*models.Plan{}, []*models.Action{a1})
|
||||
m := getEmptyModel()
|
||||
m.AddAction(a1)
|
||||
|
||||
router := routes.NewActionRouter(m)
|
||||
req, _ := http.NewRequest("GET", "/", nil)
|
||||
|
||||
@ -55,7 +57,7 @@ func TestOneAction(t *testing.T) {
|
||||
// We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp.
|
||||
expected := `[
|
||||
{
|
||||
"action_id": 3,
|
||||
"action_id": 1,
|
||||
"action_description": "testing",
|
||||
"estimated_chunks": 3,
|
||||
"completed_chunks": 1,
|
||||
@ -118,9 +120,10 @@ func TestOneActionByID(t *testing.T) {
|
||||
createdDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
updatedDate, _ := time.Parse("2006-01-02", "2021-01-02")
|
||||
a := &models.Action{ActionID: 6, ActionDescription: "howdy", CompletedOn: nil, CreatedAt: &createdDate, UpdatedAt: &updatedDate, CompletedChunks: 0, EstimatedChunks: 54, PlanID: 3}
|
||||
m := getModel([]*models.Plan{}, []*models.Action{a})
|
||||
m := getEmptyModel()
|
||||
m.InsertAction(a)
|
||||
router := routes.NewActionRouter(m)
|
||||
req, _ := http.NewRequest("GET", "/6", nil)
|
||||
req, _ := http.NewRequest("GET", "/1", nil)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
@ -132,7 +135,7 @@ func TestOneActionByID(t *testing.T) {
|
||||
assert.Equal(http.StatusOK, status)
|
||||
// We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp.
|
||||
expected := `{
|
||||
"action_id": 6,
|
||||
"action_id": 1,
|
||||
"action_description": "howdy",
|
||||
"estimated_chunks": 54,
|
||||
"completed_chunks": 0,
|
||||
@ -172,10 +175,11 @@ func TestEmptyActionErrorWriterByID(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
a := &models.Action{ActionID: 6}
|
||||
m := getModel([]*models.Plan{}, []*models.Action{a})
|
||||
m := getEmptyModel()
|
||||
m.AddAction(a)
|
||||
|
||||
router := routes.NewActionRouter(m)
|
||||
req, _ := http.NewRequest("GET", "/6", nil)
|
||||
req, _ := http.NewRequest("GET", "/1", nil)
|
||||
|
||||
rr := NewBadWriter()
|
||||
|
||||
@ -232,8 +236,9 @@ func TestActionsByPlanID(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
createdDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
updatedDate, _ := time.Parse("2006-01-02", "2021-01-02")
|
||||
a := &models.Action{ActionID: 6, ActionDescription: "howdy", CompletedOn: nil, CreatedAt: &createdDate, UpdatedAt: &updatedDate, CompletedChunks: 0, EstimatedChunks: 54, PlanID: 3}
|
||||
m := getModel([]*models.Plan{}, []*models.Action{a})
|
||||
a := &models.Action{ActionID: 1, ActionDescription: "howdy", CompletedOn: nil, CreatedAt: &createdDate, UpdatedAt: &updatedDate, CompletedChunks: 0, EstimatedChunks: 54, PlanID: 6}
|
||||
m := getEmptyModel()
|
||||
m.AddAction(a)
|
||||
router := routes.NewActionRouter(m)
|
||||
req, _ := http.NewRequest("GET", "/?plan_id=6", nil)
|
||||
|
||||
@ -248,13 +253,13 @@ func TestActionsByPlanID(t *testing.T) {
|
||||
// We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp.
|
||||
expected := `[
|
||||
{
|
||||
"action_id": 6,
|
||||
"action_id": 1,
|
||||
"action_description": "howdy",
|
||||
"estimated_chunks": 54,
|
||||
"completed_chunks": 0,
|
||||
"updated_at": "2021-01-02T00:00:00Z",
|
||||
"created_at": "2021-01-01T00:00:00Z",
|
||||
"plan_id": 3
|
||||
"plan_id": 6
|
||||
}
|
||||
]`
|
||||
assert.JSONEq(expected, rr.Body.String())
|
||||
@ -268,7 +273,8 @@ func TestActionsByPlanIDInvalidID(t *testing.T) {
|
||||
createdDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
updatedDate, _ := time.Parse("2006-01-02", "2021-01-02")
|
||||
a := &models.Action{ActionID: 6, ActionDescription: "howdy", CompletedOn: nil, CreatedAt: &createdDate, UpdatedAt: &updatedDate, CompletedChunks: 0, EstimatedChunks: 54, PlanID: 3}
|
||||
m := getModel([]*models.Plan{}, []*models.Action{a})
|
||||
m := getEmptyModel()
|
||||
m.AddAction(a)
|
||||
router := routes.NewActionRouter(m)
|
||||
req, _ := http.NewRequest("GET", "/?plan_id=aoeu", nil)
|
||||
|
||||
|
@ -38,7 +38,8 @@ func TestOnePlan(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
||||
m := getModel([]*models.Plan{p}, []*models.Action{})
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p)
|
||||
router := routes.NewPlanRouter(m)
|
||||
req, _ := http.NewRequest("GET", "/", nil)
|
||||
|
||||
@ -53,7 +54,7 @@ func TestOnePlan(t *testing.T) {
|
||||
// We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp.
|
||||
expected := `[
|
||||
{
|
||||
"plan_id": 6,
|
||||
"plan_id": 1,
|
||||
"plan_date": "2021-01-01T00:00:00Z"
|
||||
}
|
||||
]`
|
||||
@ -109,9 +110,10 @@ func TestOnePlanByID(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
||||
m := getModel([]*models.Plan{p}, []*models.Action{})
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p)
|
||||
router := routes.NewPlanRouter(m)
|
||||
req, _ := http.NewRequest("GET", "/6", nil)
|
||||
req, _ := http.NewRequest("GET", "/1", nil)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
@ -123,7 +125,7 @@ func TestOnePlanByID(t *testing.T) {
|
||||
assert.Equal(http.StatusOK, status)
|
||||
// We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp.
|
||||
expected := `{
|
||||
"plan_id": 6,
|
||||
"plan_id": 1,
|
||||
"plan_date": "2021-01-01T00:00:00Z"
|
||||
}`
|
||||
assert.JSONEq(expected, rr.Body.String())
|
||||
@ -158,11 +160,12 @@ func TestEmptyPlanErrorWriterByID(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
|
||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
||||
m := getModel([]*models.Plan{p}, []*models.Action{})
|
||||
p := &models.Plan{PlanID: 1, PlanDate: &planDate}
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p)
|
||||
|
||||
router := routes.NewPlanRouter(m)
|
||||
req, _ := http.NewRequest("GET", "/6", nil)
|
||||
req, _ := http.NewRequest("GET", "/1", nil)
|
||||
|
||||
rr := NewBadWriter()
|
||||
|
||||
|
@ -24,7 +24,8 @@ func TestPureJSONPostAction(t *testing.T) {
|
||||
CompletedChunks: 2,
|
||||
ActionDescription: "here's an action",
|
||||
}
|
||||
m := getModel([]*models.Plan{}, []*models.Action{a})
|
||||
m := getEmptyModel()
|
||||
m.AddAction(a)
|
||||
router := routes.NewActionRouter(m)
|
||||
data := []byte(`{
|
||||
"action_description": "here's an action",
|
||||
@ -51,9 +52,9 @@ func TestPureJSONPostAction(t *testing.T) {
|
||||
"completed_chunks": 2,
|
||||
"completed_on": "2021-01-01T00:00:00Z",
|
||||
"plan_id": 5,
|
||||
"action_id": 0
|
||||
"action_id": 2
|
||||
},
|
||||
"id": 0
|
||||
"id": 2
|
||||
}`
|
||||
assert.JSONEq(expected, rr.Body.String())
|
||||
contentType := rr.Header().Get("Content-Type")
|
||||
@ -65,7 +66,8 @@ func TestExtraFieldActionPostJSON(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
||||
m := getModel([]*models.Plan{p}, []*models.Action{})
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p)
|
||||
router := routes.NewActionRouter(m)
|
||||
data := []byte(`{
|
||||
"completed_on": "2021-01-01T00:00:00Z",
|
||||
@ -91,7 +93,8 @@ func TestEmptyBodyActionPost(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
||||
m := getModel([]*models.Plan{p}, []*models.Action{})
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p)
|
||||
router := routes.NewActionRouter(m)
|
||||
data := []byte(``)
|
||||
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
|
||||
@ -114,7 +117,8 @@ func TestTwoBodyActionPost(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
||||
m := getModel([]*models.Plan{p}, []*models.Action{})
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p)
|
||||
router := routes.NewActionRouter(m)
|
||||
data := []byte(`{
|
||||
"plan_id": 5
|
||||
@ -190,7 +194,8 @@ func TestErrorWriterCreateAction(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
a := &models.Action{PlanID: 6}
|
||||
m := getModel([]*models.Plan{}, []*models.Action{a})
|
||||
m := getEmptyModel()
|
||||
m.AddAction(a)
|
||||
|
||||
router := routes.NewActionRouter(m)
|
||||
data, _ := json.Marshal(a)
|
||||
|
@ -18,7 +18,8 @@ func TestCreatePlanRoute(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
||||
m := getModel([]*models.Plan{p}, []*models.Action{})
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p)
|
||||
router := routes.NewPlanRouter(m)
|
||||
data, _ := json.Marshal(p)
|
||||
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
|
||||
@ -34,10 +35,10 @@ func TestCreatePlanRoute(t *testing.T) {
|
||||
// We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp.
|
||||
expected := `{
|
||||
"created_plan": {
|
||||
"plan_id": 6,
|
||||
"plan_id": 2,
|
||||
"plan_date": "2021-01-01T00:00:00Z"
|
||||
},
|
||||
"id": 0
|
||||
"id": 2
|
||||
}`
|
||||
assert.JSONEq(expected, rr.Body.String())
|
||||
contentType := rr.Header().Get("Content-Type")
|
||||
@ -48,12 +49,13 @@ func TestPureJSON(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
||||
m := getModel([]*models.Plan{p}, []*models.Action{})
|
||||
p := &models.Plan{PlanID: 1, PlanDate: &planDate}
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p)
|
||||
router := routes.NewPlanRouter(m)
|
||||
data := []byte(`{
|
||||
"plan_date": "2021-01-01T00:00:00Z",
|
||||
"plan_id": 5
|
||||
"plan_id": 1
|
||||
}`)
|
||||
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
@ -68,10 +70,10 @@ func TestPureJSON(t *testing.T) {
|
||||
// We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp.
|
||||
expected := `{
|
||||
"created_plan": {
|
||||
"plan_id": 6,
|
||||
"plan_id": 2,
|
||||
"plan_date": "2021-01-01T00:00:00Z"
|
||||
},
|
||||
"id": 0
|
||||
"id": 2
|
||||
}`
|
||||
assert.JSONEq(expected, rr.Body.String())
|
||||
contentType := rr.Header().Get("Content-Type")
|
||||
@ -83,7 +85,8 @@ func TestExtraFieldJSON(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
||||
m := getModel([]*models.Plan{p}, []*models.Action{})
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p)
|
||||
router := routes.NewPlanRouter(m)
|
||||
data := []byte(`{
|
||||
"plan_date": "2021-01-01T00:00:00Z",
|
||||
@ -109,7 +112,8 @@ func TestEmptyBody(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
||||
m := getModel([]*models.Plan{p}, []*models.Action{})
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p)
|
||||
router := routes.NewPlanRouter(m)
|
||||
data := []byte(``)
|
||||
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
|
||||
@ -132,7 +136,8 @@ func TestTwoBody(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
||||
m := getModel([]*models.Plan{p}, []*models.Action{})
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p)
|
||||
router := routes.NewPlanRouter(m)
|
||||
data := []byte(`{
|
||||
"plan_date": "2021-01-01T00:00:00Z",
|
||||
@ -213,8 +218,8 @@ func TestErrorWriterCreatePlan(t *testing.T) {
|
||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
|
||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
||||
m := getModel([]*models.Plan{p}, []*models.Action{})
|
||||
|
||||
m := getEmptyModel()
|
||||
m.AddPlan(p)
|
||||
router := routes.NewPlanRouter(m)
|
||||
data, _ := json.Marshal(p)
|
||||
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
|
||||
|
@ -20,11 +20,12 @@ func TestPureJSONPutAction(t *testing.T) {
|
||||
a := &models.Action{
|
||||
PlanID: 5,
|
||||
CompletedOn: &compOn,
|
||||
EstimatedChunks: 3,
|
||||
CompletedChunks: 2,
|
||||
ActionDescription: "here's an action",
|
||||
EstimatedChunks: 1,
|
||||
CompletedChunks: 1,
|
||||
ActionDescription: "hn",
|
||||
}
|
||||
m := getModel([]*models.Plan{}, []*models.Action{a})
|
||||
m := getEmptyModel()
|
||||
m.AddAction(a)
|
||||
router := routes.NewActionRouter(m)
|
||||
data := []byte(`{
|
||||
"action_description": "here's an action",
|
||||
@ -33,7 +34,7 @@ func TestPureJSONPutAction(t *testing.T) {
|
||||
"completed_on": "2021-01-01T00:00:00Z",
|
||||
"plan_id": 5
|
||||
}`)
|
||||
req, _ := http.NewRequest("PUT", "/0", bytes.NewBuffer(data))
|
||||
req, _ := http.NewRequest("PUT", "/1", bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@ -51,9 +52,9 @@ func TestPureJSONPutAction(t *testing.T) {
|
||||
"completed_chunks": 2,
|
||||
"completed_on": "2021-01-01T00:00:00Z",
|
||||
"plan_id": 5,
|
||||
"action_id": 0
|
||||
"action_id": 1
|
||||
},
|
||||
"id": 0
|
||||
"id": 1
|
||||
}`
|
||||
assert.JSONEq(expected, rr.Body.String())
|
||||
contentType := rr.Header().Get("Content-Type")
|
||||
@ -63,9 +64,7 @@ func TestPureJSONPutAction(t *testing.T) {
|
||||
func TestExtraFieldActionPutJSON(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
||||
m := getModel([]*models.Plan{p}, []*models.Action{})
|
||||
m := getEmptyModel()
|
||||
router := routes.NewActionRouter(m)
|
||||
data := []byte(`{
|
||||
"completed_on": "2021-01-01T00:00:00Z",
|
||||
@ -89,9 +88,7 @@ func TestExtraFieldActionPutJSON(t *testing.T) {
|
||||
func TestEmptyBodyActionPut(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
||||
m := getModel([]*models.Plan{p}, []*models.Action{})
|
||||
m := getEmptyModel()
|
||||
router := routes.NewActionRouter(m)
|
||||
data := []byte(``)
|
||||
req, _ := http.NewRequest("PUT", "/1", bytes.NewBuffer(data))
|
||||
@ -112,9 +109,7 @@ func TestEmptyBodyActionPut(t *testing.T) {
|
||||
func TestTwoBodyActionPut(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
||||
m := getModel([]*models.Plan{p}, []*models.Action{})
|
||||
m := getEmptyModel()
|
||||
router := routes.NewActionRouter(m)
|
||||
data := []byte(`{
|
||||
"plan_id": 5
|
||||
@ -139,9 +134,7 @@ func TestTwoBodyActionPut(t *testing.T) {
|
||||
func TestBadActionIDPut(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
|
||||
m := getModel([]*models.Plan{p}, []*models.Action{})
|
||||
m := getEmptyModel()
|
||||
router := routes.NewActionRouter(m)
|
||||
data := []byte(`{
|
||||
"plan_id": 5
|
||||
@ -217,7 +210,8 @@ func TestErrorWriterUpdateAction(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
a := &models.Action{PlanID: 6}
|
||||
m := getModel([]*models.Plan{}, []*models.Action{a})
|
||||
m := getEmptyModel()
|
||||
m.AddAction(a)
|
||||
|
||||
router := routes.NewActionRouter(m)
|
||||
data, _ := json.Marshal(a)
|
||||
|
@ -3,13 +3,9 @@ package routes_test
|
||||
import (
|
||||
"fmt"
|
||||
"gitea.deepak.science/deepak/gogmagog/models"
|
||||
"gitea.deepak.science/deepak/gogmagog/store"
|
||||
)
|
||||
|
||||
type multiStore struct {
|
||||
actions []*models.Action
|
||||
plans []*models.Plan
|
||||
}
|
||||
|
||||
type testNotFoundError struct {
|
||||
error
|
||||
}
|
||||
@ -18,75 +14,11 @@ func (t *testNotFoundError) NotFound() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (ms *multiStore) SelectActions() ([]*models.Action, error) {
|
||||
return ms.actions, nil
|
||||
}
|
||||
|
||||
func (ms *multiStore) SelectActionByID(id int) (*models.Action, error) {
|
||||
if len(ms.actions) < 1 {
|
||||
err := &testNotFoundError{fmt.Errorf("too small")}
|
||||
return nil, err
|
||||
}
|
||||
return ms.actions[0], nil
|
||||
}
|
||||
|
||||
func (ms *multiStore) InsertAction(action *models.Action) (int, error) {
|
||||
return int(action.ActionID), nil
|
||||
}
|
||||
|
||||
func (ms *multiStore) UpdateAction(action *models.Action) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ms *multiStore) SelectPlans() ([]*models.Plan, error) {
|
||||
return ms.plans, nil
|
||||
}
|
||||
|
||||
func (ms *multiStore) SelectPlanByID(id int) (*models.Plan, error) {
|
||||
if len(ms.plans) < 1 {
|
||||
err := &testNotFoundError{fmt.Errorf("too small")}
|
||||
return nil, err
|
||||
}
|
||||
return ms.plans[0], nil
|
||||
}
|
||||
|
||||
func (ms *multiStore) InsertPlan(plan *models.Plan) (int, error) {
|
||||
return int(plan.PlanID), nil
|
||||
}
|
||||
|
||||
func (ms *multiStore) SelectActionsByPlanID(plan *models.Plan) ([]*models.Action, error) {
|
||||
return ms.actions, nil
|
||||
}
|
||||
|
||||
func (ms *multiStore) SelectUserByUsername(name string) (*models.User, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (ms *multiStore) InsertUser(user *models.User) (int, error) {
|
||||
return int(user.UserID), nil
|
||||
}
|
||||
|
||||
func (ms *multiStore) ConnectionLive() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func getEmptyModel() *models.Model {
|
||||
ss := &multiStore{
|
||||
[]*models.Action{},
|
||||
[]*models.Plan{},
|
||||
}
|
||||
m := models.New(ss)
|
||||
str, _ := store.GetInMemoryStore()
|
||||
m := models.New(str)
|
||||
return m
|
||||
}
|
||||
func getModel(plns []*models.Plan, acts []*models.Action) *models.Model {
|
||||
ss := &multiStore{
|
||||
actions: acts,
|
||||
plans: plns,
|
||||
}
|
||||
m := models.New(ss)
|
||||
return m
|
||||
}
|
||||
|
||||
func (e *errorStore) SelectActions() ([]*models.Action, error) {
|
||||
return nil, e.error
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user