224 lines
5.1 KiB
Go
224 lines
5.1 KiB
Go
package command_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"go-mod.ewintr.nl/planner/plan/command"
|
|
"go-mod.ewintr.nl/planner/plan/storage/memory"
|
|
)
|
|
|
|
func TestAddParse(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
aDateStr := "2024-11-02"
|
|
// aDate := time.Date(2024, 11, 9, 0, 0, 0, 0, time.UTC)
|
|
aTimeStr := "12:00"
|
|
// aTime := time.Date(0, 0, 0, 12, 0, 0, 0, time.UTC)
|
|
// aDayStr := "24h"
|
|
// aDay := time.Duration(24) * time.Hour
|
|
|
|
// flagOn := &command.FlagDate{
|
|
// Name: command.FlagOn,
|
|
// Value: aDate,
|
|
// }
|
|
// flagAt := &command.FlagTime{
|
|
// Name: command.FlagAt,
|
|
// Value: aTime,
|
|
// }
|
|
// flagFor := &command.FlagDuration{
|
|
// Name: command.FlagFor,
|
|
// Value: aDay,
|
|
// }
|
|
|
|
eventRepo := memory.NewEvent()
|
|
localRepo := memory.NewLocalID()
|
|
syncRepo := memory.NewSync()
|
|
for _, tc := range []struct {
|
|
name string
|
|
main []string
|
|
flags map[string]string
|
|
expErr bool
|
|
}{
|
|
{
|
|
name: "empty",
|
|
expErr: true,
|
|
},
|
|
{
|
|
name: "title missing",
|
|
main: []string{"add"},
|
|
flags: map[string]string{
|
|
command.FlagOn: aDateStr,
|
|
},
|
|
expErr: true,
|
|
},
|
|
{
|
|
name: "date missing",
|
|
main: []string{"add", "some", "title"},
|
|
expErr: true,
|
|
},
|
|
{
|
|
name: "minimal",
|
|
main: []string{"add", "title"},
|
|
flags: map[string]string{
|
|
command.FlagOn: aDateStr,
|
|
},
|
|
},
|
|
{
|
|
name: "start",
|
|
main: []string{"add", "title"},
|
|
flags: map[string]string{
|
|
command.FlagOn: aDateStr,
|
|
command.FlagAt: aTimeStr,
|
|
},
|
|
},
|
|
// {
|
|
// name: "start and duration",
|
|
// args: []string{"add", "title", "-on", "2024-11-10", "-at", "12:00", "-for", "1h"},
|
|
// expAS: &command.ArgSet{
|
|
// Main: "title",
|
|
// Flags: map[string]string{
|
|
// command.FlagOn: "2024-11-10",
|
|
// command.FlagAt: "12:00",
|
|
// command.FlagFor: "1h",
|
|
// },
|
|
// },
|
|
// },
|
|
// {
|
|
// name: "start without duration",
|
|
// args: []string{"add", "title", "-on", "2024-11-10", "-for", "1h"},
|
|
// expErr: true,
|
|
// },
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
cmd := command.NewAddCmd(localRepo, eventRepo, syncRepo)
|
|
actErr := cmd.Parse(tc.main, tc.flags) != nil
|
|
if tc.expErr != actErr {
|
|
t.Errorf("exp %v, got %v", tc.expErr, actErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// func TestAdd(t *testing.T) {
|
|
// t.Parallel()
|
|
|
|
// oneHour, err := time.ParseDuration("1h")
|
|
// if err != nil {
|
|
// t.Errorf("exp nil, got %v", err)
|
|
// }
|
|
// oneDay, err := time.ParseDuration("24h")
|
|
// if err != nil {
|
|
// t.Errorf("exp nil, got %v", err)
|
|
// }
|
|
|
|
// for _, tc := range []struct {
|
|
// name string
|
|
// args *command.ArgSet
|
|
// expEvent item.Event
|
|
// expErr bool
|
|
// }{
|
|
// {
|
|
// name: "time, but no duration",
|
|
// args: &command.ArgSet{
|
|
// Main: "event",
|
|
// Flags: map[string]string{
|
|
// command.FlagOn: "2024-10-01",
|
|
// command.FlagAt: "9:00",
|
|
// },
|
|
// },
|
|
// expEvent: item.Event{
|
|
// ID: "a",
|
|
// EventBody: item.EventBody{
|
|
// Title: "event",
|
|
// Start: time.Date(2024, 10, 1, 9, 0, 0, 0, time.UTC),
|
|
// },
|
|
// },
|
|
// },
|
|
// {
|
|
// name: "no time, no duration",
|
|
// args: &command.ArgSet{
|
|
// Main: "event",
|
|
// Flags: map[string]string{
|
|
// command.FlagOn: "2024-10-01",
|
|
// command.FlagFor: "24h",
|
|
// },
|
|
// },
|
|
// expEvent: item.Event{
|
|
// ID: "a",
|
|
// EventBody: item.EventBody{
|
|
// Title: "event",
|
|
// Start: time.Date(2024, 10, 1, 0, 0, 0, 0, time.UTC),
|
|
// Duration: oneDay,
|
|
// },
|
|
// },
|
|
// },
|
|
// {
|
|
// name: "full",
|
|
// args: &command.ArgSet{
|
|
// Main: "event",
|
|
// Flags: map[string]string{
|
|
// command.FlagOn: "2024-10-01",
|
|
// command.FlagAt: "9:00",
|
|
// command.FlagFor: "1h",
|
|
// },
|
|
// },
|
|
// expEvent: item.Event{
|
|
// ID: "a",
|
|
// EventBody: item.EventBody{
|
|
// Title: "event",
|
|
// Start: time.Date(2024, 10, 1, 9, 0, 0, 0, time.UTC),
|
|
// Duration: oneHour,
|
|
// },
|
|
// },
|
|
// },
|
|
// } {
|
|
// t.Run(tc.name, func(t *testing.T) {
|
|
// eventRepo := memory.NewEvent()
|
|
// localRepo := memory.NewLocalID()
|
|
// syncRepo := memory.NewSync()
|
|
// cmd := command.NewAddCmd(localRepo, eventRepo, syncRepo)
|
|
// actErr := cmd.Do(tc.args) != nil
|
|
// if tc.expErr != actErr {
|
|
// t.Errorf("exp %v, got %v", tc.expErr, actErr)
|
|
// }
|
|
// if tc.expErr {
|
|
// 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))
|
|
// }
|
|
|
|
// 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)
|
|
// }
|
|
|
|
// 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)
|
|
// }
|
|
|
|
// 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))
|
|
// }
|
|
// })
|
|
// }
|
|
// }
|