This commit is contained in:
Erik Winter 2024-11-27 07:30:52 +01:00
parent 82a7944136
commit b39a2dee48
2 changed files with 116 additions and 273 deletions

View File

@ -42,11 +42,10 @@ func (update *Update) Parse(main []string, flags map[string]string) error {
return fmt.Errorf("not a local id: %v", main[1])
}
update.localID = localID
main = main[2:]
as := update.argSet
if len(main) > 1 {
as.Main = strings.Join(main[1:], " ")
}
as.Main = strings.Join(main, " ")
for k := range as.Flags {
v, ok := flags[k]
if !ok {
@ -56,6 +55,7 @@ func (update *Update) Parse(main []string, flags map[string]string) error {
return fmt.Errorf("could not set %s: %v", k, err)
}
}
update.argSet = as
return nil
}

View File

@ -1,6 +1,7 @@
package command_test
import (
"fmt"
"testing"
"time"
@ -21,41 +22,33 @@ func TestUpdate(t *testing.T) {
}
title := "title"
start := time.Date(2024, 10, 6, 10, 0, 0, 0, time.UTC)
twoHour, err := time.ParseDuration("2h")
// twoHour, err := time.ParseDuration("2h")
if err != nil {
t.Errorf("exp nil, got %v", err)
}
for _, tc := range []struct {
name string
localID int
args map[string]string
expEvent item.Event
expErr bool
name string
localID int
main []string
flags map[string]string
expEvent item.Event
expParseErr bool
expDoErr bool
}{
{
name: "no args",
localID: lid,
expEvent: item.Event{
ID: eid,
EventBody: item.EventBody{
Title: title,
Start: start,
Duration: oneHour,
},
},
name: "no args",
expParseErr: true,
},
{
name: "not found",
localID: 1,
expErr: true,
name: "not found",
localID: 1,
expParseErr: true,
},
{
name: "name",
localID: lid,
args: map[string]string{
"name": "updated",
},
main: []string{"update", fmt.Sprintf("%d", lid), "updated"},
expEvent: item.Event{
ID: eid,
EventBody: item.EventBody{
@ -65,91 +58,91 @@ func TestUpdate(t *testing.T) {
},
},
},
{
name: "invalid on",
localID: lid,
args: map[string]string{
"on": "invalid",
},
expErr: true,
},
{
name: "on",
localID: lid,
args: 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,
args: map[string]string{
"at": "invalid",
},
expErr: true,
},
{
name: "at",
localID: lid,
args: 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,
args: 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,
args: map[string]string{
"for": "invalid",
},
expErr: true,
},
{
name: "for",
localID: lid,
args: 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,
},
},
},
// {
// name: "invalid on",
// localID: lid,
// flags: map[string]string{
// "on": "invalid",
// },
// expParseErr: true,
// },
// {
// name: "on",
// localID: 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,
// flags: map[string]string{
// "at": "invalid",
// },
// expParseErr: true,
// },
// {
// name: "at",
// localID: 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,
// 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,
// flags: map[string]string{
// "for": "invalid",
// },
// expParseErr: true,
// },
// {
// name: "for",
// localID: 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,
// },
// },
// },
} {
t.Run(tc.name, func(t *testing.T) {
eventRepo := memory.NewEvent()
@ -169,11 +162,20 @@ func TestUpdate(t *testing.T) {
t.Errorf("exp nil, ,got %v", err)
}
actErr := command.Update(localIDRepo, eventRepo, syncRepo, tc.localID, tc.args["name"], tc.args["on"], tc.args["at"], tc.args["for"]) != nil
if tc.expErr != actErr {
t.Errorf("exp %v, got %v", tc.expErr, actErr)
cmd := command.NewUpdate(localIDRepo, eventRepo, syncRepo)
actParseErr := cmd.Parse(tc.main, tc.flags) != nil
if tc.expParseErr != actParseErr {
t.Errorf("exp %v, got %v", tc.expParseErr, actParseErr)
}
if tc.expErr {
if tc.expParseErr {
return
}
actDoErr := cmd.Do() != nil
if tc.expDoErr != actDoErr {
t.Errorf("exp %v, got %v", tc.expDoErr, actDoErr)
}
if tc.expDoErr {
return
}
@ -194,162 +196,3 @@ func TestUpdate(t *testing.T) {
})
}
}
package command_test
import (
"fmt"
"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"
)
// Mock implementations
type mockLocalID struct {
ids map[string]int
}
func (m *mockLocalID) Store(id string, localID int) error {
m.ids[id] = localID
return nil
}
func (m *mockLocalID) FindAll() (map[string]int, error) {
return m.ids, nil
}
type mockEvent struct {
events map[string]item.Event
}
func (m *mockEvent) Store(event item.Event) error {
m.events[event.ID] = event
return nil
}
func (m *mockEvent) Find(id string) (item.Event, error) {
if event, ok := m.events[id]; ok {
return event, nil
}
return item.Event{}, fmt.Errorf("event not found")
}
func (m *mockEvent) FindAll() ([]item.Event, error) {
var events []item.Event
for _, e := range m.events {
events = append(events, e)
}
return events, nil
}
func (m *mockEvent) Delete(id string) error {
delete(m.events, id)
return nil
}
type mockSync struct {
items map[string]item.Item
}
func (m *mockSync) Store(it item.Item) error {
m.items[it.ID] = it
return nil
}
func TestUpdate(t *testing.T) {
baseTime := time.Date(2024, 1, 1, 10, 0, 0, 0, time.UTC)
testEvent := item.Event{
ID: "test-id",
Title: "Original Title",
Start: baseTime,
Duration: 1 * time.Hour,
}
tests := []struct {
name string
localID int
nameStr string
onStr string
atStr string
frStr string
setup func(*mockLocalID, *mockEvent, *mockSync)
wantErr bool
validate func(*testing.T, *mockEvent, *mockSync)
}{
{
name: "update title only",
localID: 1,
nameStr: "New Title",
setup: func(l *mockLocalID, e *mockEvent, s *mockSync) {
l.ids["test-id"] = 1
e.events["test-id"] = testEvent
},
validate: func(t *testing.T, e *mockEvent, s *mockSync) {
updated := e.events["test-id"]
if updated.Title != "New Title" {
t.Errorf("expected title 'New Title', got %s", updated.Title)
}
if !updated.Start.Equal(baseTime) {
t.Error("start time should not have changed")
}
},
},
{
name: "update date only",
localID: 1,
onStr: "2024-02-01",
setup: func(l *mockLocalID, e *mockEvent, s *mockSync) {
l.ids["test-id"] = 1
e.events["test-id"] = testEvent
},
validate: func(t *testing.T, e *mockEvent, s *mockSync) {
updated := e.events["test-id"]
expectedTime := time.Date(2024, 2, 1, 10, 0, 0, 0, time.UTC)
if !updated.Start.Equal(expectedTime) {
t.Errorf("expected start time %v, got %v", expectedTime, updated.Start)
}
},
},
{
name: "invalid local ID",
localID: 999,
setup: func(l *mockLocalID, e *mockEvent, s *mockSync) {
l.ids["test-id"] = 1
},
wantErr: true,
},
{
name: "invalid duration format",
localID: 1,
frStr: "invalid",
setup: func(l *mockLocalID, e *mockEvent, s *mockSync) {
l.ids["test-id"] = 1
e.events["test-id"] = testEvent
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
localRepo := &mockLocalID{ids: make(map[string]int)}
eventRepo := &mockEvent{events: make(map[string]item.Event)}
syncRepo := &mockSync{items: make(map[string]item.Item)}
tt.setup(localRepo, eventRepo, syncRepo)
err := command.Update(localRepo, eventRepo, syncRepo, tt.localID, tt.nameStr, tt.onStr, tt.atStr, tt.frStr)
if (err != nil) != tt.wantErr {
t.Errorf("Update() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && tt.validate != nil {
tt.validate(t, eventRepo, syncRepo)
}
})
}
}