metadata_test.go 863 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
package metadata

import (
	"math/rand"
	"reflect"
	"testing"

	"github.com/ipfs/go-graphsync/testbridge"

	"github.com/ipld/go-ipld-prime/linking/cid"

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

func TestDecodeEncodeMetadata(t *testing.T) {
	cids := testutil.GenerateCids(10)
17
	initialMetadata := make(Metadata, 0, 10)
18 19 20
	for _, k := range cids {
		link := cidlink.Link{Cid: k}
		blockPresent := rand.Int31()%2 == 0
21
		initialMetadata = append(initialMetadata, Item{link, blockPresent})
22 23 24 25 26 27 28 29 30 31 32 33 34 35
	}
	bridge := testbridge.NewMockIPLDBridge()
	encoded, err := EncodeMetadata(initialMetadata, bridge)
	if err != nil {
		t.Fatal("Error encoding")
	}
	decodedMetadata, err := DecodeMetadata(encoded, bridge)
	if err != nil {
		t.Fatal("Error decoding")
	}
	if !reflect.DeepEqual(initialMetadata, decodedMetadata) {
		t.Fatal("Metadata changed during encoding and decoding")
	}
}