planner/sync/service/memory_test.go

172 lines
3.9 KiB
Go
Raw Normal View History

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 11:51:43 +01:00
"github.com/google/go-cmp/cmp"
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 11:51:43 +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-08-23 10:52:17 +02:00
if actErr := mem.Update(t1); 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-08-23 10:52:17 +02:00
if actErr := mem.Update(t2); 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) != 2 {
t.Errorf("exp 2, gor %d", len(actItems))
2024-08-18 19:34:27 +02:00
}
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-23 08:19:04 +02:00
}
2024-08-28 07:21:02 +02:00
if actItems[1].ID != t2.ID {
t.Errorf("exp %v, got %v", actItems[1].ID, t2.ID)
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")
t1.Updated = time.Now()
if actErr := mem.Update(t1); 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 11:51:43 +01:00
func TestMemoryRecur(t *testing.T) {
t.Parallel()
mem := NewMemory()
now := time.Now()
earlier := now.Add(-5 * time.Minute)
today := time.Date(2024, 12, 1, 0, 0, 0, 0, time.UTC)
yesterday := time.Date(2024, 11, 30, 0, 0, 0, 0, time.UTC)
tomorrow := time.Date(2024, 12, 2, 0, 0, 0, 0, time.UTC)
t.Log("start")
i1 := item.Item{
ID: "a",
Updated: earlier,
Recurrer: &item.Recur{
Start: yesterday,
Period: item.PeriodDay,
Count: 1,
},
RecurNext: yesterday,
}
i2 := item.Item{
ID: "b",
Updated: earlier,
}
for _, i := range []item.Item{i1, i2} {
if err := mem.Update(i); err != nil {
t.Errorf("exp nil, ot %v", err)
}
}
t.Log("get recurrers")
rs, err := mem.RecursBefore(today)
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)
}
t.Log("set next")
if err := mem.RecursNext(i1.ID, tomorrow); err != nil {
t.Errorf("exp nil, got %v", err)
}
t.Log("check result")
us, err := mem.Updated([]item.Kind{}, now)
if err != nil {
t.Errorf("exp nil, got %v", err)
}
if len(us) != 1 {
t.Errorf("exp 1, got %v", len(us))
}
if us[0].ID != i1.ID {
t.Errorf("exp %v, got %v", i1.ID, us[0].ID)
}
}