planner/plan/command/update_test.go

228 lines
4.6 KiB
Go
Raw Normal View History

2024-10-06 11:28:05 +02:00
package command_test
import (
2024-10-29 07:22:04 +01:00
"fmt"
2024-10-06 11:28:05 +02:00
"testing"
"time"
"go-mod.ewintr.nl/planner/item"
"go-mod.ewintr.nl/planner/plan/command"
"go-mod.ewintr.nl/planner/plan/storage/memory"
)
2024-10-29 07:22:04 +01:00
func TestUpdateExecute(t *testing.T) {
2024-10-06 11:28:05 +02:00
t.Parallel()
2024-12-24 08:00:23 +01:00
tskID := "c"
2024-10-06 11:28:05 +02:00
lid := 3
oneHour, err := time.ParseDuration("1h")
if err != nil {
t.Errorf("exp nil, got %v", err)
}
title := "title"
2024-12-19 12:06:03 +01:00
aDate := item.NewDate(2024, 10, 6)
aTime := item.NewTime(10, 0)
2024-10-06 11:28:05 +02:00
twoHour, err := time.ParseDuration("2h")
if err != nil {
t.Errorf("exp nil, got %v", err)
}
for _, tc := range []struct {
2024-12-24 08:00:23 +01:00
name string
localID int
main []string
flags map[string]string
expTask item.Task
expErr bool
2024-10-06 11:28:05 +02:00
}{
{
2024-10-29 07:22:04 +01:00
name: "no args",
expErr: true,
2024-10-06 11:28:05 +02:00
},
{
name: "not found",
localID: 1,
expErr: true,
},
{
name: "name",
localID: lid,
2024-10-29 07:22:04 +01:00
main: []string{"update", fmt.Sprintf("%d", lid), "updated"},
2024-12-24 08:00:23 +01:00
expTask: item.Task{
ID: tskID,
2024-12-19 12:06:03 +01:00
Date: item.NewDate(2024, 10, 6),
2024-12-24 08:00:23 +01:00
TaskBody: item.TaskBody{
2024-10-06 11:28:05 +02:00
Title: "updated",
2024-12-19 12:06:03 +01:00
Time: aTime,
2024-10-06 11:28:05 +02:00
Duration: oneHour,
},
},
},
{
name: "invalid on",
localID: lid,
2024-10-29 07:22:04 +01:00
main: []string{"update", fmt.Sprintf("%d", lid)},
flags: map[string]string{
2024-10-06 11:28:05 +02:00
"on": "invalid",
},
expErr: true,
},
{
name: "on",
localID: lid,
2024-10-29 07:22:04 +01:00
main: []string{"update", fmt.Sprintf("%d", lid)},
flags: map[string]string{
2024-10-06 11:28:05 +02:00
"on": "2024-10-02",
},
2024-12-24 08:00:23 +01:00
expTask: item.Task{
ID: tskID,
2024-12-19 12:06:03 +01:00
Date: item.NewDate(2024, 10, 2),
2024-12-24 08:00:23 +01:00
TaskBody: item.TaskBody{
2024-10-06 11:28:05 +02:00
Title: title,
2024-12-19 12:06:03 +01:00
Time: aTime,
2024-10-06 11:28:05 +02:00
Duration: oneHour,
},
},
},
{
name: "invalid at",
localID: lid,
2024-10-29 07:22:04 +01:00
main: []string{"update", fmt.Sprintf("%d", lid)},
flags: map[string]string{
2024-10-06 11:28:05 +02:00
"at": "invalid",
},
expErr: true,
},
{
name: "at",
localID: lid,
2024-10-29 07:22:04 +01:00
main: []string{"update", fmt.Sprintf("%d", lid)},
flags: map[string]string{
2024-10-06 11:28:05 +02:00
"at": "11:00",
},
2024-12-24 08:00:23 +01:00
expTask: item.Task{
ID: tskID,
2024-12-19 12:06:03 +01:00
Date: item.NewDate(2024, 10, 6),
2024-12-24 08:00:23 +01:00
TaskBody: item.TaskBody{
2024-10-06 11:28:05 +02:00
Title: title,
2024-12-19 12:06:03 +01:00
Time: item.NewTime(11, 0),
2024-10-06 11:28:05 +02:00
Duration: oneHour,
},
},
},
{
name: "on and at",
localID: lid,
2024-10-29 07:22:04 +01:00
main: []string{"update", fmt.Sprintf("%d", lid)},
flags: map[string]string{
2024-10-06 11:28:05 +02:00
"on": "2024-10-02",
"at": "11:00",
},
2024-12-24 08:00:23 +01:00
expTask: item.Task{
ID: tskID,
2024-12-19 12:06:03 +01:00
Date: item.NewDate(2024, 10, 2),
2024-12-24 08:00:23 +01:00
TaskBody: item.TaskBody{
2024-10-06 11:28:05 +02:00
Title: title,
2024-12-19 12:06:03 +01:00
Time: item.NewTime(11, 0),
2024-10-06 11:28:05 +02:00
Duration: oneHour,
},
},
},
{
name: "invalid for",
localID: lid,
2024-10-29 07:22:04 +01:00
main: []string{"update", fmt.Sprintf("%d", lid)},
flags: map[string]string{
2024-10-06 11:28:05 +02:00
"for": "invalid",
},
expErr: true,
},
{
name: "for",
localID: lid,
2024-10-29 07:22:04 +01:00
main: []string{"update", fmt.Sprintf("%d", lid)},
flags: map[string]string{
2024-10-06 11:28:05 +02:00
"for": "2h",
},
2024-12-24 08:00:23 +01:00
expTask: item.Task{
ID: tskID,
2024-12-19 12:06:03 +01:00
Date: item.NewDate(2024, 10, 6),
2024-12-24 08:00:23 +01:00
TaskBody: item.TaskBody{
2024-10-06 11:28:05 +02:00
Title: title,
2024-12-19 12:06:03 +01:00
Time: aTime,
2024-10-06 11:28:05 +02:00
Duration: twoHour,
},
},
},
2024-12-01 10:22:47 +01:00
{
2024-12-19 12:06:03 +01:00
name: "invalid rec",
2024-12-01 10:22:47 +01:00
main: []string{"update", fmt.Sprintf("%d", lid)},
flags: map[string]string{
2024-12-19 12:06:03 +01:00
"rec": "invalud",
2024-12-01 10:22:47 +01:00
},
expErr: true,
},
{
2024-12-19 12:06:03 +01:00
name: "valid rec",
2024-12-01 10:22:47 +01:00
main: []string{"update", fmt.Sprintf("%d", lid)},
flags: map[string]string{
2024-12-19 12:06:03 +01:00
"rec": "2024-12-08, daily",
2024-12-01 10:22:47 +01:00
},
2024-12-24 08:00:23 +01:00
expTask: item.Task{
ID: tskID,
2024-12-19 12:06:03 +01:00
Date: aDate,
Recurrer: item.NewRecurrer("2024-12-08, daily"),
2024-12-24 08:00:23 +01:00
TaskBody: item.TaskBody{
2024-12-01 10:22:47 +01:00
Title: title,
2024-12-19 12:06:03 +01:00
Time: aTime,
2024-12-01 10:22:47 +01:00
Duration: oneHour,
},
},
},
2024-10-06 11:28:05 +02:00
} {
t.Run(tc.name, func(t *testing.T) {
2024-12-24 08:00:23 +01:00
taskRepo := memory.NewTask()
2024-10-06 11:28:05 +02:00
localIDRepo := memory.NewLocalID()
2024-10-07 11:11:18 +02:00
syncRepo := memory.NewSync()
2024-12-24 08:00:23 +01:00
if err := taskRepo.Store(item.Task{
ID: tskID,
2024-12-19 12:06:03 +01:00
Date: aDate,
2024-12-24 08:00:23 +01:00
TaskBody: item.TaskBody{
2024-10-06 11:28:05 +02:00
Title: title,
2024-12-19 12:06:03 +01:00
Time: aTime,
2024-10-06 11:28:05 +02:00
Duration: oneHour,
},
}); err != nil {
t.Errorf("exp nil, got %v", err)
}
2024-12-24 08:00:23 +01:00
if err := localIDRepo.Store(tskID, lid); err != nil {
2024-10-06 11:28:05 +02:00
t.Errorf("exp nil, ,got %v", err)
}
2024-12-24 08:00:23 +01:00
cmd := command.NewUpdate(localIDRepo, taskRepo, syncRepo)
2024-10-29 07:22:04 +01:00
actParseErr := cmd.Execute(tc.main, tc.flags) != nil
if tc.expErr != actParseErr {
t.Errorf("exp %v, got %v", tc.expErr, actParseErr)
2024-10-06 11:28:05 +02:00
}
if tc.expErr {
return
}
2024-12-24 08:00:23 +01:00
actTask, err := taskRepo.Find(tskID)
2024-10-06 11:28:05 +02:00
if err != nil {
t.Errorf("exp nil, got %v", err)
}
2024-12-24 08:00:23 +01:00
if diff := item.TaskDiff(tc.expTask, actTask); diff != "" {
2024-12-01 10:22:47 +01:00
t.Errorf("(exp -, got +)\n%s", diff)
2024-10-06 11:28:05 +02:00
}
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-06 11:28:05 +02:00
})
}
}