cacert-boardvoting/internal/models/motions_test.go

112 lines
2.6 KiB
Go
Raw Normal View History

2022-05-21 12:09:19 +00:00
/*
Copyright 2022 CAcert Inc.
SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2022-05-15 18:10:49 +00:00
package models_test
import (
2022-05-21 12:27:46 +00:00
"context"
2022-05-15 18:10:49 +00:00
"database/sql"
"log"
"os"
"path"
"testing"
"time"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2022-05-21 12:09:19 +00:00
"git.cacert.org/cacert-boardvoting/internal"
"git.cacert.org/cacert-boardvoting/internal/models"
2022-05-15 18:10:49 +00:00
)
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)
dm := models.MotionModel{DB: dbx, InfoLog: logger}
2022-05-15 18:10:49 +00:00
v := &models.User{
2022-05-15 18:10:49 +00:00
ID: 1, // sqlite does not check referential integrity. Might fail with a foreign key index.
Name: "test voter",
Reminder: "test+voter@example.com",
}
2022-05-21 12:09:19 +00:00
id, err := dm.Create(
2022-05-21 12:27:46 +00:00
context.Background(),
2022-05-21 12:09:19 +00:00
v,
models.VoteTypeMotion,
"test motion",
"I move that we should test more",
time.Now(),
time.Now().AddDate(0, 0, 7),
)
2022-05-15 18:10:49 +00:00
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.MotionModel{DB: dbx, InfoLog: logger}
2022-05-21 11:51:17 +00:00
var (
nextDue *time.Time
err error
)
2022-05-21 12:27:46 +00:00
nextDue, err = dm.NextPendingDecisionDue(context.Background())
2022-05-21 11:51:17 +00:00
assert.NoError(t, err)
assert.Empty(t, nextDue)
v := &models.User{
2022-05-21 11:51:17 +00:00
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)
2022-05-21 12:27:46 +00:00
ctx := context.Background()
_, err = dm.Create(ctx, v, models.VoteTypeMotion, "test motion", "I move that we should test more", time.Now(), due)
2022-05-21 11:51:17 +00:00
require.NoError(t, err)
2022-05-21 12:27:46 +00:00
nextDue, err = dm.NextPendingDecisionDue(ctx)
2022-05-21 11:51:17 +00:00
assert.NoError(t, err)
assert.NotEmpty(t, nextDue)
assert.Equal(t, due.UTC(), *nextDue)
}