wip
This commit is contained in:
parent
801947ab30
commit
293e7cc911
|
@ -3,7 +3,6 @@ package command
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
"go-mod.ewintr.nl/planner/item"
|
"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 {
|
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()
|
sendItems, err := syncRepo.FindAll()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not get updated items: %v", err)
|
return fmt.Errorf("could not get updated items: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// send new and updated
|
|
||||||
if err := client.Update(sendItems); err != nil {
|
if err := client.Update(sendItems); err != nil {
|
||||||
return fmt.Errorf("could not send updated items: %v", err)
|
return fmt.Errorf("could not send updated items: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := syncRepo.DeleteAll(); err != nil {
|
if err := syncRepo.DeleteAll(); err != nil {
|
||||||
return fmt.Errorf("could not clear updated items: %v", err)
|
return fmt.Errorf("could not clear updated items: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// get last updated time
|
|
||||||
|
|
||||||
// get new/updated items
|
// 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 {
|
if err != nil {
|
||||||
return fmt.Errorf("could not receive updates: %v", err)
|
return fmt.Errorf("could not receive updates: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// import to local
|
||||||
lidMap, err := localIDRepo.FindAll()
|
lidMap, err := localIDRepo.FindAll()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not get local ids: %v", err)
|
return fmt.Errorf("could not get local ids: %v", err)
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
"go-mod.ewintr.nl/planner/sync/client"
|
"go-mod.ewintr.nl/planner/sync/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSync(t *testing.T) {
|
func TestSyncSend(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
syncClient := client.NewMemory()
|
syncClient := client.NewMemory()
|
||||||
|
@ -19,8 +19,6 @@ func TestSync(t *testing.T) {
|
||||||
localIDRepo := memory.NewLocalID()
|
localIDRepo := memory.NewLocalID()
|
||||||
eventRepo := memory.NewEvent()
|
eventRepo := memory.NewEvent()
|
||||||
|
|
||||||
// now := time.Now()
|
|
||||||
|
|
||||||
it := item.Item{
|
it := item.Item{
|
||||||
ID: "a",
|
ID: "a",
|
||||||
Kind: item.KindEvent,
|
Kind: item.KindEvent,
|
||||||
|
@ -30,7 +28,9 @@ func TestSync(t *testing.T) {
|
||||||
"duration":"1h"
|
"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 {
|
for _, tc := range []struct {
|
||||||
name string
|
name string
|
||||||
|
@ -56,6 +56,36 @@ func TestSync(t *testing.T) {
|
||||||
if diff := cmp.Diff(tc.expItems, actItems); diff != "" {
|
if diff := cmp.Diff(tc.expItems, actItems); diff != "" {
|
||||||
t.Errorf("(exp +, got -)\n%s", 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) {
|
||||||
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue