This commit is contained in:
Erik Winter 2024-11-07 07:25:02 +01:00
parent 65d8ec66ef
commit 8255c4b18b
3 changed files with 211 additions and 160 deletions

View File

@ -50,19 +50,19 @@ func (add *AddCmd) Parse(args []string) (*ArgSet, error) {
if !as.HasFlag(FlagAt) && !as.HasFlag(FlagFor) {
as.SetFlag(FlagFor, "24h")
}
if err := add.Action(as); err != nil {
if err := add.Do(as); err != nil {
return nil, err
}
return as, nil
}
func (add *AddCmd) Action(as *ArgSet) error {
func (add *AddCmd) Do(as *ArgSet) error {
startFormat := "2006-01-02"
startStr := flags["on"]
if at, okAt := flags["at"]; !okAt {
startStr := as.Flag(FlagOn)
if as.HasFlag(FlagAt) {
startFormat = fmt.Sprintf("%s 15:04", startFormat)
startStr = fmt.Sprintf("%s %s", startStr, at)
startStr = fmt.Sprintf("%s %s", startStr, as.Flag(FlagAt))
}
start, err := time.Parse(startFormat, startStr)
if err != nil {
@ -72,13 +72,13 @@ func (add *AddCmd) Action(as *ArgSet) error {
e := item.Event{
ID: uuid.New().String(),
EventBody: item.EventBody{
Title: title,
Title: as.Main,
Start: start,
},
}
if forStr, okFor := flags["for"]; okFor {
fr, err := time.ParseDuration(forStr)
if as.HasFlag(FlagFor) {
fr, err := time.ParseDuration(as.Flag(FlagFor))
if err != nil {
return fmt.Errorf("%w: could not parse duration: %s", ErrInvalidArg, err)
}

View File

@ -1,153 +1,143 @@
package command_test
import (
"testing"
"time"
// func TestAdd(t *testing.T) {
// t.Parallel()
"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"
)
// 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)
// }
func TestAdd(t *testing.T) {
t.Parallel()
// for _, tc := range []struct {
// name string
// args map[string]string
// expEvent item.Event
// expErr bool
// }{
// {
// name: "no name",
// args: map[string]string{
// "on": "2024-10-01",
// "at": "9:00",
// "for": "1h",
// },
// expErr: true,
// },
// {
// name: "no date",
// args: map[string]string{
// "name": "event",
// "at": "9:00",
// "for": "1h",
// },
// expErr: true,
// },
// {
// name: "duration, but no time",
// args: map[string]string{
// "name": "event",
// "on": "2024-10-01",
// "for": "1h",
// },
// expErr: true,
// },
// {
// name: "time, but no duration",
// args: map[string]string{
// "name": "event",
// "on": "2024-10-01",
// "at": "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: map[string]string{
// "name": "event",
// "on": "2024-10-01",
// },
// 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: map[string]string{
// "name": "event",
// "on": "2024-10-01",
// "at": "9:00",
// "for": "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()
// actErr := command.Add(localRepo, eventRepo, syncRepo, tc.args["name"], tc.args["on"], tc.args["at"], tc.args["for"]) != 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))
// }
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)
}
// 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)
// }
for _, tc := range []struct {
name string
args map[string]string
expEvent item.Event
expErr bool
}{
{
name: "no name",
args: map[string]string{
"on": "2024-10-01",
"at": "9:00",
"for": "1h",
},
expErr: true,
},
{
name: "no date",
args: map[string]string{
"name": "event",
"at": "9:00",
"for": "1h",
},
expErr: true,
},
{
name: "duration, but no time",
args: map[string]string{
"name": "event",
"on": "2024-10-01",
"for": "1h",
},
expErr: true,
},
{
name: "time, but no duration",
args: map[string]string{
"name": "event",
"on": "2024-10-01",
"at": "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: map[string]string{
"name": "event",
"on": "2024-10-01",
},
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: map[string]string{
"name": "event",
"on": "2024-10-01",
"at": "9:00",
"for": "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()
actErr := command.Add(localRepo, eventRepo, syncRepo, tc.args["name"], tc.args["on"], tc.args["at"], tc.args["for"]) != 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))
}
// 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)
// }
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))
}
})
}
}
// 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))
// }
// })
// }
// }

View File

@ -2,24 +2,85 @@ package command_test
import (
"testing"
"github.com/google/go-cmp/cmp"
"go-mod.ewintr.nl/planner/plan/command"
)
func TestArgSet(t *testing.T) {
t.Parallel()
as := command.ArgSet{
Main: "main",
Flags: map[string]string{
"name 1": "value 1",
"name 2": "value 2",
"name 3": "value 3",
},
}
t.Run("hasflag", func(t *testing.T) {
t.Run("true", func(t *testing.T) {
if has := as.HasFlag("name 1"); !has {
t.Errorf("exp true, got %v", has)
}
})
t.Run("false", func(t *testing.T) {
if has := as.HasFlag("unknown"); has {
t.Errorf("exp false, got %v", has)
}
})
})
t.Run("flag", func(t *testing.T) {
t.Run("known", func(t *testing.T) {
if val := as.Flag("name 1"); val != "value 1" {
t.Errorf("exp value 1, got %v", val)
}
})
t.Run("unknown", func(t *testing.T) {
if val := as.Flag("unknown"); val != "" {
t.Errorf(`exp "", got %v`, val)
}
})
})
t.Run("setflag", func(t *testing.T) {
exp := "new value"
as.SetFlag("new name", exp)
if act := as.Flag("new name"); exp != act {
t.Errorf("exp %v, got %v", exp, act)
}
})
}
func TestParseArgs(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
args []string
expRem string
expFlags map[string]string
expErr bool
name string
args []string
expAS *command.ArgSet
expErr bool
}{
{
name: "empty",
expAS: &command.ArgSet{
Flags: map[string]string{},
},
},
} {
t.Run(tc.name, func(t *testing.T) {
actAS, actErr := command.ParseArgs(tc.args)
if tc.expErr != (actErr != nil) {
t.Errorf("exp %v, got %v", tc.expErr, actErr)
}
if tc.expErr {
return
}
if diff := cmp.Diff(tc.expAS, actAS); diff != "" {
t.Errorf("(exp +, got -)\n%s", diff)
}
})
}
}