2024-10-01 07:36:31 +02:00
|
|
|
package command_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"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-10-29 07:22:04 +01:00
|
|
|
aDateStr := "2024-11-02"
|
|
|
|
aDate := time.Date(2024, 11, 2, 0, 0, 0, 0, time.UTC)
|
|
|
|
aTimeStr := "12: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-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{
|
|
|
|
command.FlagOn: aDateStr,
|
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{
|
|
|
|
command.FlagOn: aDateStr,
|
2024-10-01 07:36:31 +02:00
|
|
|
},
|
|
|
|
expEvent: item.Event{
|
2024-10-29 07:22:04 +01:00
|
|
|
ID: "title",
|
2024-10-01 07:36:31 +02:00
|
|
|
EventBody: item.EventBody{
|
2024-10-29 07:22:04 +01:00
|
|
|
Title: "title",
|
|
|
|
Start: aDate,
|
|
|
|
Duration: aDay,
|
2024-10-01 07:36:31 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-10-29 07:22:04 +01:00
|
|
|
name: "date and time",
|
|
|
|
main: []string{"add", "title"},
|
|
|
|
flags: map[string]string{
|
|
|
|
command.FlagOn: aDateStr,
|
|
|
|
command.FlagAt: aTimeStr,
|
2024-10-01 07:36:31 +02:00
|
|
|
},
|
|
|
|
expEvent: item.Event{
|
2024-10-29 07:22:04 +01:00
|
|
|
ID: "title",
|
2024-10-01 07:36:31 +02:00
|
|
|
EventBody: item.EventBody{
|
2024-10-29 07:22:04 +01:00
|
|
|
Title: "title",
|
|
|
|
Start: aDateAndTime,
|
|
|
|
Duration: anHour,
|
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{
|
|
|
|
command.FlagOn: aDateStr,
|
|
|
|
command.FlagAt: aTimeStr,
|
|
|
|
command.FlagFor: anHourStr,
|
2024-10-01 07:36:31 +02:00
|
|
|
},
|
|
|
|
expEvent: item.Event{
|
2024-10-29 07:22:04 +01:00
|
|
|
ID: "title",
|
2024-10-01 07:36:31 +02:00
|
|
|
EventBody: item.EventBody{
|
2024-10-29 07:22:04 +01:00
|
|
|
Title: "title",
|
|
|
|
Start: aDateAndTime,
|
|
|
|
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{
|
|
|
|
command.FlagOn: aDateStr,
|
|
|
|
command.FlagFor: anHourStr,
|
|
|
|
},
|
|
|
|
expErr: true,
|
|
|
|
},
|
2024-12-01 10:22:47 +01:00
|
|
|
{
|
|
|
|
name: "rec-start without rec-period",
|
|
|
|
main: []string{"add", "title"},
|
|
|
|
flags: map[string]string{
|
|
|
|
command.FlagOn: aDateStr,
|
|
|
|
command.FlagRecStart: "2024-12-08",
|
|
|
|
},
|
|
|
|
expErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "rec-period without rec-start",
|
|
|
|
main: []string{"add", "title"},
|
|
|
|
flags: map[string]string{
|
|
|
|
command.FlagOn: aDateStr,
|
|
|
|
command.FlagRecPeriod: "day",
|
|
|
|
},
|
|
|
|
expErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "rec-start with rec-period",
|
|
|
|
main: []string{"add", "title"},
|
|
|
|
flags: map[string]string{
|
|
|
|
command.FlagOn: aDateStr,
|
|
|
|
command.FlagRecStart: "2024-12-08",
|
|
|
|
command.FlagRecPeriod: "day",
|
|
|
|
},
|
|
|
|
expEvent: item.Event{
|
|
|
|
ID: "title",
|
|
|
|
Recurrer: &item.Recur{
|
|
|
|
Start: time.Date(2024, 12, 8, 0, 0, 0, 0, time.UTC),
|
|
|
|
Period: item.PeriodDay,
|
|
|
|
},
|
|
|
|
RecurNext: time.Time{},
|
|
|
|
EventBody: item.EventBody{
|
|
|
|
Title: "title",
|
|
|
|
Start: aDate,
|
|
|
|
Duration: aDay,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
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
|
|
|
|
if diff := cmp.Diff(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
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|