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

     1  // Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based system RPC calls
     2  //
     3  // Copyright 2020 Stafi Protocol
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package system
    18  
    19  import (
    20  	"os"
    21  	"testing"
    22  
    23  	"github.com/stafiprotocol/go-substrate-rpc-client/pkg/client"
    24  	"github.com/stafiprotocol/go-substrate-rpc-client/pkg/rpcmocksrv"
    25  	"github.com/stafiprotocol/go-substrate-rpc-client/types"
    26  )
    27  
    28  var system *System
    29  
    30  func TestMain(m *testing.M) {
    31  	s := rpcmocksrv.New()
    32  	err := s.RegisterName("system", &mockSrv)
    33  	if err != nil {
    34  		panic(err)
    35  	}
    36  
    37  	cl, err := client.Connect(s.URL)
    38  	// cl, err := client.Connect(config.Default().RPCURL)
    39  	if err != nil {
    40  		panic(err)
    41  	}
    42  	system = NewSystem(cl)
    43  
    44  	os.Exit(m.Run())
    45  }
    46  
    47  // MockSrv holds data and methods exposed by the RPC Mock Server used in integration tests
    48  type MockSrv struct {
    49  	chain        types.Text
    50  	health       types.Health
    51  	name         types.Text
    52  	networkState types.NetworkState
    53  	peers        []types.PeerInfo
    54  	properties   types.ChainProperties
    55  	version      types.Text
    56  }
    57  
    58  func (s *MockSrv) Chain() types.Text {
    59  	return mockSrv.chain
    60  }
    61  
    62  func (s *MockSrv) Health() types.Health {
    63  	return mockSrv.health
    64  }
    65  
    66  func (s *MockSrv) Name() types.Text {
    67  	return mockSrv.name
    68  }
    69  
    70  func (s *MockSrv) NetworkState() types.NetworkState {
    71  	return mockSrv.networkState
    72  }
    73  
    74  func (s *MockSrv) Peers() []types.PeerInfo {
    75  	return mockSrv.peers
    76  }
    77  
    78  func (s *MockSrv) Properties() types.ChainProperties {
    79  	return mockSrv.properties
    80  }
    81  
    82  func (s *MockSrv) Version() types.Text {
    83  	return mockSrv.version
    84  }
    85  
    86  // mockSrv sets default data used in tests. This data might become stale when substrate is updated – just run the tests
    87  // against real servers and update the values stored here. To do that, replace s.URL with
    88  // config.Default().RPCURL
    89  var mockSrv = MockSrv{
    90  	chain:        "test-chain",
    91  	health:       types.Health{Peers: 2, IsSyncing: false, ShouldHavePeers: true},
    92  	name:         "test-node",
    93  	networkState: types.NetworkState{PeerID: "my-peer-id"},
    94  	peers: []types.PeerInfo{{PeerID: "another-peer-id", Roles: "Role", ProtocolVersion: 42,
    95  		BestHash: types.NewHash(types.MustHexDecodeString("0xabcd")), BestNumber: 420}},
    96  	properties: types.ChainProperties{IsTokenDecimals: true, AsTokenDecimals: 18,
    97  		IsTokenSymbol: true, AsTokenSymbol: "GSRPCCOIN"},
    98  	version: "My version",
    99  }