Skip to content

Commit

Permalink
[1834]: Add unit test on send restriction when we can't get attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
SpicyLemon committed Feb 10, 2024
1 parent 04f012f commit 2e3f0db
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 10 deletions.
6 changes: 6 additions & 0 deletions x/marker/keeper/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@ func (k Keeper) WithAuthzKeeper(authzKeeper types.AuthzKeeper) Keeper {
k.authzKeeper = authzKeeper
return k
}

// WithAttrKeeper is a TEST ONLY func that returns a copy of this marker keeper but with the provided attr keeper instead.
func (k Keeper) WithAttrKeeper(attrKeeper types.AttrKeeper) Keeper {
k.attrKeeper = attrKeeper
return k
}
46 changes: 44 additions & 2 deletions x/marker/keeper/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/authz"

attrtypes "github.com/provenance-io/provenance/x/attribute/types"
"github.com/provenance-io/provenance/x/marker/types"
)

// wrappedBankKeeper wraps a BankKeeper such that some functions can be set up to return specific things instead
// WrappedBankKeeper wraps a BankKeeper such that some functions can be set up to return specific things instead.
type WrappedBankKeeper struct {
types.BankKeeper
SendCoinsErrs []string
Expand All @@ -33,7 +34,7 @@ func (w *WrappedBankKeeper) WithParent(bankKeeper types.BankKeeper) *WrappedBank
}

// WithSendCoinsErrs adds the provided error strings to the list of errors that will be returned by SendCoins.
// An non-empty entry will be returned as an error (when its time comes).
// A non-empty entry will be returned as an error (when its time comes).
// An empty entry (or if there aren't any entries left when SendCoins is called) will
// result in the parent's SendCoins function being called and returned.
func (w *WrappedBankKeeper) WithSendCoinsErrs(errs ...string) *WrappedBankKeeper {
Expand Down Expand Up @@ -246,3 +247,44 @@ func (a *mockAuthorization) String() string {

// ProtoMessage does nothing. Satisfies the authz.Authorization interface.
func (a *mockAuthorization) ProtoMessage() {}

// WrappedAttrKeeper wraps an AttrKeeper such that some functions can be set up to return specific things instead.
type WrappedAttrKeeper struct {
types.AttrKeeper
GetAllAttributesAddrErrs []string
}

var _ types.AttrKeeper = (*WrappedAttrKeeper)(nil)

// NewWrappedBankKeeper creates a new WrappedBankKeeper.
// You'll need to call WithParent on the result before anything will work in here.
func NewWrappedAttrKeeper() *WrappedAttrKeeper {
return &WrappedAttrKeeper{}
}

// WithParent sets the parent bank keeper for this wrapping.
func (w *WrappedAttrKeeper) WithParent(attrKeeper types.AttrKeeper) *WrappedAttrKeeper {
w.AttrKeeper = attrKeeper
return w
}

// WithGetAllAttributesAddrErrs adds the provided error strings to the list of errors that will be
// returned by GetAllAttributesAddr. A non-empty entry will be returned as an error (when its time comes).
// An empty entry (or if there aren't any entries left when SendCoins is called) will
// result in the parent's SendCoins function being called and returned.
func (w *WrappedAttrKeeper) WithGetAllAttributesAddrErrs(errs ...string) *WrappedAttrKeeper {
w.GetAllAttributesAddrErrs = append(w.GetAllAttributesAddrErrs, errs...)
return w
}

// GetAllAttributesAddr either returns a pre-defined error, or, if there isn't one, calls GetAllAttributesAddr on the parent.
func (w *WrappedAttrKeeper) GetAllAttributesAddr(ctx sdk.Context, addr []byte) ([]attrtypes.Attribute, error) {
if len(w.GetAllAttributesAddrErrs) > 0 {
rv := w.GetAllAttributesAddrErrs[0]
w.GetAllAttributesAddrErrs = w.GetAllAttributesAddrErrs[1:]
if len(rv) > 0 {
return nil, errors.New(rv)
}
}
return w.AttrKeeper.GetAllAttributesAddr(ctx, addr)
}
28 changes: 20 additions & 8 deletions x/marker/keeper/send_restrictions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,13 @@ func TestSendRestrictionFn(t *testing.T) {
}

testCases := []struct {
name string
ctx *sdk.Context
from sdk.AccAddress
to sdk.AccAddress
amt sdk.Coins
expErr string
name string
attrKeeper *WrappedAttrKeeper
ctx *sdk.Context
from sdk.AccAddress
to sdk.AccAddress
amt sdk.Coins
expErr string
}{
{
name: "unknown denom",
Expand Down Expand Up @@ -202,7 +203,14 @@ func TestSendRestrictionFn(t *testing.T) {
amt: cz(c(1, rDenom3Attrs)),
expErr: "",
},
// Untested: GetAllAttributesAddr returns an error. Only happens when store data can't be unmarshalled. Can't do that from here.
{
name: "error getting attrs",
attrKeeper: NewWrappedAttrKeeper().WithGetAllAttributesAddrErrs("crazy injected attr error"),
from: addrOther,
to: addrWithAttrs,
amt: cz(c(1, rDenom3Attrs)),
expErr: "could not get attributes for " + addrWithAttrsStr + ": crazy injected attr error",
},
{
name: "restricted marker with empty required attributes and no transfer rights",
from: owner,
Expand Down Expand Up @@ -553,7 +561,11 @@ func TestSendRestrictionFn(t *testing.T) {
if tc.ctx != nil {
tCtx = *tc.ctx
}
newTo, err := app.MarkerKeeper.SendRestrictionFn(tCtx, tc.from, tc.to, tc.amt)
kpr := app.MarkerKeeper
if tc.attrKeeper != nil {
kpr = kpr.WithAttrKeeper(tc.attrKeeper.WithParent(app.AttributeKeeper))
}
newTo, err := kpr.SendRestrictionFn(tCtx, tc.from, tc.to, tc.amt)
if len(tc.expErr) > 0 {
assert.EqualError(t, err, tc.expErr, "SendRestrictionFn error")
} else {
Expand Down

0 comments on commit 2e3f0db

Please sign in to comment.