sync test
This commit is contained in:
parent
293e7cc911
commit
575ff61fe4
|
@ -45,7 +45,7 @@ func Sync(client client.Client, syncRepo storage.Sync, localIDRepo storage.Local
|
|||
// get new/updated items
|
||||
ts, err := syncRepo.LastUpdate()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not find timestamp of last update", err)
|
||||
return fmt.Errorf("could not find timestamp of last update: %v", err)
|
||||
}
|
||||
recItems, err := client.Updated([]item.Kind{item.KindEvent}, ts)
|
||||
if err != nil {
|
||||
|
|
|
@ -71,21 +71,117 @@ func TestSyncSend(t *testing.T) {
|
|||
func TestSyncReceive(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
syncClient := client.NewMemory()
|
||||
syncRepo := memory.NewSync()
|
||||
localIDRepo := memory.NewLocalID()
|
||||
eventRepo := memory.NewEvent()
|
||||
oneHour, err := time.ParseDuration("1h")
|
||||
if err != nil {
|
||||
t.Errorf("exp nil, got %v", err)
|
||||
}
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
present []item.Event
|
||||
updated []item.Item
|
||||
exp []item.Event
|
||||
expEvent []item.Event
|
||||
expLocalID map[string]int
|
||||
}{
|
||||
{},
|
||||
{
|
||||
name: "no new",
|
||||
expEvent: []item.Event{},
|
||||
expLocalID: map[string]int{},
|
||||
},
|
||||
{
|
||||
name: "new",
|
||||
updated: []item.Item{{
|
||||
ID: "a",
|
||||
Kind: item.KindEvent,
|
||||
Body: `{
|
||||
"title":"title",
|
||||
"start":"2024-10-23T08:00:00Z",
|
||||
"duration":"1h"
|
||||
}`,
|
||||
}},
|
||||
expEvent: []item.Event{{
|
||||
ID: "a",
|
||||
EventBody: item.EventBody{
|
||||
Title: "title",
|
||||
Start: time.Date(2024, 10, 23, 8, 0, 0, 0, time.UTC),
|
||||
Duration: oneHour,
|
||||
},
|
||||
}},
|
||||
expLocalID: map[string]int{
|
||||
"a": 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "update existing",
|
||||
present: []item.Event{{
|
||||
ID: "a",
|
||||
EventBody: item.EventBody{
|
||||
Title: "title",
|
||||
Start: time.Date(2024, 10, 23, 8, 0, 0, 0, time.UTC),
|
||||
Duration: oneHour,
|
||||
},
|
||||
}},
|
||||
updated: []item.Item{{
|
||||
ID: "a",
|
||||
Kind: item.KindEvent,
|
||||
Body: `{
|
||||
"title":"new title",
|
||||
"start":"2024-10-23T08:00:00Z",
|
||||
"duration":"1h"
|
||||
}`,
|
||||
}},
|
||||
expEvent: []item.Event{{
|
||||
ID: "a",
|
||||
EventBody: item.EventBody{
|
||||
Title: "new title",
|
||||
Start: time.Date(2024, 10, 23, 8, 0, 0, 0, time.UTC),
|
||||
Duration: oneHour,
|
||||
},
|
||||
}},
|
||||
expLocalID: map[string]int{
|
||||
"a": 1,
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
// setup
|
||||
syncClient := client.NewMemory()
|
||||
syncRepo := memory.NewSync()
|
||||
localIDRepo := memory.NewLocalID()
|
||||
eventRepo := memory.NewEvent()
|
||||
|
||||
for i, p := range tc.present {
|
||||
if err := eventRepo.Store(p); err != nil {
|
||||
t.Errorf("exp nil, got %v", err)
|
||||
}
|
||||
if err := localIDRepo.Store(p.ID, i+1); err != nil {
|
||||
t.Errorf("exp nil, got %v", err)
|
||||
}
|
||||
}
|
||||
if err := syncClient.Update(tc.updated); err != nil {
|
||||
t.Errorf("exp nil, got %v", err)
|
||||
}
|
||||
|
||||
// sync
|
||||
if err := command.Sync(syncClient, syncRepo, localIDRepo, eventRepo, false); err != nil {
|
||||
t.Errorf("exp nil, got %v", err)
|
||||
}
|
||||
|
||||
// check result
|
||||
actEvents, err := eventRepo.FindAll()
|
||||
if err != nil {
|
||||
t.Errorf("exp nil, got %v", err)
|
||||
}
|
||||
if diff := cmp.Diff(tc.expEvent, actEvents); diff != "" {
|
||||
t.Errorf("(exp +, got -)\n%s", diff)
|
||||
}
|
||||
actLocalIDs, err := localIDRepo.FindAll()
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue