neoneo-go/pkg/core/transaction/oracle_test.go

77 lines
1.8 KiB
Go
Raw Normal View History

package transaction
import (
"encoding/json"
"errors"
"math/rand"
"testing"
"github.com/nspcc-dev/neo-go/internal/testserdes"
"github.com/stretchr/testify/require"
)
func TestOracleResponse_EncodeBinary(t *testing.T) {
t.Run("Success", func(t *testing.T) {
r := &OracleResponse{
ID: rand.Uint64(),
Code: Success,
Result: []byte{1, 2, 3, 4, 5},
}
testserdes.EncodeDecodeBinary(t, r, new(OracleResponse))
})
t.Run("ErrorCodes", func(t *testing.T) {
codes := []OracleResponseCode{NotFound, Timeout, Forbidden, Error}
for _, c := range codes {
r := &OracleResponse{
ID: rand.Uint64(),
Code: c,
Result: []byte{},
}
testserdes.EncodeDecodeBinary(t, r, new(OracleResponse))
}
})
t.Run("Error", func(t *testing.T) {
t.Run("InvalidCode", func(t *testing.T) {
r := &OracleResponse{
ID: rand.Uint64(),
Code: 0x42,
Result: []byte{},
}
bs, err := testserdes.EncodeBinary(r)
require.NoError(t, err)
err = testserdes.DecodeBinary(bs, new(OracleResponse))
require.True(t, errors.Is(err, ErrInvalidResponseCode), "got: %v", err)
})
t.Run("InvalidResult", func(t *testing.T) {
r := &OracleResponse{
ID: rand.Uint64(),
Code: Error,
Result: []byte{1},
}
bs, err := testserdes.EncodeBinary(r)
require.NoError(t, err)
err = testserdes.DecodeBinary(bs, new(OracleResponse))
require.True(t, errors.Is(err, ErrInvalidResult), "got: %v", err)
})
})
}
func TestOracleResponse_toJSONMap(t *testing.T) {
r := &OracleResponse{
ID: rand.Uint64(),
Code: Success,
Result: []byte{1},
}
b1, err := json.Marshal(r)
require.NoError(t, err)
m := map[string]interface{}{}
r.toJSONMap(m)
b2, err := json.Marshal(m)
require.NoError(t, err)
require.JSONEq(t, string(b1), string(b2))
}