Skip to content

Commit

Permalink
Migrated some of the unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Olshansk committed Jan 16, 2025
1 parent 9108902 commit 6b028dc
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 165 deletions.
4 changes: 2 additions & 2 deletions localnet/poktrolld/config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ timeout_broadcast_tx_commit = "10s"
max_request_batch_size = 10

# Maximum size of request body, in bytes
max_body_bytes = 1000000
max_body_bytes = 100000000

# Maximum size of request header, in bytes
max_header_bytes = 1048576
Expand Down Expand Up @@ -330,7 +330,7 @@ keep-invalid-txs-in-cache = false

# Maximum size of a single transaction.
# NOTE: the max size of a tx transmitted over the network is {max_tx_bytes}.
max_tx_bytes = 1048576
max_tx_bytes = 100000000

# Maximum size of a batch of transactions to send to a peer
# Including space needed by encoding (one varint per transaction).
Expand Down
176 changes: 137 additions & 39 deletions x/supplier/keeper/supplier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/gogo/status"
"github.com/stretchr/testify/require"
Expand All @@ -29,13 +30,7 @@ func init() {
cmd.InitSDKConfig()
}

// The module address is derived off of its semantic name.
// This test is a helper for us to easily identify the underlying address.
func TestModuleAddressSupplier(t *testing.T) {
moduleAddress := authtypes.NewModuleAddress(types.ModuleName)
require.Equal(t, "pokt1j40dzzmn6cn9kxku7a5tjnud6hv37vesr5ccaa", moduleAddress.String())
}

// createNSuppliers creates n suppliers and stores them in the keeper
func createNSuppliers(keeper keeper.Keeper, ctx context.Context, n int) []sharedtypes.Supplier {
suppliers := make([]sharedtypes.Supplier, n)
for i := range suppliers {
Expand All @@ -61,7 +56,51 @@ func createNSuppliers(keeper keeper.Keeper, ctx context.Context, n int) []shared
return suppliers
}

func TestSupplierQuery(t *testing.T) {
// DEV_NOTE: The account address is derived off of the module's semantic name (supplier).
// This test is a helper for us to easily identify the underlying address.
// See Module Accounts for more details: https://docs.cosmos.network/main/learn/beginner/accounts#module-accounts
func TestModuleAddressSupplier(t *testing.T) {
moduleAddress := authtypes.NewModuleAddress(types.ModuleName)
require.Equal(t, "pokt1j40dzzmn6cn9kxku7a5tjnud6hv37vesr5ccaa", moduleAddress.String())
}

func TestSupplier_Get(t *testing.T) {
supplierModuleKeepers, ctx := keepertest.SupplierKeeper(t)
suppliers := createNSuppliers(*supplierModuleKeepers.Keeper, ctx, 10)
for _, supplier := range suppliers {
supplierFound, isSupplierFound := supplierModuleKeepers.GetSupplier(ctx,
supplier.OperatorAddress,
)
require.True(t, isSupplierFound)
require.Equal(t,
nullify.Fill(&supplier),
nullify.Fill(&supplierFound),
)
}
}

func TestSupplier_Remove(t *testing.T) {
supplierModuleKeepers, ctx := keepertest.SupplierKeeper(t)
suppliers := createNSuppliers(*supplierModuleKeepers.Keeper, ctx, 10)
for _, supplier := range suppliers {
supplierModuleKeepers.RemoveSupplier(ctx, supplier.OperatorAddress)
_, isSupplierFound := supplierModuleKeepers.GetSupplier(ctx,
supplier.OperatorAddress,
)
require.False(t, isSupplierFound)
}
}

func TestSupplier_GetAll(t *testing.T) {
supplierModuleKeepers, ctx := keepertest.SupplierKeeper(t)
suppliers := createNSuppliers(*supplierModuleKeepers.Keeper, ctx, 10)
require.ElementsMatch(t,
nullify.Fill(suppliers),
nullify.Fill(supplierModuleKeepers.GetAllSuppliers(ctx)),
)
}

func TestSupplier_Query(t *testing.T) {
keeper, ctx := keepertest.SupplierKeeper(t)
suppliers := createNSuppliers(*keeper.Keeper, ctx, 2)

Expand Down Expand Up @@ -95,7 +134,7 @@ func TestSupplierQuery(t *testing.T) {
if test.expectedErr != nil {
stat, ok := status.FromError(test.expectedErr)
require.True(t, ok)
require.ErrorIs(t, stat.Err(), test.expectedErr)
require.ErrorContains(t, stat.Err(), test.expectedErr.Error())
} else {
require.NoError(t, err)
require.NotNil(t, response)
Expand All @@ -108,38 +147,97 @@ func TestSupplierQuery(t *testing.T) {
}
}

func TestSupplierGet(t *testing.T) {
supplierModuleKeepers, ctx := keepertest.SupplierKeeper(t)
suppliers := createNSuppliers(*supplierModuleKeepers.Keeper, ctx, 10)
for _, supplier := range suppliers {
supplierFound, isSupplierFound := supplierModuleKeepers.GetSupplier(ctx,
supplier.OperatorAddress,
)
require.True(t, isSupplierFound)
require.Equal(t,
nullify.Fill(&supplier),
nullify.Fill(&supplierFound),
)
}
}
func TestSuppliers_QueryAll_Pagination(t *testing.T) {
keeper, ctx := keepertest.SupplierKeeper(t)
suppliers := createNSuppliers(*keeper.Keeper, ctx, 5)

func TestSupplierRemove(t *testing.T) {
supplierModuleKeepers, ctx := keepertest.SupplierKeeper(t)
suppliers := createNSuppliers(*supplierModuleKeepers.Keeper, ctx, 10)
for _, supplier := range suppliers {
supplierModuleKeepers.RemoveSupplier(ctx, supplier.OperatorAddress)
_, isSupplierFound := supplierModuleKeepers.GetSupplier(ctx,
supplier.OperatorAddress,
t.Run("ByOffset", func(t *testing.T) {
step := 2
for i := 0; i < len(suppliers); i += step {
req := &types.QueryAllSuppliersRequest{
Pagination: &query.PageRequest{
Offset: uint64(i),
Limit: uint64(step),
},
}
resp, err := keeper.AllSuppliers(ctx, req)
require.NoError(t, err)
require.LessOrEqual(t, len(resp.Supplier), step)
require.Subset(t,
nullify.Fill(suppliers),
nullify.Fill(resp.Supplier),
)
}
})

t.Run("ByKey", func(t *testing.T) {
step := 2
var nextKey []byte
for i := 0; i < len(suppliers); i += step {
req := &types.QueryAllSuppliersRequest{
Pagination: &query.PageRequest{
Key: nextKey,
Limit: uint64(step),
},
}
resp, err := keeper.AllSuppliers(ctx, req)
require.NoError(t, err)
require.LessOrEqual(t, len(resp.Supplier), step)
require.Subset(t,
nullify.Fill(suppliers),
nullify.Fill(resp.Supplier),
)
nextKey = resp.Pagination.NextKey
}
})

t.Run("Total", func(t *testing.T) {
req := &types.QueryAllSuppliersRequest{
Pagination: &query.PageRequest{
Offset: 0,
Limit: uint64(len(suppliers)),
CountTotal: true,
},
}
resp, err := keeper.AllSuppliers(ctx, req)
require.NoError(t, err)
require.Equal(t, len(suppliers), int(resp.Pagination.Total))
require.ElementsMatch(t,
nullify.Fill(suppliers),
nullify.Fill(resp.Supplier),
)
require.False(t, isSupplierFound)
}
})
}

func TestSupplierGetAll(t *testing.T) {
supplierModuleKeepers, ctx := keepertest.SupplierKeeper(t)
suppliers := createNSuppliers(*supplierModuleKeepers.Keeper, ctx, 10)
require.ElementsMatch(t,
nullify.Fill(suppliers),
nullify.Fill(supplierModuleKeepers.GetAllSuppliers(ctx)),
)
func TestSuppliers_QueryAll_Filters(t *testing.T) {
keeper, ctx := keepertest.SupplierKeeper(t)
suppliers := createNSuppliers(*keeper.Keeper, ctx, 5)

t.Run("Filter By ServiceId", func(t *testing.T) {
// Assuming the first supplier has at least one service
serviceId := suppliers[0].Services[0].ServiceId
req := &types.QueryAllSuppliersRequest{
Pagination: &query.PageRequest{
Offset: 0,
Limit: uint64(len(suppliers)),
},
Filter: &types.QueryAllSuppliersRequest_ServiceId{
ServiceId: serviceId,
},
}
resp, err := keeper.AllSuppliers(ctx, req)
require.NoError(t, err)

// Verify each returned supplier has the specified service
for _, s := range resp.Supplier {
hasService := false
for _, service := range s.Services {
if service.ServiceId == serviceId {
hasService = true
break
}
}
require.True(t, hasService, "Returned supplier does not have the specified service")
}
})
}
124 changes: 0 additions & 124 deletions x/supplier/module/query_supplier_test.go

This file was deleted.

0 comments on commit 6b028dc

Please sign in to comment.