Skip to content

Commit 4f10fc1

Browse files
authored
testing (rpc/modules): write new unit tests to increase code coverage (#1995)
* migrate rpc/module tests to integration tests TODO:write unit tests * fix TestAuthorModule_InsertKey_Valid * fix TestAuthorModule_InsertKey_Valid_Gran_Keytype * migrate rpc/module tests to integration tests TODO:write unit tests * use mockery to generate BlockProducerAPI * WIP/test for dev control function * 100% test coverage for rpc/module/dev unit tests * test cov for getKeys in childState * 100% cover for getStorage size * 100% cover for getStorage hash * 100% coverage for childstate unit tests * format childState * lint * tests for offchain get * full coverage for offchain unit tests * unit tests for payment * 100% for grandpa proveFinality * grandpa unit tests * GenSyncSpec coverage * sync state unit tests * GetBlock * getBlockHash tests * chain tests * Wip/system tests * AccountNextIndex test * syncState test * tes localListenAndAddresses * test localPeerId * system tests * state getPairs test * getKeysPages test * test GetMetadata * test GetReadProof * test GetRuntime Version * test GetStorage * test GetStorageHash * test GetStorageSize * test queryStorage * state tests * remove subscribe func tests * format * rebase * fix naming and remove unnecessary variable inits * remove log * push test data * remove AnythingOfType from chain test * remove AnythingOfType from childState test * remove AnythingOfType from Grandpa test * remove AnythingOfType from offchain test * remove AnythingOfType from payment test * remove mocking anything in state test * remove mocking anything in sync state test * WIP/Finish removing mock.AnythingOfType * WIP/author test * author test * add exp cases to chain test * WIP/figure out error assertion issue * WIP/fix errors still * fix chain test error handling * author module tests fix * childstate test CR feedback * dev tests * wip/grandpa tests * grandpa tests * offchain tests * payment tests * syc state test updates * state test CR feedback * system tests * fix inconsistent test to use reflect on maps * use zip file for test data * delete test data hex file * fix up tests * close to final changes * finish CR feedback * clean up error checking * CR feedback TODO/Fix integration test error * move response init into subtests * remove panic from code * WIP/clean up tests * change res variable to be non pointer * WIP/add expected response check for error case * add response check for error case * finish first round of CR feedback * hasSessionKey tests * remove old author module tests * rename err to expErr * use system module constructor instead of fields * rename getter for test metadata * wip/CR feedback * separate out system getter tests * fix naming * clean up imports * wip/cr feedback * remove wantErr from rpc/module tests * finalize feedback * rebase * CR feedback * remove apimocks alias * final CR feedback * finalize CR changes * fix failing http test * fix LocalPeerId naming
1 parent 14f996f commit 4f10fc1

27 files changed

+3992
-249
lines changed

dot/rpc/modules/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ type RuntimeStorageAPI interface {
147147
GetPersistent(k []byte) ([]byte, error)
148148
}
149149

150+
//go:generate mockery --name SyncStateAPI --structname SyncStateAPI --case underscore --keeptree
151+
150152
// SyncStateAPI is the interface to interact with sync state.
151153
type SyncStateAPI interface {
152154
GenSyncSpec(raw bool) (*genesis.Genesis, error)

dot/rpc/modules/author.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func (am *AuthorModule) PendingExtrinsics(r *http.Request, req *EmptyRequest, re
191191
}
192192

193193
// RemoveExtrinsic Remove given extrinsic from the pool and temporarily ban it to prevent reimporting
194-
func (am *AuthorModule) RemoveExtrinsic(r *http.Request, req *ExtrinsicOrHashRequest, res *RemoveExtrinsicsResponse) error {
194+
func (am *AuthorModule) RemoveExtrinsic(r *http.Request, _ *ExtrinsicOrHashRequest, _ *RemoveExtrinsicsResponse) error {
195195
return nil
196196
}
197197

dot/rpc/modules/author_integration_test.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package modules
88

99
import (
1010
"fmt"
11+
"io"
1112
"os"
1213
"reflect"
1314
"testing"
@@ -26,7 +27,9 @@ import (
2627
)
2728

2829
// https://github.com/paritytech/substrate/blob/5420de3face1349a97eb954ae71c5b0b940c31de/core/transaction-pool/src/tests.rs#L95
29-
var testExt = common.MustHexToBytes("0x410284ffd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d01f8efbe48487e57a22abf7e3acd491b7f3528a33a111b1298601554863d27eb129eaa4e718e1365414ff3d028b62bebc651194c6b5001e5c2839b982757e08a8c0000000600ff8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a480b00c465f14670")
30+
var testExt = common.MustHexToBytes("0x410284ffd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d01f8e" +
31+
"fbe48487e57a22abf7e3acd491b7f3528a33a111b1298601554863d27eb129eaa4e718e1365414ff3d028b62bebc651194c6b5001e5c2839b98" +
32+
"2757e08a8c0000000600ff8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a480b00c465f14670")
3033

3134
// invalid transaction (above tx, with last byte changed)
3235
//nolint
@@ -35,7 +38,7 @@ var testInvalidExt = []byte{1, 212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26,
3538
func TestMain(m *testing.M) {
3639
wasmFilePaths, err := runtime.GenerateRuntimeWasmFile()
3740
if err != nil {
38-
log.Errorf("failed to generate runtime wasm file %s", err)
41+
log.Errorf("failed to generate runtime wasm file: %s", err)
3942
os.Exit(1)
4043
}
4144

@@ -48,7 +51,7 @@ func TestMain(m *testing.M) {
4851

4952
func TestAuthorModule_Pending(t *testing.T) {
5053
txQueue := state.NewTransactionState()
51-
auth := NewAuthorModule(nil, nil, txQueue)
54+
auth := NewAuthorModule(log.New(log.SetWriter(io.Discard)), nil, txQueue)
5255

5356
res := new(PendingExtrinsicsResponse)
5457
err := auth.PendingExtrinsics(nil, nil, res)
@@ -208,15 +211,20 @@ func TestAuthorModule_InsertKey_Valid_Gran_Keytype(t *testing.T) {
208211

209212
func TestAuthorModule_InsertKey_InValid(t *testing.T) {
210213
auth := setupAuthModule(t, nil)
211-
req := &KeyInsertRequest{"babe", "0xb7e9185065667390d2ad952a5324e8c365c9bf503dcf97c67a5ce861afe97309", "0x0000000000000000000000000000000000000000000000000000000000000000"}
214+
req := &KeyInsertRequest{
215+
"babe",
216+
"0xb7e9185065667390d2ad952a5324e8c365c9bf503dcf97c67a5ce861afe97309",
217+
"0x0000000000000000000000000000000000000000000000000000000000000000"}
212218
res := &KeyInsertResponse{}
213219
err := auth.InsertKey(nil, req, res)
214220
require.EqualError(t, err, "generated public key does not equal provide public key")
215221
}
216222

217223
func TestAuthorModule_InsertKey_UnknownKeyType(t *testing.T) {
218224
auth := setupAuthModule(t, nil)
219-
req := &KeyInsertRequest{"mack", "0xb7e9185065667390d2ad952a5324e8c365c9bf503dcf97c67a5ce861afe97309", "0x6246ddf254e0b4b4e7dffefc8adf69d212b98ac2b579c362b473fec8c40b4c0a"}
225+
req := &KeyInsertRequest{"mack",
226+
"0xb7e9185065667390d2ad952a5324e8c365c9bf503dcf97c67a5ce861afe97309",
227+
"0x6246ddf254e0b4b4e7dffefc8adf69d212b98ac2b579c362b473fec8c40b4c0a"}
220228
res := &KeyInsertResponse{}
221229
err := auth.InsertKey(nil, req, res)
222230
require.EqualError(t, err, "cannot decode key: invalid key type")
@@ -277,5 +285,5 @@ func setupAuthModule(t *testing.T, txq *state.TransactionState) *AuthorModule {
277285
t.Cleanup(func() {
278286
rt.Stop()
279287
})
280-
return NewAuthorModule(nil, cs, txq)
288+
return NewAuthorModule(log.New(log.SetWriter(io.Discard)), cs, txq)
281289
}

0 commit comments

Comments
 (0)