github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/module/executiondatasync/execution_data/serializer_test.go (about)

     1  package execution_data_test
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/onflow/flow-go/module/executiondatasync/execution_data"
    11  	"github.com/onflow/flow-go/module/executiondatasync/execution_data/internal"
    12  	"github.com/onflow/flow-go/utils/unittest"
    13  )
    14  
    15  func TestSerializeDeserializeChunkExecutionData(t *testing.T) {
    16  	serializer := execution_data.DefaultSerializer
    17  
    18  	// Test serializing the current ChunkExecutionData version, then deserializing it back to the
    19  	// same type. Make sure the start and end data structures are the same.
    20  	t.Run("serialize and deserialize ChunkExecutionData", func(t *testing.T) {
    21  		ced := unittest.ChunkExecutionDataFixture(t, 1024, unittest.WithChunkEvents(unittest.EventsFixture(5)))
    22  
    23  		buf := new(bytes.Buffer)
    24  		err := serializer.Serialize(buf, ced)
    25  		require.NoError(t, err)
    26  
    27  		raw, err := serializer.Deserialize(buf)
    28  		require.NoError(t, err)
    29  
    30  		actual, ok := raw.(*execution_data.ChunkExecutionData)
    31  		assert.True(t, ok)
    32  		assert.Equal(t, ced, actual)
    33  	})
    34  
    35  	// Test serializing the past ChunkExecutionDataV1 version, then deserializing it to the current
    36  	// ChunkExecutionData version. Make sure the fields in the start and end data structures are
    37  	// the same.
    38  	//
    39  	// This test ensures that the current code is backwards compatible with the previous version of
    40  	// the data structure. It does NOT ensure that the current data structure is backwards compatible
    41  	// with the previous code.
    42  	t.Run("serialize ChunkExecutionDataV1 and deserialize to ChunkExecutionData", func(t *testing.T) {
    43  		cedV2 := unittest.ChunkExecutionDataFixture(t, 1024, unittest.WithChunkEvents(unittest.EventsFixture(5)))
    44  		cedV2.TransactionResults = nil
    45  		cedV1 := &internal.ChunkExecutionDataV1{
    46  			Collection: cedV2.Collection,
    47  			Events:     cedV2.Events,
    48  			TrieUpdate: cedV2.TrieUpdate,
    49  		}
    50  
    51  		buf := new(bytes.Buffer)
    52  		err := serializer.Serialize(buf, cedV1)
    53  		require.NoError(t, err)
    54  
    55  		raw, err := serializer.Deserialize(buf)
    56  		require.NoError(t, err)
    57  
    58  		actual, ok := raw.(*execution_data.ChunkExecutionData)
    59  		assert.True(t, ok)
    60  		assert.Equal(t, cedV2, actual)
    61  	})
    62  }