github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/ws/ws_test.go (about)

     1  package ws
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/iotexproject/iotex-proto/golang/iotextypes"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func Test_getEventInputsByName(t *testing.T) {
    11  	var (
    12  		eventName  = "ProjectUpserted"
    13  		eventTopic = []byte{136, 157, 97, 20, 123, 253, 22, 248, 153, 111, 242, 89, 226, 42, 196, 66, 136, 167, 214, 250, 9, 87, 132, 77, 185, 136, 240, 113, 183, 136, 60, 35}
    14  	)
    15  
    16  	r := require.New(t)
    17  
    18  	inputs := wsProjectRegisterContractABI.Events[eventName].Inputs.NonIndexed()
    19  	data, err := inputs.Pack("http://a.b.c", [32]byte{})
    20  	r.NoError(err)
    21  
    22  	_, err = inputs.Unpack(data)
    23  	r.NoError(err)
    24  
    25  	t.Run("FailedToGetEventABI_InvalidTopic", func(t *testing.T) {
    26  		_, err = getEventInputsByName([]*iotextypes.Log{{
    27  			Topics: [][]byte{{}},
    28  		}}, "any")
    29  		r.Error(err)
    30  	})
    31  	t.Run("NotParsedTargetEvent_EmptyLogs", func(t *testing.T) {
    32  		_, err := getEventInputsByName([]*iotextypes.Log{}, "any")
    33  		r.Error(err)
    34  	})
    35  	t.Run("FailedToUnpackIntoMap_InvalidLogData", func(t *testing.T) {
    36  		_, err = getEventInputsByName([]*iotextypes.Log{{
    37  			Topics: [][]byte{eventTopic},
    38  			Data:   make([]byte, 10),
    39  		}}, eventName)
    40  		r.Error(err)
    41  	})
    42  	t.Run("FailedToParseTopicsIntoMap", func(t *testing.T) {
    43  		_, err = getEventInputsByName([]*iotextypes.Log{{
    44  			Topics: [][]byte{eventTopic},
    45  			Data:   data,
    46  		}}, eventName)
    47  		r.Error(err)
    48  	})
    49  	t.Run("Succeed", func(t *testing.T) {
    50  		_, err = getEventInputsByName([]*iotextypes.Log{{
    51  			Topics: [][]byte{eventTopic, append(make([]byte, 31), byte(11))},
    52  			Data:   data,
    53  		}}, eventName)
    54  		r.NoError(err)
    55  	})
    56  }