cacert-boardvoting/internal/models/decisions_test.go

83 lines
1.9 KiB
Go
Raw Normal View History

2022-05-15 18:10:49 +00:00
package models_test
import (
"database/sql"
"log"
"os"
"path"
"testing"
"time"
"git.cacert.org/cacert-boardvoting/internal"
"git.cacert.org/cacert-boardvoting/internal/models"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
2022-05-21 11:51:17 +00:00
func prepareTestDb(t *testing.T) (*sqlx.DB, *log.Logger) {
t.Helper()
2022-05-15 18:10:49 +00:00
testDir := t.TempDir()
db, err := sql.Open("sqlite3", path.Join(testDir, "test.sqlite"))
require.NoError(t, err)
dbx := sqlx.NewDb(db, "sqlite3")
logger := log.New(os.Stdout, "", log.LstdFlags)
err = internal.InitializeDb(dbx.DB, logger)
require.NoError(t, err)
2022-05-21 11:51:17 +00:00
return dbx, logger
}
func TestDecisionModel_Create(t *testing.T) {
dbx, logger := prepareTestDb(t)
2022-05-15 18:10:49 +00:00
dm := models.DecisionModel{DB: dbx, InfoLog: logger}
v := &models.Voter{
ID: 1, // sqlite does not check referential integrity. Might fail with a foreign key index.
Name: "test voter",
Reminder: "test+voter@example.com",
}
id, err := dm.Create(v, models.VoteTypeMotion, "test motion", "I move that we should test more", time.Now(), time.Now().AddDate(0, 0, 7))
assert.NoError(t, err)
assert.NotEmpty(t, id)
}
2022-05-21 11:51:17 +00:00
func TestDecisionModel_GetNextPendingDecisionDue(t *testing.T) {
dbx, logger := prepareTestDb(t)
dm := models.DecisionModel{DB: dbx, InfoLog: logger}
var (
nextDue *time.Time
err error
)
nextDue, err = dm.GetNextPendingDecisionDue()
assert.NoError(t, err)
assert.Empty(t, nextDue)
v := &models.Voter{
ID: 1, // sqlite does not check referential integrity. Might fail with a foreign key index.
Name: "test voter",
Reminder: "test+voter@example.com",
}
due := time.Now().Add(10 * time.Minute)
_, err = dm.Create(v, models.VoteTypeMotion, "test motion", "I move that we should test more", time.Now(), due)
require.NoError(t, err)
nextDue, err = dm.GetNextPendingDecisionDue()
assert.NoError(t, err)
assert.NotEmpty(t, nextDue)
assert.Equal(t, due.UTC(), *nextDue)
}