Skip to content

Commit

Permalink
Tweaking unit test batches (#787)
Browse files Browse the repository at this point in the history
  • Loading branch information
otherview authored Jul 12, 2024
1 parent 1e3fe35 commit e5d76aa
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 88 deletions.
31 changes: 18 additions & 13 deletions api/accounts/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,24 @@ var ts *httptest.Server
func TestAccount(t *testing.T) {
initAccountServer(t)
defer ts.Close()
getAccount(t)
getAccountWithNonExisitingRevision(t)
getAccountWithGenesisRevision(t)
getAccountWithFinalizedRevision(t)
getCode(t)
getCodeWithNonExisitingRevision(t)
getStorage(t)
getStorageWithNonExisitingRevision(t)
deployContractWithCall(t)
callContract(t)
callContractWithNonExisitingRevision(t)
batchCall(t)
batchCallWithNonExisitingRevision(t)

for name, tt := range map[string]func(*testing.T){
"getAccount": getAccount,
"getAccountWithNonExisitingRevision": getAccountWithNonExisitingRevision,
"getAccountWithGenesisRevision": getAccountWithGenesisRevision,
"getAccountWithFinalizedRevision": getAccountWithFinalizedRevision,
"getCode": getCode,
"getCodeWithNonExisitingRevision": getCodeWithNonExisitingRevision,
"getStorage": getStorage,
"getStorageWithNonExisitingRevision": getStorageWithNonExisitingRevision,
"deployContractWithCall": deployContractWithCall,
"callContract": callContract,
"callContractWithNonExisitingRevision": callContractWithNonExisitingRevision,
"batchCall": batchCall,
"batchCallWithNonExisitingRevision": batchCallWithNonExisitingRevision,
} {
t.Run(name, tt)
}
}

func getAccount(t *testing.T) {
Expand Down
31 changes: 13 additions & 18 deletions api/blocks/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,19 @@ func TestBlock(t *testing.T) {
initBlockServer(t)
defer ts.Close()

tests := []struct {
name string
run func(*testing.T)
}{
{"testBadQueryParams", testBadQueryParams},
{"testInvalidBlockId", testInvalidBlockId},
{"testInvalidBlockNumber", testInvalidBlockNumber},
{"testGetBlockById", testGetBlockById},
{"testGetBlockNotFound", testGetBlockNotFound},
{"testGetExpandedBlockById", testGetExpandedBlockById},
{"testGetBlockByHeight", testGetBlockByHeight},
{"testGetBestBlock", testGetBestBlock},
{"testGetFinalizedBlock", testGetFinalizedBlock},
{"testGetBlockWithRevisionNumberTooHigh", testGetBlockWithRevisionNumberTooHigh},
}

for _, tt := range tests {
t.Run(tt.name, tt.run)
for name, tt := range map[string]func(*testing.T){
"testBadQueryParams": testBadQueryParams,
"testInvalidBlockId": testInvalidBlockId,
"testInvalidBlockNumber": testInvalidBlockNumber,
"testGetBlockById": testGetBlockById,
"testGetBlockNotFound": testGetBlockNotFound,
"testGetExpandedBlockById": testGetExpandedBlockById,
"testGetBlockByHeight": testGetBlockByHeight,
"testGetBestBlock": testGetBestBlock,
"testGetFinalizedBlock": testGetFinalizedBlock,
"testGetBlockWithRevisionNumberTooHigh": testGetBlockWithRevisionNumberTooHigh,
} {
t.Run(name, tt)
}
}

Expand Down
58 changes: 35 additions & 23 deletions api/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,45 @@ func TestDebug(t *testing.T) {
defer ts.Close()

// /tracers endpoint
testTraceClauseWithEmptyTracerTarget(t)
testTraceClauseWithBadBlockId(t)
testTraceClauseWithNonExistingBlockId(t)
testTraceClauseWithBadTxId(t)
testTraceClauseWithNonExistingTx(t)
testTraceClauseWithBadClauseIndex(t)
testTraceClauseWithTxIndexOutOfBound(t)
testTraceClauseWithClauseIndexOutOfBound(t)
testTraceClauseWithCustomTracer(t)
testTraceClause(t)
for name, tt := range map[string]func(*testing.T){
"testTraceClauseWithEmptyTracerTarget": testTraceClauseWithEmptyTracerTarget,
"testTraceClauseWithBadBlockId": testTraceClauseWithBadBlockId,
"testTraceClauseWithNonExistingBlockId": testTraceClauseWithNonExistingBlockId,
"testTraceClauseWithBadTxId": testTraceClauseWithBadTxId,
"testTraceClauseWithNonExistingTx": testTraceClauseWithNonExistingTx,
"testTraceClauseWithBadClauseIndex": testTraceClauseWithBadClauseIndex,
"testTraceClauseWithTxIndexOutOfBound": testTraceClauseWithTxIndexOutOfBound,
"testTraceClauseWithClauseIndexOutOfBound": testTraceClauseWithClauseIndexOutOfBound,
"testTraceClauseWithCustomTracer": testTraceClauseWithCustomTracer,
"testTraceClause": testTraceClause,
} {
t.Run(name, tt)
}

// /tracers/call endpoint
testHandleTraceCallWithMalformedBodyRequest(t)
testHandleTraceCallWithEmptyTraceCallOption(t)
testHandleTraceCall(t)
testTraceCallNextBlock(t)
testHandleTraceCallWithValidRevisions(t)
testHandleTraceCallWithRevisionAsNonExistingHeight(t)
testHandleTraceCallWithRevisionAsNonExistingId(t)
testHandleTraceCallWithMalfomredRevision(t)
testHandleTraceCallWithInsufficientGas(t)
testHandleTraceCallWithBadBlockRef(t)
testHandleTraceCallWithInvalidLengthBlockRef(t)
for name, tt := range map[string]func(*testing.T){
"testHandleTraceCallWithMalformedBodyRequest": testHandleTraceCallWithMalformedBodyRequest,
"testHandleTraceCallWithEmptyTraceCallOption": testHandleTraceCallWithEmptyTraceCallOption,
"testHandleTraceCall": testHandleTraceCall,
"testHandleTraceCallWithValidRevisions": testHandleTraceCallWithValidRevisions,
"testHandleTraceCallWithRevisionAsNonExistingHeight": testHandleTraceCallWithRevisionAsNonExistingHeight,
"testHandleTraceCallWithRevisionAsNonExistingId": testHandleTraceCallWithRevisionAsNonExistingId,
"testHandleTraceCallWithMalfomredRevision": testHandleTraceCallWithMalfomredRevision,
"testHandleTraceCallWithInsufficientGas": testHandleTraceCallWithInsufficientGas,
"testHandleTraceCallWithBadBlockRef": testHandleTraceCallWithBadBlockRef,
"testHandleTraceCallWithInvalidLengthBlockRef": testHandleTraceCallWithInvalidLengthBlockRef,
"testTraceCallNextBlock": testTraceCallNextBlock,
} {
t.Run(name, tt)
}

// /storage/range endpoint
testStorageRangeWithError(t)
testStorageRange(t)
for name, tt := range map[string]func(*testing.T){
"testStorageRangeWithError": testStorageRangeWithError,
"testStorageRange": testStorageRange,
} {
t.Run(name, tt)
}
}

func TestStorageRangeFunc(t *testing.T) {
Expand Down
9 changes: 6 additions & 3 deletions api/events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ func TestEmptyEvents(t *testing.T) {
initEventServer(t, db, defaultLogLimit)
defer ts.Close()

testEventsBadRequest(t)
testEventWithEmptyDb(t)
for name, tt := range map[string]func(*testing.T){
"testEventsBadRequest": testEventsBadRequest,
"testEventWithEmptyDb": testEventWithEmptyDb,
} {
t.Run(name, tt)
}
}

func TestEvents(t *testing.T) {
Expand All @@ -52,7 +56,6 @@ func TestEvents(t *testing.T) {

blocksToInsert := 5
insertBlocks(t, db, blocksToInsert)

testEventWithBlocks(t, blocksToInsert)
}

Expand Down
17 changes: 11 additions & 6 deletions api/events/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ import (
)

func TestEventsTypes(t *testing.T) {
chain := initChain(t)

testConvertRangeWithBlockRangeType(t, chain)
testConvertRangeWithTimeRangeTypeLessThenGenesis(t, chain)
testConvertRangeWithTimeRangeType(t, chain)
testConvertRangeWithFromGreaterThanGenesis(t, chain)
c := initChain(t)
for name, tt := range map[string]func(*testing.T, *chain.Chain){
"testConvertRangeWithBlockRangeType": testConvertRangeWithBlockRangeType,
"testConvertRangeWithTimeRangeTypeLessThenGenesis": testConvertRangeWithTimeRangeTypeLessThenGenesis,
"testConvertRangeWithTimeRangeType": testConvertRangeWithTimeRangeType,
"testConvertRangeWithFromGreaterThanGenesis": testConvertRangeWithFromGreaterThanGenesis,
} {
t.Run(name, func(t *testing.T) {
tt(t, c)
})
}
}

func testConvertRangeWithBlockRangeType(t *testing.T, chain *chain.Chain) {
Expand Down
18 changes: 11 additions & 7 deletions api/subscriptions/subscriptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,20 @@ var txPool *txpool.TxPool
var repo *chain.Repository
var blocks []*block.Block

func TestMain(t *testing.T) {
func TestSubscriptions(t *testing.T) {
initSubscriptionsServer(t)
defer ts.Close()

testHandleSubjectWithBlock(t)
testHandleSubjectWithEvent(t)
testHandleSubjectWithTransfer(t)
testHandleSubjectWithBeat(t)
testHandleSubjectWithBeat2(t)
testHandleSubjectWithNonValidArgument(t)
for name, tt := range map[string]func(*testing.T){
"testHandleSubjectWithBlock": testHandleSubjectWithBlock,
"testHandleSubjectWithEvent": testHandleSubjectWithEvent,
"testHandleSubjectWithTransfer": testHandleSubjectWithTransfer,
"testHandleSubjectWithBeat": testHandleSubjectWithBeat,
"testHandleSubjectWithBeat2": testHandleSubjectWithBeat2,
"testHandleSubjectWithNonValidArgument": testHandleSubjectWithNonValidArgument,
} {
t.Run(name, tt)
}
}

func testHandleSubjectWithBlock(t *testing.T) {
Expand Down
44 changes: 28 additions & 16 deletions api/transactions/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,38 @@ func TestTransaction(t *testing.T) {
defer ts.Close()

// Send tx
sendTx(t)
sendTxWithBadFormat(t)
sendTxThatCannotBeAcceptedInLocalMempool(t)
for name, tt := range map[string]func(*testing.T){
"sendTx": sendTx,
"sendTxWithBadFormat": sendTxWithBadFormat,
"sendTxThatCannotBeAcceptedInLocalMempool": sendTxThatCannotBeAcceptedInLocalMempool,
} {
t.Run(name, tt)
}

// Get tx
getTx(t)
getTxWithBadId(t)
txWithBadHeader(t)
getNonExistingRawTransactionWhenTxStillInMempool(t)
getNonPendingRawTransactionWhenTxStillInMempool(t)
getRawTransactionWhenTxStillInMempool(t)
getTransactionByIDTxNotFound(t)
getTransactionByIDPendingTxNotFound(t)
handleGetTransactionByIDWithBadQueryParams(t)
handleGetTransactionByIDWithNonExistingHead(t)
for name, tt := range map[string]func(*testing.T){
"getTx": getTx,
"getTxWithBadId": getTxWithBadId,
"txWithBadHeader": txWithBadHeader,
"getNonExistingRawTransactionWhenTxStillInMempool": getNonExistingRawTransactionWhenTxStillInMempool,
"getNonPendingRawTransactionWhenTxStillInMempool": getNonPendingRawTransactionWhenTxStillInMempool,
"getRawTransactionWhenTxStillInMempool": getRawTransactionWhenTxStillInMempool,
"getTransactionByIDTxNotFound": getTransactionByIDTxNotFound,
"getTransactionByIDPendingTxNotFound": getTransactionByIDPendingTxNotFound,
"handleGetTransactionByIDWithBadQueryParams": handleGetTransactionByIDWithBadQueryParams,
"handleGetTransactionByIDWithNonExistingHead": handleGetTransactionByIDWithNonExistingHead,
} {
t.Run(name, tt)
}

// Get tx receipt
getTxReceipt(t)
getReceiptWithBadId(t)
handleGetTransactionReceiptByIDWithNonExistingHead(t)
for name, tt := range map[string]func(*testing.T){
"getTxReceipt": getTxReceipt,
"getReceiptWithBadId": getReceiptWithBadId,
"handleGetTransactionReceiptByIDWithNonExistingHead": handleGetTransactionReceiptByIDWithNonExistingHead,
} {
t.Run(name, tt)
}
}

func getTx(t *testing.T) {
Expand Down
2 changes: 0 additions & 2 deletions trie/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package trie
import (
"bytes"
crand "crypto/rand"
"fmt"
mrand "math/rand"
"testing"
"time"
Expand All @@ -37,7 +36,6 @@ func TestProof(t *testing.T) {
trie, vals := randomTrie(500)
root := trie.Hash()
for _, kv := range vals {
fmt.Println(kv)
proofs := ethdb.NewMemDatabase()
if trie.Prove(kv.k, 0, proofs) != nil {
t.Fatalf("missing key %x while constructing proof", kv.k)
Expand Down

0 comments on commit e5d76aa

Please sign in to comment.