From 5460e8cd903dfaf4191f15fe59f1908f4d58c191 Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Sat, 30 Nov 2024 11:20:10 +0100 Subject: [PATCH] list test --- plan/command/list.go | 29 ++++++++++++-------- plan/command/list_test.go | 58 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 11 deletions(-) create mode 100644 plan/command/list_test.go diff --git a/plan/command/list.go b/plan/command/list.go index 417c433..e94ca91 100644 --- a/plan/command/list.go +++ b/plan/command/list.go @@ -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 } diff --git a/plan/command/list_test.go b/plan/command/list_test.go new file mode 100644 index 0000000..53c8707 --- /dev/null +++ b/plan/command/list_test.go @@ -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) + } + }) + } +}