2025-01-19 10:56:03 +01:00
|
|
|
package schedule_test
|
2024-10-01 07:36:31 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"go-mod.ewintr.nl/planner/item"
|
2025-01-19 10:56:03 +01:00
|
|
|
"go-mod.ewintr.nl/planner/plan/command/schedule"
|
2024-10-03 07:32:48 +02:00
|
|
|
"go-mod.ewintr.nl/planner/plan/storage/memory"
|
2024-10-01 07:36:31 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAdd(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2024-12-19 12:06:03 +01:00
|
|
|
aDate := item.NewDate(2024, 11, 2)
|
2024-10-01 07:36:31 +02:00
|
|
|
|
|
|
|
for _, tc := range []struct {
|
2025-01-19 10:56:03 +01:00
|
|
|
name string
|
|
|
|
main []string
|
|
|
|
fields map[string]string
|
|
|
|
expErr bool
|
|
|
|
expSchedule item.Schedule
|
2024-10-01 07:36:31 +02:00
|
|
|
}{
|
|
|
|
{
|
2024-10-29 07:22:04 +01:00
|
|
|
name: "empty",
|
2024-10-01 07:36:31 +02:00
|
|
|
expErr: true,
|
|
|
|
},
|
|
|
|
{
|
2024-10-29 07:22:04 +01:00
|
|
|
name: "title missing",
|
2025-01-19 10:56:03 +01:00
|
|
|
main: []string{"sched", "add"},
|
2024-12-27 11:20:32 +01:00
|
|
|
fields: map[string]string{
|
|
|
|
"date": aDate.String(),
|
2024-10-01 07:36:31 +02:00
|
|
|
},
|
|
|
|
expErr: true,
|
|
|
|
},
|
|
|
|
{
|
2024-12-31 08:37:00 +01:00
|
|
|
name: "all",
|
2025-01-19 10:56:03 +01:00
|
|
|
main: []string{"sched", "add", "title"},
|
2024-12-27 11:20:32 +01:00
|
|
|
fields: map[string]string{
|
2025-01-19 10:56:03 +01:00
|
|
|
"date": aDate.String(),
|
2024-10-01 07:36:31 +02:00
|
|
|
},
|
2025-01-19 10:56:03 +01:00
|
|
|
expSchedule: item.Schedule{
|
2024-12-19 12:06:03 +01:00
|
|
|
ID: "title",
|
|
|
|
Date: aDate,
|
2025-01-19 10:56:03 +01:00
|
|
|
ScheduleBody: item.ScheduleBody{
|
|
|
|
Title: "title",
|
2024-10-01 07:36:31 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2024-12-29 09:32:49 +01:00
|
|
|
// setup
|
2025-01-13 09:13:48 +01:00
|
|
|
mems := memory.New()
|
2024-12-29 09:32:49 +01:00
|
|
|
|
|
|
|
// parse
|
2025-01-19 10:56:03 +01:00
|
|
|
cmd, actParseErr := schedule.NewAddArgs().Parse(tc.main, tc.fields)
|
2024-12-27 11:20:32 +01:00
|
|
|
if tc.expErr != (actParseErr != nil) {
|
2024-10-29 07:22:04 +01:00
|
|
|
t.Errorf("exp %v, got %v", tc.expErr, actParseErr)
|
2024-10-01 07:36:31 +02:00
|
|
|
}
|
|
|
|
if tc.expErr {
|
|
|
|
return
|
|
|
|
}
|
2024-12-29 09:32:49 +01:00
|
|
|
|
|
|
|
// do
|
2025-01-13 09:13:48 +01:00
|
|
|
if _, err := cmd.Do(mems, nil); err != nil {
|
2024-12-27 11:20:32 +01:00
|
|
|
t.Errorf("exp nil, got %v", err)
|
|
|
|
}
|
2024-10-29 07:22:04 +01:00
|
|
|
|
2024-12-29 09:32:49 +01:00
|
|
|
// check
|
2025-01-19 10:56:03 +01:00
|
|
|
actSchedules, err := mems.Schedule(nil).Find(aDate.Add(-1), aDate.Add(1))
|
2024-10-01 07:36:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("exp nil, got %v", err)
|
|
|
|
}
|
2025-01-19 10:56:03 +01:00
|
|
|
if len(actSchedules) != 1 {
|
|
|
|
t.Errorf("exp 1, got %d", len(actSchedules))
|
2024-10-01 07:36:31 +02:00
|
|
|
}
|
2024-10-03 07:32:48 +02:00
|
|
|
|
2025-01-13 09:13:48 +01:00
|
|
|
actLocalIDs, err := mems.LocalID(nil).FindAll()
|
2024-10-03 07:32:48 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("exp nil, got %v", err)
|
|
|
|
}
|
|
|
|
if len(actLocalIDs) != 1 {
|
|
|
|
t.Errorf("exp 1, got %v", len(actLocalIDs))
|
|
|
|
}
|
2025-01-19 10:56:03 +01:00
|
|
|
if _, ok := actLocalIDs[actSchedules[0].ID]; !ok {
|
2024-10-03 07:32:48 +02:00
|
|
|
t.Errorf("exp true, got %v", ok)
|
|
|
|
}
|
|
|
|
|
2025-01-19 10:56:03 +01:00
|
|
|
if actSchedules[0].ID == "" {
|
2024-10-01 07:36:31 +02:00
|
|
|
t.Errorf("exp string not te be empty")
|
|
|
|
}
|
2025-01-19 10:56:03 +01:00
|
|
|
tc.expSchedule.ID = actSchedules[0].ID
|
|
|
|
if diff := item.ScheduleDiff(tc.expSchedule, actSchedules[0]); diff != "" {
|
2024-12-01 10:22:47 +01:00
|
|
|
t.Errorf("(exp -, got +)\n%s", diff)
|
2024-10-01 07:36:31 +02:00
|
|
|
}
|
2024-10-07 11:11:18 +02:00
|
|
|
|
2025-01-13 09:13:48 +01:00
|
|
|
updated, err := mems.Sync(nil).FindAll()
|
2024-10-07 11:11:18 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("exp nil, got %v", err)
|
|
|
|
}
|
|
|
|
if len(updated) != 1 {
|
|
|
|
t.Errorf("exp 1, got %v", len(updated))
|
|
|
|
}
|
2024-10-01 07:36:31 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|