From f6f4946c914cb43b15ad95f814243012581071b2 Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Sun, 22 Dec 2024 13:27:45 +0100 Subject: [PATCH] event --- item/event.go | 57 +++++------ item/event_test.go | 239 +++++++++++++++++++++++---------------------- 2 files changed, 147 insertions(+), 149 deletions(-) diff --git a/item/event.go b/item/event.go index 6313455..b84c1e0 100644 --- a/item/event.go +++ b/item/event.go @@ -70,41 +70,34 @@ func NewEvent(i Item) (Event, error) { return e, nil } -// func (e Event) Item() (Item, error) { -// body, err := json.Marshal(EventBody{ -// Title: e.Title, -// Start: e.Start, -// Duration: e.Duration, -// }) -// if err != nil { -// return Item{}, fmt.Errorf("could not marshal event to json") -// } +func (e Event) Item() (Item, error) { + body, err := json.Marshal(e.EventBody) + if err != nil { + return Item{}, fmt.Errorf("could not marshal event body to json") + } -// return Item{ -// ID: e.ID, -// Kind: KindEvent, -// Recurrer: e.Recurrer, -// RecurNext: e.RecurNext, -// Body: string(body), -// }, nil -// } + return Item{ + ID: e.ID, + Kind: KindEvent, + Recurrer: e.Recurrer, + RecurNext: e.RecurNext, + Body: string(body), + }, nil +} -// func (e Event) Valid() bool { -// if e.Title == "" { -// return false -// } -// if e.Start.IsZero() || e.Start.Year() < 2024 { -// return false -// } -// if e.Duration.Seconds() < 1 { -// return false -// } -// // if e.Recurrer != nil && !e.Recurrer.Valid() { -// // return false -// // } +func (e Event) Valid() bool { + if e.Title == "" { + return false + } + if e.Date.IsZero() { + return false + } + if e.Duration.Seconds() < 1 { + return false + } -// return true -// } + return true +} func EventDiff(a, b Event) string { aJSON, _ := json.Marshal(a) diff --git a/item/event_test.go b/item/event_test.go index cb5ef0c..da86c2e 100644 --- a/item/event_test.go +++ b/item/event_test.go @@ -4,6 +4,7 @@ import ( "testing" "time" + "github.com/google/go-cmp/cmp" "go-mod.ewintr.nl/planner/item" ) @@ -83,125 +84,129 @@ func TestNewEvent(t *testing.T) { } } -// func TestEventItem(t *testing.T) { -// t.Parallel() +func TestEventItem(t *testing.T) { + t.Parallel() -// oneHour, err := time.ParseDuration("1h") -// if err != nil { -// t.Errorf("exp nil, got %v", err) -// } -// for _, tc := range []struct { -// name string -// event item.Event -// expItem item.Item -// expErr bool -// }{ -// { -// name: "empty", -// expItem: item.Item{ -// Kind: item.KindEvent, -// Updated: time.Time{}, -// Body: `{"start":"0001-01-01T00:00:00Z","duration":"0s","title":""}`, -// }, -// }, -// { -// name: "normal", -// event: item.Event{ -// ID: "a", -// EventBody: item.EventBody{ -// Title: "title", -// Start: time.Date(2024, 9, 23, 8, 0, 0, 0, time.UTC), -// Duration: oneHour, -// }, -// }, -// expItem: item.Item{ -// ID: "a", -// Kind: item.KindEvent, -// Updated: time.Time{}, -// Body: `{"start":"2024-09-23T08:00:00Z","duration":"1h0m0s","title":"title"}`, -// }, -// }, -// } { -// t.Run(tc.name, func(t *testing.T) { -// actItem, actErr := tc.event.Item() -// if tc.expErr != (actErr != nil) { -// t.Errorf("exp nil, got %v", actErr) -// } -// if tc.expErr { -// return -// } -// if diff := cmp.Diff(tc.expItem, actItem); diff != "" { -// t.Errorf("(exp+, got -)\n%s", diff) -// } -// }) -// } -// } + oneHour, err := time.ParseDuration("1h") + if err != nil { + t.Errorf("exp nil, got %v", err) + } + for _, tc := range []struct { + name string + event item.Event + expItem item.Item + expErr bool + }{ + { + name: "empty", + expItem: item.Item{ + Kind: item.KindEvent, + Updated: time.Time{}, + Body: `{"duration":"0s","title":"","date":"no date","time":"00:00"}`, + }, + }, + { + name: "normal", + event: item.Event{ + ID: "a", + EventBody: item.EventBody{ + Title: "title", + Date: item.NewDate(2024, 9, 23), + Time: item.NewTime(8, 0), + Duration: oneHour, + }, + }, + expItem: item.Item{ + ID: "a", + Kind: item.KindEvent, + Updated: time.Time{}, + Body: `{"duration":"1h0m0s","title":"title","date":"2024-09-23","time":"08:00"}`, + }, + }, + } { + t.Run(tc.name, func(t *testing.T) { + actItem, actErr := tc.event.Item() + if tc.expErr != (actErr != nil) { + t.Errorf("exp nil, got %v", actErr) + } + if tc.expErr { + return + } + if diff := cmp.Diff(tc.expItem, actItem); diff != "" { + t.Errorf("(exp+, got -)\n%s", diff) + } + }) + } +} -// func TestEventValidate(t *testing.T) { -// t.Parallel() +func TestEventValidate(t *testing.T) { + t.Parallel() -// oneHour, err := time.ParseDuration("1h") -// if err != nil { -// t.Errorf("exp nil, got %v", err) -// } + oneHour, err := time.ParseDuration("1h") + if err != nil { + t.Errorf("exp nil, got %v", err) + } -// for _, tc := range []struct { -// name string -// event item.Event -// exp bool -// }{ -// { -// name: "empty", -// }, -// { -// name: "missing title", -// event: item.Event{ -// ID: "a", -// EventBody: item.EventBody{ -// Start: time.Date(2024, 9, 20, 8, 0, 0, 0, time.UTC), -// Duration: oneHour, -// }, -// }, -// }, -// { -// name: "no date", -// event: item.Event{ -// ID: "a", -// EventBody: item.EventBody{ -// Title: "title", -// Start: time.Date(0, 0, 0, 8, 0, 0, 0, time.UTC), -// Duration: oneHour, -// }, -// }, -// }, -// { -// name: "no duration", -// event: item.Event{ -// ID: "a", -// EventBody: item.EventBody{ -// Title: "title", -// Start: time.Date(2024, 9, 20, 8, 0, 0, 0, time.UTC), -// }, -// }, -// }, -// { -// name: "valid", -// event: item.Event{ -// ID: "a", -// EventBody: item.EventBody{ -// Title: "title", -// Start: time.Date(2024, 9, 20, 8, 0, 0, 0, time.UTC), -// Duration: oneHour, -// }, -// }, -// exp: true, -// }, -// } { -// t.Run(tc.name, func(t *testing.T) { -// if act := tc.event.Valid(); tc.exp != act { -// t.Errorf("exp %v, got %v", tc.exp, act) -// } + for _, tc := range []struct { + name string + event item.Event + exp bool + }{ + { + name: "empty", + }, + { + name: "missing title", + event: item.Event{ + ID: "a", + EventBody: item.EventBody{ + Date: item.NewDate(2024, 9, 20), + Time: item.NewTime(8, 0), + Duration: oneHour, + }, + }, + }, + { + name: "no date", + event: item.Event{ + ID: "a", + EventBody: item.EventBody{ + Title: "title", + Time: item.NewTime(8, 0), + Duration: oneHour, + }, + }, + }, + { + name: "no duration", + event: item.Event{ + ID: "a", + EventBody: item.EventBody{ + Title: "title", + Date: item.NewDate(2024, 9, 20), + Time: item.NewTime(8, 0), + }, + }, + }, + { + name: "valid", + event: item.Event{ + ID: "a", + EventBody: item.EventBody{ + Title: "title", + Date: item.NewDate(2024, 9, 20), + Time: item.NewTime(8, 0), + Duration: oneHour, + }, + }, + exp: true, + }, + } { + t.Run(tc.name, func(t *testing.T) { + if act := tc.event.Valid(); tc.exp != act { + t.Errorf("exp %v, got %v", tc.exp, act) + } -// }) -// } -// } + }) + } +}