planner/plan/command/update_test.go

197 lines
4.1 KiB
Go
Raw Normal View History

2024-10-06 11:28:05 +02:00
package command_test
import (
2024-11-27 07:30:52 +01:00
"fmt"
2024-10-06 11:28:05 +02:00
"testing"
"time"
"github.com/google/go-cmp/cmp"
"go-mod.ewintr.nl/planner/item"
"go-mod.ewintr.nl/planner/plan/command"
"go-mod.ewintr.nl/planner/plan/storage/memory"
)
2024-11-28 07:20:41 +01:00
func TestUpdateExecute(t *testing.T) {
2024-10-06 11:28:05 +02:00
t.Parallel()
eid := "c"
lid := 3
oneHour, err := time.ParseDuration("1h")
if err != nil {
t.Errorf("exp nil, got %v", err)
}
title := "title"
start := time.Date(2024, 10, 6, 10, 0, 0, 0, time.UTC)
2024-11-28 07:20:41 +01:00
twoHour, err := time.ParseDuration("2h")
2024-10-06 11:28:05 +02:00
if err != nil {
t.Errorf("exp nil, got %v", err)
}
for _, tc := range []struct {
2024-11-28 07:20:41 +01:00
name string
localID int
main []string
flags map[string]string
expEvent item.Event
expErr bool
2024-10-06 11:28:05 +02:00
}{
{
2024-11-28 07:20:41 +01:00
name: "no args",
expErr: true,
2024-10-06 11:28:05 +02:00
},
{
2024-11-28 07:20:41 +01:00
name: "not found",
localID: 1,
expErr: true,
2024-10-06 11:28:05 +02:00
},
{
name: "name",
localID: lid,
2024-11-27 07:30:52 +01:00
main: []string{"update", fmt.Sprintf("%d", lid), "updated"},
2024-10-06 11:28:05 +02:00
expEvent: item.Event{
ID: eid,
EventBody: item.EventBody{
Title: "updated",
Start: start,
Duration: oneHour,
},
},
},
2024-11-28 07:20:41 +01:00
{
name: "invalid on",
localID: lid,
main: []string{"update", fmt.Sprintf("%d", lid)},
flags: map[string]string{
"on": "invalid",
},
expErr: true,
},
{
name: "on",
localID: lid,
main: []string{"update", fmt.Sprintf("%d", lid)},
flags: map[string]string{
"on": "2024-10-02",
},
expEvent: item.Event{
ID: eid,
EventBody: item.EventBody{
Title: title,
Start: time.Date(2024, 10, 2, 10, 0, 0, 0, time.UTC),
Duration: oneHour,
},
},
},
{
name: "invalid at",
localID: lid,
main: []string{"update", fmt.Sprintf("%d", lid)},
flags: map[string]string{
"at": "invalid",
},
expErr: true,
},
{
name: "at",
localID: lid,
main: []string{"update", fmt.Sprintf("%d", lid)},
flags: map[string]string{
"at": "11:00",
},
expEvent: item.Event{
ID: eid,
EventBody: item.EventBody{
Title: title,
Start: time.Date(2024, 10, 6, 11, 0, 0, 0, time.UTC),
Duration: oneHour,
},
},
},
{
name: "on and at",
localID: lid,
main: []string{"update", fmt.Sprintf("%d", lid)},
flags: map[string]string{
"on": "2024-10-02",
"at": "11:00",
},
expEvent: item.Event{
ID: eid,
EventBody: item.EventBody{
Title: title,
Start: time.Date(2024, 10, 2, 11, 0, 0, 0, time.UTC),
Duration: oneHour,
},
},
},
{
name: "invalid for",
localID: lid,
main: []string{"update", fmt.Sprintf("%d", lid)},
flags: map[string]string{
"for": "invalid",
},
expErr: true,
},
{
name: "for",
localID: lid,
main: []string{"update", fmt.Sprintf("%d", lid)},
flags: map[string]string{
"for": "2h",
},
expEvent: item.Event{
ID: eid,
EventBody: item.EventBody{
Title: title,
Start: time.Date(2024, 10, 6, 10, 0, 0, 0, time.UTC),
Duration: twoHour,
},
},
},
2024-10-06 11:28:05 +02:00
} {
t.Run(tc.name, func(t *testing.T) {
eventRepo := memory.NewEvent()
localIDRepo := memory.NewLocalID()
2024-10-07 11:11:18 +02:00
syncRepo := memory.NewSync()
2024-10-06 11:28:05 +02:00
if err := eventRepo.Store(item.Event{
ID: eid,
EventBody: item.EventBody{
Title: title,
Start: start,
Duration: oneHour,
},
}); err != nil {
t.Errorf("exp nil, got %v", err)
}
if err := localIDRepo.Store(eid, lid); err != nil {
t.Errorf("exp nil, ,got %v", err)
}
2024-11-27 07:30:52 +01:00
cmd := command.NewUpdate(localIDRepo, eventRepo, syncRepo)
2024-11-28 07:20:41 +01:00
actParseErr := cmd.Execute(tc.main, tc.flags) != nil
if tc.expErr != actParseErr {
t.Errorf("exp %v, got %v", tc.expErr, actParseErr)
2024-11-27 07:30:52 +01:00
}
2024-11-28 07:20:41 +01:00
if tc.expErr {
2024-10-06 11:28:05 +02:00
return
}
actEvent, err := eventRepo.Find(eid)
if err != nil {
t.Errorf("exp nil, got %v", err)
}
if diff := cmp.Diff(tc.expEvent, actEvent); diff != "" {
t.Errorf("(exp +, got -)\n%s", diff)
}
2024-10-07 11:11:18 +02:00
updated, err := syncRepo.FindAll()
if err != nil {
t.Errorf("exp nil, got %v", err)
}
if len(updated) != 1 {
t.Errorf("exp 1, got %v", len(updated))
}
2024-10-06 11:28:05 +02:00
})
}
}