planner/plan/command/update_test.go

245 lines
5.0 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-31 08:37:00 +01:00
project := "project"
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-27 11:20:32 +01:00
name string
localID int
main []string
fields map[string]string
expTask item.Task
expParseErr bool
expDoErr bool
2024-10-06 11:28:05 +02:00
}{
{
2024-12-27 11:20:32 +01:00
name: "no args",
expParseErr: true,
2024-10-06 11:28:05 +02:00
},
{
2024-12-27 11:20:32 +01:00
name: "not found",
main: []string{"update", "1"},
expDoErr: true,
2024-10-06 11:28:05 +02:00
},
{
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-31 08:37:00 +01:00
Project: project,
Time: aTime,
Duration: oneHour,
},
},
},
{
name: "project",
localID: lid,
main: []string{"update", fmt.Sprintf("%d", lid)},
fields: map[string]string{
"p": "updated",
},
expTask: item.Task{
ID: tskID,
Date: item.NewDate(2024, 10, 2),
TaskBody: item.TaskBody{
Title: title,
Project: "updated",
2024-12-19 12:06:03 +01:00
Time: aTime,
2024-10-06 11:28:05 +02:00
Duration: oneHour,
},
},
},
{
2024-12-27 11:20:32 +01:00
name: "invalid date",
2024-10-06 11:28:05 +02:00
localID: lid,
2024-10-29 07:22:04 +01:00
main: []string{"update", fmt.Sprintf("%d", lid)},
2024-12-27 11:20:32 +01:00
fields: map[string]string{
2024-10-06 11:28:05 +02:00
"on": "invalid",
},
2024-12-27 11:20:32 +01:00
expParseErr: true,
2024-10-06 11:28:05 +02:00
},
{
2024-12-27 11:20:32 +01:00
name: "date",
2024-10-06 11:28:05 +02:00
localID: lid,
2024-10-29 07:22:04 +01:00
main: []string{"update", fmt.Sprintf("%d", lid)},
2024-12-27 11:20:32 +01:00
fields: 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-31 08:37:00 +01:00
Project: project,
2024-12-19 12:06:03 +01:00
Time: aTime,
2024-10-06 11:28:05 +02:00
Duration: oneHour,
},
},
},
{
2024-12-27 11:20:32 +01:00
name: "invalid time",
2024-10-06 11:28:05 +02:00
localID: lid,
2024-10-29 07:22:04 +01:00
main: []string{"update", fmt.Sprintf("%d", lid)},
2024-12-27 11:20:32 +01:00
fields: map[string]string{
2024-10-06 11:28:05 +02:00
"at": "invalid",
},
2024-12-27 11:20:32 +01:00
expParseErr: true,
2024-10-06 11:28:05 +02:00
},
{
2024-12-27 11:20:32 +01:00
name: "time",
2024-10-06 11:28:05 +02:00
localID: lid,
2024-10-29 07:22:04 +01:00
main: []string{"update", fmt.Sprintf("%d", lid)},
2024-12-27 11:20:32 +01:00
fields: map[string]string{
"time": "11:00",
2024-10-06 11:28:05 +02: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-31 08:37:00 +01:00
Project: project,
2024-12-19 12:06:03 +01:00
Time: item.NewTime(11, 0),
2024-10-06 11:28:05 +02:00
Duration: oneHour,
},
},
},
{
2024-12-27 11:20:32 +01:00
name: "invalid duration",
2024-10-06 11:28:05 +02:00
localID: lid,
2024-10-29 07:22:04 +01:00
main: []string{"update", fmt.Sprintf("%d", lid)},
2024-12-27 11:20:32 +01:00
fields: map[string]string{
2024-10-06 11:28:05 +02:00
"for": "invalid",
},
2024-12-27 11:20:32 +01:00
expParseErr: true,
2024-10-06 11:28:05 +02:00
},
{
2024-12-27 11:20:32 +01:00
name: "duration",
2024-10-06 11:28:05 +02:00
localID: lid,
2024-10-29 07:22:04 +01:00
main: []string{"update", fmt.Sprintf("%d", lid)},
2024-12-27 11:20:32 +01:00
fields: 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-31 08:37:00 +01:00
Project: project,
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-27 11:20:32 +01:00
name: "invalid recurrer",
2024-12-01 10:22:47 +01:00
main: []string{"update", fmt.Sprintf("%d", lid)},
2024-12-27 11:20:32 +01:00
fields: map[string]string{
2024-12-19 12:06:03 +01:00
"rec": "invalud",
2024-12-01 10:22:47 +01:00
},
2024-12-27 11:20:32 +01:00
expParseErr: true,
2024-12-01 10:22:47 +01:00
},
{
2024-12-27 11:20:32 +01:00
name: "valid recurrer",
2024-12-01 10:22:47 +01:00
main: []string{"update", fmt.Sprintf("%d", lid)},
2024-12-27 11:20:32 +01:00
fields: 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-31 08:37:00 +01:00
Project: project,
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-29 09:32:49 +01:00
// setup
2025-01-13 09:13:48 +01:00
mems := memory.New()
if err := mems.Task(nil).Store(item.Task{
2024-12-24 08:00:23 +01:00
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-31 08:37:00 +01:00
Project: project,
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)
}
2025-01-13 09:13:48 +01:00
if err := mems.LocalID(nil).Store(tskID, lid); err != nil {
2024-10-06 11:28:05 +02:00
t.Errorf("exp nil, ,got %v", err)
}
2024-12-29 09:32:49 +01:00
// parse
2024-12-27 11:20:32 +01:00
cmd, actErr := command.NewUpdateArgs().Parse(tc.main, tc.fields)
if tc.expParseErr != (actErr != nil) {
t.Errorf("exp %v, got %v", tc.expParseErr, actErr)
}
if tc.expParseErr {
return
}
2024-12-29 09:32:49 +01:00
// do
2025-01-13 09:13:48 +01:00
_, actDoErr := cmd.Do(mems, nil)
2024-12-29 09:32:49 +01:00
if tc.expDoErr != (actDoErr != nil) {
2024-12-27 11:20:32 +01:00
t.Errorf("exp %v, got %v", tc.expDoErr, actDoErr)
2024-10-06 11:28:05 +02:00
}
2024-12-27 11:20:32 +01:00
if tc.expDoErr {
2024-10-06 11:28:05 +02:00
return
}
2024-12-29 09:32:49 +01:00
// check
2025-01-13 09:13:48 +01:00
actTask, err := mems.Task(nil).FindOne(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
}
2025-01-13 09:13:48 +01:00
updated, err := mems.Sync(nil).FindAll()
2024-10-07 11:11:18 +02:00
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
})
}
}