2024-10-03 07:32:48 +02:00
|
|
|
package memory_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"go-mod.ewintr.nl/planner/plan/storage"
|
|
|
|
"go-mod.ewintr.nl/planner/plan/storage/memory"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLocalID(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
repo := memory.NewLocalID()
|
|
|
|
|
|
|
|
t.Log("start empty")
|
|
|
|
actIDs, actErr := repo.FindAll()
|
|
|
|
if actErr != nil {
|
|
|
|
t.Errorf("exp nil, got %v", actErr)
|
|
|
|
}
|
|
|
|
if len(actIDs) != 0 {
|
|
|
|
t.Errorf("exp nil, got %v", actErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("next id")
|
|
|
|
actNext, actErr := repo.Next()
|
|
|
|
if actErr != nil {
|
|
|
|
t.Errorf("exp nil, got %v", actErr)
|
|
|
|
}
|
|
|
|
if actNext != 1 {
|
|
|
|
t.Errorf("exp 1, got %v", actNext)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("store")
|
|
|
|
if actErr = repo.Store("test", 1); actErr != nil {
|
|
|
|
t.Errorf("exp nil, got %v", actErr)
|
|
|
|
}
|
|
|
|
|
2024-10-15 07:28:46 +02:00
|
|
|
t.Log("retrieve known")
|
|
|
|
actLid, actErr := repo.FindOrNext("test")
|
|
|
|
if actErr != nil {
|
|
|
|
t.Errorf("exp nil, got %v", actErr)
|
|
|
|
}
|
|
|
|
if actLid != 1 {
|
|
|
|
t.Errorf("exp 1, git %v", actLid)
|
|
|
|
}
|
2024-10-16 07:20:08 +02:00
|
|
|
t.Log("retrieve unknown")
|
|
|
|
actLid, actErr = repo.FindOrNext("new")
|
|
|
|
if actErr != nil {
|
|
|
|
t.Errorf("exp nil, got %v", actErr)
|
|
|
|
}
|
|
|
|
if actLid != 2 {
|
|
|
|
t.Errorf("exp 2, got %v", actLid)
|
|
|
|
}
|
2024-10-15 07:28:46 +02:00
|
|
|
|
2024-10-03 07:32:48 +02:00
|
|
|
actIDs, actErr = repo.FindAll()
|
|
|
|
if actErr != nil {
|
|
|
|
t.Errorf("exp nil, got %v", actErr)
|
|
|
|
}
|
|
|
|
expIDs := map[string]int{
|
|
|
|
"test": 1,
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(expIDs, actIDs); diff != "" {
|
|
|
|
t.Errorf("(exp +, got -)\n%s", diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("delete")
|
|
|
|
if actErr = repo.Delete("test"); actErr != nil {
|
|
|
|
t.Errorf("exp nil, got %v", actErr)
|
|
|
|
}
|
|
|
|
actIDs, actErr = repo.FindAll()
|
|
|
|
if actErr != nil {
|
|
|
|
t.Errorf("exp nil, got %v", actErr)
|
|
|
|
}
|
|
|
|
if len(actIDs) != 0 {
|
|
|
|
t.Errorf("exp 0, got %v", actErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("delete non-existing")
|
|
|
|
actErr = repo.Delete("non-existing")
|
|
|
|
if !errors.Is(actErr, storage.ErrNotFound) {
|
|
|
|
t.Errorf("exp %v, got %v", storage.ErrNotFound, actErr)
|
|
|
|
}
|
|
|
|
}
|