github.com/ipld/go-ipld-prime@v0.21.0/traversal/selector/exploreRange_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 TestParseExploreRange(t *testing.T) {
    14  	t.Run("parsing non map node should error", func(t *testing.T) {
    15  		sn := basicnode.NewInt(0)
    16  		_, err := ParseContext{}.ParseExploreRange(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 next field should error", func(t *testing.T) {
    20  		sn := fluent.MustBuildMap(basicnode.Prototype__Map{}, 2, func(na fluent.MapAssembler) {
    21  			na.AssembleEntry(SelectorKey_Start).AssignInt(2)
    22  			na.AssembleEntry(SelectorKey_End).AssignInt(3)
    23  		})
    24  		_, err := ParseContext{}.ParseExploreRange(sn)
    25  		qt.Check(t, err, qt.ErrorMatches, "selector spec parse rejected: next field must be present in ExploreRange selector")
    26  	})
    27  	t.Run("parsing map node without start field should error", func(t *testing.T) {
    28  		sn := fluent.MustBuildMap(basicnode.Prototype__Map{}, 2, func(na fluent.MapAssembler) {
    29  			na.AssembleEntry(SelectorKey_End).AssignInt(3)
    30  			na.AssembleEntry(SelectorKey_Next).CreateMap(1, func(na fluent.MapAssembler) {
    31  				na.AssembleEntry(SelectorKey_Matcher).CreateMap(0, func(na fluent.MapAssembler) {})
    32  			})
    33  		})
    34  		_, err := ParseContext{}.ParseExploreRange(sn)
    35  		qt.Check(t, err, qt.ErrorMatches, "selector spec parse rejected: start field must be present in ExploreRange selector")
    36  	})
    37  	t.Run("parsing map node with start field that is not an int should error", func(t *testing.T) {
    38  		sn := fluent.MustBuildMap(basicnode.Prototype__Map{}, 3, func(na fluent.MapAssembler) {
    39  			na.AssembleEntry(SelectorKey_Start).AssignString("cheese")
    40  			na.AssembleEntry(SelectorKey_End).AssignInt(3)
    41  			na.AssembleEntry(SelectorKey_Next).CreateMap(1, func(na fluent.MapAssembler) {
    42  				na.AssembleEntry(SelectorKey_Matcher).CreateMap(0, func(na fluent.MapAssembler) {})
    43  			})
    44  		})
    45  		_, err := ParseContext{}.ParseExploreRange(sn)
    46  		qt.Check(t, err, qt.ErrorMatches, "selector spec parse rejected: start field must be a number in ExploreRange selector")
    47  	})
    48  	t.Run("parsing map node without end field should error", func(t *testing.T) {
    49  		sn := fluent.MustBuildMap(basicnode.Prototype__Map{}, 2, func(na fluent.MapAssembler) {
    50  			na.AssembleEntry(SelectorKey_Start).AssignInt(2)
    51  			na.AssembleEntry(SelectorKey_Next).CreateMap(1, func(na fluent.MapAssembler) {
    52  				na.AssembleEntry(SelectorKey_Matcher).CreateMap(0, func(na fluent.MapAssembler) {})
    53  			})
    54  		})
    55  		_, err := ParseContext{}.ParseExploreRange(sn)
    56  		qt.Check(t, err, qt.ErrorMatches, "selector spec parse rejected: end field must be present in ExploreRange selector")
    57  	})
    58  	t.Run("parsing map node with end field that is not an int should error", func(t *testing.T) {
    59  		sn := fluent.MustBuildMap(basicnode.Prototype__Map{}, 3, func(na fluent.MapAssembler) {
    60  			na.AssembleEntry(SelectorKey_Start).AssignInt(2)
    61  			na.AssembleEntry(SelectorKey_End).AssignString("cheese")
    62  			na.AssembleEntry(SelectorKey_Next).CreateMap(1, func(na fluent.MapAssembler) {
    63  				na.AssembleEntry(SelectorKey_Matcher).CreateMap(0, func(na fluent.MapAssembler) {})
    64  			})
    65  		})
    66  		_, err := ParseContext{}.ParseExploreRange(sn)
    67  		qt.Check(t, err, qt.ErrorMatches, "selector spec parse rejected: end field must be a number in ExploreRange selector")
    68  	})
    69  	t.Run("parsing map node where end is not greater than start should error", func(t *testing.T) {
    70  		sn := fluent.MustBuildMap(basicnode.Prototype__Map{}, 3, func(na fluent.MapAssembler) {
    71  			na.AssembleEntry(SelectorKey_Start).AssignInt(3)
    72  			na.AssembleEntry(SelectorKey_End).AssignInt(2)
    73  			na.AssembleEntry(SelectorKey_Next).CreateMap(1, func(na fluent.MapAssembler) {
    74  				na.AssembleEntry(SelectorKey_Matcher).CreateMap(0, func(na fluent.MapAssembler) {})
    75  			})
    76  		})
    77  		_, err := ParseContext{}.ParseExploreRange(sn)
    78  		qt.Check(t, err, qt.ErrorMatches, "selector spec parse rejected: end field must be greater than start field in ExploreRange selector")
    79  	})
    80  	t.Run("parsing map node with next field with invalid selector node should return child's error", func(t *testing.T) {
    81  		sn := fluent.MustBuildMap(basicnode.Prototype__Map{}, 3, func(na fluent.MapAssembler) {
    82  			na.AssembleEntry(SelectorKey_Start).AssignInt(2)
    83  			na.AssembleEntry(SelectorKey_End).AssignInt(3)
    84  			na.AssembleEntry(SelectorKey_Next).AssignInt(0)
    85  		})
    86  		_, err := ParseContext{}.ParseExploreRange(sn)
    87  		qt.Check(t, err, qt.ErrorMatches, "selector spec parse rejected: selector is a keyed union and thus must be a map")
    88  	})
    89  
    90  	t.Run("parsing map node with next field with valid selector node should parse", func(t *testing.T) {
    91  		sn := fluent.MustBuildMap(basicnode.Prototype__Map{}, 3, func(na fluent.MapAssembler) {
    92  			na.AssembleEntry(SelectorKey_Start).AssignInt(2)
    93  			na.AssembleEntry(SelectorKey_End).AssignInt(3)
    94  			na.AssembleEntry(SelectorKey_Next).CreateMap(1, func(na fluent.MapAssembler) {
    95  				na.AssembleEntry(SelectorKey_Matcher).CreateMap(0, func(na fluent.MapAssembler) {})
    96  			})
    97  		})
    98  		s, err := ParseContext{}.ParseExploreRange(sn)
    99  		qt.Check(t, err, qt.IsNil)
   100  		qt.Check(t, s, deepEqualsAllowAllUnexported, ExploreRange{Matcher{}, 2, 3, []datamodel.PathSegment{datamodel.PathSegmentOfInt(2)}})
   101  	})
   102  }
   103  
   104  func TestExploreRangeExplore(t *testing.T) {
   105  	s := ExploreRange{Matcher{}, 3, 4, []datamodel.PathSegment{datamodel.PathSegmentOfInt(3)}}
   106  	t.Run("exploring should return nil unless node is a list", func(t *testing.T) {
   107  		n := fluent.MustBuildMap(basicnode.Prototype__Map{}, 0, func(na fluent.MapAssembler) {})
   108  		returnedSelector, _ := s.Explore(n, datamodel.PathSegmentOfInt(3))
   109  		qt.Check(t, returnedSelector, qt.IsNil)
   110  	})
   111  	n := fluent.MustBuildList(basicnode.Prototype__List{}, 4, func(na fluent.ListAssembler) {
   112  		na.AssembleValue().AssignInt(0)
   113  		na.AssembleValue().AssignInt(1)
   114  		na.AssembleValue().AssignInt(2)
   115  		na.AssembleValue().AssignInt(3)
   116  	})
   117  	t.Run("exploring should return nil when given a path segment out of range", func(t *testing.T) {
   118  		returnedSelector, _ := s.Explore(n, datamodel.PathSegmentOfInt(2))
   119  		qt.Check(t, returnedSelector, qt.IsNil)
   120  	})
   121  	t.Run("exploring should return nil when given a path segment that isn't an index", func(t *testing.T) {
   122  		returnedSelector, _ := s.Explore(n, datamodel.PathSegmentOfString("cheese"))
   123  		qt.Check(t, returnedSelector, qt.IsNil)
   124  	})
   125  	t.Run("exploring should return the next selector when given a path segment with index in range", func(t *testing.T) {
   126  		returnedSelector, _ := s.Explore(n, datamodel.PathSegmentOfInt(3))
   127  		qt.Check(t, returnedSelector, qt.Equals, Matcher{})
   128  	})
   129  }