Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
YeahNotSewerSide committed Nov 29, 2024
1 parent 86ecdab commit dc802e2
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions api/blocks/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package blocks_test

import (
"encoding/hex"
"encoding/json"
"math"
"math/big"
Expand All @@ -15,6 +16,7 @@ import (
"strings"
"testing"

"github.com/ethereum/go-ethereum/rlp"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -55,6 +57,8 @@ func TestBlock(t *testing.T) {
"testGetFinalizedBlock": testGetFinalizedBlock,
"testGetJustifiedBlock": testGetJustifiedBlock,
"testGetBlockWithRevisionNumberTooHigh": testGetBlockWithRevisionNumberTooHigh,
"testMutuallyExclusiveQueries": testMutuallyExclusiveQueries,
"testGetRawBlock": testGetRawBlock,
} {
t.Run(name, tt)
}
Expand All @@ -67,6 +71,23 @@ func testBadQueryParams(t *testing.T) {

assert.Equal(t, http.StatusBadRequest, statusCode)
assert.Equal(t, "expanded: should be boolean", strings.TrimSpace(string(res)))

badQueryParams = "?raw=1"
res, statusCode, err = tclient.RawHTTPClient().RawHTTPGet("/blocks/best" + badQueryParams)
require.NoError(t, err)

assert.Equal(t, http.StatusBadRequest, statusCode)
assert.Equal(t, "raw: should be boolean", strings.TrimSpace(string(res)))
}

func testMutuallyExclusiveQueries(t *testing.T) {
badQueryParams := "?expanded=true&raw=true"
res, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/blocks/best" + badQueryParams)
require.NoError(t, err)

assert.Equal(t, http.StatusBadRequest, statusCode)
assert.Equal(t, "raw&expanded: Raw and Expanded are mutually exclusive", strings.TrimSpace(string(res)))

}

Check failure on line 91 in api/blocks/blocks_test.go

View workflow job for this annotation

GitHub Actions / Lint / golangci-lint

unnecessary trailing newline (whitespace)

func testGetBestBlock(t *testing.T) {
Expand All @@ -80,6 +101,41 @@ func testGetBestBlock(t *testing.T) {
assert.Equal(t, http.StatusOK, statusCode)
}

func testGetRawBlock(t *testing.T) {
res, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/blocks/best?raw=true")
require.NoError(t, err)
rawBlock := new(blocks.JSONRawBlockSummary)
if err := json.Unmarshal(res, &rawBlock); err != nil {
t.Fatal(err)
}

blockBytes, err := hex.DecodeString(rawBlock.Raw[2:len(rawBlock.Raw)])
if err != nil {
t.Fatal(err)
}

header := block.Header{}
err = rlp.DecodeBytes(blockBytes, &header)
if err != nil {
t.Fatal(err)
}

expHeader := blk.Header()
assert.Equal(t, expHeader.Number(), header.Number(), "Number should be equal")
assert.Equal(t, expHeader.ID(), header.ID(), "Hash should be equal")
assert.Equal(t, expHeader.ParentID(), header.ParentID(), "ParentID should be equal")
assert.Equal(t, expHeader.Timestamp(), header.Timestamp(), "Timestamp should be equal")
assert.Equal(t, expHeader.TotalScore(), header.TotalScore(), "TotalScore should be equal")
assert.Equal(t, expHeader.GasLimit(), header.GasLimit(), "GasLimit should be equal")
assert.Equal(t, expHeader.GasUsed(), header.GasUsed(), "GasUsed should be equal")
assert.Equal(t, expHeader.Beneficiary(), header.Beneficiary(), "Beneficiary should be equal")
assert.Equal(t, expHeader.TxsRoot(), header.TxsRoot(), "TxsRoot should be equal")
assert.Equal(t, expHeader.StateRoot(), header.StateRoot(), "StateRoot should be equal")
assert.Equal(t, expHeader.ReceiptsRoot(), header.ReceiptsRoot(), "ReceiptsRoot should be equal")

assert.Equal(t, http.StatusOK, statusCode)
}

func testGetBlockByHeight(t *testing.T) {
res, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/blocks/1")
require.NoError(t, err)
Expand Down

0 comments on commit dc802e2

Please sign in to comment.