Skip to content

Commit

Permalink
feat: implement deletion method
Browse files Browse the repository at this point in the history
  • Loading branch information
tauraamui committed Jul 11, 2023
1 parent 4934b1b commit f22bf91
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
13 changes: 13 additions & 0 deletions storage/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ func saveValue(db kvs.KVDB, tableName string, ownerID kvs.UUID, rowID uint32, v
return kvs.LoadID(v, rowID)
}

func (s Store) Delete(owner kvs.UUID, value Value, rowID uint32) error {
db := s.db

blankEntries := kvs.ConvertToBlankEntries(value.TableName(), owner, rowID, value)
for _, ent := range blankEntries {
db.Update(func(txn *badger.Txn) error {
return txn.Delete(ent.Key())
})
}

return nil
}

func Load[T Value](s Store, dest T, owner kvs.UUID, rowID uint32) error {
db := s.db

Expand Down
38 changes: 34 additions & 4 deletions storage/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,33 @@ type Cake struct {

func (b Cake) TableName() string { return "cakes" }

func TestStoreAndLoadMultipleBalloonsSuccess(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))

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: "YELLOW", Size: 112})
is.Equal(bs[2], Balloon{ID: 2, Color: "WHITE", Size: 366})
}

func TestStoreMultipleAndUpdateSingleBalloonsSuccess(t *testing.T) {
is := is.New(t)

Expand Down Expand Up @@ -55,7 +82,7 @@ func TestStoreMultipleAndUpdateSingleBalloonsSuccess(t *testing.T) {
is.Equal(bs[2], Balloon{ID: 2, Color: "WHITE", Size: 366})
}

func TestStoreAndLoadMultipleBalloonsSuccess(t *testing.T) {
func TestStoreAndDeleteSingleBalloonSuccess(t *testing.T) {
is := is.New(t)

db, err := kvs.NewMemKVDB()
Expand All @@ -77,9 +104,12 @@ func TestStoreAndLoadMultipleBalloonsSuccess(t *testing.T) {

is.True(len(bs) == 3)

is.Equal(bs[0], Balloon{ID: 0, Color: "RED", Size: 695})
is.Equal(bs[1], Balloon{ID: 1, Color: "YELLOW", Size: 112})
is.Equal(bs[2], Balloon{ID: 2, Color: "WHITE", Size: 366})
is.NoErr(store.Delete(kvs.RootOwner{}, &smallYellowBalloon, smallYellowBalloon.ID))

bs, err = storage.LoadAll(store, Balloon{}, kvs.RootOwner{})
is.NoErr(err)

is.True(len(bs) == 2)
}

func TestStoreLoadMultipleLoadIndividualBalloonsSuccess(t *testing.T) {
Expand Down

0 comments on commit f22bf91

Please sign in to comment.