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-12-27 11:20:32 +01:00
|
|
|
name string
|
|
|
|
main []string
|
|
|
|
flags map[string]string
|
|
|
|
expParseErr bool
|
|
|
|
expDoErr bool
|
2024-10-07 09:34:17 +02:00
|
|
|
}{
|
|
|
|
{
|
2024-12-27 11:20:32 +01:00
|
|
|
name: "invalid",
|
|
|
|
main: []string{"update"},
|
|
|
|
expParseErr: true,
|
2024-10-29 07:22:04 +01:00
|
|
|
},
|
|
|
|
{
|
2024-12-27 11:20:32 +01:00
|
|
|
name: "not found",
|
|
|
|
main: []string{"delete", "5"},
|
|
|
|
expDoErr: true,
|
2024-10-29 07:22:04 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid",
|
|
|
|
main: []string{"delete", "1"},
|
2024-10-07 09:34:17 +02:00
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2024-12-25 10:11:56 +01:00
|
|
|
taskRepo := memory.NewTask()
|
2024-10-07 11:11:18 +02:00
|
|
|
syncRepo := memory.NewSync()
|
2024-12-25 10:11:56 +01:00
|
|
|
if err := taskRepo.Store(e); err != nil {
|
2024-10-07 09:34:17 +02:00
|
|
|
t.Errorf("exp nil, got %v", err)
|
|
|
|
}
|
2024-12-27 11:20:32 +01:00
|
|
|
localIDRepo := memory.NewLocalID()
|
|
|
|
if err := localIDRepo.Store(e.ID, 1); err != nil {
|
2024-10-07 09:34:17 +02:00
|
|
|
t.Errorf("exp nil, got %v", err)
|
|
|
|
}
|
|
|
|
|
2024-12-27 11:20:32 +01:00
|
|
|
cmd, actParseErr := command.NewDeleteArgs().Parse(tc.main, tc.flags)
|
|
|
|
if tc.expParseErr != (actParseErr != nil) {
|
|
|
|
t.Errorf("exp %v, got %v", tc.expParseErr, actParseErr)
|
|
|
|
}
|
|
|
|
if tc.expParseErr {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
actDoErr := cmd.Do(command.Dependencies{
|
|
|
|
TaskRepo: taskRepo,
|
|
|
|
LocalIDRepo: localIDRepo,
|
|
|
|
SyncRepo: syncRepo,
|
|
|
|
}) != nil
|
|
|
|
if tc.expDoErr != actDoErr {
|
|
|
|
t.Errorf("exp false, got %v", actDoErr)
|
2024-10-07 09:34:17 +02:00
|
|
|
}
|
2024-12-27 11:20:32 +01:00
|
|
|
if tc.expDoErr {
|
2024-10-07 09:34:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-12-25 10:11:56 +01:00
|
|
|
_, repoErr := taskRepo.Find(e.ID)
|
2024-10-07 09:34:17 +02:00
|
|
|
if !errors.Is(repoErr, storage.ErrNotFound) {
|
2024-12-27 11:20:32 +01:00
|
|
|
t.Errorf("exp %v, got %v", storage.ErrNotFound, repoErr)
|
2024-10-07 09:34:17 +02:00
|
|
|
}
|
2024-12-27 11:20:32 +01:00
|
|
|
idMap, idErr := localIDRepo.FindAll()
|
2024-10-07 09:34:17 +02:00
|
|
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|