planner/plan/command/add_test.go

138 lines
2.9 KiB
Go
Raw Normal View History

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-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-23 11:30:24 +01:00
aDate := item.NewDate(2024, 11, 2)
aTime := item.NewTime(12, 0)
2024-10-29 07:22:04 +01:00
aDay := time.Duration(24) * time.Hour
anHourStr := "1h"
anHour := time.Hour
2024-10-01 07:36:31 +02:00
for _, tc := range []struct {
name string
2024-10-29 07:22:04 +01:00
main []string
flags map[string]string
2024-10-01 07:36:31 +02:00
expErr bool
2024-10-29 07:22:04 +01:00
expEvent item.Event
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"},
flags: map[string]string{
2024-12-23 11:30:24 +01:00
command.FlagOn: aDate.String(),
2024-10-01 07:36:31 +02:00
},
expErr: true,
},
{
2024-10-29 07:22:04 +01:00
name: "date missing",
main: []string{"add", "some", "title"},
2024-10-01 07:36:31 +02:00
expErr: true,
},
{
2024-10-29 07:22:04 +01:00
name: "only date",
main: []string{"add", "title"},
flags: map[string]string{
2024-12-23 11:30:24 +01:00
command.FlagOn: aDate.String(),
2024-10-01 07:36:31 +02:00
},
expEvent: item.Event{
2024-12-23 11:30:24 +01:00
ID: "title",
Date: aDate,
2024-10-01 07:36:31 +02:00
EventBody: item.EventBody{
2024-10-29 07:22:04 +01:00
Title: "title",
Duration: aDay,
2024-10-01 07:36:31 +02:00
},
},
},
{
2024-10-29 07:22:04 +01:00
name: "date, time and duration",
main: []string{"add", "title"},
flags: map[string]string{
2024-12-23 11:30:24 +01:00
command.FlagOn: aDate.String(),
command.FlagAt: aTime.String(),
2024-10-29 07:22:04 +01:00
command.FlagFor: anHourStr,
2024-10-01 07:36:31 +02:00
},
expEvent: item.Event{
2024-12-23 11:30:24 +01:00
ID: "title",
Date: aDate,
2024-10-01 07:36:31 +02:00
EventBody: item.EventBody{
2024-10-29 07:22:04 +01:00
Title: "title",
2024-12-23 11:30:24 +01:00
Time: aTime,
2024-10-29 07:22:04 +01:00
Duration: anHour,
2024-10-01 07:36:31 +02:00
},
},
},
2024-10-29 07:22:04 +01:00
{
name: "date and duration",
main: []string{"add", "title"},
flags: map[string]string{
2024-12-23 11:30:24 +01:00
command.FlagOn: aDate.String(),
2024-10-29 07:22:04 +01:00
command.FlagFor: anHourStr,
},
expErr: true,
},
2024-10-01 07:36:31 +02:00
} {
t.Run(tc.name, func(t *testing.T) {
2024-10-03 07:32:48 +02:00
eventRepo := memory.NewEvent()
localRepo := memory.NewLocalID()
2024-10-07 11:11:18 +02:00
syncRepo := memory.NewSync()
2024-10-29 07:22:04 +01:00
cmd := command.NewAdd(localRepo, eventRepo, syncRepo)
actParseErr := cmd.Execute(tc.main, tc.flags) != nil
if tc.expErr != actParseErr {
t.Errorf("exp %v, got %v", tc.expErr, actParseErr)
2024-10-01 07:36:31 +02:00
}
if tc.expErr {
return
}
2024-10-29 07:22:04 +01:00
2024-10-03 07:32:48 +02:00
actEvents, err := eventRepo.FindAll()
2024-10-01 07:36:31 +02:00
if err != nil {
t.Errorf("exp nil, got %v", err)
}
if len(actEvents) != 1 {
t.Errorf("exp 1, got %d", len(actEvents))
}
2024-10-03 07:32:48 +02:00
actLocalIDs, err := localRepo.FindAll()
if err != nil {
t.Errorf("exp nil, got %v", err)
}
if len(actLocalIDs) != 1 {
t.Errorf("exp 1, got %v", len(actLocalIDs))
}
if _, ok := actLocalIDs[actEvents[0].ID]; !ok {
t.Errorf("exp true, got %v", ok)
}
2024-10-01 07:36:31 +02:00
if actEvents[0].ID == "" {
t.Errorf("exp string not te be empty")
}
tc.expEvent.ID = actEvents[0].ID
2024-12-23 11:30:24 +01:00
if diff := item.EventDiff(tc.expEvent, actEvents[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
})
}
}