planner/item/event_test.go

214 lines
3.9 KiB
Go
Raw Normal View History

2024-09-20 07:09:30 +02:00
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()
2024-09-26 07:21:48 +02:00
oneHour, err := time.ParseDuration("1h")
if err != nil {
t.Errorf("exp nil, got %v", err)
}
2024-09-20 07:09:30 +02:00
for _, tc := range []struct {
name string
it item.Item
expEvent item.Event
expErr bool
}{
{
name: "wrong kind",
it: item.Item{
ID: "a",
2024-12-19 12:06:03 +01:00
Date: item.NewDate(2024, 9, 20),
2024-09-20 07:09:30 +02:00
Kind: item.KindTask,
Body: `{
"title":"title",
2024-12-19 12:06:03 +01:00
"time":"08:00",
"duration":"1h"
2024-09-20 07:09:30 +02:00
}`,
},
expErr: true,
},
{
name: "invalid json",
it: item.Item{
ID: "a",
Kind: item.KindEvent,
Body: `{"id":"a"`,
},
expErr: true,
},
{
name: "valid",
it: item.Item{
2024-12-19 12:06:03 +01:00
ID: "a",
Kind: item.KindEvent,
Date: item.NewDate(2024, 9, 20),
Recurrer: item.NewRecurrer("2024-12-08, daily"),
2024-09-20 07:09:30 +02:00
Body: `{
"title":"title",
2024-12-19 12:06:03 +01:00
"time":"08:00",
"duration":"1h"
2024-09-20 07:09:30 +02:00
}`,
},
expEvent: item.Event{
2024-12-19 12:06:03 +01:00
ID: "a",
Date: item.NewDate(2024, 9, 20),
Recurrer: item.NewRecurrer("2024-12-08, daily"),
2024-09-20 07:09:30 +02:00
EventBody: item.EventBody{
2024-09-26 07:21:48 +02:00
Title: "title",
2024-12-19 12:06:03 +01:00
Time: item.NewTime(8, 0),
2024-09-26 07:21:48 +02:00
Duration: oneHour,
2024-09-20 07:09:30 +02:00
},
},
},
} {
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
}
2024-12-19 12:06:03 +01:00
if diff := item.EventDiff(tc.expEvent, actEvent); diff != "" {
t.Errorf("(+exp, -got)\n%s", diff)
2024-09-20 07:09:30 +02:00
}
})
}
}
func TestEventItem(t *testing.T) {
t.Parallel()
2024-09-26 07:21:48 +02:00
oneHour, err := time.ParseDuration("1h")
if err != nil {
t.Errorf("exp nil, got %v", err)
}
2024-09-20 07:09:30 +02:00
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{},
2024-12-19 12:06:03 +01:00
Body: `{"duration":"0s","title":"","time":"00:00"}`,
2024-09-20 07:09:30 +02:00
},
},
{
name: "normal",
event: item.Event{
2024-12-19 12:06:03 +01:00
ID: "a",
Date: item.NewDate(2024, 9, 23),
2024-09-20 07:09:30 +02:00
EventBody: item.EventBody{
2024-09-26 07:21:48 +02:00
Title: "title",
2024-12-19 12:06:03 +01:00
Time: item.NewTime(8, 0),
2024-09-26 07:21:48 +02:00
Duration: oneHour,
2024-09-20 07:09:30 +02:00
},
},
expItem: item.Item{
ID: "a",
Kind: item.KindEvent,
Updated: time.Time{},
2024-12-19 12:06:03 +01:00
Date: item.NewDate(2024, 9, 23),
Body: `{"duration":"1h0m0s","title":"title","time":"08:00"}`,
2024-09-20 07:09:30 +02: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)
}
})
}
}
2024-10-29 07:22:04 +01:00
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{
2024-12-19 12:06:03 +01:00
ID: "a",
Date: item.NewDate(2024, 9, 20),
2024-10-29 07:22:04 +01:00
EventBody: item.EventBody{
2024-12-19 12:06:03 +01:00
Time: item.NewTime(8, 0),
2024-10-29 07:22:04 +01:00
Duration: oneHour,
},
},
},
{
name: "no date",
event: item.Event{
ID: "a",
EventBody: item.EventBody{
Title: "title",
2024-12-19 12:06:03 +01:00
Time: item.NewTime(8, 0),
2024-10-29 07:22:04 +01:00
Duration: oneHour,
},
},
},
{
name: "no duration",
event: item.Event{
2024-12-19 12:06:03 +01:00
ID: "a",
Date: item.NewDate(2024, 9, 20),
2024-10-29 07:22:04 +01:00
EventBody: item.EventBody{
Title: "title",
2024-12-19 12:06:03 +01:00
Time: item.NewTime(8, 0),
2024-10-29 07:22:04 +01:00
},
},
},
{
name: "valid",
event: item.Event{
2024-12-19 12:06:03 +01:00
ID: "a",
Date: item.NewDate(2024, 9, 20),
2024-10-29 07:22:04 +01:00
EventBody: item.EventBody{
Title: "title",
2024-12-19 12:06:03 +01:00
Time: item.NewTime(8, 0),
2024-10-29 07:22:04 +01:00
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)
}
})
}
}