2022-05-21 12:09:19 +00:00
|
|
|
/*
|
2024-06-08 08:29:35 +00:00
|
|
|
Copyright CAcert Inc.
|
2022-05-21 12:09:19 +00:00
|
|
|
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-29 13:36:27 +00:00
|
|
|
func prepareTestDb(t *testing.T) *sqlx.DB {
|
2022-05-21 11:51:17 +00:00
|
|
|
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-29 13:36:27 +00:00
|
|
|
return dbx
|
2022-05-21 11:51:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecisionModel_Create(t *testing.T) {
|
2022-05-29 13:36:27 +00:00
|
|
|
dbx := prepareTestDb(t)
|
2022-05-21 11:51:17 +00:00
|
|
|
|
2022-05-29 13:36:27 +00:00
|
|
|
dm := models.MotionModel{DB: dbx}
|
2022-05-15 18:10:49 +00:00
|
|
|
|
2022-05-26 13:27:25 +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",
|
2022-06-04 12:48:24 +00:00
|
|
|
Reminder: sql.NullString{String: "test+voter@example.com", Valid: true},
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2022-05-29 13:36:27 +00:00
|
|
|
dbx := prepareTestDb(t)
|
2022-05-21 11:51:17 +00:00
|
|
|
|
2022-05-29 13:36:27 +00:00
|
|
|
dm := models.MotionModel{DB: dbx}
|
2022-05-21 11:51:17 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
nextDue *time.Time
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2022-06-04 17:30:58 +00:00
|
|
|
nextDue, err = dm.NextPendingDue(context.Background(), time.Now().UTC())
|
2022-05-21 11:51:17 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Empty(t, nextDue)
|
|
|
|
|
2022-05-26 13:27:25 +00:00
|
|
|
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",
|
2022-06-04 12:48:24 +00:00
|
|
|
Reminder: sql.NullString{String: "test+voter@example.com", Valid: true},
|
2022-05-21 11:51:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-06-04 17:30:58 +00:00
|
|
|
nextDue, err = dm.NextPendingDue(ctx, time.Now().UTC())
|
2022-05-21 11:51:17 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, nextDue)
|
|
|
|
|
|
|
|
assert.Equal(t, due.UTC(), *nextDue)
|
|
|
|
}
|