-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
test(orm) :add test case for bytes.go #22878
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package ormfield_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"cosmossdk.io/orm/encoding/ormfield" | ||
"github.com/stretchr/testify/assert" | ||
"google.golang.org/protobuf/reflect/protoreflect" | ||
) | ||
|
||
// MockReader implements the Reader interface for testing | ||
type MockReader struct { | ||
*bytes.Reader | ||
} | ||
|
||
func NewMockReader(data []byte) *MockReader { | ||
return &MockReader{bytes.NewReader(data)} | ||
} | ||
|
||
// TestBytesCodecEncodeDecode tests encoding and decoding for BytesCodec | ||
func TestBytesCodecEncodeDecode(t *testing.T) { | ||
codec := ormfield.BytesCodec{} | ||
|
||
original := protoreflect.ValueOfBytes([]byte("hello")) | ||
buffer := &bytes.Buffer{} | ||
|
||
// Encode | ||
err := codec.Encode(original, buffer) | ||
assert.NoError(t, err) | ||
assert.Equal(t, []byte("hello"), buffer.Bytes()) | ||
|
||
// Decode | ||
decoded, err := codec.Decode(NewMockReader(buffer.Bytes())) | ||
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. You could hav passed in buffer directly. |
||
assert.NoError(t, err) | ||
assert.Equal(t, original.Bytes(), decoded.Bytes()) | ||
} | ||
|
||
// TestBytesCodecCompare tests comparison for BytesCodec | ||
func TestBytesCodecCompare(t *testing.T) { | ||
codec := ormfield.BytesCodec{} | ||
|
||
v1 := protoreflect.ValueOfBytes([]byte("abc")) | ||
v2 := protoreflect.ValueOfBytes([]byte("xyz")) | ||
v3 := protoreflect.ValueOfBytes([]byte("abc")) | ||
|
||
assert.Equal(t, -1, codec.Compare(v1, v2)) | ||
assert.Equal(t, 0, codec.Compare(v1, v3)) | ||
assert.Equal(t, 1, codec.Compare(v2, v1)) | ||
} | ||
|
||
// TestBytesCodecComputeBufferSize tests buffer size computation for BytesCodec | ||
func TestBytesCodecComputeBufferSize(t *testing.T) { | ||
codec := ormfield.BytesCodec{} | ||
|
||
value := protoreflect.ValueOfBytes([]byte("hello")) | ||
size, err := codec.ComputeBufferSize(value) | ||
assert.NoError(t, err) | ||
assert.Equal(t, 5, size) | ||
} | ||
|
||
// TestNonTerminalBytesCodecEncodeDecode tests encoding and decoding for NonTerminalBytesCodec | ||
func TestNonTerminalBytesCodecEncodeDecode(t *testing.T) { | ||
codec := ormfield.NonTerminalBytesCodec{} | ||
|
||
original := protoreflect.ValueOfBytes([]byte("world")) | ||
buffer := &bytes.Buffer{} | ||
|
||
// Encode | ||
err := codec.Encode(original, buffer) | ||
assert.NoError(t, err) | ||
|
||
// Decode | ||
decoded, err := codec.Decode(NewMockReader(buffer.Bytes())) | ||
assert.NoError(t, err) | ||
assert.Equal(t, original.Bytes(), decoded.Bytes()) | ||
} | ||
|
||
// TestNonTerminalBytesCodecComputeBufferSize tests buffer size computation for NonTerminalBytesCodec | ||
func TestNonTerminalBytesCodecComputeBufferSize(t *testing.T) { | ||
codec := ormfield.NonTerminalBytesCodec{} | ||
|
||
value := protoreflect.ValueOfBytes([]byte("protobuf")) | ||
size, err := codec.ComputeBufferSize(value) | ||
assert.NoError(t, err) | ||
|
||
// Length prefix is 1 byte, and "protobuf" is 8 bytes | ||
assert.Equal(t, 9, size) | ||
} | ||
|
||
// TestNonTerminalBytesCodecCompare tests comparison for NonTerminalBytesCodec | ||
func TestNonTerminalBytesCodecCompare(t *testing.T) { | ||
codec := ormfield.NonTerminalBytesCodec{} | ||
|
||
v1 := protoreflect.ValueOfBytes([]byte("abc")) | ||
v2 := protoreflect.ValueOfBytes([]byte("xyz")) | ||
v3 := protoreflect.ValueOfBytes([]byte("abc")) | ||
|
||
assert.Equal(t, -1, codec.Compare(v1, v2)) | ||
assert.Equal(t, 0, codec.Compare(v1, v3)) | ||
assert.Equal(t, 1, codec.Compare(v2, v1)) | ||
} | ||
|
||
// TestNonTerminalBytesCodecHandlesEmptyBytes tests handling of empty byte arrays | ||
func TestNonTerminalBytesCodecHandlesEmptyBytes(t *testing.T) { | ||
codec := ormfield.NonTerminalBytesCodec{} | ||
buffer := &bytes.Buffer{} | ||
|
||
// Encode empty bytes | ||
err := codec.Encode(protoreflect.ValueOfBytes([]byte{}), buffer) | ||
assert.NoError(t, err) | ||
assert.Equal(t, []byte{0}, buffer.Bytes()) | ||
|
||
// Decode empty bytes | ||
decoded, err := codec.Decode(NewMockReader(buffer.Bytes())) | ||
assert.NoError(t, err) | ||
assert.Equal(t, []byte{}, decoded.Bytes()) | ||
} | ||
|
||
// TestNonTerminalBytesCodecHandlesLargeInput tests handling of large input byte arrays | ||
func TestNonTerminalBytesCodecHandlesLargeInput(t *testing.T) { | ||
codec := ormfield.NonTerminalBytesCodec{} | ||
buffer := &bytes.Buffer{} | ||
|
||
// Large input (length 300 bytes) | ||
input := make([]byte, 300) | ||
for i := range input { | ||
input[i] = byte(i % 256) | ||
} | ||
|
||
err := codec.Encode(protoreflect.ValueOfBytes(input), buffer) | ||
assert.NoError(t, err) | ||
|
||
decoded, err := codec.Decode(NewMockReader(buffer.Bytes())) | ||
assert.NoError(t, err) | ||
assert.Equal(t, input, decoded.Bytes()) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why this new definition and type just to avoid typing bytes.NewReader? You simply could write
var newMockReader = bytes.NewReader
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.
ok, i will check it