Compare commits
3 Commits
0c6f686ce5
...
9f5f5413d1
| Author | SHA1 | Date | |
|---|---|---|---|
|
9f5f5413d1
|
|||
|
9c56ea56e2
|
|||
|
b6a6c9375f
|
@@ -1,7 +1,7 @@
|
||||
app:
|
||||
environment: "devel"
|
||||
port: 5151
|
||||
|
||||
timezone: Africa/Abidjan
|
||||
db:
|
||||
type: "aoeu"
|
||||
host: "aeihn"
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
type AppConfig struct {
|
||||
Environment string
|
||||
Port string
|
||||
Timezone string
|
||||
}
|
||||
|
||||
// DBConfig is the config for the DB connection.
|
||||
@@ -33,6 +34,7 @@ func createDefaultConf() *Conf {
|
||||
App: AppConfig{
|
||||
Environment: "local",
|
||||
Port: "8080",
|
||||
Timezone: "America/New_York",
|
||||
},
|
||||
Db: DBConfig{
|
||||
Type: "postgres",
|
||||
|
||||
@@ -16,6 +16,7 @@ func TestSample(t *testing.T) {
|
||||
appConf := conf.App
|
||||
assert.Equal("devel", appConf.Environment)
|
||||
assert.Equal("5151", appConf.Port)
|
||||
assert.Equal("Africa/Abidjan", appConf.Timezone)
|
||||
|
||||
dbConf := conf.Db
|
||||
assert.Equal("aoeu", dbConf.Type)
|
||||
@@ -35,6 +36,7 @@ func TestDefault(t *testing.T) {
|
||||
appConf := conf.App
|
||||
assert.Equal("missingfield", appConf.Environment)
|
||||
assert.Equal("8080", appConf.Port)
|
||||
assert.Equal("America/New_York", appConf.Timezone)
|
||||
|
||||
dbConf := conf.Db
|
||||
assert.Equal("typical", dbConf.Type)
|
||||
|
||||
@@ -6,6 +6,7 @@ type Store interface {
|
||||
SelectActionByID(id int) (*Action, error)
|
||||
SelectPlans() ([]*Plan, error)
|
||||
SelectPlanByID(id int) (*Plan, error)
|
||||
InsertPlan(plan *Plan) (int, error)
|
||||
}
|
||||
|
||||
// Model represents a current model item.
|
||||
|
||||
@@ -27,6 +27,10 @@ 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 TestModelActions(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
a1 := &models.Action{ActionID: 3}
|
||||
@@ -53,3 +57,18 @@ func TestModelActions(t *testing.T) {
|
||||
assert.Nil(err)
|
||||
assert.EqualValues(6, firstPlan.PlanID)
|
||||
}
|
||||
|
||||
func TestModelInsertPlan(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
p := &models.Plan{PlanID: 7}
|
||||
|
||||
ss := &multiStore{
|
||||
[]*models.Action{},
|
||||
[]*models.Plan{p},
|
||||
}
|
||||
m := models.New(ss)
|
||||
|
||||
planId, err := m.AddPlan(p)
|
||||
assert.Nil(err)
|
||||
assert.EqualValues(7, planId)
|
||||
}
|
||||
|
||||
@@ -19,3 +19,8 @@ func (m *Model) Plans() ([]*Plan, error) {
|
||||
func (m *Model) Plan(id int) (*Plan, error) {
|
||||
return m.SelectPlanByID(id)
|
||||
}
|
||||
|
||||
// AddPlan inserts a given plan into the store, returning the generated PlanID. The provided PlanID is ignored.
|
||||
func (m *Model) AddPlan(plan *Plan) (int, error) {
|
||||
return m.InsertPlan(plan)
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
"gitea.deepak.science/deepak/gogmagog/models"
|
||||
"gitea.deepak.science/deepak/gogmagog/util"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
type postgresStore struct {
|
||||
@@ -53,3 +52,19 @@ func (store *postgresStore) SelectPlanByID(id int) (*models.Plan, error) {
|
||||
}
|
||||
return &plan, nil
|
||||
}
|
||||
|
||||
func (store *postgresStore) InsertPlan(plan *models.Plan) (int, error) {
|
||||
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) {
|
||||
assert := assert.New(t)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user