list test

This commit is contained in:
Erik Winter 2024-11-30 11:20:10 +01:00
parent 7701a5615b
commit 5460e8cd90
2 changed files with 76 additions and 11 deletions

View File

@ -4,28 +4,35 @@ import (
"fmt"
"time"
"github.com/urfave/cli/v2"
"go-mod.ewintr.nl/planner/plan/storage"
)
var ListCmd = &cli.Command{
Name: "list",
Usage: "List everything",
type List struct {
localIDRepo storage.LocalID
eventRepo storage.Event
}
func NewListCmd(localRepo storage.LocalID, eventRepo storage.Event) *cli.Command {
ListCmd.Action = func(cCtx *cli.Context) error {
return List(localRepo, eventRepo)
func NewList(localIDRepo storage.LocalID, eventRepo storage.Event) Command {
return &List{
localIDRepo: localIDRepo,
eventRepo: eventRepo,
}
return ListCmd
}
func List(localRepo storage.LocalID, eventRepo storage.Event) error {
localIDs, err := localRepo.FindAll()
func (list *List) Execute(main []string, flags map[string]string) error {
if len(main) > 0 && main[0] != "list" {
return ErrWrongCommand
}
return list.do()
}
func (list *List) do() error {
localIDs, err := list.localIDRepo.FindAll()
if err != nil {
return fmt.Errorf("could not get local ids: %v", err)
}
all, err := eventRepo.FindAll()
all, err := list.eventRepo.FindAll()
if err != nil {
return err
}

58
plan/command/list_test.go Normal file
View File

@ -0,0 +1,58 @@
package command_test
import (
"testing"
"time"
"go-mod.ewintr.nl/planner/item"
"go-mod.ewintr.nl/planner/plan/command"
"go-mod.ewintr.nl/planner/plan/storage/memory"
)
func TestList(t *testing.T) {
t.Parallel()
eventRepo := memory.NewEvent()
localRepo := memory.NewLocalID()
e := item.Event{
ID: "id",
EventBody: item.EventBody{
Title: "name",
Start: time.Date(2024, 10, 7, 9, 30, 0, 0, time.UTC),
},
}
if err := eventRepo.Store(e); err != nil {
t.Errorf("exp nil, got %v", err)
}
if err := localRepo.Store(e.ID, 1); err != nil {
t.Errorf("exp nil, got %v", err)
}
for _, tc := range []struct {
name string
main []string
expErr bool
}{
{
name: "empty",
main: []string{},
},
{
name: "list",
main: []string{"list"},
},
{
name: "wrong",
main: []string{"delete"},
expErr: true,
},
} {
t.Run(tc.name, func(t *testing.T) {
cmd := command.NewList(localRepo, eventRepo)
actErr := cmd.Execute(tc.main, nil) != nil
if tc.expErr != actErr {
t.Errorf("exp %v, got %v", tc.expErr, actErr)
}
})
}
}