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" ) func prepareTestDb(t *testing.T) (*sqlx.DB, *log.Logger) { t.Helper() 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) return dbx, logger } func TestDecisionModel_Create(t *testing.T) { dbx, logger := prepareTestDb(t) 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) } 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) }