From e03618efec67926ac56e4a0fb12bb5f536c50609 Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Sun, 8 Dec 2024 10:31:07 +0100 Subject: [PATCH] tests --- item/event_test.go | 10 +++++++ plan/command/argset_test.go | 10 +++++++ plan/command/flag_test.go | 28 +++++++++++++++++++ plan/command/update_test.go | 54 ++++++++++++++++++++++++++++++++++++- 4 files changed, 101 insertions(+), 1 deletion(-) diff --git a/item/event_test.go b/item/event_test.go index 2e78cda..86e0a3f 100644 --- a/item/event_test.go +++ b/item/event_test.go @@ -48,6 +48,11 @@ func TestNewEvent(t *testing.T) { it: item.Item{ ID: "a", Kind: item.KindEvent, + Recurrer: &item.Recur{ + Start: time.Date(2024, 12, 8, 9, 0, 0, 0, time.UTC), + Period: item.PeriodDay, + Count: 1, + }, Body: `{ "title":"title", "start":"2024-09-20T08:00:00Z", @@ -56,6 +61,11 @@ func TestNewEvent(t *testing.T) { }, expEvent: item.Event{ ID: "a", + Recurrer: &item.Recur{ + Start: time.Date(2024, 12, 8, 9, 0, 0, 0, time.UTC), + Period: item.PeriodDay, + Count: 1, + }, EventBody: item.EventBody{ Title: "title", Start: time.Date(2024, 9, 20, 8, 0, 0, 0, time.UTC), diff --git a/plan/command/argset_test.go b/plan/command/argset_test.go index e8711e8..2261074 100644 --- a/plan/command/argset_test.go +++ b/plan/command/argset_test.go @@ -4,6 +4,7 @@ import ( "testing" "time" + "go-mod.ewintr.nl/planner/item" "go-mod.ewintr.nl/planner/plan/command" ) @@ -52,6 +53,15 @@ func TestArgSet(t *testing.T) { setValue: "2h30m", exp: 2*time.Hour + 30*time.Minute, }, + { + name: "recur period flag success", + flags: map[string]command.Flag{ + "period": &command.FlagPeriod{Name: "period"}, + }, + flagName: "period", + setValue: "month", + exp: item.PeriodMonth, + }, { name: "unknown flag error", flags: map[string]command.Flag{}, diff --git a/plan/command/flag_test.go b/plan/command/flag_test.go index a663d2c..ae552f4 100644 --- a/plan/command/flag_test.go +++ b/plan/command/flag_test.go @@ -4,6 +4,7 @@ import ( "testing" "time" + "go-mod.ewintr.nl/planner/item" "go-mod.ewintr.nl/planner/plan/command" ) @@ -113,3 +114,30 @@ func TestFlagDurationTime(t *testing.T) { t.Errorf("exp %v, got %v", valid, act) } } + +func TestFlagPeriod(t *testing.T) { + t.Parallel() + + valid := item.PeriodMonth + validStr := "month" + f := command.FlagPeriod{} + if f.IsSet() { + t.Errorf("exp false, got true") + } + + if err := f.Set(validStr); err != nil { + t.Errorf("exp nil, got %v", err) + } + + if !f.IsSet() { + t.Errorf("exp true, got false") + } + + act, ok := f.Get().(item.RecurPeriod) + if !ok { + t.Errorf("exp true, got false") + } + if act != valid { + t.Errorf("exp %v, got %v", valid, act) + } +} diff --git a/plan/command/update_test.go b/plan/command/update_test.go index 767ef2d..14b5575 100644 --- a/plan/command/update_test.go +++ b/plan/command/update_test.go @@ -149,6 +149,58 @@ func TestUpdateExecute(t *testing.T) { }, }, }, + { + name: "invalid rec start", + main: []string{"update", fmt.Sprintf("%d", lid)}, + flags: map[string]string{ + "rec-start": "invalud", + }, + expErr: true, + }, + { + name: "valid rec start", + main: []string{"update", fmt.Sprintf("%d", lid)}, + flags: map[string]string{ + "rec-start": "2024-12-08", + }, + expEvent: item.Event{ + ID: eid, + Recurrer: &item.Recur{ + Start: time.Date(2024, 12, 8, 0, 0, 0, 0, time.UTC), + }, + EventBody: item.EventBody{ + Title: title, + Start: start, + Duration: oneHour, + }, + }, + }, + { + name: "invalid rec period", + main: []string{"update", fmt.Sprintf("%d", lid)}, + flags: map[string]string{ + "rec-period": "invalid", + }, + expErr: true, + }, + { + name: "valid rec period", + main: []string{"update", fmt.Sprintf("%d", lid)}, + flags: map[string]string{ + "rec-period": "month", + }, + expEvent: item.Event{ + ID: eid, + Recurrer: &item.Recur{ + Period: item.PeriodMonth, + }, + EventBody: item.EventBody{ + Title: title, + Start: start, + Duration: oneHour, + }, + }, + }, } { t.Run(tc.name, func(t *testing.T) { eventRepo := memory.NewEvent() @@ -182,7 +234,7 @@ func TestUpdateExecute(t *testing.T) { t.Errorf("exp nil, got %v", err) } if diff := cmp.Diff(tc.expEvent, actEvent); diff != "" { - t.Errorf("(exp +, got -)\n%s", diff) + t.Errorf("(exp -, got +)\n%s", diff) } updated, err := syncRepo.FindAll() if err != nil {