planetmint-go/x/asset/keeper/asset_test.go
Lorenz Herzberger 468fbb5305
402 bug asset query does not repsect lookupperiodinmin value (#405)
* chore: remove unused asset.proto and asset.pb.go files
* feat: add AssetByAddress store functionality
* fix: asset query now returns numElements passed in req
* chore: add migration for new store mechanics
* chore: set upgradehandler for assetmodule migration
* chore: removed obsolete GetCIDsByAddress function
* chore: adjust cmd usage
---------

Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
2024-06-06 14:39:05 +02:00

83 lines
2.4 KiB
Go

package keeper_test
import (
"crypto/sha256"
"strconv"
"testing"
keepertest "github.com/planetmint/planetmint-go/testutil/keeper"
"github.com/planetmint/planetmint-go/util"
"github.com/planetmint/planetmint-go/x/asset/keeper"
"github.com/planetmint/planetmint-go/x/asset/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/assert"
)
func createNAsset(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.MsgNotarizeAsset {
items := make([]types.MsgNotarizeAsset, n)
for i := range items {
hash := sha256.Sum256([]byte(strconv.FormatInt(int64(i), 2)))
hashStr := string(hash[:])
items[i].Cid = hashStr
items[i].Creator = "plmnt_address"
if i%2 == 0 {
items[i].Creator = "plmnt_address1"
}
keeper.StoreAsset(ctx, items[i])
}
return items
}
func TestGetAssetbyCid(t *testing.T) {
t.Parallel()
keeper, ctx := keepertest.AssetKeeper(t)
items := createNAsset(keeper, ctx, 10)
for _, item := range items {
asset, found := keeper.GetAsset(ctx, item.Cid)
assert.True(t, found)
assert.Equal(t, item, asset)
}
}
func TestAssetCount(t *testing.T) {
t.Parallel()
keeper, ctx := keepertest.AssetKeeper(t)
numItems := 10
items := createNAsset(keeper, ctx, numItems)
count, found := keeper.GetAddressAssetCount(ctx, items[0].Creator)
assert.True(t, found)
assert.Equal(t, uint64(5), count)
count, found = keeper.GetAddressAssetCount(ctx, items[1].Creator)
assert.True(t, found)
assert.Equal(t, uint64(5), count)
}
func TestGetAssetByAddressAndID(t *testing.T) {
t.Parallel()
keeper, ctx := keepertest.AssetKeeper(t)
items := createNAsset(keeper, ctx, 1)
cid, found := keeper.GetAssetByAddressAndID(ctx, items[0].Creator, 1)
assert.True(t, found)
assert.Equal(t, items[0].Cid, cid)
}
func TestGetAssetsByAddress(t *testing.T) {
t.Parallel()
keeper, ctx := keepertest.AssetKeeper(t)
items := createNAsset(keeper, ctx, 10)
cids, found := keeper.GetAssetsByAddress(ctx, items[0].Creator, nil, nil)
assert.True(t, found)
assert.Equal(t, items[8].Cid, cids[0])
assert.Equal(t, items[4].Cid, cids[2])
cids, found = keeper.GetAssetsByAddress(ctx, items[1].Creator, nil, nil)
assert.True(t, found)
assert.Equal(t, items[9].Cid, cids[0])
assert.Equal(t, items[5].Cid, cids[2])
cids, found = keeper.GetAssetsByAddress(ctx, items[0].Creator, util.SerializeUint64(3), nil)
assert.True(t, found)
assert.Equal(t, 3, len(cids))
assert.Equal(t, items[8].Cid, cids[0])
}