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]) return fmt.Errorf("not a local id: %v", main[1])
} }
update.localID = localID update.localID = localID
main = main[2:]
as := update.argSet as := update.argSet
if len(main) > 1 { as.Main = strings.Join(main, " ")
as.Main = strings.Join(main[1:], " ")
}
for k := range as.Flags { for k := range as.Flags {
v, ok := flags[k] v, ok := flags[k]
if !ok { 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) return fmt.Errorf("could not set %s: %v", k, err)
} }
} }
update.argSet = as
return nil return nil
} }

View File

@ -1,6 +1,7 @@
package command_test package command_test
import ( import (
"fmt"
"testing" "testing"
"time" "time"
@ -21,7 +22,7 @@ func TestUpdate(t *testing.T) {
} }
title := "title" title := "title"
start := time.Date(2024, 10, 6, 10, 0, 0, 0, time.UTC) 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 { if err != nil {
t.Errorf("exp nil, got %v", err) t.Errorf("exp nil, got %v", err)
} }
@ -29,33 +30,25 @@ func TestUpdate(t *testing.T) {
for _, tc := range []struct { for _, tc := range []struct {
name string name string
localID int localID int
args map[string]string main []string
flags map[string]string
expEvent item.Event expEvent item.Event
expErr bool expParseErr bool
expDoErr bool
}{ }{
{ {
name: "no args", name: "no args",
localID: lid, expParseErr: true,
expEvent: item.Event{
ID: eid,
EventBody: item.EventBody{
Title: title,
Start: start,
Duration: oneHour,
},
},
}, },
{ {
name: "not found", name: "not found",
localID: 1, localID: 1,
expErr: true, expParseErr: true,
}, },
{ {
name: "name", name: "name",
localID: lid, localID: lid,
args: map[string]string{ main: []string{"update", fmt.Sprintf("%d", lid), "updated"},
"name": "updated",
},
expEvent: item.Event{ expEvent: item.Event{
ID: eid, ID: eid,
EventBody: item.EventBody{ EventBody: item.EventBody{
@ -65,91 +58,91 @@ func TestUpdate(t *testing.T) {
}, },
}, },
}, },
{ // {
name: "invalid on", // name: "invalid on",
localID: lid, // localID: lid,
args: map[string]string{ // flags: map[string]string{
"on": "invalid", // "on": "invalid",
}, // },
expErr: true, // expParseErr: true,
}, // },
{ // {
name: "on", // name: "on",
localID: lid, // localID: lid,
args: map[string]string{ // flags: map[string]string{
"on": "2024-10-02", // "on": "2024-10-02",
}, // },
expEvent: item.Event{ // expEvent: item.Event{
ID: eid, // ID: eid,
EventBody: item.EventBody{ // EventBody: item.EventBody{
Title: title, // Title: title,
Start: time.Date(2024, 10, 2, 10, 0, 0, 0, time.UTC), // Start: time.Date(2024, 10, 2, 10, 0, 0, 0, time.UTC),
Duration: oneHour, // Duration: oneHour,
}, // },
}, // },
}, // },
{ // {
name: "invalid at", // name: "invalid at",
localID: lid, // localID: lid,
args: map[string]string{ // flags: map[string]string{
"at": "invalid", // "at": "invalid",
}, // },
expErr: true, // expParseErr: true,
}, // },
{ // {
name: "at", // name: "at",
localID: lid, // localID: lid,
args: map[string]string{ // flags: map[string]string{
"at": "11:00", // "at": "11:00",
}, // },
expEvent: item.Event{ // expEvent: item.Event{
ID: eid, // ID: eid,
EventBody: item.EventBody{ // EventBody: item.EventBody{
Title: title, // Title: title,
Start: time.Date(2024, 10, 6, 11, 0, 0, 0, time.UTC), // Start: time.Date(2024, 10, 6, 11, 0, 0, 0, time.UTC),
Duration: oneHour, // Duration: oneHour,
}, // },
}, // },
}, // },
{ // {
name: "on and at", // name: "on and at",
localID: lid, // localID: lid,
args: map[string]string{ // flags: map[string]string{
"on": "2024-10-02", // "on": "2024-10-02",
"at": "11:00", // "at": "11:00",
}, // },
expEvent: item.Event{ // expEvent: item.Event{
ID: eid, // ID: eid,
EventBody: item.EventBody{ // EventBody: item.EventBody{
Title: title, // Title: title,
Start: time.Date(2024, 10, 2, 11, 0, 0, 0, time.UTC), // Start: time.Date(2024, 10, 2, 11, 0, 0, 0, time.UTC),
Duration: oneHour, // Duration: oneHour,
}, // },
}, // },
}, // },
{ // {
name: "invalid for", // name: "invalid for",
localID: lid, // localID: lid,
args: map[string]string{ // flags: map[string]string{
"for": "invalid", // "for": "invalid",
}, // },
expErr: true, // expParseErr: true,
}, // },
{ // {
name: "for", // name: "for",
localID: lid, // localID: lid,
args: map[string]string{ // flags: map[string]string{
"for": "2h", // "for": "2h",
}, // },
expEvent: item.Event{ // expEvent: item.Event{
ID: eid, // ID: eid,
EventBody: item.EventBody{ // EventBody: item.EventBody{
Title: title, // Title: title,
Start: time.Date(2024, 10, 6, 10, 0, 0, 0, time.UTC), // Start: time.Date(2024, 10, 6, 10, 0, 0, 0, time.UTC),
Duration: twoHour, // Duration: twoHour,
}, // },
}, // },
}, // },
} { } {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
eventRepo := memory.NewEvent() eventRepo := memory.NewEvent()
@ -169,11 +162,20 @@ func TestUpdate(t *testing.T) {
t.Errorf("exp nil, ,got %v", err) 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 cmd := command.NewUpdate(localIDRepo, eventRepo, syncRepo)
if tc.expErr != actErr { actParseErr := cmd.Parse(tc.main, tc.flags) != nil
t.Errorf("exp %v, got %v", tc.expErr, actErr) 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 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)
}
})
}
}