Skip to content

Commit

Permalink
fix: removing item tagged by collection
Browse files Browse the repository at this point in the history
- fixing issue with `wallet.Remove()` where mapping data with collection
wasn't deleted.
- Part of hyperledger-archives#2433

Signed-off-by: sudesh.shetty <sudesh.shetty@securekey.com>
  • Loading branch information
sudeshrshetty committed Dec 9, 2021
1 parent db62fc7 commit b27feff
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
7 changes: 7 additions & 0 deletions pkg/wallet/contents.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,13 @@ func (cs *contentStore) Remove(auth, key string, ct ContentType) error {
return err
}

// delete mapping
err = store.Delete(getCollectionMappingKeyPrefix(key))
if err != nil {
return err
}

// delete from store
return store.Delete(getContentKeyPrefix(ct, key))
}

Expand Down
31 changes: 27 additions & 4 deletions pkg/wallet/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -963,19 +963,28 @@ func TestWallet_GetAll(t *testing.T) {

const count = 5

var taggedKeys, untaggedKeys [count]string

// save test data without collection
for i := 0; i < count; i++ {
require.NoError(t, walletInstance.Add(tkn,
Credential, []byte(fmt.Sprintf(vcContent, uuid.New().String()))))
k := uuid.New().String()

require.NoError(t, walletInstance.Add(tkn, Credential, []byte(fmt.Sprintf(vcContent, k))))

untaggedKeys[i] = k
}

// save a collection
require.NoError(t, walletInstance.Add(tkn, Collection, []byte(orgCollection)))

// save contents by collection
for i := 0; i < count; i++ {
require.NoError(t, walletInstance.Add(tkn,
Credential, []byte(fmt.Sprintf(vcContent, uuid.New().String())), AddByCollection(collectionID)))
k := uuid.New().String()

require.NoError(t, walletInstance.Add(tkn, Credential, []byte(fmt.Sprintf(vcContent, k)),
AddByCollection(collectionID)))

taggedKeys[i] = k
}

// get all by content
Expand All @@ -987,6 +996,20 @@ func TestWallet_GetAll(t *testing.T) {
vcs, err = walletInstance.GetAll(tkn, Credential, FilterByCollection(collectionID))
require.NoError(t, err)
require.Len(t, vcs, count)

// delete one item under collection
require.NoError(t, walletInstance.Remove(tkn, Credential, taggedKeys[0]))
// get all by content & collection
vcs, err = walletInstance.GetAll(tkn, Credential, FilterByCollection(collectionID))
require.NoError(t, err)
require.Len(t, vcs, count-1)

// delete one item which is not under collection
require.NoError(t, walletInstance.Remove(tkn, Credential, untaggedKeys[0]))
// get all by content
vcs, err = walletInstance.GetAll(tkn, Credential)
require.NoError(t, err)
require.Len(t, vcs, count*2-2)
}

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

0 comments on commit b27feff

Please sign in to comment.