-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add block encoding param to block index api #1752
base: main
Are you sure you want to change the base?
Changes from 1 commit
730a934
5e44941
d08fb30
de360b4
7aa3a29
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package indexer | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/ava-labs/avalanchego/ids" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ava-labs/hypersdk/chain/chaintest" | ||
"github.com/ava-labs/hypersdk/codec" | ||
) | ||
|
||
func TestEncodingValidate(t *testing.T) { | ||
tests := []struct { | ||
input Encoding | ||
exp Encoding | ||
expErr error | ||
}{ | ||
{ | ||
input: JSON, | ||
exp: JSON, | ||
}, | ||
{ | ||
input: Hex, | ||
exp: Hex, | ||
}, | ||
{ | ||
input: Encoding(""), | ||
exp: JSON, | ||
}, | ||
{ | ||
input: Encoding("another"), | ||
expErr: ErrInvalidEncodingParameter, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
err := test.input.Validate() | ||
if test.expErr != nil { | ||
require.EqualError(t, ErrInvalidEncodingParameter, err.Error()) | ||
} else { | ||
require.Equal(t, test.exp, test.input) | ||
} | ||
} | ||
} | ||
|
||
func TestBlockResponse(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we test via ex of setting up httptest server: https://github.com/ava-labs/hypersdk/blob/main/tests/integration/integration.go#L236 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are two ways we could go testing here imo:
Could we add basic tests here that use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also avoid this pattern of using t.Run("JSON", func(t *testing.T) {
// independent test logic
})
t.Run("Hex", func(t *testing.T) {
// independent test logic
}) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea by the way! The client was broken because of my changes ! |
||
blocks := chaintest.GenerateEmptyExecutedBlocks(require.New(t), ids.GenerateTestID(), 0, 0, 0, 1) | ||
t.Run("JSON", func(t *testing.T) { | ||
require := require.New(t) | ||
r := GetBlockResponse{} | ||
err := r.setResponse(blocks[0], JSON) | ||
require.NoError(err) | ||
marshaledBlock, err := json.Marshal(blocks[0]) | ||
require.NoError(err) | ||
res, err := json.Marshal(r) | ||
require.NoError(err) | ||
require.Equal([]byte(fmt.Sprintf(`{"block":%s}`, string(marshaledBlock))), res) | ||
}) | ||
t.Run("Hex", func(t *testing.T) { | ||
require := require.New(t) | ||
r := GetBlockResponse{} | ||
err := r.setResponse(blocks[0], Hex) | ||
require.NoError(err) | ||
blockBytes, err := blocks[0].Marshal() | ||
require.NoError(err) | ||
res, err := json.Marshal(r) | ||
require.NoError(err) | ||
marshaledBlock, err := codec.Bytes(blockBytes).MarshalText() | ||
require.NoError(err) | ||
require.Equal([]byte(fmt.Sprintf(`{"block":"%s"}`, string(marshaledBlock))), res) | ||
}) | ||
t.Run("Unknown", func(t *testing.T) { | ||
require := require.New(t) | ||
r := GetBlockResponse{} | ||
err := r.setResponse(blocks[0], Encoding("unknown")) | ||
require.EqualError(err, ErrUnsupportedEncoding.Error()) | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍