github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/rpc/chain/chain_test.go (about) 1 // Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain 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 chain 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 chain *Chain 29 30 func TestMain(m *testing.M) { 31 s := rpcmocksrv.New() 32 err := s.RegisterName("chain", &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 chain = NewChain(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 blockHash types.Hash 50 blockHashLatest types.Hash 51 header types.Header 52 signedBlock types.SignedBlock 53 } 54 55 func (s *MockSrv) GetBlockHash(height *uint64) string { 56 if height != nil { 57 return mockSrv.blockHash.Hex() 58 } 59 return mockSrv.blockHashLatest.Hex() 60 } 61 62 func (s *MockSrv) GetBlock(hash *string) types.SignedBlock { 63 return mockSrv.signedBlock 64 } 65 66 func (s *MockSrv) GetHeader(hash *string) types.Header { 67 return mockSrv.header 68 } 69 70 func (s *MockSrv) GetFinalizedHead() string { 71 return mockSrv.blockHashLatest.Hex() 72 } 73 74 // mockSrv sets default data used in tests. This data might become stale when substrate is updated – just run the tests 75 // against real servers and update the values stored here. To do that, replace s.URL with 76 // config.Default().RPCURL 77 var mockSrv = MockSrv{ 78 blockHash: types.Hash{0xc4, 0x07, 0xff, 0x9f, 0x28, 0xda, 0x7e, 0x8c, 0xed, 0xda, 0x95, 0x61, 0x95, 0xd3, 0xe9, 0x11, 0xc8, 0x61, 0x5a, 0x2e, 0xcf, 0x0d, 0xbd, 0x6c, 0x25, 0xcf, 0x26, 0x67, 0xfb, 0x09, 0xa7, 0x2a}, //nolint:lll 79 blockHashLatest: types.Hash{0xc4, 0x07, 0xff, 0x9f, 0x28, 0xda, 0x7e, 0x8c, 0xed, 0xda, 0x95, 0x61, 0x95, 0xd3, 0xe9, 0x11, 0xc8, 0x61, 0x5a, 0x2e, 0xcf, 0x0d, 0xbd, 0x6c, 0x25, 0xcf, 0x26, 0x67, 0xfb, 0x09, 0xa7, 0x2b}, //nolint:lll 80 header: types.ExamplaryHeader, 81 signedBlock: types.ExamplarySignedBlock, 82 }