github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/metadataV13_test.go (about)

     1  package types_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	. "github.com/stafiprotocol/go-substrate-rpc-client/types"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  var exampleMetadataV13 = Metadata{
    12  	MagicNumber:   0x6174656d,
    13  	Version:       13,
    14  	IsMetadataV13: true,
    15  	AsMetadataV13: exampleRuntimeMetadataV13,
    16  }
    17  
    18  var exampleRuntimeMetadataV13 = MetadataV13{
    19  	Modules: []ModuleMetadataV13{exampleModuleMetadataV13Empty, exampleModuleMetadataV131, exampleModuleMetadataV132},
    20  }
    21  
    22  var exampleModuleMetadataV13Empty = ModuleMetadataV13{
    23  	Name:       "EmptyModule_13",
    24  	HasStorage: false,
    25  	Storage:    StorageMetadataV13{},
    26  	HasCalls:   false,
    27  	Calls:      nil,
    28  	HasEvents:  false,
    29  	Events:     nil,
    30  	Constants:  nil,
    31  	Errors:     nil,
    32  	Index:      0,
    33  }
    34  
    35  var exampleModuleMetadataV131 = ModuleMetadataV13{
    36  	Name:       "Module1_13",
    37  	HasStorage: true,
    38  	Storage:    exampleStorageMetadataV13,
    39  	HasCalls:   true,
    40  	Calls:      []FunctionMetadataV4{exampleFunctionMetadataV4},
    41  	HasEvents:  true,
    42  	Events:     []EventMetadataV4{exampleEventMetadataV4},
    43  	Constants:  []ModuleConstantMetadataV6{exampleModuleConstantMetadataV6},
    44  	Errors:     []ErrorMetadataV8{exampleErrorMetadataV8},
    45  	Index:      1,
    46  }
    47  
    48  var exampleModuleMetadataV132 = ModuleMetadataV13{
    49  	Name:       "Module2_13",
    50  	HasStorage: true,
    51  	Storage:    exampleStorageMetadataV13,
    52  	HasCalls:   true,
    53  	Calls:      []FunctionMetadataV4{exampleFunctionMetadataV4},
    54  	HasEvents:  true,
    55  	Events:     []EventMetadataV4{exampleEventMetadataV4},
    56  	Constants:  []ModuleConstantMetadataV6{exampleModuleConstantMetadataV6},
    57  	Errors:     []ErrorMetadataV8{exampleErrorMetadataV8},
    58  	Index:      2,
    59  }
    60  
    61  var exampleStorageMetadataV13 = StorageMetadataV13{
    62  	Prefix: "myStoragePrefix_13",
    63  	Items: []StorageFunctionMetadataV13{exampleStorageFunctionMetadataV13Type, exampleStorageFunctionMetadataV13Map,
    64  		exampleStorageFunctionMetadataV13DoubleMap, exampleStorageFunctionMetadataV13NMap},
    65  }
    66  
    67  var exampleStorageFunctionMetadataV13Type = StorageFunctionMetadataV13{
    68  	Name:          "myStorageFunc_13",
    69  	Modifier:      StorageFunctionModifierV0{IsOptional: true},
    70  	Type:          StorageFunctionTypeV13{IsType: true, AsType: "U8"},
    71  	Fallback:      []byte{23, 14},
    72  	Documentation: []Text{"My", "storage func", "doc"},
    73  }
    74  
    75  var exampleStorageFunctionMetadataV13Map = StorageFunctionMetadataV13{
    76  	Name:          "myStorageFunc2_13",
    77  	Modifier:      StorageFunctionModifierV0{IsOptional: true},
    78  	Type:          StorageFunctionTypeV13{IsMap: true, AsMap: exampleMapTypeV10},
    79  	Fallback:      []byte{23, 14},
    80  	Documentation: []Text{"My", "storage func", "doc"},
    81  }
    82  
    83  var exampleStorageFunctionMetadataV13DoubleMap = StorageFunctionMetadataV13{
    84  	Name:          "myStorageFunc3_13",
    85  	Modifier:      StorageFunctionModifierV0{IsOptional: true},
    86  	Type:          StorageFunctionTypeV13{IsDoubleMap: true, AsDoubleMap: exampleDoubleMapTypeV10},
    87  	Fallback:      []byte{23, 14},
    88  	Documentation: []Text{"My", "storage func", "doc"},
    89  }
    90  
    91  var exampleStorageFunctionMetadataV13NMap = StorageFunctionMetadataV13{
    92  	Name:          "myStorageFunc4_13",
    93  	Modifier:      StorageFunctionModifierV0{IsOptional: true},
    94  	Type:          StorageFunctionTypeV13{IsNMap: true, AsNMap: exampleNMapTypeV13},
    95  	Fallback:      []byte{23, 14},
    96  	Documentation: []Text{"My", "storage func", "doc"},
    97  }
    98  
    99  var exampleNMapTypeV13 = NMapTypeV13{
   100  	Hashers: []StorageHasherV10{{IsBlake2_256: true}, {IsBlake2_128Concat: true}, {IsIdentity: true}},
   101  	Keys:    []Type{"myKey1", "myKey2", "myKey3"},
   102  	Value:   "and a value",
   103  }
   104  
   105  func TestMetadataV13_ExistsModuleMetadata(t *testing.T) {
   106  	assert.True(t, exampleMetadataV13.ExistsModuleMetadata("EmptyModule_13"))
   107  	assert.False(t, exampleMetadataV13.ExistsModuleMetadata("NotExistModule"))
   108  }
   109  
   110  func TestMetadataV13_FindEventNamesForEventID(t *testing.T) {
   111  	module, event, err := exampleMetadataV13.FindEventNamesForEventID(EventID([2]byte{1, 0}))
   112  
   113  	assert.NoError(t, err)
   114  	assert.Equal(t, exampleModuleMetadataV131.Name, module)
   115  	assert.Equal(t, exampleEventMetadataV4.Name, event)
   116  }
   117  
   118  func TestMetadataV13_FindEventNamesForUnknownModule(t *testing.T) {
   119  	_, _, err := exampleMetadataV13.FindEventNamesForEventID(EventID([2]byte{1, 18}))
   120  
   121  	assert.Error(t, err)
   122  }
   123  
   124  func TestMetadataV13_TestFindStorageEntryMetadata(t *testing.T) {
   125  	_, err := exampleMetadataV13.FindStorageEntryMetadata("myStoragePrefix_13", "myStorageFunc2_13")
   126  	assert.NoError(t, err)
   127  }
   128  
   129  func TestMetadataV13_TestFindCallIndex(t *testing.T) {
   130  	callIndex, err := exampleMetadataV13.FindCallIndex("Module2_13.my function")
   131  	assert.NoError(t, err)
   132  	assert.Equal(t, exampleModuleMetadataV132.Index, callIndex.SectionIndex)
   133  	assert.Equal(t, uint8(0), callIndex.MethodIndex)
   134  }
   135  
   136  func TestMetadataV13_TestFindCallIndexWithUnknownModule(t *testing.T) {
   137  	_, err := exampleMetadataV13.FindCallIndex("UnknownModule.my function")
   138  	assert.Error(t, err)
   139  }
   140  
   141  func TestMetadataV13_TestFindCallIndexWithUnknownFunction(t *testing.T) {
   142  	_, err := exampleMetadataV13.FindCallIndex("Module2_13.unknownFunction")
   143  	assert.Error(t, err)
   144  }
   145  
   146  func TestNewMetadataV13_Decode(t *testing.T) {
   147  	metadata := NewMetadataV13()
   148  	err := DecodeFromBytes(MustHexDecodeString(ExamplaryMetadataV13SubstrateString), metadata)
   149  	assert.True(t, metadata.IsMetadataV13)
   150  	assert.NoError(t, err)
   151  	fmt.Printf("%#v", metadata)
   152  	data, err := EncodeToBytes(metadata)
   153  	assert.NoError(t, err)
   154  	assert.Equal(t, ExamplaryMetadataV13SubstrateString, HexEncodeToString(data))
   155  }