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

     1  package types_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "github.com/stafiprotocol/go-substrate-rpc-client/types"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  var exampleMetadataV12 = Metadata{
    11  	MagicNumber:   0x6174656d,
    12  	Version:       12,
    13  	IsMetadataV12: true,
    14  	AsMetadataV12: exampleRuntimeMetadataV12,
    15  }
    16  
    17  var exampleRuntimeMetadataV12 = MetadataV12{
    18  	Modules: []ModuleMetadataV12{exampleModuleMetadataV12Empty, exampleModuleMetadataV121, exampleModuleMetadataV122},
    19  }
    20  
    21  var exampleModuleMetadataV12Empty = ModuleMetadataV12{
    22  	Name:       "EmptyModule",
    23  	HasStorage: false,
    24  	Storage:    StorageMetadataV10{},
    25  	HasCalls:   false,
    26  	Calls:      nil,
    27  	HasEvents:  false,
    28  	Events:     nil,
    29  	Constants:  nil,
    30  	Errors:     nil,
    31  	Index:      0,
    32  }
    33  
    34  var exampleModuleMetadataV121 = ModuleMetadataV12{
    35  	Name:       "Module1",
    36  	HasStorage: true,
    37  	Storage:    exampleStorageMetadataV10,
    38  	HasCalls:   true,
    39  	Calls:      []FunctionMetadataV4{exampleFunctionMetadataV4},
    40  	HasEvents:  true,
    41  	Events:     []EventMetadataV4{exampleEventMetadataV4},
    42  	Constants:  []ModuleConstantMetadataV6{exampleModuleConstantMetadataV6},
    43  	Errors:     []ErrorMetadataV8{exampleErrorMetadataV8},
    44  	Index:      1,
    45  }
    46  
    47  var exampleModuleMetadataV122 = ModuleMetadataV12{
    48  	Name:       "Module2",
    49  	HasStorage: true,
    50  	Storage:    exampleStorageMetadataV10,
    51  	HasCalls:   true,
    52  	Calls:      []FunctionMetadataV4{exampleFunctionMetadataV4},
    53  	HasEvents:  true,
    54  	Events:     []EventMetadataV4{exampleEventMetadataV4},
    55  	Constants:  []ModuleConstantMetadataV6{exampleModuleConstantMetadataV6},
    56  	Errors:     []ErrorMetadataV8{exampleErrorMetadataV8},
    57  	Index:      2,
    58  }
    59  
    60  func TestNewMetadataV12_Decode(t *testing.T) {
    61  	tests := []struct {
    62  		name, hexData string
    63  	}{
    64  		{
    65  			"PolkadotV12", ExamplaryMetadataV12PolkadotString,
    66  		},
    67  	}
    68  
    69  	for _, s := range tests {
    70  		t.Run(s.name, func(t *testing.T) {
    71  			metadata := NewMetadataV12()
    72  			err := DecodeFromBytes(MustHexDecodeString(s.hexData), metadata)
    73  			assert.True(t, metadata.IsMetadataV12)
    74  			assert.NoError(t, err)
    75  			data, err := EncodeToBytes(metadata)
    76  			assert.NoError(t, err)
    77  			assert.Equal(t, s.hexData, HexEncodeToString(data))
    78  		})
    79  
    80  	}
    81  }
    82  
    83  func TestMetadataV12_ExistsModuleMetadata(t *testing.T) {
    84  	assert.True(t, exampleMetadataV12.ExistsModuleMetadata("EmptyModule"))
    85  	assert.False(t, exampleMetadataV12.ExistsModuleMetadata("NotExistModule"))
    86  }
    87  
    88  func TestMetadataV12_FindEventNamesForEventID(t *testing.T) {
    89  	module, event, err := exampleMetadataV12.FindEventNamesForEventID(EventID([2]byte{1, 0}))
    90  
    91  	assert.NoError(t, err)
    92  	assert.Equal(t, exampleModuleMetadataV121.Name, module)
    93  	assert.Equal(t, exampleEventMetadataV4.Name, event)
    94  }
    95  
    96  func TestMetadataV12_FindEventNamesForUnknownModule(t *testing.T) {
    97  	_, _, err := exampleMetadataV12.FindEventNamesForEventID(EventID([2]byte{1, 18}))
    98  
    99  	assert.Error(t, err)
   100  }
   101  
   102  func TestMetadataV12_TestFindStorageEntryMetadata(t *testing.T) {
   103  	_, err := exampleMetadataV12.FindStorageEntryMetadata("myStoragePrefix", "myStorageFunc2")
   104  	assert.NoError(t, err)
   105  }
   106  
   107  func TestMetadataV12_TestFindCallIndex(t *testing.T) {
   108  	callIndex, err := exampleMetadataV12.FindCallIndex("Module2.my function")
   109  	assert.NoError(t, err)
   110  	assert.Equal(t, exampleModuleMetadataV122.Index, callIndex.SectionIndex)
   111  	assert.Equal(t, uint8(0), callIndex.MethodIndex)
   112  }
   113  
   114  func TestMetadataV12_TestFindCallIndexWithUnknownModule(t *testing.T) {
   115  	_, err := exampleMetadataV12.FindCallIndex("UnknownModule.my function")
   116  	assert.Error(t, err)
   117  }
   118  
   119  func TestMetadataV12_TestFindCallIndexWithUnknownFunction(t *testing.T) {
   120  	_, err := exampleMetadataV12.FindCallIndex("Module2.unknownFunction")
   121  	assert.Error(t, err)
   122  }