planner/storage/memory_test.go

118 lines
2.7 KiB
Go
Raw Normal View History

2024-08-20 08:34:11 +02:00
package storage_test
2024-08-16 14:25:06 +02:00
import (
2024-08-24 11:46:27 +02:00
"errors"
2024-08-16 14:25:06 +02:00
"testing"
2024-08-23 08:19:04 +02:00
"time"
2024-08-16 14:25:06 +02:00
2024-08-23 08:19:04 +02:00
"code.ewintr.nl/planner/planner"
"code.ewintr.nl/planner/storage"
2024-08-16 14:25:06 +02:00
)
2024-08-23 08:19:04 +02:00
func TestMemoryItem(t *testing.T) {
2024-08-16 14:25:06 +02:00
t.Parallel()
2024-08-23 08:19:04 +02:00
mem := storage.NewMemory()
2024-08-18 19:34:27 +02:00
t.Log("start empty")
2024-08-23 10:52:17 +02:00
actItems, actErr := mem.Updated(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-08-23 08:19:04 +02:00
t1 := planner.NewTask("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-08-23 10:52:17 +02:00
actItems, actErr = mem.Updated(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))
}
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-08-23 08:19:04 +02:00
t2 := planner.NewTask("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-08-23 10:52:17 +02:00
actItems, actErr = mem.Updated(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-23 08:19:04 +02:00
if actItems[0].ID() != t1.ID() {
t.Errorf("exp %v, got %v", actItems[0].ID(), t1.ID())
}
if actItems[1].ID() != t2.ID() {
t.Errorf("exp %v, got %v", actItems[1].ID(), t2.ID())
2024-08-18 19:34:27 +02:00
}
2024-08-24 11:46:27 +02:00
actDeleted, actErr := mem.Deleted(time.Time{})
if actErr != nil {
t.Errorf("exp nil, got %v", actErr)
}
if len(actDeleted) != 0 {
t.Errorf("exp 0, got %d", len(actDeleted))
}
2024-08-23 08:19:04 +02:00
2024-08-23 10:52:17 +02:00
actItems, actErr = mem.Updated(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-23 08:19:04 +02:00
if actItems[0].ID() != t2.ID() {
t.Errorf("exp %v, got %v", actItems[0].ID(), t2.ID())
}
2024-08-24 11:46:27 +02:00
t.Log("remove first")
if actErr := mem.Delete(t1.ID()); actErr != nil {
t.Errorf("exp nil, got %v", actErr)
}
actItems, actErr = mem.Updated(time.Time{})
if actErr != nil {
t.Errorf("exp nil, got %v", actErr)
}
if len(actItems) != 1 {
t.Errorf("exp 2, gor %d", len(actItems))
}
if actItems[0].ID() != t2.ID() {
t.Errorf("exp %v, got %v", actItems[0].ID(), t1.ID())
}
actDeleted, actErr = mem.Deleted(time.Time{})
if actErr != nil {
t.Errorf("exp nil, got %v", actErr)
}
if len(actDeleted) != 1 {
t.Errorf("exp 1, got %d", len(actDeleted))
}
if actDeleted[0] != t1.ID() {
t.Errorf("exp %v, got %v", actDeleted[0], t1.ID())
}
actDeleted, actErr = mem.Deleted(time.Now())
if actErr != nil {
t.Errorf("exp nil, got %v", actErr)
}
if len(actDeleted) != 0 {
t.Errorf("exp 0, got %d", len(actDeleted))
}
t.Log("remove non-existing")
if actErr := mem.Delete("test"); !errors.Is(actErr, storage.ErrNotFound) {
t.Errorf("exp %v, got %v", storage.ErrNotFound, actErr)
}
2024-08-16 14:25:06 +02:00
}