planner/item/task_test.go

192 lines
3.5 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"
)
2024-12-24 08:00:23 +01:00
func TestNewTask(t *testing.T) {
2024-09-20 07:09:30 +02:00
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 {
2024-12-24 08:00:23 +01:00
name string
it item.Item
expTask item.Task
expErr bool
2024-09-20 07:09:30 +02:00
}{
{
name: "wrong kind",
it: item.Item{
ID: "a",
2024-12-19 12:06:03 +01:00
Date: item.NewDate(2024, 9, 20),
2024-12-24 08:00:23 +01:00
Kind: item.KindSchedule,
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
}`,
},
expErr: true,
},
{
name: "invalid json",
it: item.Item{
ID: "a",
2024-12-24 08:00:23 +01:00
Kind: item.KindTask,
2024-09-20 07:09:30 +02:00
Body: `{"id":"a"`,
},
expErr: true,
},
{
name: "valid",
it: item.Item{
2024-12-19 12:06:03 +01:00
ID: "a",
2024-12-24 08:00:23 +01:00
Kind: item.KindTask,
2024-12-19 12:06:03 +01:00
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
}`,
},
2024-12-24 08:00:23 +01:00
expTask: item.Task{
2024-12-19 12:06:03 +01:00
ID: "a",
Date: item.NewDate(2024, 9, 20),
Recurrer: item.NewRecurrer("2024-12-08, daily"),
2024-12-24 08:00:23 +01:00
TaskBody: item.TaskBody{
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) {
2024-12-24 08:00:23 +01:00
actTask, actErr := item.NewTask(tc.it)
2024-09-20 07:09:30 +02:00
if tc.expErr != (actErr != nil) {
t.Errorf("exp nil, got %v", actErr)
}
if tc.expErr {
return
}
2024-12-24 08:00:23 +01:00
if diff := item.TaskDiff(tc.expTask, actTask); diff != "" {
2024-12-19 12:06:03 +01:00
t.Errorf("(+exp, -got)\n%s", diff)
2024-09-20 07:09:30 +02:00
}
})
}
}
2024-12-24 08:00:23 +01:00
func TestTaskItem(t *testing.T) {
2024-09-20 07:09:30 +02:00
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
2024-12-24 08:00:23 +01:00
tsk item.Task
2024-09-20 07:09:30 +02:00
expItem item.Item
expErr bool
}{
{
name: "empty",
expItem: item.Item{
2024-12-24 08:00:23 +01:00
Kind: item.KindTask,
2024-09-20 07:09:30 +02:00
Updated: time.Time{},
2024-12-25 10:20:33 +01:00
Body: `{"duration":"0s","title":"","time":""}`,
2024-09-20 07:09:30 +02:00
},
},
{
name: "normal",
2024-12-24 08:00:23 +01:00
tsk: item.Task{
2024-12-19 12:06:03 +01:00
ID: "a",
Date: item.NewDate(2024, 9, 23),
2024-12-24 08:00:23 +01:00
TaskBody: item.TaskBody{
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",
2024-12-24 08:00:23 +01:00
Kind: item.KindTask,
2024-09-20 07:09:30 +02:00
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) {
2024-12-24 08:00:23 +01:00
actItem, actErr := tc.tsk.Item()
2024-09-20 07:09:30 +02:00
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
2024-12-24 08:00:23 +01:00
func TestTaskValidate(t *testing.T) {
2024-10-29 07:22:04 +01:00
t.Parallel()
oneHour, err := time.ParseDuration("1h")
if err != nil {
t.Errorf("exp nil, got %v", err)
}
for _, tc := range []struct {
2024-12-24 08:00:23 +01:00
name string
tsk item.Task
exp bool
2024-10-29 07:22:04 +01:00
}{
{
name: "empty",
},
{
name: "missing title",
2024-12-24 08:00:23 +01:00
tsk: item.Task{
2024-12-19 12:06:03 +01:00
ID: "a",
Date: item.NewDate(2024, 9, 20),
2024-12-24 08:00:23 +01:00
TaskBody: item.TaskBody{
2024-12-19 12:06:03 +01:00
Time: item.NewTime(8, 0),
2024-10-29 07:22:04 +01:00
Duration: oneHour,
},
},
},
{
name: "valid",
2024-12-24 08:00:23 +01:00
tsk: item.Task{
2024-12-19 12:06:03 +01:00
ID: "a",
Date: item.NewDate(2024, 9, 20),
2024-12-24 08:00:23 +01:00
TaskBody: item.TaskBody{
2024-10-29 07:22:04 +01:00
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) {
2024-12-24 08:00:23 +01:00
if act := tc.tsk.Valid(); tc.exp != act {
2024-10-29 07:22:04 +01:00
t.Errorf("exp %v, got %v", tc.exp, act)
}
})
}
}