-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(maintainers): add tests for super maintainers auxiliaries keeper
- Loading branch information
Showing
1 changed file
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
// Copyright [2021] - [2022], AssetMantle Pte. Ltd. and the code contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package super | ||
|
||
import ( | ||
"fmt" | ||
"github.com/AssetMantle/modules/modules/maintainers/internal/key" | ||
"github.com/AssetMantle/modules/modules/maintainers/internal/mappable" | ||
"github.com/AssetMantle/modules/modules/maintainers/internal/parameters" | ||
"github.com/AssetMantle/modules/schema" | ||
baseData "github.com/AssetMantle/modules/schema/data/base" | ||
errorConstants "github.com/AssetMantle/modules/schema/errors/constants" | ||
"github.com/AssetMantle/modules/schema/helpers" | ||
baseHelpers "github.com/AssetMantle/modules/schema/helpers/base" | ||
baseIDs "github.com/AssetMantle/modules/schema/ids/base" | ||
"github.com/AssetMantle/modules/schema/lists/base" | ||
baseProperties "github.com/AssetMantle/modules/schema/properties/base" | ||
baseQualified "github.com/AssetMantle/modules/schema/qualified/base" | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
"github.com/cosmos/cosmos-sdk/store" | ||
"github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/auth/vesting" | ||
"github.com/cosmos/cosmos-sdk/x/params" | ||
"github.com/stretchr/testify/require" | ||
abciTypes "github.com/tendermint/tendermint/abci/types" | ||
"github.com/tendermint/tendermint/libs/log" | ||
tendermintDB "github.com/tendermint/tm-db" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
type TestKeepers struct { | ||
SuperKeeper helpers.AuxiliaryKeeper | ||
} | ||
|
||
var ( | ||
immutables = baseQualified.NewImmutables(base.NewPropertyList(baseProperties.NewMesaProperty(baseIDs.NewStringID("ID2"), baseData.NewStringData("Data2")))) | ||
mutables = baseQualified.NewMutables(base.NewPropertyList(baseProperties.NewMesaProperty(baseIDs.NewStringID("ID1"), baseData.NewStringData("Data1")))) | ||
testClassificationID = baseIDs.NewClassificationID(immutables, mutables) | ||
testFromID = baseIDs.NewIdentityID(testClassificationID, immutables) | ||
) | ||
|
||
func createTestInput(t *testing.T) (types.Context, TestKeepers, helpers.Mapper, helpers.Parameters) { | ||
var Codec = codec.New() | ||
schema.RegisterCodec(Codec) | ||
types.RegisterCodec(Codec) | ||
codec.RegisterCrypto(Codec) | ||
codec.RegisterEvidences(Codec) | ||
vesting.RegisterCodec(Codec) | ||
Codec.Seal() | ||
|
||
storeKey := types.NewKVStoreKey("test") | ||
paramsStoreKey := types.NewKVStoreKey("testParams") | ||
paramsTransientStoreKeys := types.NewTransientStoreKey("testParamsTransient") | ||
Mapper := baseHelpers.NewMapper(key.Prototype, mappable.Prototype).Initialize(storeKey) | ||
paramsKeeper := params.NewKeeper( | ||
Codec, | ||
paramsStoreKey, | ||
paramsTransientStoreKeys, | ||
) | ||
Parameters := parameters.Prototype().Initialize(paramsKeeper.Subspace("test")) | ||
|
||
memDB := tendermintDB.NewMemDB() | ||
commitMultiStore := store.NewCommitMultiStore(memDB) | ||
commitMultiStore.MountStoreWithDB(storeKey, types.StoreTypeIAVL, memDB) | ||
commitMultiStore.MountStoreWithDB(paramsStoreKey, types.StoreTypeIAVL, memDB) | ||
commitMultiStore.MountStoreWithDB(paramsTransientStoreKeys, types.StoreTypeTransient, memDB) | ||
err := commitMultiStore.LoadLatestVersion() | ||
require.Nil(t, err) | ||
|
||
context := types.NewContext(commitMultiStore, abciTypes.Header{ | ||
ChainID: "test", | ||
}, false, log.NewNopLogger()) | ||
|
||
keepers := TestKeepers{ | ||
SuperKeeper: keeperPrototype().Initialize(Mapper, Parameters, []interface{}{}).(helpers.AuxiliaryKeeper), | ||
} | ||
|
||
return context, keepers, Mapper, Parameters | ||
} | ||
|
||
func Test_auxiliaryKeeper_Help(t *testing.T) { | ||
context, _, Mapper, _ := createTestInput(t) | ||
type fields struct { | ||
mapper helpers.Mapper | ||
} | ||
type args struct { | ||
context types.Context | ||
request helpers.AuxiliaryRequest | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want helpers.AuxiliaryResponse | ||
}{ | ||
{"+ve", fields{Mapper}, args{context, NewAuxiliaryRequest(testClassificationID, testFromID, mutables)}, newAuxiliaryResponse(nil)}, | ||
{"+ve Already exists", fields{Mapper}, args{context, NewAuxiliaryRequest(testClassificationID, testFromID, mutables)}, newAuxiliaryResponse(errorConstants.EntityAlreadyExists)}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
auxiliaryKeeper := auxiliaryKeeper{ | ||
mapper: tt.fields.mapper, | ||
} | ||
if got := auxiliaryKeeper.Help(tt.args.context, tt.args.request); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("Help() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_auxiliaryKeeper_Initialize(t *testing.T) { | ||
_, _, Mapper, Parameters := createTestInput(t) | ||
type fields struct { | ||
mapper helpers.Mapper | ||
} | ||
type args struct { | ||
mapper helpers.Mapper | ||
in1 helpers.Parameters | ||
in2 []interface{} | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want helpers.Keeper | ||
}{ | ||
{"+ve", fields{Mapper}, args{Mapper, Parameters, []interface{}{}}, auxiliaryKeeper{Mapper}}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
au := auxiliaryKeeper{ | ||
mapper: tt.fields.mapper, | ||
} | ||
if got := au.Initialize(tt.args.mapper, tt.args.in1, tt.args.in2); !reflect.DeepEqual(fmt.Sprint(got), fmt.Sprint(tt.want)) { | ||
t.Errorf("Initialize() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_keeperPrototype(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
want helpers.AuxiliaryKeeper | ||
}{ | ||
{"+ve", auxiliaryKeeper{}}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := keeperPrototype(); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("keeperPrototype() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |