Adds insert plan function
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:
@@ -1,7 +1,6 @@
|
|||||||
package store
|
package store
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"gitea.deepak.science/deepak/gogmagog/models"
|
"gitea.deepak.science/deepak/gogmagog/models"
|
||||||
"gitea.deepak.science/deepak/gogmagog/util"
|
"gitea.deepak.science/deepak/gogmagog/util"
|
||||||
"github.com/jmoiron/sqlx"
|
"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) {
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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) {
|
func TestErrPlanByID(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user