github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/common/rpc/convert/collections_test.go (about)

     1  package convert_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/onflow/flow-go/engine/common/rpc/convert"
    10  	"github.com/onflow/flow-go/model/flow"
    11  	"github.com/onflow/flow-go/utils/unittest"
    12  
    13  	"github.com/onflow/flow/protobuf/go/flow/entities"
    14  )
    15  
    16  // TestConvertCollection tests that converting a collection to a protobuf message results in the correct
    17  // set of transaction IDs
    18  func TestConvertCollection(t *testing.T) {
    19  	t.Parallel()
    20  
    21  	collection := unittest.CollectionFixture(5)
    22  	txIDs := make([]flow.Identifier, 0, len(collection.Transactions))
    23  	for _, tx := range collection.Transactions {
    24  		txIDs = append(txIDs, tx.ID())
    25  	}
    26  
    27  	t.Run("convert collection to message", func(t *testing.T) {
    28  		msg, err := convert.CollectionToMessage(&collection)
    29  		require.NoError(t, err)
    30  
    31  		assert.Len(t, msg.TransactionIds, len(txIDs))
    32  		for i, txID := range txIDs {
    33  			assert.Equal(t, txID[:], msg.TransactionIds[i])
    34  		}
    35  	})
    36  
    37  	var msg *entities.Collection
    38  	lightCollection := flow.LightCollection{Transactions: txIDs}
    39  
    40  	t.Run("convert light collection to message", func(t *testing.T) {
    41  		var err error
    42  		msg, err = convert.LightCollectionToMessage(&lightCollection)
    43  		require.NoError(t, err)
    44  
    45  		assert.Len(t, msg.TransactionIds, len(txIDs))
    46  		for i, txID := range txIDs {
    47  			assert.Equal(t, txID[:], msg.TransactionIds[i])
    48  		}
    49  	})
    50  
    51  	t.Run("convert message to light collection", func(t *testing.T) {
    52  		lightColl, err := convert.MessageToLightCollection(msg)
    53  		require.NoError(t, err)
    54  
    55  		assert.Equal(t, len(txIDs), len(lightColl.Transactions))
    56  		for _, txID := range lightColl.Transactions {
    57  			assert.Equal(t, txID[:], txID[:])
    58  		}
    59  	})
    60  }
    61  
    62  // TestConvertCollectionGuarantee tests that converting a collection guarantee to and from a protobuf
    63  // message results in the same collection guarantee
    64  func TestConvertCollectionGuarantee(t *testing.T) {
    65  	t.Parallel()
    66  
    67  	guarantee := unittest.CollectionGuaranteeFixture(unittest.WithCollRef(unittest.IdentifierFixture()))
    68  
    69  	msg := convert.CollectionGuaranteeToMessage(guarantee)
    70  	converted := convert.MessageToCollectionGuarantee(msg)
    71  
    72  	assert.Equal(t, guarantee, converted)
    73  }
    74  
    75  // TestConvertCollectionGuarantees tests that converting a collection guarantee to and from a protobuf
    76  // message results in the same collection guarantee
    77  func TestConvertCollectionGuarantees(t *testing.T) {
    78  	t.Parallel()
    79  
    80  	guarantees := unittest.CollectionGuaranteesFixture(5, unittest.WithCollRef(unittest.IdentifierFixture()))
    81  
    82  	msg := convert.CollectionGuaranteesToMessages(guarantees)
    83  	converted := convert.MessagesToCollectionGuarantees(msg)
    84  
    85  	assert.Equal(t, guarantees, converted)
    86  }