You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
themis/absences_test.go

59 lines
1.6 KiB

package themis
import (
"context"
"database/sql"
"fmt"
"testing"
"time"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAddAbsence(t *testing.T) {
db, err := sql.Open("sqlite3", fmt.Sprintf(TEST_CONN_STRING_PATTERN, "TestAddAbsence"))
require.NoError(t, err)
store, err := NewStore(db, zerolog.Nop())
require.NoError(t, err)
now := NextMonday()
assert.NoError(t, store.AddAbsence(context.TODO(), now, "foobarbaz"))
absentees, err := store.GetAbsentees(context.TODO(), now)
assert.NoError(t, err)
assert.Equal(t, 1, len(absentees))
assert.Equal(t, "foobarbaz", absentees[0])
assert.NoError(t, store.AddAbsence(context.TODO(), now, "foobarbaz"))
absentees, err = store.GetAbsentees(context.TODO(), now)
assert.NoError(t, err)
assert.Equal(t, 1, len(absentees))
_, err = store.LastOf(context.TODO(), EventAbsence)
require.NoError(t, err)
}
func TestGetSchedule(t *testing.T) {
db, err := sql.Open("sqlite3", fmt.Sprintf(TEST_CONN_STRING_PATTERN, "TestGetSchedule"))
require.NoError(t, err)
store, err := NewStore(db, zerolog.Nop())
require.NoError(t, err)
now := NextMonday()
_ = store.AddAbsence(context.TODO(), now.Add(7*24*time.Hour), "foobar")
schedule, err := store.GetSchedule(context.TODO(), now, now.AddDate(0, 0, 14))
assert.NoError(t, err)
// reason being, the schedule should initialize to the desired time range
assert.Equal(t, 3, len(schedule))
for d, a := range schedule {
if d == now.Add(7*24*time.Hour).Format(time.DateOnly) {
assert.Equal(t, 1, len(a))
} else {
assert.Equal(t, 0, len(a))
}
}
}