Adds insert plan function
All checks were successful
gitea-deepak/gogmagog/pipeline/head This commit looks good

This commit is contained in:
Deepak Mallubhotla 2020-12-29 18:44:37 -06:00
parent 9c56ea56e2
commit 9f5f5413d1
Signed by: deepak
GPG Key ID: 64BF53A3369104E7
2 changed files with 42 additions and 2 deletions

View File

@ -1,7 +1,6 @@
package store
import (
"fmt"
"gitea.deepak.science/deepak/gogmagog/models"
"gitea.deepak.science/deepak/gogmagog/util"
"github.com/jmoiron/sqlx"
@ -55,5 +54,17 @@ func (store *postgresStore) SelectPlanByID(id int) (*models.Plan, error) {
}
func (store *postgresStore) InsertPlan(plan *models.Plan) (int, error) {
return 0, fmt.Errorf("Unimplemented method")
queryString := store.db.Rebind("INSERT INTO plans (plan_date) VALUES (?) RETURNING plan_id")
tx := store.db.MustBegin()
var id int
err := tx.Get(&id, queryString, plan.PlanDate)
if err != nil {
tx.Rollback()
return -1, err
}
err = tx.Commit()
if err != nil {
return -1, err
}
return id, nil
}

View File

@ -68,6 +68,35 @@ func TestSelectPlanByID(t *testing.T) {
}
}
func TestInsertPlan(t *testing.T) {
// setup
assert := assert.New(t)
str, mock := getDbMock(t)
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
plan := &models.Plan{PlanDate: planDate}
idToUse := 8
rows := sqlmock.NewRows([]string{"plan_id"}).AddRow(8)
mock.ExpectBegin()
mock.ExpectQuery("^INSERT INTO plans \\(plan_date\\) VALUES \\(\\$1\\) RETURNING plan_id$").
WithArgs(planDate).
WillReturnRows(rows)
mock.ExpectCommit()
// function under test
insertedId, err := str.InsertPlan(plan)
// check results
assert.Nil(err)
assert.EqualValues(idToUse, insertedId)
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("unfulfilled expectations: %s", err)
}
}
func TestErrPlanByID(t *testing.T) {
assert := assert.New(t)