neo-go/pkg/core/transaction/oracle_test.go
Anna Shaleva 6b21ad9922 *: replace interface{} with any keyword
Everywhere including examples, external interop APIs, bindings generators
code and in other valuable places. A couple of `interface{}` usages are
intentionally left in the CHANGELOG.md, documentation and tests.
2023-04-04 13:22:42 +03:00

76 lines
1.8 KiB
Go

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]any{}
r.toJSONMap(m)
b2, err := json.Marshal(m)
require.NoError(t, err)
require.JSONEq(t, string(b1), string(b2))
}