Uses plan description instead of date

This commit is contained in:
Deepak Mallubhotla 2021-02-02 15:58:18 -06:00
parent b47abbfc0b
commit 5e030a5bc3
Signed by: deepak
GPG Key ID: 64BF53A3369104E7
10 changed files with 78 additions and 89 deletions

View File

@ -1,14 +1,10 @@
package models
import (
"time"
)
// Plan represents a single day's agenda of actions.
type Plan struct {
PlanID int64 `json:"plan_id"`
PlanDate *time.Time `json:"plan_date"`
UserID int64 `json:"user_id"`
PlanID int64 `json:"plan_id"`
PlanDescription string `json:"plan_description"`
UserID int64 `json:"user_id"`
}
// Plans returns all plans in the model.

View File

@ -10,7 +10,6 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestEmptyActionEmptyContext(t *testing.T) {
@ -34,8 +33,8 @@ func TestEmptyActionEmptyContext(t *testing.T) {
func TestOneActionEmptyContext(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, UserID: 3}
planDescription := "2006-01-02"
p := &models.Plan{PlanID: 6, PlanDescription: planDescription, UserID: 3}
m := getEmptyModel()
m.AddPlan(p, 3)
router := routes.NewActionRouter(m)
@ -55,8 +54,8 @@ func TestOneActionEmptyContext(t *testing.T) {
func TestOneActionByIDEmptyContext(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, UserID: 3}
planDescription := "2021-01-01"
p := &models.Plan{PlanID: 6, PlanDescription: planDescription, UserID: 3}
m := getEmptyModel()
m.AddPlan(p, 3)
router := routes.NewActionRouter(m)
@ -79,7 +78,7 @@ func TestPureJSONActionEmptyContext(t *testing.T) {
m := getEmptyModel()
router := routes.NewActionRouter(m)
data := []byte(`{
"plan_date": "2021-01-01T00:00:00Z",
"plan_description": "2021-01-01T00:00:00Z",
"plan_id": 1,
"user_id": 3
}`)

View File

@ -99,7 +99,7 @@ func postPlanFunc(m *models.Model) http.HandlerFunc {
}
// Map the fields we allow to be set to the plan to be created.
plan := &models.Plan{PlanDate: p.PlanDate, UserID: p.UserID}
plan := &models.Plan{PlanDescription: p.PlanDescription, UserID: p.UserID}
id, err := m.AddPlan(plan, userID)
if err != nil {
serverError(w, err)

View File

@ -9,7 +9,6 @@ import (
"net/http/httptest"
"strings"
"testing"
"time"
)
var sampleContext = tokens.GetContextForUserValues(3, "testing")
@ -39,8 +38,8 @@ func TestEmptyPlans(t *testing.T) {
func TestOnePlan(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, UserID: 3}
planDescription := "2021-01-01"
p := &models.Plan{PlanID: 6, PlanDescription: planDescription, UserID: 3}
m := getEmptyModel()
m.AddPlan(p, 3)
router := routes.NewPlanRouter(m)
@ -58,7 +57,7 @@ func TestOnePlan(t *testing.T) {
expected := `[
{
"plan_id": 1,
"plan_date": "2021-01-01T00:00:00Z",
"plan_description": "2021-01-01",
"user_id": 3
}
]`
@ -84,7 +83,6 @@ func TestErrorPlan(t *testing.T) {
// check results
status := rr.Code
assert.Equal(http.StatusInternalServerError, status)
// We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp.
expected := `Internal Server Error`
assert.Equal(expected, strings.TrimSpace(rr.Body.String()))
}
@ -112,8 +110,8 @@ func TestEmptyPlanErrorWriter(t *testing.T) {
func TestOnePlanByID(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, UserID: 3}
planDescription := "2021-01-01"
p := &models.Plan{PlanID: 6, PlanDescription: planDescription, UserID: 3}
m := getEmptyModel()
m.AddPlan(p, 3)
router := routes.NewPlanRouter(m)
@ -127,10 +125,10 @@ func TestOnePlanByID(t *testing.T) {
// check results
status := rr.Code
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": 1,
"plan_date": "2021-01-01T00:00:00Z",
"plan_description": "2021-01-01",
"user_id": 3
}`
assert.JSONEq(expected, rr.Body.String())
@ -163,9 +161,8 @@ func TestErrorPlanByID(t *testing.T) {
func TestEmptyPlanErrorWriterByID(t *testing.T) {
// set up
assert := assert.New(t)
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
p := &models.Plan{PlanID: 1, PlanDate: &planDate}
planDescription := "2021-01-01"
p := &models.Plan{PlanID: 1, PlanDescription: planDescription}
m := getEmptyModel()
m.AddPlan(p, 3)

View File

@ -9,7 +9,6 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestEmptyPlanEmptyContext(t *testing.T) {
@ -33,8 +32,8 @@ func TestEmptyPlanEmptyContext(t *testing.T) {
func TestOnePlanEmptyContext(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, UserID: 3}
planDescription := "2021-01-01"
p := &models.Plan{PlanID: 6, PlanDescription: planDescription, UserID: 3}
m := getEmptyModel()
m.AddPlan(p, 3)
router := routes.NewPlanRouter(m)
@ -54,8 +53,8 @@ func TestOnePlanEmptyContext(t *testing.T) {
func TestOnePlanByIDEmptyContext(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, UserID: 3}
planDescription := "2021-01-01"
p := &models.Plan{PlanID: 6, PlanDescription: planDescription, UserID: 3}
m := getEmptyModel()
m.AddPlan(p, 3)
router := routes.NewPlanRouter(m)
@ -78,7 +77,7 @@ func TestPureJSONEmptyContext(t *testing.T) {
m := getEmptyModel()
router := routes.NewPlanRouter(m)
data := []byte(`{
"plan_date": "2021-01-01T00:00:00Z",
"plan_description": "2021-01-01T00:00:00Z",
"plan_id": 1,
"user_id": 3
}`)

View File

@ -65,8 +65,8 @@ func TestPureJSONPostAction(t *testing.T) {
func TestExtraFieldActionPostJSON(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}
planDescription := "2021-01-01"
p := &models.Plan{PlanID: 6, PlanDescription: planDescription}
m := getEmptyModel()
m.AddPlan(p, 3)
router := routes.NewActionRouter(m)
@ -92,8 +92,8 @@ func TestExtraFieldActionPostJSON(t *testing.T) {
func TestEmptyBodyActionPost(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}
planDescription := "2021-01-01"
p := &models.Plan{PlanID: 6, PlanDescription: planDescription}
m := getEmptyModel()
m.AddPlan(p, 3)
router := routes.NewActionRouter(m)
@ -116,8 +116,8 @@ func TestEmptyBodyActionPost(t *testing.T) {
func TestTwoBodyActionPost(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}
planDescription := "2021-01-01"
p := &models.Plan{PlanID: 6, PlanDescription: planDescription}
m := getEmptyModel()
m.AddPlan(p, 3)
router := routes.NewActionRouter(m)

View File

@ -10,15 +10,14 @@ import (
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestCreatePlanRoute(t *testing.T) {
// set up
assert := assert.New(t)
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
planDescription := "2021-01-01"
userID := 3
p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: int64(userID)}
p := &models.Plan{PlanID: 6, PlanDescription: planDescription, UserID: int64(userID)}
m := getEmptyModel()
m.AddPlan(p, userID)
router := routes.NewPlanRouter(m)
@ -37,7 +36,7 @@ func TestCreatePlanRoute(t *testing.T) {
expected := `{
"created_plan": {
"plan_id": 2,
"plan_date": "2021-01-01T00:00:00Z",
"plan_description": "2021-01-01",
"user_id": 3
},
"id": 2
@ -50,14 +49,14 @@ func TestCreatePlanRoute(t *testing.T) {
func TestPureJSON(t *testing.T) {
// set up
assert := assert.New(t)
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
planDescription := "2021-01-01"
userID := 3
p := &models.Plan{PlanID: 1, PlanDate: &planDate, UserID: int64(userID)}
p := &models.Plan{PlanID: 1, PlanDescription: planDescription, UserID: int64(userID)}
m := getEmptyModel()
m.AddPlan(p, userID)
router := routes.NewPlanRouter(m)
data := []byte(`{
"plan_date": "2021-01-01T00:00:00Z",
"plan_description": "2021-01-01",
"plan_id": 1,
"user_id": 3
}`)
@ -76,7 +75,7 @@ func TestPureJSON(t *testing.T) {
"created_plan": {
"plan_id": 2,
"user_id": 3,
"plan_date": "2021-01-01T00:00:00Z"
"plan_description": "2021-01-01"
},
"id": 2
}`
@ -88,14 +87,14 @@ func TestPureJSON(t *testing.T) {
func TestExtraFieldJSON(t *testing.T) {
// set up
assert := assert.New(t)
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
planDescription := "2021-01-01"
userID := 3
p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: int64(userID)}
p := &models.Plan{PlanID: 6, PlanDescription: planDescription, UserID: int64(userID)}
m := getEmptyModel()
m.AddPlan(p, userID)
router := routes.NewPlanRouter(m)
data := []byte(`{
"plan_date": "2021-01-01T00:00:00Z",
"plan_description": "2021-01-01",
"plan_id": 5,
"plan_sabotage": "omg"
}`)
@ -116,9 +115,9 @@ func TestExtraFieldJSON(t *testing.T) {
func TestEmptyBody(t *testing.T) {
// set up
assert := assert.New(t)
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
planDescription := "2021-01-01"
userID := 3
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
p := &models.Plan{PlanID: 6, PlanDescription: planDescription}
m := getEmptyModel()
m.AddPlan(p, userID)
router := routes.NewPlanRouter(m)
@ -141,16 +140,16 @@ func TestEmptyBody(t *testing.T) {
func TestTwoBody(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}
planDescription := "2021-01-01"
p := &models.Plan{PlanID: 6, PlanDescription: planDescription}
m := getEmptyModel()
m.AddPlan(p, 3)
router := routes.NewPlanRouter(m)
data := []byte(`{
"plan_date": "2021-01-01T00:00:00Z",
"plan_description": "2021-01-01",
"plan_id": 5
}, {
"plan_date": "2021-01-01T00:00:00Z",
"plan_description": "2021-01-01",
"plan_id": 6
}`)
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
@ -175,8 +174,8 @@ func TestErrorCreatePlan(t *testing.T) {
m := getErrorModel("Model always errors")
router := routes.NewPlanRouter(m)
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
planDescription := "2021-01-01"
p := &models.Plan{PlanID: 6, PlanDescription: planDescription}
data, _ := json.Marshal(p)
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")
@ -201,8 +200,8 @@ func TestErrorOnRetrieveCreatePlan(t *testing.T) {
m := getErrorOnGetModel("Model always errors")
router := routes.NewPlanRouter(m)
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
planDescription := "2021-01-01"
p := &models.Plan{PlanID: 6, PlanDescription: planDescription}
data, _ := json.Marshal(p)
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")
@ -222,9 +221,9 @@ func TestErrorOnRetrieveCreatePlan(t *testing.T) {
func TestErrorWriterCreatePlan(t *testing.T) {
// set up
assert := assert.New(t)
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
planDescription := "2021-01-01"
p := &models.Plan{PlanID: 6, PlanDate: &planDate}
p := &models.Plan{PlanID: 6, PlanDescription: planDescription}
m := getEmptyModel()
m.AddPlan(p, 3)
router := routes.NewPlanRouter(m)

View File

@ -9,7 +9,7 @@ CREATE TABLE IF NOT EXISTS users(
CREATE TABLE IF NOT EXISTS plans(
plan_id serial PRIMARY KEY,
plan_date DATE NOT NULL,
plan_description VARCHAR (500) NOT NULL,
user_id int REFERENCES users(user_id),
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,

View File

@ -112,7 +112,7 @@ func (store *postgresStore) UpdateAction(action *models.Action, userID int) erro
}
func (store *postgresStore) SelectPlans(userID int) ([]*models.Plan, error) {
queryString := store.db.Rebind("SELECT plan_id, plan_date, user_id FROM plans WHERE user_id = ?")
queryString := store.db.Rebind("SELECT plan_id, plan_description, user_id FROM plans WHERE user_id = ?")
plans := make([]*models.Plan, 0)
err := store.db.Select(&plans, queryString, userID)
if err != nil {
@ -123,7 +123,7 @@ func (store *postgresStore) SelectPlans(userID int) ([]*models.Plan, error) {
func (store *postgresStore) SelectPlanByID(id int, userID int) (*models.Plan, error) {
plan := models.Plan{}
err := store.db.Get(&plan, store.db.Rebind("SELECT plan_id, plan_date, user_id FROM plans WHERE plan_id = ? AND user_id = ?"), id, userID)
err := store.db.Get(&plan, store.db.Rebind("SELECT plan_id, plan_description, user_id FROM plans WHERE plan_id = ? AND user_id = ?"), id, userID)
if err != nil {
return nil, err
}
@ -131,10 +131,10 @@ func (store *postgresStore) SelectPlanByID(id int, userID int) (*models.Plan, er
}
func (store *postgresStore) InsertPlan(plan *models.Plan, userID int) (int, error) {
queryString := store.db.Rebind("INSERT INTO plans (plan_date, user_id) VALUES (?, ?) RETURNING plan_id")
queryString := store.db.Rebind("INSERT INTO plans (plan_description, user_id) VALUES (?, ?) RETURNING plan_id")
tx := store.db.MustBegin()
var id int
err := tx.Get(&id, queryString, plan.PlanDate, userID)
err := tx.Get(&id, queryString, plan.PlanDescription, userID)
if err != nil {
tx.Rollback()
return -1, err

View File

@ -6,20 +6,19 @@ import (
"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestSelectPlans(t *testing.T) {
assert := assert.New(t)
currentTime := time.Now()
planDesc := "testing"
idToUse := 1
userIDToUse := 2
str, mock := getDbMock(t)
rows := sqlmock.NewRows([]string{"plan_id", "plan_date", "user_id"}).AddRow(idToUse, currentTime, userIDToUse)
mock.ExpectQuery(`^SELECT plan_id, plan_date, user_id FROM plans WHERE user_id = \$1`).
rows := sqlmock.NewRows([]string{"plan_id", "plan_description", "user_id"}).AddRow(idToUse, planDesc, userIDToUse)
mock.ExpectQuery(`^SELECT plan_id, plan_description, user_id FROM plans WHERE user_id = \$1`).
WithArgs(userIDToUse).
WillReturnRows(rows)
@ -28,7 +27,7 @@ func TestSelectPlans(t *testing.T) {
assert.Equal(1, len(plans))
plan := plans[0]
assert.EqualValues(idToUse, plan.PlanID)
assert.Equal(currentTime, *plan.PlanDate)
assert.Equal(planDesc, plan.PlanDescription)
assert.EqualValues(userIDToUse, plan.UserID)
if err := mock.ExpectationsWereMet(); err != nil {
@ -39,21 +38,21 @@ func TestSelectPlans(t *testing.T) {
func TestSelectPlanByID(t *testing.T) {
assert := assert.New(t)
currentTime := time.Now()
planDesc := "tsaoeu"
idToUse := 1
userIDToUse := 2
str, mock := getDbMock(t)
rows := sqlmock.NewRows([]string{"plan_id", "plan_date", "user_id"}).AddRow(idToUse, currentTime, userIDToUse)
mock.ExpectQuery(`^SELECT plan_id, plan_date, user_id FROM plans WHERE plan_id = \$1 AND user_id = \$2$`).
rows := sqlmock.NewRows([]string{"plan_id", "plan_description", "user_id"}).AddRow(idToUse, planDesc, userIDToUse)
mock.ExpectQuery(`^SELECT plan_id, plan_description, user_id FROM plans WHERE plan_id = \$1 AND user_id = \$2$`).
WithArgs(idToUse, userIDToUse).
WillReturnRows(rows)
plan, err := str.SelectPlanByID(idToUse, userIDToUse)
assert.Nil(err)
assert.EqualValues(idToUse, plan.PlanID)
assert.Equal(currentTime, *plan.PlanDate)
assert.Equal(planDesc, plan.PlanDescription)
assert.EqualValues(userIDToUse, plan.UserID)
if err := mock.ExpectationsWereMet(); err != nil {
@ -66,19 +65,19 @@ func TestInsertPlan(t *testing.T) {
assert := assert.New(t)
str, mock := getDbMock(t)
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
planDescription := "2021-01-01"
userID := 2
badUserID := 7
plan := &models.Plan{PlanDate: &planDate, UserID: int64(badUserID)}
plan := &models.Plan{PlanDescription: planDescription, UserID: int64(badUserID)}
idToUse := 8
rows := sqlmock.NewRows([]string{"plan_id"}).AddRow(8)
mock.ExpectBegin()
mock.ExpectQuery(`^INSERT INTO plans \(plan_date, user_id\) VALUES \(\$1, \$2\) RETURNING plan_id$`).
WithArgs(planDate, userID).
mock.ExpectQuery(`^INSERT INTO plans \(plan_description, user_id\) VALUES \(\$1, \$2\) RETURNING plan_id$`).
WithArgs(planDescription, userID).
WillReturnRows(rows)
mock.ExpectCommit()
@ -98,14 +97,14 @@ func TestInsertPlanErr(t *testing.T) {
assert := assert.New(t)
str, mock := getDbMock(t)
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
planDescription := "2021-01-01"
userID := 2
badUserID := 7
plan := &models.Plan{PlanDate: &planDate, UserID: int64(badUserID)}
plan := &models.Plan{PlanDescription: planDescription, UserID: int64(badUserID)}
mock.ExpectBegin()
mock.ExpectQuery(`^INSERT INTO plans \(plan_date, user_id\) VALUES \(\$1, \$2\) RETURNING plan_id$`).
WithArgs(planDate, userID).
mock.ExpectQuery(`^INSERT INTO plans \(plan_description, user_id\) VALUES \(\$1, \$2\) RETURNING plan_id$`).
WithArgs(planDescription, userID).
WillReturnError(fmt.Errorf("example error"))
mock.ExpectRollback()
@ -124,16 +123,16 @@ func TestInsertPlanCommitErr(t *testing.T) {
assert := assert.New(t)
str, mock := getDbMock(t)
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
planDescription := "2021-01-01"
userID := 2
plan := &models.Plan{PlanDate: &planDate, UserID: int64(userID)}
plan := &models.Plan{PlanDescription: planDescription, UserID: int64(userID)}
idToUse := 8
rows := sqlmock.NewRows([]string{"plan_id"}).AddRow(idToUse)
mock.ExpectBegin()
mock.ExpectQuery(`^INSERT INTO plans \(plan_date, user_id\) VALUES \(\$1, \$2\) RETURNING plan_id$`).
WithArgs(planDate, userID).
mock.ExpectQuery(`^INSERT INTO plans \(plan_description, user_id\) VALUES \(\$1, \$2\) RETURNING plan_id$`).
WithArgs(planDescription, userID).
WillReturnRows(rows)
mock.ExpectCommit().WillReturnError(fmt.Errorf("another error example"))
@ -154,7 +153,7 @@ func TestErrPlanByID(t *testing.T) {
str, mock := getDbMock(t)
mock.ExpectQuery(`^SELECT plan_id, plan_date, user_id FROM plans WHERE plan_id = \$1 AND user_id = \$2$`).
mock.ExpectQuery(`^SELECT plan_id, plan_description, user_id FROM plans WHERE plan_id = \$1 AND user_id = \$2$`).
WithArgs(idToUse, 8).
WillReturnError(fmt.Errorf("example error"))
@ -172,7 +171,7 @@ func TestErrPlans(t *testing.T) {
assert := assert.New(t)
str, mock := getDbMock(t)
mock.ExpectQuery(`^SELECT plan_id, plan_date, user_id FROM plans WHERE user_id = \$1$`).
mock.ExpectQuery(`^SELECT plan_id, plan_description, user_id FROM plans WHERE user_id = \$1$`).
WithArgs(8).
WillReturnError(fmt.Errorf("example error"))
// function under test