metadata_test.go 767 Bytes
Newer Older
1 2 3 4 5 6
package metadata

import (
	"math/rand"
	"testing"

7
	cidlink "github.com/ipld/go-ipld-prime/linking/cid"
Hannah Howard's avatar
Hannah Howard committed
8
	"github.com/stretchr/testify/require"
9 10 11 12 13 14

	"github.com/ipfs/go-graphsync/testutil"
)

func TestDecodeEncodeMetadata(t *testing.T) {
	cids := testutil.GenerateCids(10)
15
	initialMetadata := make(Metadata, 0, 10)
16 17 18
	for _, k := range cids {
		link := cidlink.Link{Cid: k}
		blockPresent := rand.Int31()%2 == 0
19
		initialMetadata = append(initialMetadata, Item{link, blockPresent})
20
	}
21
	encoded, err := EncodeMetadata(initialMetadata)
Hannah Howard's avatar
Hannah Howard committed
22
	require.NoError(t, err, "encode errored")
23
	decodedMetadata, err := DecodeMetadata(encoded)
Hannah Howard's avatar
Hannah Howard committed
24 25
	require.NoError(t, err, "decode errored")
	require.Equal(t, initialMetadata, decodedMetadata, "metadata changed during encoding and decoding")
26
}