|
| 1 | +// Copyright [2021] - [2022], AssetMantle Pte. Ltd. and the code contributors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package deputize |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "github.com/AssetMantle/modules/modules/classifications/auxiliaries/member" |
| 9 | + "github.com/AssetMantle/modules/modules/maintainers/internal/key" |
| 10 | + "github.com/AssetMantle/modules/modules/maintainers/internal/mappable" |
| 11 | + "github.com/AssetMantle/modules/modules/maintainers/internal/parameters" |
| 12 | + maintainerUtilities "github.com/AssetMantle/modules/modules/maintainers/internal/utilities" |
| 13 | + "github.com/AssetMantle/modules/schema" |
| 14 | + baseData "github.com/AssetMantle/modules/schema/data/base" |
| 15 | + baseDocuments "github.com/AssetMantle/modules/schema/documents/base" |
| 16 | + "github.com/AssetMantle/modules/schema/helpers" |
| 17 | + baseHelpers "github.com/AssetMantle/modules/schema/helpers/base" |
| 18 | + baseIDs "github.com/AssetMantle/modules/schema/ids/base" |
| 19 | + "github.com/AssetMantle/modules/schema/lists/base" |
| 20 | + "github.com/AssetMantle/modules/schema/lists/utilities" |
| 21 | + baseProperties "github.com/AssetMantle/modules/schema/properties/base" |
| 22 | + baseQualified "github.com/AssetMantle/modules/schema/qualified/base" |
| 23 | + "github.com/cosmos/cosmos-sdk/codec" |
| 24 | + "github.com/cosmos/cosmos-sdk/store" |
| 25 | + "github.com/cosmos/cosmos-sdk/types" |
| 26 | + "github.com/cosmos/cosmos-sdk/x/auth/vesting" |
| 27 | + "github.com/cosmos/cosmos-sdk/x/params" |
| 28 | + "github.com/stretchr/testify/require" |
| 29 | + abciTypes "github.com/tendermint/tendermint/abci/types" |
| 30 | + "github.com/tendermint/tendermint/libs/log" |
| 31 | + tendermintDB "github.com/tendermint/tm-db" |
| 32 | + "reflect" |
| 33 | + "testing" |
| 34 | +) |
| 35 | + |
| 36 | +type TestKeepers struct { |
| 37 | + DeputizeKeeper helpers.AuxiliaryKeeper |
| 38 | +} |
| 39 | + |
| 40 | +var ( |
| 41 | + memberAuxiliary helpers.Auxiliary |
| 42 | + immutables = baseQualified.NewImmutables(base.NewPropertyList(baseProperties.NewMesaProperty(baseIDs.NewStringID("ID2"), baseData.NewStringData("Data2")))) |
| 43 | + mutables = baseQualified.NewMutables(base.NewPropertyList(baseProperties.NewMesaProperty(baseIDs.NewStringID("ID1"), baseData.NewStringData("Data1")))) |
| 44 | + testClassificationID = baseIDs.NewClassificationID(immutables, mutables) |
| 45 | + testFromID = baseIDs.NewIdentityID(testClassificationID, immutables) |
| 46 | + maintainedProperty = "maintainedProperty:S|maintainedProperty" |
| 47 | + maintainedProperties, _ = utilities.ReadMetaPropertyList(maintainedProperty) |
| 48 | + permissions = maintainerUtilities.SetPermissions(true, true, true, true, true, true) |
| 49 | +) |
| 50 | + |
| 51 | +func createTestInput(t *testing.T) (types.Context, TestKeepers, helpers.Mapper, helpers.Parameters) { |
| 52 | + var Codec = codec.New() |
| 53 | + schema.RegisterCodec(Codec) |
| 54 | + types.RegisterCodec(Codec) |
| 55 | + codec.RegisterCrypto(Codec) |
| 56 | + codec.RegisterEvidences(Codec) |
| 57 | + vesting.RegisterCodec(Codec) |
| 58 | + Codec.Seal() |
| 59 | + |
| 60 | + storeKey := types.NewKVStoreKey("test") |
| 61 | + paramsStoreKey := types.NewKVStoreKey("testParams") |
| 62 | + paramsTransientStoreKeys := types.NewTransientStoreKey("testParamsTransient") |
| 63 | + Mapper := baseHelpers.NewMapper(key.Prototype, mappable.Prototype).Initialize(storeKey) |
| 64 | + paramsKeeper := params.NewKeeper( |
| 65 | + Codec, |
| 66 | + paramsStoreKey, |
| 67 | + paramsTransientStoreKeys, |
| 68 | + ) |
| 69 | + Parameters := parameters.Prototype().Initialize(paramsKeeper.Subspace("test")) |
| 70 | + |
| 71 | + memDB := tendermintDB.NewMemDB() |
| 72 | + commitMultiStore := store.NewCommitMultiStore(memDB) |
| 73 | + commitMultiStore.MountStoreWithDB(storeKey, types.StoreTypeIAVL, memDB) |
| 74 | + commitMultiStore.MountStoreWithDB(paramsStoreKey, types.StoreTypeIAVL, memDB) |
| 75 | + commitMultiStore.MountStoreWithDB(paramsTransientStoreKeys, types.StoreTypeTransient, memDB) |
| 76 | + err := commitMultiStore.LoadLatestVersion() |
| 77 | + require.Nil(t, err) |
| 78 | + |
| 79 | + context := types.NewContext(commitMultiStore, abciTypes.Header{ |
| 80 | + ChainID: "test", |
| 81 | + }, false, log.NewNopLogger()) |
| 82 | + |
| 83 | + memberAuxiliary = member.AuxiliaryMock.Initialize(Mapper, Parameters) |
| 84 | + keepers := TestKeepers{ |
| 85 | + DeputizeKeeper: keeperPrototype().Initialize(Mapper, Parameters, []interface{}{}).(helpers.AuxiliaryKeeper), |
| 86 | + } |
| 87 | + |
| 88 | + return context, keepers, Mapper, Parameters |
| 89 | +} |
| 90 | + |
| 91 | +func Test_auxiliaryKeeper_Help(t *testing.T) { |
| 92 | + context, keepers, Mapper, _ := createTestInput(t) |
| 93 | + keepers.DeputizeKeeper.(auxiliaryKeeper).mapper.NewCollection(context).Add(mappable.NewMappable(baseDocuments.NewMaintainer(testFromID, testClassificationID, maintainedProperties.GetPropertyIDList(), permissions))) |
| 94 | + type fields struct { |
| 95 | + mapper helpers.Mapper |
| 96 | + memberAuxiliary helpers.Auxiliary |
| 97 | + } |
| 98 | + type args struct { |
| 99 | + context types.Context |
| 100 | + request helpers.AuxiliaryRequest |
| 101 | + } |
| 102 | + tests := []struct { |
| 103 | + name string |
| 104 | + fields fields |
| 105 | + args args |
| 106 | + want helpers.AuxiliaryResponse |
| 107 | + }{ |
| 108 | + {"+ve", fields{Mapper, memberAuxiliary}, args{context, NewAuxiliaryRequest(testFromID, testFromID, testClassificationID, maintainedProperties, true, true, true, true, true, true)}, newAuxiliaryResponse(nil)}, |
| 109 | + } |
| 110 | + for _, tt := range tests { |
| 111 | + t.Run(tt.name, func(t *testing.T) { |
| 112 | + auxiliaryKeeper := auxiliaryKeeper{ |
| 113 | + mapper: tt.fields.mapper, |
| 114 | + memberAuxiliary: tt.fields.memberAuxiliary, |
| 115 | + } |
| 116 | + if got := auxiliaryKeeper.Help(tt.args.context, tt.args.request); !reflect.DeepEqual(got, tt.want) { |
| 117 | + t.Errorf("Help() = %v, want %v", got, tt.want) |
| 118 | + } |
| 119 | + }) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func Test_auxiliaryKeeper_Initialize(t *testing.T) { |
| 124 | + _, _, Mapper, Parameters := createTestInput(t) |
| 125 | + type fields struct { |
| 126 | + mapper helpers.Mapper |
| 127 | + memberAuxiliary helpers.Auxiliary |
| 128 | + } |
| 129 | + type args struct { |
| 130 | + mapper helpers.Mapper |
| 131 | + in1 helpers.Parameters |
| 132 | + auxiliaries []interface{} |
| 133 | + } |
| 134 | + tests := []struct { |
| 135 | + name string |
| 136 | + fields fields |
| 137 | + args args |
| 138 | + want helpers.Keeper |
| 139 | + }{ |
| 140 | + {"+ve", fields{Mapper, memberAuxiliary}, args{Mapper, Parameters, []interface{}{}}, auxiliaryKeeper{Mapper, memberAuxiliary}}, |
| 141 | + } |
| 142 | + for _, tt := range tests { |
| 143 | + t.Run(tt.name, func(t *testing.T) { |
| 144 | + auxiliaryKeeper := auxiliaryKeeper{ |
| 145 | + mapper: tt.fields.mapper, |
| 146 | + memberAuxiliary: tt.fields.memberAuxiliary, |
| 147 | + } |
| 148 | + if got := auxiliaryKeeper.Initialize(tt.args.mapper, tt.args.in1, tt.args.auxiliaries); !reflect.DeepEqual(fmt.Sprint(got), fmt.Sprint(tt.want)) { |
| 149 | + t.Errorf("Initialize() = %v, want %v", got, tt.want) |
| 150 | + } |
| 151 | + }) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +func Test_keeperPrototype(t *testing.T) { |
| 156 | + tests := []struct { |
| 157 | + name string |
| 158 | + want helpers.AuxiliaryKeeper |
| 159 | + }{ |
| 160 | + {"+ve", auxiliaryKeeper{}}, |
| 161 | + } |
| 162 | + for _, tt := range tests { |
| 163 | + t.Run(tt.name, func(t *testing.T) { |
| 164 | + if got := keeperPrototype(); !reflect.DeepEqual(got, tt.want) { |
| 165 | + t.Errorf("keeperPrototype() = %v, want %v", got, tt.want) |
| 166 | + } |
| 167 | + }) |
| 168 | + } |
| 169 | +} |
0 commit comments