356 lines
7.4 KiB
Go
356 lines
7.4 KiB
Go
package command_test
|
|
|
|
import (
|
|
"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"
|
|
)
|
|
|
|
func TestUpdate(t *testing.T) {
|
|
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)
|
|
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: "no args",
|
|
localID: lid,
|
|
expEvent: item.Event{
|
|
ID: eid,
|
|
EventBody: item.EventBody{
|
|
Title: title,
|
|
Start: start,
|
|
Duration: oneHour,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "not found",
|
|
localID: 1,
|
|
expErr: true,
|
|
},
|
|
{
|
|
name: "name",
|
|
localID: lid,
|
|
args: map[string]string{
|
|
"name": "updated",
|
|
},
|
|
expEvent: item.Event{
|
|
ID: eid,
|
|
EventBody: item.EventBody{
|
|
Title: "updated",
|
|
Start: start,
|
|
Duration: oneHour,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
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,
|
|
},
|
|
},
|
|
},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
eventRepo := memory.NewEvent()
|
|
localIDRepo := memory.NewLocalID()
|
|
syncRepo := memory.NewSync()
|
|
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)
|
|
}
|
|
|
|
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)
|
|
}
|
|
if tc.expErr {
|
|
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)
|
|
}
|
|
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))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
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)
|
|
}
|
|
})
|
|
}
|
|
}
|