github.com/ipld/go-ipld-prime@v0.21.0/codec/json/marshal_test.go (about)

     1  package json
     2  
     3  import (
     4  	"testing"
     5  
     6  	qt "github.com/frankban/quicktest"
     7  	"github.com/ipfs/go-cid"
     8  	"github.com/ipld/go-ipld-prime"
     9  	"github.com/ipld/go-ipld-prime/datamodel"
    10  	"github.com/ipld/go-ipld-prime/fluent/qp"
    11  	cidlink "github.com/ipld/go-ipld-prime/linking/cid"
    12  	"github.com/ipld/go-ipld-prime/node/basicnode"
    13  )
    14  
    15  var link = cid.MustParse("bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi")
    16  
    17  // mirrored in dag-json but without errors
    18  func TestMarshalLinks(t *testing.T) {
    19  	linkNode := basicnode.NewLink(cidlink.Link{Cid: link})
    20  	mapNode, err := qp.BuildMap(basicnode.Prototype.Any, -1, func(ma datamodel.MapAssembler) {
    21  		qp.MapEntry(ma, "Lnk", qp.Node(linkNode))
    22  	})
    23  	qt.Assert(t, err, qt.IsNil)
    24  
    25  	t.Run("link json", func(t *testing.T) {
    26  		_, err := ipld.Encode(linkNode, Encode)
    27  		qt.Assert(t, err, qt.ErrorMatches, "cannot marshal IPLD links to this codec")
    28  	})
    29  	t.Run("nested link json", func(t *testing.T) {
    30  		_, err := ipld.Encode(mapNode, Encode)
    31  		qt.Assert(t, err, qt.ErrorMatches, "cannot marshal IPLD links to this codec")
    32  	})
    33  }
    34  
    35  // mirrored in dag-json but without errors
    36  func TestMarshalBytes(t *testing.T) {
    37  	bytsNode := basicnode.NewBytes([]byte("byte me"))
    38  	mapNode, err := qp.BuildMap(basicnode.Prototype.Any, -1, func(ma datamodel.MapAssembler) {
    39  		qp.MapEntry(ma, "Byts", qp.Node(bytsNode))
    40  	})
    41  	qt.Assert(t, err, qt.IsNil)
    42  
    43  	t.Run("bytes json", func(t *testing.T) {
    44  		_, err := ipld.Encode(bytsNode, Encode)
    45  		qt.Assert(t, err, qt.ErrorMatches, "cannot marshal IPLD bytes to this codec")
    46  	})
    47  	t.Run("nested bytes json", func(t *testing.T) {
    48  		_, err := ipld.Encode(mapNode, Encode)
    49  		qt.Assert(t, err, qt.ErrorMatches, "cannot marshal IPLD bytes to this codec")
    50  	})
    51  }