github.com/ipld/go-ipld-prime@v0.21.0/traversal/selector/exploreFields_test.go (about)

     1  package selector
     2  
     3  import (
     4  	"testing"
     5  
     6  	qt "github.com/frankban/quicktest"
     7  
     8  	"github.com/ipld/go-ipld-prime/datamodel"
     9  	"github.com/ipld/go-ipld-prime/fluent"
    10  	"github.com/ipld/go-ipld-prime/node/basicnode"
    11  )
    12  
    13  func TestParseExploreFields(t *testing.T) {
    14  	t.Run("parsing non map node should error", func(t *testing.T) {
    15  		sn := basicnode.NewInt(0)
    16  		_, err := ParseContext{}.ParseExploreFields(sn)
    17  		qt.Check(t, err, qt.ErrorMatches, "selector spec parse rejected: selector body must be a map")
    18  	})
    19  	t.Run("parsing map node without fields value should error", func(t *testing.T) {
    20  		sn := fluent.MustBuildMap(basicnode.Prototype.Map, 0, func(na fluent.MapAssembler) {})
    21  		_, err := ParseContext{}.ParseExploreFields(sn)
    22  		qt.Check(t, err, qt.ErrorMatches, "selector spec parse rejected: fields in ExploreFields selector must be present")
    23  	})
    24  	t.Run("parsing map node with fields value that is not a map should error", func(t *testing.T) {
    25  		sn := fluent.MustBuildMap(basicnode.Prototype.Map, 1, func(na fluent.MapAssembler) {
    26  			na.AssembleEntry(SelectorKey_Fields).AssignString("cheese")
    27  		})
    28  		_, err := ParseContext{}.ParseExploreFields(sn)
    29  		qt.Check(t, err, qt.ErrorMatches, "selector spec parse rejected: fields in ExploreFields selector must be a map")
    30  	})
    31  	t.Run("parsing map node with selector node in fields that is invalid should return child's error", func(t *testing.T) {
    32  		sn := fluent.MustBuildMap(basicnode.Prototype.Map, 1, func(na fluent.MapAssembler) {
    33  			na.AssembleEntry(SelectorKey_Fields).CreateMap(1, func(na fluent.MapAssembler) {
    34  				na.AssembleEntry("applesauce").AssignInt(0)
    35  			})
    36  		})
    37  		_, err := ParseContext{}.ParseExploreFields(sn)
    38  		qt.Check(t, err, qt.ErrorMatches, "selector spec parse rejected: selector is a keyed union and thus must be a map")
    39  	})
    40  	t.Run("parsing map node with fields value that is map of only valid selector node should parse", func(t *testing.T) {
    41  		sn := fluent.MustBuildMap(basicnode.Prototype.Map, 1, func(na fluent.MapAssembler) {
    42  			na.AssembleEntry(SelectorKey_Fields).CreateMap(1, func(na fluent.MapAssembler) {
    43  				na.AssembleEntry("applesauce").CreateMap(1, func(na fluent.MapAssembler) {
    44  					na.AssembleEntry(SelectorKey_Matcher).CreateMap(0, func(na fluent.MapAssembler) {})
    45  				})
    46  			})
    47  		})
    48  		s, err := ParseContext{}.ParseExploreFields(sn)
    49  		qt.Check(t, err, qt.IsNil)
    50  		qt.Check(t, s, deepEqualsAllowAllUnexported, ExploreFields{map[string]Selector{"applesauce": Matcher{}}, []datamodel.PathSegment{datamodel.PathSegmentOfString("applesauce")}})
    51  	})
    52  }