Add more tests

This commit is contained in:
Slavomir 2021-07-20 17:29:06 +02:00
parent f81c562eef
commit 9041cb5bc4
4 changed files with 243 additions and 9 deletions

View File

@ -78,7 +78,7 @@ func TestBase58(t *testing.T) {
assert.NoError(t, err)
}
func TestData(t *testing.T) {
func TestData_base64(t *testing.T) {
val := "dGVzdA=="
in := `["` + val + `", "base64"]`
@ -86,6 +86,16 @@ func TestData(t *testing.T) {
err := data.UnmarshalJSON([]byte(in))
assert.NoError(t, err)
assert.Equal(t,
[]byte("test"),
data.Content,
)
assert.Equal(t,
EncodingBase64,
data.Encoding,
)
assert.Equal(t,
[]interface{}{
val,
@ -95,6 +105,140 @@ func TestData(t *testing.T) {
)
}
func TestData_base64_empty(t *testing.T) {
val := ""
in := `["", "base64"]`
var data Data
err := data.UnmarshalJSON([]byte(in))
assert.NoError(t, err)
assert.Equal(t,
[]byte(""),
data.Content,
)
assert.Equal(t,
EncodingBase64,
data.Encoding,
)
assert.Equal(t,
[]interface{}{
val,
"base64",
},
mustJSONToInterface(mustAnyToJSON(data)),
)
}
func TestData_base64_zstd(t *testing.T) {
val := "KLUv/QQAWQAAaGVsbG8td29ybGTcLcaB"
in := `["` + val + `", "base64+zstd"]`
var data Data
err := data.UnmarshalJSON([]byte(in))
assert.NoError(t, err)
assert.Equal(t,
[]byte("hello-world"),
data.Content,
)
assert.Equal(t,
EncodingBase64Zstd,
data.Encoding,
)
assert.Equal(t,
[]interface{}{
val,
"base64+zstd",
},
mustJSONToInterface(mustAnyToJSON(data)),
)
}
func TestData_base64_zstd_empty(t *testing.T) {
in := `["", "base64+zstd"]`
var data Data
err := data.UnmarshalJSON([]byte(in))
assert.NoError(t, err)
assert.Equal(t,
[]byte(""),
data.Content,
)
assert.Equal(t,
EncodingBase64Zstd,
data.Encoding,
)
assert.Equal(t,
[]interface{}{
"",
"base64+zstd",
},
mustJSONToInterface(mustAnyToJSON(data)),
)
}
func TestData_base58(t *testing.T) {
val := "3yZe7d"
in := `["` + val + `", "base58"]`
var data Data
err := data.UnmarshalJSON([]byte(in))
assert.NoError(t, err)
assert.Equal(t,
[]byte("test"),
data.Content,
)
assert.Equal(t,
EncodingBase58,
data.Encoding,
)
assert.Equal(t,
[]interface{}{
val,
"base58",
},
mustJSONToInterface(mustAnyToJSON(data)),
)
}
func TestData_base58_empty(t *testing.T) {
val := ""
in := `["", "base58"]`
var data Data
err := data.UnmarshalJSON([]byte(in))
assert.NoError(t, err)
assert.Equal(t,
[]byte(""),
data.Content,
)
assert.Equal(t,
EncodingBase58,
data.Encoding,
)
assert.Equal(t,
[]interface{}{
val,
"base58",
},
mustJSONToInterface(mustAnyToJSON(data)),
)
}
// mustAnyToJSON marshals the provided variable
// to JSON bytes.
func mustAnyToJSON(raw interface{}) []byte {

View File

@ -74,7 +74,6 @@ func (ha Hash) String() string {
return base58.Encode(ha[:])
}
///
type Signature [64]byte
var zeroSignature = Signature{}
@ -137,7 +136,6 @@ func (p Signature) String() string {
return base58.Encode(p[:])
}
///
type Base58 []byte
func (t Base58) MarshalJSON() ([]byte, error) {
@ -273,7 +271,6 @@ func (t Data) String() string {
return ""
}
///
type ByteWrapper struct {
io.Reader
}

View File

@ -222,12 +222,11 @@ type DataBytesOrJSON struct {
asJSON json.RawMessage
}
func (dt *DataBytesOrJSON) MarshalJSON() ([]byte, error) {
// TODO: invert check?
if dt.asDecodedBinary.Content != nil {
return json.Marshal(dt.asDecodedBinary)
func (dt DataBytesOrJSON) MarshalJSON() ([]byte, error) {
if dt.rawDataEncoding == solana.EncodingJSONParsed || dt.rawDataEncoding == solana.EncodingJSON {
return json.Marshal(dt.asJSON)
}
return json.Marshal(dt.asJSON)
return json.Marshal(dt.asDecodedBinary)
}
func (wrap *DataBytesOrJSON) UnmarshalJSON(data []byte) error {

94
rpc/types_test.go Normal file
View File

@ -0,0 +1,94 @@
package rpc
import (
"encoding/json"
"testing"
"github.com/gagliardetto/solana-go"
"github.com/stretchr/testify/assert"
)
func TestData_base64_zstd(t *testing.T) {
val := "KLUv/QQAWQAAaGVsbG8td29ybGTcLcaB"
in := `["` + val + `", "base64+zstd"]`
var data DataBytesOrJSON
err := data.UnmarshalJSON([]byte(in))
assert.NoError(t, err)
assert.Equal(t,
[]byte("hello-world"),
data.GetBinary(),
)
assert.Equal(t,
solana.EncodingBase64Zstd,
data.asDecodedBinary.Encoding,
)
assert.Equal(t,
[]interface{}{
val,
"base64+zstd",
},
mustJSONToInterface(mustAnyToJSON(data)),
)
}
func TestData_base64_zstd_empty(t *testing.T) {
in := `["", "base64+zstd"]`
var data DataBytesOrJSON
err := data.UnmarshalJSON([]byte(in))
assert.NoError(t, err)
assert.Equal(t,
[]byte(""),
data.GetBinary(),
)
assert.Equal(t,
solana.EncodingBase64Zstd,
data.asDecodedBinary.Encoding,
)
assert.Equal(t,
[]interface{}{
"",
"base64+zstd",
},
mustJSONToInterface(mustAnyToJSON(data)),
)
}
func TestData_jsonParsed(t *testing.T) {
in := `{"hello":"world"}`
var data DataBytesOrJSON
err := data.UnmarshalJSON([]byte(in))
assert.NoError(t, err)
assert.Equal(t,
json.RawMessage(in),
data.GetRawJSON(),
)
assert.Equal(t,
map[string]interface{}{
"hello": "world",
},
mustJSONToInterface(mustAnyToJSON(data)),
)
}
func TestData_jsonParsed_empty(t *testing.T) {
in := `{}`
var data DataBytesOrJSON
err := data.UnmarshalJSON([]byte(in))
assert.NoError(t, err)
assert.Equal(t,
json.RawMessage(in),
data.GetRawJSON(),
)
assert.Equal(t,
map[string]interface{}{},
mustJSONToInterface(mustAnyToJSON(data)),
)
}