planner/plan/command/list_test.go

131 lines
2.5 KiB
Go
Raw Normal View History

2024-10-29 07:22:04 +01:00
package command_test
import (
"testing"
2025-01-04 11:03:57 +01:00
"time"
2024-10-29 07:22:04 +01:00
2025-01-04 11:03:57 +01:00
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
2024-10-29 07:22:04 +01:00
"go-mod.ewintr.nl/planner/item"
"go-mod.ewintr.nl/planner/plan/command"
"go-mod.ewintr.nl/planner/plan/storage/memory"
)
2025-01-04 11:03:57 +01:00
func TestListParse(t *testing.T) {
t.Parallel()
now := time.Now()
today := item.NewDate(now.Year(), int(now.Month()), now.Day())
for _, tc := range []struct {
name string
main []string
fields map[string]string
expArgs command.ListArgs
expErr bool
}{
{
2025-01-05 10:52:41 +01:00
name: "empty",
main: []string{},
fields: map[string]string{},
expArgs: command.ListArgs{},
2025-01-04 11:03:57 +01:00
},
{
name: "today",
main: []string{"tod"},
fields: map[string]string{},
expArgs: command.ListArgs{
To: today,
},
},
{
name: "tomorrow",
main: []string{"tom"},
fields: map[string]string{},
expArgs: command.ListArgs{
From: today.Add(1),
To: today.Add(1),
},
},
{
name: "week",
main: []string{"week"},
fields: map[string]string{},
expArgs: command.ListArgs{
From: today,
To: today.Add(7),
},
},
} {
t.Run(tc.name, func(t *testing.T) {
nla := command.NewListArgs()
cmd, actErr := nla.Parse(tc.main, tc.fields)
if tc.expErr != (actErr != nil) {
t.Errorf("exp %v, got %v", tc.expErr, actErr != nil)
}
if tc.expErr {
return
}
listCmd, ok := cmd.(command.List)
if !ok {
t.Errorf("exp true, got false")
}
if diff := cmp.Diff(tc.expArgs, listCmd.Args, cmpopts.IgnoreTypes(map[string][]string{})); diff != "" {
t.Errorf("(+exp, -got)\n%s\n", diff)
}
})
}
}
2024-10-29 07:22:04 +01:00
func TestList(t *testing.T) {
t.Parallel()
2025-01-13 09:13:48 +01:00
mems := memory.New()
2024-12-24 08:00:23 +01:00
e := item.Task{
2024-12-19 12:06:03 +01:00
ID: "id",
Date: item.NewDate(2024, 10, 7),
2024-12-24 08:00:23 +01:00
TaskBody: item.TaskBody{
2024-10-29 07:22:04 +01:00
Title: "name",
},
}
2025-01-13 09:13:48 +01:00
if err := mems.Task(nil).Store(e); err != nil {
2024-10-29 07:22:04 +01:00
t.Errorf("exp nil, got %v", err)
}
2025-01-13 09:13:48 +01:00
if err := mems.LocalID(nil).Store(e.ID, 1); err != nil {
2024-10-29 07:22:04 +01:00
t.Errorf("exp nil, got %v", err)
}
for _, tc := range []struct {
name string
2025-01-04 11:03:57 +01:00
cmd command.List
2024-12-29 09:32:49 +01:00
expRes bool
2024-10-29 07:22:04 +01:00
expErr bool
}{
{
2024-12-29 09:32:49 +01:00
name: "empty",
expRes: true,
},
{
name: "empty list",
2025-01-04 11:03:57 +01:00
cmd: command.List{
Args: command.ListArgs{
HasRecurrer: true,
},
},
2024-10-29 07:22:04 +01:00
},
} {
t.Run(tc.name, func(t *testing.T) {
2025-01-13 09:13:48 +01:00
res, err := tc.cmd.Do(mems, nil)
2024-12-29 09:32:49 +01:00
if err != nil {
2024-12-27 11:20:32 +01:00
t.Errorf("exp nil, got %v", err)
}
2024-12-29 09:32:49 +01:00
listRes := res.(command.ListResult)
actRes := len(listRes.Tasks) > 0
if tc.expRes != actRes {
t.Errorf("exp %v, got %v", tc.expRes, actRes)
}
2024-10-29 07:22:04 +01:00
})
}
}