github.com/Finschia/finschia-sdk@v0.48.1/types/utils_test.go (about)

     1  package types_test
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/suite"
     9  
    10  	sdk "github.com/Finschia/finschia-sdk/types"
    11  )
    12  
    13  type utilsTestSuite struct {
    14  	suite.Suite
    15  }
    16  
    17  func TestUtilsTestSuite(t *testing.T) {
    18  	suite.Run(t, new(utilsTestSuite))
    19  }
    20  
    21  func (s *utilsTestSuite) SetupSuite() {
    22  	s.T().Parallel()
    23  }
    24  
    25  func (s *utilsTestSuite) TestSortJSON() {
    26  	cases := []struct {
    27  		unsortedJSON string
    28  		want         string
    29  		wantErr      bool
    30  	}{
    31  		// simple case
    32  		{
    33  			unsortedJSON: `{"cosmos":"foo", "atom":"bar",  "tendermint":"foobar"}`,
    34  			want:         `{"atom":"bar","cosmos":"foo","tendermint":"foobar"}`, wantErr: false,
    35  		},
    36  		// failing case (invalid JSON):
    37  		{
    38  			unsortedJSON: `"cosmos":"foo",,,, "atom":"bar",  "tendermint":"foobar"}`,
    39  			want:         "",
    40  			wantErr:      true,
    41  		},
    42  		// genesis.json
    43  		{
    44  			unsortedJSON: `{"consensus_params":{"block_size_params":{"max_bytes":22020096,"max_txs":100000,"max_gas":-1},"tx_size_params":{"max_bytes":10240,"max_gas":-1},"block_gossip_params":{"block_part_size_bytes":65536},"evidence_params":{"max_age":100000}},"validators":[{"pub_key":{"type":"AC26791624DE60","value":"c7UMMAbjFuc5GhGPy0E5q5tefy12p9Tq0imXqdrKXwo="},"power":100,"name":""}],"app_hash":"","genesis_time":"2018-05-11T15:52:25.424795506Z","chain_id":"test-chain-Q6VeoW","app_state":{"accounts":[{"address":"718C9C23F98C9642569742ADDD9F9AB9743FBD5D","coins":[{"denom":"Token","amount":1000},{"denom":"stake","amount":50}]}],"stake":{"pool":{"total_supply":50,"bonded_shares":"0","unbonded_shares":"0","bonded_pool":0,"unbonded_pool":0,"inflation_last_time":0,"inflation":"7/100"},"params":{"inflation_rate_change":"13/100","inflation_max":"1/5","inflation_min":"7/100","goal_bonded":"67/100","max_validators":100,"bond_denom":"stake"},"candidates":null,"bonds":null}}}`,
    45  			want:         `{"app_hash":"","app_state":{"accounts":[{"address":"718C9C23F98C9642569742ADDD9F9AB9743FBD5D","coins":[{"amount":1000,"denom":"Token"},{"amount":50,"denom":"stake"}]}],"stake":{"bonds":null,"candidates":null,"params":{"bond_denom":"stake","goal_bonded":"67/100","inflation_max":"1/5","inflation_min":"7/100","inflation_rate_change":"13/100","max_validators":100},"pool":{"bonded_pool":0,"bonded_shares":"0","inflation":"7/100","inflation_last_time":0,"total_supply":50,"unbonded_pool":0,"unbonded_shares":"0"}}},"chain_id":"test-chain-Q6VeoW","consensus_params":{"block_gossip_params":{"block_part_size_bytes":65536},"block_size_params":{"max_bytes":22020096,"max_gas":-1,"max_txs":100000},"evidence_params":{"max_age":100000},"tx_size_params":{"max_bytes":10240,"max_gas":-1}},"genesis_time":"2018-05-11T15:52:25.424795506Z","validators":[{"name":"","power":100,"pub_key":{"type":"AC26791624DE60","value":"c7UMMAbjFuc5GhGPy0E5q5tefy12p9Tq0imXqdrKXwo="}}]}`,
    46  			wantErr:      false,
    47  		},
    48  		// from the TXSpec:
    49  		{
    50  			unsortedJSON: `{"chain_id":"test-chain-1","sequence":1,"fee_bytes":{"amount":[{"amount":5,"denom":"photon"}],"gas":10000},"msg_bytes":{"inputs":[{"address":"696E707574","coins":[{"amount":10,"denom":"atom"}]}],"outputs":[{"address":"6F7574707574","coins":[{"amount":10,"denom":"atom"}]}]},"alt_bytes":null}`,
    51  			want:         `{"alt_bytes":null,"chain_id":"test-chain-1","fee_bytes":{"amount":[{"amount":5,"denom":"photon"}],"gas":10000},"msg_bytes":{"inputs":[{"address":"696E707574","coins":[{"amount":10,"denom":"atom"}]}],"outputs":[{"address":"6F7574707574","coins":[{"amount":10,"denom":"atom"}]}]},"sequence":1}`,
    52  			wantErr:      false,
    53  		},
    54  	}
    55  
    56  	for tcIndex, tc := range cases {
    57  		tc := tc
    58  		got, err := sdk.SortJSON([]byte(tc.unsortedJSON))
    59  		if tc.wantErr {
    60  			s.Require().NotNil(err, "tc #%d", tcIndex)
    61  			s.Require().Panics(func() { sdk.MustSortJSON([]byte(tc.unsortedJSON)) })
    62  		} else {
    63  			s.Require().Nil(err, "tc #%d, err=%s", tcIndex, err)
    64  			s.Require().NotPanics(func() { sdk.MustSortJSON([]byte(tc.unsortedJSON)) })
    65  			s.Require().Equal(got, sdk.MustSortJSON([]byte(tc.unsortedJSON)))
    66  		}
    67  
    68  		s.Require().Equal(string(got), tc.want)
    69  	}
    70  }
    71  
    72  func (s *utilsTestSuite) TestTimeFormatAndParse() {
    73  	cases := []struct {
    74  		RFC3339NanoStr     string
    75  		SDKSortableTimeStr string
    76  		Equal              bool
    77  	}{
    78  		{"2009-11-10T23:00:00Z", "2009-11-10T23:00:00.000000000", true},
    79  		{"2011-01-10T23:10:05.758230235Z", "2011-01-10T23:10:05.758230235", true},
    80  	}
    81  	for _, tc := range cases {
    82  		tc := tc
    83  		timeFromRFC, err := time.Parse(time.RFC3339Nano, tc.RFC3339NanoStr)
    84  		s.Require().Nil(err)
    85  		timeFromSDKFormat, err := time.Parse(sdk.SortableTimeFormat, tc.SDKSortableTimeStr)
    86  		s.Require().Nil(err)
    87  
    88  		s.Require().True(timeFromRFC.Equal(timeFromSDKFormat))
    89  		s.Require().Equal(timeFromRFC.Format(sdk.SortableTimeFormat), tc.SDKSortableTimeStr)
    90  	}
    91  }
    92  
    93  func (s *utilsTestSuite) TestCopyBytes() {
    94  	s.Require().Nil(sdk.CopyBytes(nil))
    95  	s.Require().Equal(0, len(sdk.CopyBytes([]byte{})))
    96  	bs := []byte("test")
    97  	bsCopy := sdk.CopyBytes(bs)
    98  	s.Require().True(bytes.Equal(bs, bsCopy))
    99  }
   100  
   101  func (s *utilsTestSuite) TestUint64ToBigEndian() {
   102  	s.Require().Equal([]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, sdk.Uint64ToBigEndian(uint64(0)))
   103  	s.Require().Equal([]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa}, sdk.Uint64ToBigEndian(uint64(10)))
   104  }
   105  
   106  func (s *utilsTestSuite) TestFormatTimeBytes() {
   107  	tm, err := time.Parse("Jan 2, 2006 at 3:04pm (MST)", "Mar 3, 2020 at 7:54pm (UTC)")
   108  	s.Require().NoError(err)
   109  	s.Require().Equal("2020-03-03T19:54:00.000000000", string(sdk.FormatTimeBytes(tm)))
   110  }
   111  
   112  func (s *utilsTestSuite) TestParseTimeBytes() {
   113  	tm, err := sdk.ParseTimeBytes([]byte("2020-03-03T19:54:00.000000000"))
   114  	s.Require().NoError(err)
   115  	s.Require().True(tm.Equal(time.Date(2020, 3, 3, 19, 54, 0, 0, time.UTC)))
   116  
   117  	_, err = sdk.ParseTimeBytes([]byte{})
   118  	s.Require().Error(err)
   119  }