2024-10-01 07:36:31 +02:00
|
|
|
package command_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go-mod.ewintr.nl/planner/item"
|
|
|
|
"go-mod.ewintr.nl/planner/plan/command"
|
2024-12-29 11:31:33 +01:00
|
|
|
"go-mod.ewintr.nl/planner/plan/storage"
|
2024-10-03 07:32:48 +02:00
|
|
|
"go-mod.ewintr.nl/planner/plan/storage/memory"
|
2024-10-01 07:36:31 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAdd(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2024-12-19 12:06:03 +01:00
|
|
|
aDate := item.NewDate(2024, 11, 2)
|
|
|
|
aTime := item.NewTime(12, 0)
|
2024-10-29 07:22:04 +01:00
|
|
|
anHourStr := "1h"
|
|
|
|
anHour := time.Hour
|
2024-10-01 07:36:31 +02:00
|
|
|
|
|
|
|
for _, tc := range []struct {
|
2024-12-24 08:00:23 +01:00
|
|
|
name string
|
|
|
|
main []string
|
2024-12-27 11:20:32 +01:00
|
|
|
fields map[string]string
|
2024-12-24 08:00:23 +01:00
|
|
|
expErr bool
|
|
|
|
expTask item.Task
|
2024-10-01 07:36:31 +02:00
|
|
|
}{
|
|
|
|
{
|
2024-10-29 07:22:04 +01:00
|
|
|
name: "empty",
|
2024-10-01 07:36:31 +02:00
|
|
|
expErr: true,
|
|
|
|
},
|
|
|
|
{
|
2024-10-29 07:22:04 +01:00
|
|
|
name: "title missing",
|
|
|
|
main: []string{"add"},
|
2024-12-27 11:20:32 +01:00
|
|
|
fields: map[string]string{
|
|
|
|
"date": aDate.String(),
|
2024-10-01 07:36:31 +02:00
|
|
|
},
|
|
|
|
expErr: true,
|
|
|
|
},
|
|
|
|
{
|
2024-12-27 11:20:32 +01:00
|
|
|
name: "date time duration",
|
2024-10-29 07:22:04 +01:00
|
|
|
main: []string{"add", "title"},
|
2024-12-27 11:20:32 +01:00
|
|
|
fields: map[string]string{
|
|
|
|
"date": aDate.String(),
|
|
|
|
"time": aTime.String(),
|
|
|
|
"duration": anHourStr,
|
2024-10-01 07:36:31 +02:00
|
|
|
},
|
2024-12-24 08:00:23 +01:00
|
|
|
expTask: item.Task{
|
2024-12-19 12:06:03 +01:00
|
|
|
ID: "title",
|
|
|
|
Date: aDate,
|
2024-12-24 08:00:23 +01:00
|
|
|
TaskBody: item.TaskBody{
|
2024-10-29 07:22:04 +01:00
|
|
|
Title: "title",
|
2024-12-19 12:06:03 +01:00
|
|
|
Time: aTime,
|
2024-10-29 07:22:04 +01:00
|
|
|
Duration: anHour,
|
2024-10-01 07:36:31 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2024-12-24 08:00:23 +01:00
|
|
|
taskRepo := memory.NewTask()
|
2024-12-27 11:20:32 +01:00
|
|
|
localIDRepo := memory.NewLocalID()
|
2024-10-07 11:11:18 +02:00
|
|
|
syncRepo := memory.NewSync()
|
2024-12-27 11:20:32 +01:00
|
|
|
cmd, actParseErr := command.NewAddArgs().Parse(tc.main, tc.fields)
|
|
|
|
if tc.expErr != (actParseErr != nil) {
|
2024-10-29 07:22:04 +01:00
|
|
|
t.Errorf("exp %v, got %v", tc.expErr, actParseErr)
|
2024-10-01 07:36:31 +02:00
|
|
|
}
|
|
|
|
if tc.expErr {
|
|
|
|
return
|
|
|
|
}
|
2024-12-29 10:16:03 +01:00
|
|
|
if _, err := cmd.Do(command.Dependencies{
|
2024-12-27 11:20:32 +01:00
|
|
|
TaskRepo: taskRepo,
|
|
|
|
LocalIDRepo: localIDRepo,
|
|
|
|
SyncRepo: syncRepo,
|
|
|
|
}); err != nil {
|
|
|
|
t.Errorf("exp nil, got %v", err)
|
|
|
|
}
|
2024-10-29 07:22:04 +01:00
|
|
|
|
2024-12-29 11:31:33 +01:00
|
|
|
actTasks, err := taskRepo.FindMany(storage.TaskListParams{})
|
2024-10-01 07:36:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("exp nil, got %v", err)
|
|
|
|
}
|
2024-12-24 08:00:23 +01:00
|
|
|
if len(actTasks) != 1 {
|
|
|
|
t.Errorf("exp 1, got %d", len(actTasks))
|
2024-10-01 07:36:31 +02:00
|
|
|
}
|
2024-10-03 07:32:48 +02:00
|
|
|
|
2024-12-27 11:20:32 +01:00
|
|
|
actLocalIDs, err := localIDRepo.FindAll()
|
2024-10-03 07:32:48 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("exp nil, got %v", err)
|
|
|
|
}
|
|
|
|
if len(actLocalIDs) != 1 {
|
|
|
|
t.Errorf("exp 1, got %v", len(actLocalIDs))
|
|
|
|
}
|
2024-12-24 08:00:23 +01:00
|
|
|
if _, ok := actLocalIDs[actTasks[0].ID]; !ok {
|
2024-10-03 07:32:48 +02:00
|
|
|
t.Errorf("exp true, got %v", ok)
|
|
|
|
}
|
|
|
|
|
2024-12-24 08:00:23 +01:00
|
|
|
if actTasks[0].ID == "" {
|
2024-10-01 07:36:31 +02:00
|
|
|
t.Errorf("exp string not te be empty")
|
|
|
|
}
|
2024-12-24 08:00:23 +01:00
|
|
|
tc.expTask.ID = actTasks[0].ID
|
|
|
|
if diff := item.TaskDiff(tc.expTask, actTasks[0]); diff != "" {
|
2024-12-01 10:22:47 +01:00
|
|
|
t.Errorf("(exp -, got +)\n%s", diff)
|
2024-10-01 07:36:31 +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-01 07:36:31 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|