package item_test import ( "testing" "time" "github.com/google/go-cmp/cmp" "go-mod.ewintr.nl/planner/item" ) func TestNewEvent(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 it item.Item expEvent item.Event expErr bool }{ { name: "wrong kind", it: item.Item{ ID: "a", Kind: item.KindTask, Body: `{ "title":"title", "date":"2024-09-20", "time":"08:00", "duration":"1h" }`, }, expErr: true, }, { name: "invalid json", it: item.Item{ ID: "a", Kind: item.KindEvent, Body: `{"id":"a"`, }, expErr: true, }, { name: "valid", it: item.Item{ ID: "a", Kind: item.KindEvent, Recurrer: item.NewRecurrer("2024-12-08, daily"), Body: `{ "title":"title", "date":"2024-09-20", "time":"08:00", "duration":"1h" }`, }, expEvent: item.Event{ ID: "a", Recurrer: item.NewRecurrer("2024-12-08, daily"), EventBody: item.EventBody{ Title: "title", Date: item.NewDate(2024, 9, 20), Time: item.NewTime(8, 0), Duration: oneHour, }, }, }, } { t.Run(tc.name, func(t *testing.T) { actEvent, actErr := item.NewEvent(tc.it) if tc.expErr != (actErr != nil) { t.Errorf("exp nil, got %v", actErr) } if tc.expErr { return } if diff := item.EventDiff(tc.expEvent, actEvent); diff != "" { t.Errorf("(+exp, -got)\n%s", diff) } }) } } 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: `{"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() 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{ 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) } }) } }