2024-09-18 07:40:50 +02:00
|
|
|
package main
|
2024-08-16 14:25:06 +02:00
|
|
|
|
|
|
|
import (
|
2024-09-19 07:07:28 +02:00
|
|
|
"sort"
|
2024-08-16 14:25:06 +02:00
|
|
|
"testing"
|
2024-08-23 08:19:04 +02:00
|
|
|
"time"
|
2024-09-18 07:55:14 +02:00
|
|
|
|
2024-12-01 10:22:47 +01:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
2024-09-20 07:09:30 +02:00
|
|
|
"go-mod.ewintr.nl/planner/item"
|
2024-08-16 14:25:06 +02:00
|
|
|
)
|
|
|
|
|
2024-12-01 10:22:47 +01:00
|
|
|
func TestMemoryUpdate(t *testing.T) {
|
2024-08-16 14:25:06 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2024-09-08 10:09:54 +02:00
|
|
|
mem := NewMemory()
|
2024-08-18 19:34:27 +02:00
|
|
|
|
|
|
|
t.Log("start empty")
|
2024-09-18 07:55:14 +02:00
|
|
|
actItems, actErr := mem.Updated([]item.Kind{}, time.Time{})
|
2024-08-16 14:25:06 +02:00
|
|
|
if actErr != nil {
|
|
|
|
t.Errorf("exp nil, got %v", actErr)
|
|
|
|
}
|
2024-08-23 08:19:04 +02:00
|
|
|
if len(actItems) != 0 {
|
|
|
|
t.Errorf("exp 0, got %d", len(actItems))
|
2024-08-16 14:25:06 +02:00
|
|
|
}
|
2024-08-18 19:34:27 +02:00
|
|
|
|
|
|
|
t.Log("add one")
|
2024-09-18 07:55:14 +02:00
|
|
|
t1 := item.NewItem(item.Kind("kinda"), "test")
|
2024-12-01 10:22:47 +01:00
|
|
|
if actErr := mem.Update(t1, t1.Updated); actErr != nil {
|
2024-08-18 19:34:27 +02:00
|
|
|
t.Errorf("exp nil, got %v", actErr)
|
|
|
|
}
|
2024-09-18 07:55:14 +02:00
|
|
|
actItems, actErr = mem.Updated([]item.Kind{}, time.Time{})
|
2024-08-18 19:34:27 +02:00
|
|
|
if actErr != nil {
|
|
|
|
t.Errorf("exp nil, got %v", actErr)
|
|
|
|
}
|
2024-08-23 08:19:04 +02:00
|
|
|
if len(actItems) != 1 {
|
|
|
|
t.Errorf("exp 1, gor %d", len(actItems))
|
|
|
|
}
|
2024-08-28 07:21:02 +02:00
|
|
|
if actItems[0].ID != t1.ID {
|
|
|
|
t.Errorf("exp %v, got %v", actItems[0].ID, t1.ID)
|
2024-08-18 19:34:27 +02:00
|
|
|
}
|
|
|
|
|
2024-08-23 08:19:04 +02:00
|
|
|
before := time.Now()
|
|
|
|
|
2024-08-18 19:34:27 +02:00
|
|
|
t.Log("add second")
|
2024-09-18 07:55:14 +02:00
|
|
|
t2 := item.NewItem(item.Kind("kindb"), "test 2")
|
2024-12-01 10:22:47 +01:00
|
|
|
if actErr := mem.Update(t2, t2.Updated); actErr != nil {
|
2024-08-18 19:34:27 +02:00
|
|
|
t.Errorf("exp nil, got %v", actErr)
|
|
|
|
}
|
2024-09-18 07:55:14 +02:00
|
|
|
actItems, actErr = mem.Updated([]item.Kind{}, time.Time{})
|
2024-08-18 19:34:27 +02:00
|
|
|
if actErr != nil {
|
|
|
|
t.Errorf("exp nil, got %v", actErr)
|
|
|
|
}
|
2024-12-01 10:22:47 +01:00
|
|
|
if diff := cmp.Diff([]item.Item{t1, t2}, actItems, cmpopts.SortSlices(func(i, j item.Item) bool {
|
|
|
|
return i.ID < j.ID
|
|
|
|
})); diff != "" {
|
|
|
|
t.Errorf("(exp +, got -)\n%s", diff)
|
2024-08-24 11:46:27 +02:00
|
|
|
}
|
2024-08-23 08:19:04 +02:00
|
|
|
|
2024-09-18 07:55:14 +02:00
|
|
|
actItems, actErr = mem.Updated([]item.Kind{}, before)
|
2024-08-18 19:34:27 +02:00
|
|
|
if actErr != nil {
|
|
|
|
t.Errorf("exp nil, got %v", actErr)
|
|
|
|
}
|
2024-08-23 08:19:04 +02:00
|
|
|
if len(actItems) != 1 {
|
|
|
|
t.Errorf("exp 1, gor %d", len(actItems))
|
2024-08-18 19:34:27 +02:00
|
|
|
}
|
2024-08-28 07:21:02 +02:00
|
|
|
if actItems[0].ID != t2.ID {
|
|
|
|
t.Errorf("exp %v, got %v", actItems[0].ID, t2.ID)
|
2024-08-23 08:19:04 +02:00
|
|
|
}
|
|
|
|
|
2024-08-28 07:21:02 +02:00
|
|
|
t.Log("update first")
|
2024-12-01 10:22:47 +01:00
|
|
|
if actErr := mem.Update(t1, time.Now()); actErr != nil {
|
2024-08-24 11:46:27 +02:00
|
|
|
t.Errorf("exp nil, got %v", actErr)
|
|
|
|
}
|
2024-09-18 07:55:14 +02:00
|
|
|
actItems, actErr = mem.Updated([]item.Kind{}, before)
|
2024-08-24 11:46:27 +02:00
|
|
|
if actErr != nil {
|
|
|
|
t.Errorf("exp nil, got %v", actErr)
|
|
|
|
}
|
2024-08-28 07:21:02 +02:00
|
|
|
if len(actItems) != 2 {
|
2024-08-24 11:46:27 +02:00
|
|
|
t.Errorf("exp 2, gor %d", len(actItems))
|
|
|
|
}
|
2024-09-19 07:07:28 +02:00
|
|
|
sort.Slice(actItems, func(i, j int) bool {
|
|
|
|
return actItems[i].ID < actItems[j].ID
|
|
|
|
})
|
|
|
|
expItems := []item.Item{t1, t2}
|
|
|
|
sort.Slice(expItems, func(i, j int) bool {
|
2024-09-20 07:09:30 +02:00
|
|
|
return expItems[i].ID < expItems[j].ID
|
2024-09-19 07:07:28 +02:00
|
|
|
})
|
|
|
|
|
2024-09-20 07:09:30 +02:00
|
|
|
if actItems[0].ID != expItems[0].ID {
|
|
|
|
t.Errorf("exp %v, got %v", actItems[0].ID, expItems[0].ID)
|
2024-08-24 11:46:27 +02:00
|
|
|
}
|
2024-09-20 07:09:30 +02:00
|
|
|
if actItems[1].ID != expItems[1].ID {
|
|
|
|
t.Errorf("exp %v, got %v", actItems[1].ID, expItems[1].ID)
|
2024-08-24 11:46:27 +02:00
|
|
|
}
|
2024-09-11 07:33:22 +02:00
|
|
|
|
|
|
|
t.Log("select kind")
|
2024-09-18 07:55:14 +02:00
|
|
|
actItems, actErr = mem.Updated([]item.Kind{"kinda"}, time.Time{})
|
2024-09-11 07:33:22 +02:00
|
|
|
if actErr != nil {
|
|
|
|
t.Errorf("exp nil, got %v", actErr)
|
|
|
|
}
|
|
|
|
if len(actItems) != 1 {
|
|
|
|
t.Errorf("exp 1, got %d", len(actItems))
|
|
|
|
}
|
|
|
|
if actItems[0].ID != t1.ID {
|
|
|
|
t.Errorf("exp %v, got %v", t1.ID, actItems[0].ID)
|
|
|
|
}
|
2024-08-16 14:25:06 +02:00
|
|
|
}
|
2024-12-01 10:22:47 +01:00
|
|
|
|
|
|
|
func TestMemoryRecur(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
mem := NewMemory()
|
|
|
|
now := time.Now()
|
|
|
|
earlier := now.Add(-5 * time.Minute)
|
2024-12-23 10:06:59 +01:00
|
|
|
today := item.NewDate(2024, 12, 1)
|
|
|
|
yesterday := item.NewDate(2024, 11, 30)
|
2024-12-01 10:22:47 +01:00
|
|
|
|
|
|
|
t.Log("start")
|
|
|
|
i1 := item.Item{
|
2024-12-23 10:06:59 +01:00
|
|
|
ID: "a",
|
|
|
|
Updated: earlier,
|
|
|
|
Recurrer: item.NewRecurrer("2024-11-30, daily"),
|
2024-12-01 10:22:47 +01:00
|
|
|
RecurNext: yesterday,
|
|
|
|
}
|
|
|
|
i2 := item.Item{
|
|
|
|
ID: "b",
|
|
|
|
Updated: earlier,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, i := range []item.Item{i1, i2} {
|
|
|
|
if err := mem.Update(i, i.Updated); err != nil {
|
|
|
|
t.Errorf("exp nil, ot %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("get recurrers")
|
2024-12-23 10:06:59 +01:00
|
|
|
rs, err := mem.ShouldRecur(today)
|
2024-12-01 10:22:47 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("exp nil, gt %v", err)
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff([]item.Item{i1}, rs); diff != "" {
|
|
|
|
t.Errorf("(exp +, got -)\n%s", diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|