planner/plan/command/add_test.go

164 lines
3.5 KiB
Go
Raw Normal View History

2024-10-01 07:36:31 +02:00
package command_test
2024-11-09 17:22:05 +01:00
import (
"testing"
2024-11-19 07:31:50 +01:00
"time"
2024-11-09 17:22:05 +01:00
2024-11-19 07:31:50 +01:00
"github.com/google/go-cmp/cmp"
"go-mod.ewintr.nl/planner/item"
2024-11-09 17:22:05 +01:00
"go-mod.ewintr.nl/planner/plan/command"
2024-11-10 15:04:14 +01:00
"go-mod.ewintr.nl/planner/plan/storage/memory"
2024-11-09 17:22:05 +01:00
)
func TestAddParse(t *testing.T) {
t.Parallel()
2024-11-14 07:31:50 +01:00
aDateStr := "2024-11-02"
2024-11-19 07:31:50 +01:00
aDate := time.Date(2024, 11, 2, 0, 0, 0, 0, time.UTC)
2024-11-14 07:31:50 +01:00
aTimeStr := "12:00"
2024-11-19 07:31:50 +01:00
aDay := time.Duration(24) * time.Hour
anHourStr := "1h"
anHour := time.Hour
aDateAndTime := time.Date(2024, 11, 2, 12, 0, 0, 0, time.UTC)
2024-11-14 07:31:50 +01:00
2024-11-09 17:22:05 +01:00
for _, tc := range []struct {
2024-11-19 07:31:50 +01:00
name string
main []string
flags map[string]string
expParseErr bool
expEvent item.Event
expDoErr bool
2024-11-09 17:22:05 +01:00
}{
{
2024-11-19 07:31:50 +01:00
name: "empty",
expParseErr: true,
2024-11-09 17:22:05 +01:00
},
{
2024-11-14 07:31:50 +01:00
name: "title missing",
main: []string{"add"},
2024-11-15 07:25:23 +01:00
flags: map[string]string{
command.FlagOn: aDateStr,
2024-11-14 07:31:50 +01:00
},
2024-11-19 07:31:50 +01:00
expParseErr: true,
2024-11-09 17:22:05 +01:00
},
{
2024-11-19 07:31:50 +01:00
name: "date missing",
main: []string{"add", "some", "title"},
expParseErr: true,
2024-11-09 17:22:05 +01:00
},
{
2024-11-19 07:31:50 +01:00
name: "only date",
2024-11-14 07:31:50 +01:00
main: []string{"add", "title"},
2024-11-15 07:25:23 +01:00
flags: map[string]string{
command.FlagOn: aDateStr,
2024-11-09 17:22:05 +01:00
},
2024-11-19 07:31:50 +01:00
expEvent: item.Event{
ID: "title",
EventBody: item.EventBody{
Title: "title",
Start: aDate,
Duration: aDay,
},
},
2024-11-09 17:22:05 +01:00
},
{
2024-11-19 07:31:50 +01:00
name: "date and time",
2024-11-14 07:31:50 +01:00
main: []string{"add", "title"},
2024-11-15 07:25:23 +01:00
flags: map[string]string{
command.FlagOn: aDateStr,
command.FlagAt: aTimeStr,
2024-11-09 17:22:05 +01:00
},
2024-11-19 07:31:50 +01:00
expEvent: item.Event{
ID: "title",
EventBody: item.EventBody{
Title: "title",
Start: aDateAndTime,
Duration: anHour,
},
},
},
{
name: "date, time and duration",
main: []string{"add", "title"},
flags: map[string]string{
command.FlagOn: aDateStr,
command.FlagAt: aTimeStr,
command.FlagFor: anHourStr,
},
expEvent: item.Event{
ID: "title",
EventBody: item.EventBody{
Title: "title",
Start: aDateAndTime,
Duration: anHour,
},
},
},
{
name: "date and duration",
main: []string{"add", "title"},
flags: map[string]string{
command.FlagOn: aDateStr,
command.FlagFor: anHourStr,
},
expParseErr: true,
2024-11-09 17:22:05 +01:00
},
} {
t.Run(tc.name, func(t *testing.T) {
2024-11-19 07:31:50 +01:00
eventRepo := memory.NewEvent()
localRepo := memory.NewLocalID()
syncRepo := memory.NewSync()
2024-11-22 08:23:13 +01:00
cmd := command.NewAdd(localRepo, eventRepo, syncRepo)
2024-11-19 07:31:50 +01:00
actParseErr := cmd.Parse(tc.main, tc.flags) != nil
if tc.expParseErr != actParseErr {
t.Errorf("exp %v, got %v", tc.expParseErr, actParseErr)
}
if tc.expParseErr {
return
2024-11-09 17:22:05 +01:00
}
2024-10-01 07:36:31 +02:00
2024-11-19 07:31:50 +01:00
actDoErr := cmd.Do() != nil
if tc.expDoErr != actDoErr {
t.Errorf("exp %v, got %v", tc.expDoErr, actDoErr)
}
if tc.expDoErr {
return
}
actEvents, err := eventRepo.FindAll()
if err != nil {
t.Errorf("exp nil, got %v", err)
}
if len(actEvents) != 1 {
t.Errorf("exp 1, got %d", len(actEvents))
}
2024-10-01 07:36:31 +02:00
2024-11-19 07:31:50 +01: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
2024-11-19 07:31:50 +01:00
if actEvents[0].ID == "" {
t.Errorf("exp string not te be empty")
}
tc.expEvent.ID = actEvents[0].ID
if diff := cmp.Diff(tc.expEvent, actEvents[0]); diff != "" {
t.Errorf("(exp +, got -)\n%s", diff)
}
2024-10-03 07:32:48 +02:00
2024-11-19 07:31:50 +01: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))
}
})
}
}