From 293e7cc911226b064e82e78ad132d3611c8194dd Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Tue, 22 Oct 2024 07:26:59 +0200 Subject: [PATCH] wip --- plan/command/sync.go | 15 +++++++-------- plan/command/sync_test.go | 38 ++++++++++++++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/plan/command/sync.go b/plan/command/sync.go index f77a63c..dea1043 100644 --- a/plan/command/sync.go +++ b/plan/command/sync.go @@ -3,7 +3,6 @@ package command import ( "encoding/json" "fmt" - "time" "github.com/urfave/cli/v2" "go-mod.ewintr.nl/planner/item" @@ -31,29 +30,29 @@ func NewSyncCmd(client client.Client, syncRepo storage.Sync, localIDRepo storage } func Sync(client client.Client, syncRepo storage.Sync, localIDRepo storage.LocalID, eventRepo storage.Event, full bool) error { - // find local new and updated + // local new and updated sendItems, err := syncRepo.FindAll() if err != nil { return fmt.Errorf("could not get updated items: %v", err) } - - // send new and updated if err := client.Update(sendItems); err != nil { return fmt.Errorf("could not send updated items: %v", err) } - if err := syncRepo.DeleteAll(); err != nil { return fmt.Errorf("could not clear updated items: %v", err) } - // get last updated time - // get new/updated items - recItems, err := client.Updated([]item.Kind{item.KindEvent}, time.Time{}) + ts, err := syncRepo.LastUpdate() + if err != nil { + return fmt.Errorf("could not find timestamp of last update", err) + } + recItems, err := client.Updated([]item.Kind{item.KindEvent}, ts) if err != nil { return fmt.Errorf("could not receive updates: %v", err) } + // import to local lidMap, err := localIDRepo.FindAll() if err != nil { return fmt.Errorf("could not get local ids: %v", err) diff --git a/plan/command/sync_test.go b/plan/command/sync_test.go index 9f7de01..6eb43a5 100644 --- a/plan/command/sync_test.go +++ b/plan/command/sync_test.go @@ -11,7 +11,7 @@ import ( "go-mod.ewintr.nl/planner/sync/client" ) -func TestSync(t *testing.T) { +func TestSyncSend(t *testing.T) { t.Parallel() syncClient := client.NewMemory() @@ -19,8 +19,6 @@ func TestSync(t *testing.T) { localIDRepo := memory.NewLocalID() eventRepo := memory.NewEvent() - // now := time.Now() - it := item.Item{ ID: "a", Kind: item.KindEvent, @@ -30,7 +28,9 @@ func TestSync(t *testing.T) { "duration":"1h" }`, } - syncClient.Update([]item.Item{it}) + if err := syncRepo.Store(it); err != nil { + t.Errorf("exp nil, got %v", err) + } for _, tc := range []struct { name string @@ -56,6 +56,36 @@ func TestSync(t *testing.T) { if diff := cmp.Diff(tc.expItems, actItems); diff != "" { t.Errorf("(exp +, got -)\n%s", diff) } + + actLeft, actErr := syncRepo.FindAll() + 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() + + syncClient := client.NewMemory() + syncRepo := memory.NewSync() + localIDRepo := memory.NewLocalID() + eventRepo := memory.NewEvent() + + for _, tc := range []struct { + name string + present []item.Event + updated []item.Item + exp []item.Event + }{ + {}, + } { + t.Run(tc.name, func(t *testing.T) { + }) } }