planner/plan/command/delete_test.go

87 lines
1.7 KiB
Go
Raw Normal View History

2024-10-07 09:34:17 +02:00
package command_test
import (
"errors"
"testing"
"go-mod.ewintr.nl/planner/item"
"go-mod.ewintr.nl/planner/plan/command"
"go-mod.ewintr.nl/planner/plan/storage"
"go-mod.ewintr.nl/planner/plan/storage/memory"
)
func TestDelete(t *testing.T) {
t.Parallel()
2024-12-24 08:00:23 +01:00
e := item.Task{
2024-12-19 12:06:03 +01:00
ID: "id",
Date: item.NewDate(2024, 10, 7),
2024-12-24 08:00:23 +01:00
TaskBody: item.TaskBody{
2024-10-07 09:34:17 +02:00
Title: "name",
},
}
for _, tc := range []struct {
2024-10-29 07:22:04 +01:00
name string
main []string
flags map[string]string
expErr bool
2024-10-07 09:34:17 +02:00
}{
{
2024-10-29 07:22:04 +01:00
name: "invalid",
main: []string{"update"},
expErr: true,
},
{
name: "not found",
main: []string{"delete", "5"},
expErr: true,
},
{
name: "valid",
main: []string{"delete", "1"},
2024-10-07 09:34:17 +02:00
},
} {
t.Run(tc.name, func(t *testing.T) {
2024-12-24 08:00:23 +01:00
eventRepo := memory.NewTask()
2024-10-07 11:11:18 +02:00
syncRepo := memory.NewSync()
2024-10-07 09:34:17 +02:00
if err := eventRepo.Store(e); err != nil {
t.Errorf("exp nil, got %v", err)
}
localRepo := memory.NewLocalID()
if err := localRepo.Store(e.ID, 1); err != nil {
t.Errorf("exp nil, got %v", err)
}
2024-10-29 07:22:04 +01:00
cmd := command.NewDelete(localRepo, eventRepo, syncRepo)
actErr := cmd.Execute(tc.main, tc.flags) != nil
2024-10-07 09:34:17 +02:00
if tc.expErr != actErr {
t.Errorf("exp %v, got %v", tc.expErr, actErr)
}
if tc.expErr {
return
}
_, repoErr := eventRepo.Find(e.ID)
if !errors.Is(repoErr, storage.ErrNotFound) {
t.Errorf("exp %v, got %v", storage.ErrNotFound, actErr)
}
idMap, idErr := localRepo.FindAll()
if idErr != nil {
t.Errorf("exp nil, got %v", idErr)
}
if len(idMap) != 0 {
t.Errorf("exp 0, got %v", len(idMap))
}
2024-10-07 11:11:18 +02:00
updated, err := syncRepo.FindAll()
if err != nil {
t.Errorf("exp nil, got %v", err)
}
if len(updated) != 1 {
t.Errorf("exp 1, got %v", len(updated))
}
2024-10-07 09:34:17 +02:00
})
}
}