Skip to content

Commit

Permalink
feat: implement ability to update entries
Browse files Browse the repository at this point in the history
  • Loading branch information
tauraamui committed Jul 11, 2023
1 parent fb89ddc commit 4934b1b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions storage/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions storage/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 4934b1b

Please sign in to comment.