diff --git a/storage/store.go b/storage/store.go index 20be3da..9751ddd 100644 --- a/storage/store.go +++ b/storage/store.go @@ -27,6 +27,10 @@ func (s Store) Save(owner kvs.UUID, value Value) error { return saveValue(s.db, value.TableName(), owner, rowID, value) } +func (s Store) Update(owner kvs.UUID, value Value, rowID uint32) error { + return saveValue(s.db, value.TableName(), owner, rowID, value) +} + func saveValue(db kvs.KVDB, tableName string, ownerID kvs.UUID, rowID uint32, v Value) error { if v == nil { return nil diff --git a/storage/store_test.go b/storage/store_test.go index 47bd516..59fa920 100644 --- a/storage/store_test.go +++ b/storage/store_test.go @@ -24,6 +24,37 @@ type Cake struct { func (b Cake) TableName() string { return "cakes" } +func TestStoreMultipleAndUpdateSingleBalloonsSuccess(t *testing.T) { + is := is.New(t) + + db, err := kvs.NewMemKVDB() + is.NoErr(err) + defer db.Close() + + store := storage.New(db) + defer store.Close() + + bigRedBalloon := Balloon{Color: "RED", Size: 695} + smallYellowBalloon := Balloon{Color: "YELLOW", Size: 112} + mediumWhiteBalloon := Balloon{Color: "WHITE", Size: 366} + + is.NoErr(store.Save(kvs.RootOwner{}, &bigRedBalloon)) + is.NoErr(store.Save(kvs.RootOwner{}, &smallYellowBalloon)) + is.NoErr(store.Save(kvs.RootOwner{}, &mediumWhiteBalloon)) + + smallYellowBalloon.Color = "PINK" + is.NoErr(store.Update(kvs.RootOwner{}, &smallYellowBalloon, smallYellowBalloon.ID)) + + bs, err := storage.LoadAll(store, Balloon{}, kvs.RootOwner{}) + is.NoErr(err) + + is.True(len(bs) == 3) + + is.Equal(bs[0], Balloon{ID: 0, Color: "RED", Size: 695}) + is.Equal(bs[1], Balloon{ID: 1, Color: "PINK", Size: 112}) + is.Equal(bs[2], Balloon{ID: 2, Color: "WHITE", Size: 366}) +} + func TestStoreAndLoadMultipleBalloonsSuccess(t *testing.T) { is := is.New(t)