This commit is contained in:
Erik Winter 2024-10-22 07:26:59 +02:00
parent 801947ab30
commit 293e7cc911
2 changed files with 41 additions and 12 deletions

View File

@ -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)

View File

@ -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) {
})
}
}