github.com/gagliardetto/solana-go@v1.11.0/rpc/types_test.go (about)

     1  // Copyright 2021 github.com/gagliardetto
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package rpc
    16  
    17  import (
    18  	stdjson "encoding/json"
    19  	"testing"
    20  
    21  	"github.com/gagliardetto/solana-go"
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func TestData_base64_zstd(t *testing.T) {
    26  	val := "KLUv/QQAWQAAaGVsbG8td29ybGTcLcaB"
    27  	in := `["` + val + `", "base64+zstd"]`
    28  
    29  	var data DataBytesOrJSON
    30  	err := data.UnmarshalJSON([]byte(in))
    31  	assert.NoError(t, err)
    32  
    33  	assert.Equal(t,
    34  		[]byte("hello-world"),
    35  		data.GetBinary(),
    36  	)
    37  	assert.Equal(t,
    38  		solana.EncodingBase64Zstd,
    39  		data.asDecodedBinary.Encoding,
    40  	)
    41  	assert.Equal(t,
    42  		[]interface{}{
    43  			val,
    44  			"base64+zstd",
    45  		},
    46  		mustJSONToInterface(mustAnyToJSON(data)),
    47  	)
    48  }
    49  
    50  func TestData_base64_zstd_empty(t *testing.T) {
    51  	in := `["", "base64+zstd"]`
    52  
    53  	var data DataBytesOrJSON
    54  	err := data.UnmarshalJSON([]byte(in))
    55  	assert.NoError(t, err)
    56  
    57  	assert.Equal(t,
    58  		[]byte(""),
    59  		data.GetBinary(),
    60  	)
    61  	assert.Equal(t,
    62  		solana.EncodingBase64Zstd,
    63  		data.asDecodedBinary.Encoding,
    64  	)
    65  	assert.Equal(t,
    66  		[]interface{}{
    67  			"",
    68  			"base64+zstd",
    69  		},
    70  		mustJSONToInterface(mustAnyToJSON(data)),
    71  	)
    72  }
    73  
    74  func TestData_jsonParsed(t *testing.T) {
    75  	in := `{"hello":"world"}`
    76  
    77  	var data DataBytesOrJSON
    78  	err := data.UnmarshalJSON([]byte(in))
    79  	assert.NoError(t, err)
    80  
    81  	assert.Equal(t,
    82  		stdjson.RawMessage(in),
    83  		data.GetRawJSON(),
    84  	)
    85  	assert.Equal(t,
    86  		map[string]interface{}{
    87  			"hello": "world",
    88  		},
    89  		mustJSONToInterface(mustAnyToJSON(data)),
    90  	)
    91  }
    92  
    93  func TestData_jsonParsed_empty(t *testing.T) {
    94  	in := `{}`
    95  
    96  	var data DataBytesOrJSON
    97  	err := data.UnmarshalJSON([]byte(in))
    98  	assert.NoError(t, err)
    99  
   100  	assert.Equal(t,
   101  		stdjson.RawMessage(in),
   102  		data.GetRawJSON(),
   103  	)
   104  	assert.Equal(t,
   105  		map[string]interface{}{},
   106  		mustJSONToInterface(mustAnyToJSON(data)),
   107  	)
   108  }
   109  
   110  func TestData_DataBytesOrJSONFromBytes(t *testing.T) {
   111  	in := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
   112  	dataBytesOrJSON := DataBytesOrJSONFromBytes(in)
   113  	out := dataBytesOrJSON.GetBinary()
   114  	assert.Equal(t, in, out)
   115  }