add tests

This commit is contained in:
Erik Winter 2024-11-19 07:31:50 +01:00
parent 9276a3771f
commit 6ead021edb
2 changed files with 120 additions and 175 deletions

View File

@ -64,6 +64,11 @@ func (add *AddCmd) Parse(main []string, flags map[string]string) error {
if !as.IsSet(FlagAt) && as.IsSet(FlagFor) {
return fmt.Errorf("%w: can not have duration without start time", ErrInvalidArg)
}
if as.IsSet(FlagAt) && !as.IsSet(FlagFor) {
if err := as.Flags[FlagFor].Set("1h"); err != nil {
return fmt.Errorf("could not set duration to one hour")
}
}
if !as.IsSet(FlagAt) && !as.IsSet(FlagFor) {
if err := as.Flags[FlagFor].Set("24h"); err != nil {
return fmt.Errorf("could not set duration to 24 hours")

View File

@ -2,7 +2,10 @@ 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"
"go-mod.ewintr.nl/planner/plan/storage/memory"
)
@ -11,37 +14,24 @@ func TestAddParse(t *testing.T) {
t.Parallel()
aDateStr := "2024-11-02"
// aDate := time.Date(2024, 11, 9, 0, 0, 0, 0, time.UTC)
aDate := time.Date(2024, 11, 2, 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
aDay := time.Duration(24) * time.Hour
anHourStr := "1h"
anHour := time.Hour
aDateAndTime := time.Date(2024, 11, 2, 12, 0, 0, 0, time.UTC)
// 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 string
main []string
flags map[string]string
expParseErr bool
expEvent item.Event
expDoErr bool
}{
{
name: "empty",
expErr: true,
name: "empty",
expParseErr: true,
},
{
name: "title missing",
@ -49,175 +39,125 @@ func TestAddParse(t *testing.T) {
flags: map[string]string{
command.FlagOn: aDateStr,
},
expErr: true,
expParseErr: true,
},
{
name: "date missing",
main: []string{"add", "some", "title"},
expErr: true,
name: "date missing",
main: []string{"add", "some", "title"},
expParseErr: true,
},
{
name: "minimal",
name: "only date",
main: []string{"add", "title"},
flags: map[string]string{
command.FlagOn: aDateStr,
},
expEvent: item.Event{
ID: "title",
EventBody: item.EventBody{
Title: "title",
Start: aDate,
Duration: aDay,
},
},
},
{
name: "start",
name: "date and time",
main: []string{"add", "title"},
flags: map[string]string{
command.FlagOn: aDateStr,
command.FlagAt: aTimeStr,
},
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,
},
// {
// 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) {
eventRepo := memory.NewEvent()
localRepo := memory.NewLocalID()
syncRepo := memory.NewSync()
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)
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
}
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))
}
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))
}
})
}
}
// 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))
// }
// })
// }
// }