79 lines
1.4 KiB
Go
79 lines
1.4 KiB
Go
package item_test
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"go-mod.ewintr.nl/planner/item"
|
|
)
|
|
|
|
func TestNewEvent(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
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",
|
|
"start":"2024-09-20T08:00:00Z",
|
|
"end":"2024-09-20T10:00:00Z"
|
|
}`,
|
|
},
|
|
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,
|
|
Body: `{
|
|
"title":"title",
|
|
"start":"2024-09-20T08:00:00Z",
|
|
"end":"2024-09-20T10:00:00Z"
|
|
}`,
|
|
},
|
|
expEvent: item.Event{
|
|
ID: "a",
|
|
EventBody: item.EventBody{
|
|
Title: "title",
|
|
Start: time.Date(2024, 9, 20, 8, 0, 0, 0, time.UTC),
|
|
End: time.Date(2024, 9, 20, 10, 0, 0, 0, time.UTC),
|
|
},
|
|
},
|
|
},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
actEvent, err := item.NewEvent(tc.it)
|
|
actErr := err != nil
|
|
if tc.expErr != actErr {
|
|
t.Errorf("exp %v, got %v", tc.expErr, actErr)
|
|
}
|
|
if tc.expErr {
|
|
return
|
|
}
|
|
if diff := cmp.Diff(tc.expEvent, actEvent); diff != "" {
|
|
fmt.Errorf("(exp +, got -)\n%s", diff)
|
|
}
|
|
})
|
|
}
|
|
}
|