From 801947ab30f9c53d87b19b425e269139a65406a9 Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Fri, 18 Oct 2024 07:26:11 +0200 Subject: [PATCH] wip --- plan/command/sync.go | 2 +- plan/command/sync_test.go | 40 +++++++++++++++++++++++++--------- plan/main.go | 4 ++-- plan/storage/sqlite/localid.go | 4 ++++ plan/storage/sqlite/sqlite.go | 9 ++++---- plan/storage/sqlite/sync.go | 29 ++++++++++++++++++++++++ 6 files changed, 71 insertions(+), 17 deletions(-) create mode 100644 plan/storage/sqlite/sync.go diff --git a/plan/command/sync.go b/plan/command/sync.go index 1919f1f..f77a63c 100644 --- a/plan/command/sync.go +++ b/plan/command/sync.go @@ -60,7 +60,7 @@ func Sync(client client.Client, syncRepo storage.Sync, localIDRepo storage.Local } for _, ri := range recItems { var eBody item.EventBody - if err := json.Unmarshal([]byte(ri.Body), eBody); err != nil { + if err := json.Unmarshal([]byte(ri.Body), &eBody); err != nil { return fmt.Errorf("could not unmarshal event body: %v", err) } e := item.Event{ diff --git a/plan/command/sync_test.go b/plan/command/sync_test.go index 60d0617..9f7de01 100644 --- a/plan/command/sync_test.go +++ b/plan/command/sync_test.go @@ -24,18 +24,38 @@ func TestSync(t *testing.T) { it := item.Item{ ID: "a", Kind: item.KindEvent, - Body: `{}`, + Body: `{ + "title":"title", + "start":"2024-10-18T08:00:00Z", + "duration":"1h" +}`, } syncClient.Update([]item.Item{it}) - if err := command.Sync(syncClient, syncRepo, localIDRepo, eventRepo, false); err != nil { - t.Errorf("exp nil, got %v", err) - } - actItems, actErr := syncClient.Updated([]item.Kind{item.KindEvent}, time.Time{}) - if actErr != nil { - t.Errorf("exp nil, got %v", actErr) - } - if diff := cmp.Diff([]item.Item{it}, actItems); diff != "" { - t.Errorf("(exp +, got -)\n%s", diff) + for _, tc := range []struct { + name string + ks []item.Kind + ts time.Time + expItems []item.Item + }{ + { + name: "single", + ks: []item.Kind{item.KindEvent}, + expItems: []item.Item{it}, + }, + } { + t.Run(tc.name, func(t *testing.T) { + + if err := command.Sync(syncClient, syncRepo, localIDRepo, eventRepo, false); err != nil { + t.Errorf("exp nil, got %v", err) + } + actItems, actErr := syncClient.Updated(tc.ks, tc.ts) + if actErr != nil { + t.Errorf("exp nil, got %v", actErr) + } + if diff := cmp.Diff(tc.expItems, actItems); diff != "" { + t.Errorf("(exp +, got -)\n%s", diff) + } + }) } } diff --git a/plan/main.go b/plan/main.go index a1933c1..78d52c3 100644 --- a/plan/main.go +++ b/plan/main.go @@ -24,7 +24,7 @@ func main() { os.Exit(1) } - localIDRepo, eventRepo, err := sqlite.NewSqlites(conf.DBPath) + localIDRepo, eventRepo, syncRepo, err := sqlite.NewSqlites(conf.DBPath) if err != nil { fmt.Printf("could not open db file: %s\n", err) os.Exit(1) @@ -40,7 +40,7 @@ func main() { command.NewListCmd(localIDRepo, eventRepo), command.NewUpdateCmd(localIDRepo, eventRepo), command.NewDeleteCmd(localIDRepo, eventRepo), - command.NewSyncCmd(syncClient, localIDRepo, eventRepo), + command.NewSyncCmd(syncClient, syncRepo, localIDRepo, eventRepo), }, } diff --git a/plan/storage/sqlite/localid.go b/plan/storage/sqlite/localid.go index 1e213c3..f97f1d0 100644 --- a/plan/storage/sqlite/localid.go +++ b/plan/storage/sqlite/localid.go @@ -33,6 +33,10 @@ FROM localids return result, nil } +func (l *LocalID) FindOrNext(id string) (int, error) { + return 0, nil +} + func (l *LocalID) Next() (int, error) { idMap, err := l.FindAll() if err != nil { diff --git a/plan/storage/sqlite/sqlite.go b/plan/storage/sqlite/sqlite.go index 90f84f3..4b8e951 100644 --- a/plan/storage/sqlite/sqlite.go +++ b/plan/storage/sqlite/sqlite.go @@ -27,10 +27,10 @@ var ( ErrSqliteFailure = errors.New("sqlite returned an error") ) -func NewSqlites(dbPath string) (*LocalID, *SqliteEvent, error) { +func NewSqlites(dbPath string) (*LocalID, *SqliteEvent, *SqliteSync, error) { db, err := sql.Open("sqlite", dbPath) if err != nil { - return nil, nil, fmt.Errorf("%w: %v", ErrInvalidConfiguration, err) + return nil, nil, nil, fmt.Errorf("%w: %v", ErrInvalidConfiguration, err) } sl := &LocalID{ @@ -39,12 +39,13 @@ func NewSqlites(dbPath string) (*LocalID, *SqliteEvent, error) { se := &SqliteEvent{ db: db, } + ss := &SqliteSync{} if err := migrate(db, migrations); err != nil { - return nil, nil, err + return nil, nil, nil, err } - return sl, se, nil + return sl, se, ss, nil } func migrate(db *sql.DB, wanted []string) error { diff --git a/plan/storage/sqlite/sync.go b/plan/storage/sqlite/sync.go new file mode 100644 index 0000000..d0ef0e5 --- /dev/null +++ b/plan/storage/sqlite/sync.go @@ -0,0 +1,29 @@ +package sqlite + +import ( + "time" + + "go-mod.ewintr.nl/planner/item" +) + +type SqliteSync struct{} + +func NewSqliteSync() *SqliteSync { + return &SqliteSync{} +} + +func (s *SqliteSync) FindAll() ([]item.Item, error) { + return nil, nil +} + +func (s *SqliteSync) Store(i item.Item) error { + return nil +} + +func (s *SqliteSync) DeleteAll() error { + return nil +} + +func (s *SqliteSync) LastUpdate() (time.Time, error) { + return time.Time{}, nil +}