github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/model/flow/payload_test.go (about) 1 package flow_test 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 "github.com/vmihailenco/msgpack/v4" 10 11 "github.com/onflow/flow-go/model/flow" 12 "github.com/onflow/flow-go/utils/unittest" 13 ) 14 15 func TestPayloadEncodeEmptyJSON(t *testing.T) { 16 // nil slices 17 payload := unittest.PayloadFixture() 18 payloadHash1 := payload.Hash() 19 encoded1, err := json.Marshal(payload) 20 require.NoError(t, err) 21 var decoded flow.Payload 22 err = json.Unmarshal(encoded1, &decoded) 23 require.NoError(t, err) 24 assert.Equal(t, payloadHash1, decoded.Hash()) 25 assert.Equal(t, payload, decoded) 26 27 // empty slices converted to nil 28 payload.Seals = []*flow.Seal{} 29 payloadHash2 := payload.Hash() 30 assert.Equal(t, payloadHash2, payloadHash1) 31 encoded2, err := json.Marshal(payload) 32 assert.Equal(t, `{"Guarantees":null,"Seals":null,"Receipts":null,"Results":null,"ProtocolStateID":"0000000000000000000000000000000000000000000000000000000000000000"}`, string(encoded2)) 33 assert.Equal(t, string(encoded1), string(encoded2)) 34 require.NoError(t, err) 35 err = json.Unmarshal(encoded2, &decoded) 36 require.NoError(t, err) 37 require.Nil(t, decoded.Seals) 38 assert.Equal(t, payloadHash2, decoded.Hash()) 39 } 40 41 func TestPayloadEncodeJSON(t *testing.T) { 42 payload := unittest.PayloadFixture() 43 payload.Seals = []*flow.Seal{{}} 44 payloadHash := payload.Hash() 45 data, err := json.Marshal(payload) 46 require.NoError(t, err) 47 var decoded flow.Payload 48 err = json.Unmarshal(data, &decoded) 49 require.NoError(t, err) 50 assert.Equal(t, payloadHash, decoded.Hash()) 51 assert.Equal(t, payload, decoded) 52 } 53 54 func TestPayloadEncodingMsgpack(t *testing.T) { 55 payload := unittest.PayloadFixture() 56 payloadHash := payload.Hash() 57 data, err := msgpack.Marshal(payload) 58 require.NoError(t, err) 59 var decoded flow.Payload 60 err = msgpack.Unmarshal(data, &decoded) 61 require.NoError(t, err) 62 decodedHash := decoded.Hash() 63 assert.Equal(t, payloadHash, decodedHash) 64 assert.Equal(t, payload, decoded) 65 }