planner/plan/command/sync_test.go

223 lines
4.5 KiB
Go
Raw Permalink Normal View History

2024-10-07 11:11:18 +02:00
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"
2024-12-29 09:32:49 +01:00
"go-mod.ewintr.nl/planner/plan/storage"
2024-10-07 11:11:18 +02:00
"go-mod.ewintr.nl/planner/plan/storage/memory"
"go-mod.ewintr.nl/planner/sync/client"
)
2024-10-29 07:22:04 +01:00
func TestSyncParse(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
main []string
expErr bool
}{
{
name: "empty",
expErr: true,
},
{
name: "wrong",
main: []string{"wrong"},
expErr: true,
},
{
name: "valid",
main: []string{"sync"},
},
} {
t.Run(tc.name, func(t *testing.T) {
2024-12-27 11:20:32 +01:00
_, actErr := command.SyncArgs{}.Parse(tc.main, nil)
if tc.expErr != (actErr != nil) {
2024-10-29 07:22:04 +01:00
t.Errorf("exp %v, got %v", tc.expErr, actErr)
}
})
}
}
2024-10-07 11:11:18 +02:00
func TestSyncSend(t *testing.T) {
t.Parallel()
syncClient := client.NewMemory()
2025-01-13 09:13:48 +01:00
mems := memory.New()
2024-10-07 11:11:18 +02:00
it := item.Item{
ID: "a",
2024-12-24 08:00:23 +01:00
Kind: item.KindTask,
2024-10-07 11:11:18 +02:00
Body: `{
"title":"title",
"start":"2024-10-18T08:00:00Z",
"duration":"1h"
}`,
}
2025-01-13 09:13:48 +01:00
if err := mems.Sync(nil).Store(it); err != nil {
2024-10-07 11:11:18 +02:00
t.Errorf("exp nil, got %v", err)
}
for _, tc := range []struct {
name string
ks []item.Kind
ts time.Time
expItems []item.Item
}{
{
name: "single",
2024-12-24 08:00:23 +01:00
ks: []item.Kind{item.KindTask},
2024-10-07 11:11:18 +02:00
expItems: []item.Item{it},
},
} {
t.Run(tc.name, func(t *testing.T) {
2024-12-27 11:20:32 +01:00
cmd, err := command.SyncArgs{}.Parse([]string{"sync"}, nil)
if err != nil {
t.Errorf("exp nil, got %v", err)
}
2025-01-13 09:13:48 +01:00
if _, err := cmd.Do(mems, syncClient); err != nil {
2024-10-07 11:11:18 +02:00
t.Errorf("exp nil, got %v", err)
}
actItems, actErr := syncClient.Updated(tc.ks, tc.ts)
if actErr != nil {
t.Errorf("exp nil, got %v", actErr)
}
if diff := cmp.Diff(tc.expItems, actItems); diff != "" {
t.Errorf("(exp +, got -)\n%s", diff)
}
2025-01-13 09:13:48 +01:00
actLeft, actErr := mems.Sync(nil).FindAll()
2024-10-07 11:11:18 +02:00
if actErr != nil {
t.Errorf("exp nil, got %v", actErr)
}
if len(actLeft) != 0 {
t.Errorf("exp 0, got %v", actLeft)
}
})
}
}
func TestSyncReceive(t *testing.T) {
t.Parallel()
oneHour, err := time.ParseDuration("1h")
if err != nil {
t.Errorf("exp nil, got %v", err)
}
for _, tc := range []struct {
name string
2024-12-24 08:00:23 +01:00
present []item.Task
2024-10-07 11:11:18 +02:00
updated []item.Item
2024-12-24 08:00:23 +01:00
expTask []item.Task
2024-10-07 11:11:18 +02:00
expLocalID map[string]int
}{
{
name: "no new",
2024-12-24 08:00:23 +01:00
expTask: []item.Task{},
2024-10-07 11:11:18 +02:00
expLocalID: map[string]int{},
},
{
name: "new",
updated: []item.Item{{
ID: "a",
2024-12-24 08:00:23 +01:00
Kind: item.KindTask,
2024-10-07 11:11:18 +02:00
Body: `{
"title":"title",
"start":"2024-10-23T08:00:00Z",
"duration":"1h"
}`,
}},
2024-12-24 08:00:23 +01:00
expTask: []item.Task{{
2024-12-19 12:06:03 +01:00
ID: "a",
Date: item.NewDate(2024, 10, 23),
2024-12-24 08:00:23 +01:00
TaskBody: item.TaskBody{
2024-10-07 11:11:18 +02:00
Title: "title",
Duration: oneHour,
},
}},
expLocalID: map[string]int{
"a": 1,
},
},
{
name: "update existing",
2024-12-24 08:00:23 +01:00
present: []item.Task{{
2024-12-19 12:06:03 +01:00
ID: "a",
Date: item.NewDate(2024, 10, 23),
2024-12-24 08:00:23 +01:00
TaskBody: item.TaskBody{
2024-10-07 11:11:18 +02:00
Title: "title",
Duration: oneHour,
},
}},
updated: []item.Item{{
ID: "a",
2024-12-24 08:00:23 +01:00
Kind: item.KindTask,
2024-10-07 11:11:18 +02:00
Body: `{
"title":"new title",
"start":"2024-10-23T08:00:00Z",
"duration":"1h"
}`,
}},
2024-12-24 08:00:23 +01:00
expTask: []item.Task{{
2024-12-19 12:06:03 +01:00
ID: "a",
Date: item.NewDate(2024, 10, 23),
2024-12-24 08:00:23 +01:00
TaskBody: item.TaskBody{
2024-10-07 11:11:18 +02:00
Title: "new title",
Duration: oneHour,
},
}},
expLocalID: map[string]int{
"a": 1,
},
},
} {
t.Run(tc.name, func(t *testing.T) {
syncClient := client.NewMemory()
2025-01-13 09:13:48 +01:00
mems := memory.New()
2024-10-07 11:11:18 +02:00
2024-12-27 11:20:32 +01:00
// setup
2024-10-07 11:11:18 +02:00
for i, p := range tc.present {
2025-01-13 09:13:48 +01:00
if err := mems.Task(nil).Store(p); err != nil {
2024-10-07 11:11:18 +02:00
t.Errorf("exp nil, got %v", err)
}
2025-01-13 09:13:48 +01:00
if err := mems.LocalID(nil).Store(p.ID, i+1); err != nil {
2024-10-07 11:11:18 +02:00
t.Errorf("exp nil, got %v", err)
}
}
if err := syncClient.Update(tc.updated); err != nil {
t.Errorf("exp nil, got %v", err)
}
// sync
2024-12-27 11:20:32 +01:00
cmd, err := command.NewSyncArgs().Parse([]string{"sync"}, nil)
if err != nil {
t.Errorf("exp nil, got %v", err)
}
2025-01-13 09:13:48 +01:00
if _, err := cmd.Do(mems, syncClient); err != nil {
2024-10-07 11:11:18 +02:00
t.Errorf("exp nil, got %v", err)
}
// check result
2025-01-13 09:13:48 +01:00
actTasks, err := mems.Task(nil).FindMany(storage.TaskListParams{})
2024-10-07 11:11:18 +02:00
if err != nil {
t.Errorf("exp nil, got %v", err)
}
2024-12-24 08:00:23 +01:00
if diff := item.TaskDiffs(tc.expTask, actTasks); diff != "" {
2024-10-07 11:11:18 +02:00
t.Errorf("(exp +, got -)\n%s", diff)
}
2025-01-13 09:13:48 +01:00
actLocalIDs, err := mems.LocalID(nil).FindAll()
2024-10-07 11:11:18 +02:00
if err != nil {
t.Errorf("exp nil, got %v", err)
}
if diff := cmp.Diff(tc.expLocalID, actLocalIDs); diff != "" {
t.Errorf("(exp +, got -)\n%s", diff)
}
})
}
}