From 575ff61fe462ec4a35949b3a84b3c606e4e4ef2b Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Wed, 23 Oct 2024 07:36:17 +0200 Subject: [PATCH] sync test --- plan/command/sync.go | 2 +- plan/command/sync_test.go | 114 +++++++++++++++++++++++++++++++++++--- 2 files changed, 106 insertions(+), 10 deletions(-) diff --git a/plan/command/sync.go b/plan/command/sync.go index dea1043..40075e0 100644 --- a/plan/command/sync.go +++ b/plan/command/sync.go @@ -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 { diff --git a/plan/command/sync_test.go b/plan/command/sync_test.go index 6eb43a5..cd0935b 100644 --- a/plan/command/sync_test.go +++ b/plan/command/sync_test.go @@ -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 + name string + present []item.Event + updated []item.Item + 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) + } }) } }