selectorvalidator_test.go 3.56 KB
Newer Older
1 2 3 4 5 6 7
package selectorvalidator

import (
	"testing"

	ipld "github.com/ipld/go-ipld-prime"
	ipldfree "github.com/ipld/go-ipld-prime/impl/free"
Hannah Howard's avatar
Hannah Howard committed
8
	"github.com/stretchr/testify/require"
9 10 11 12 13

	"github.com/ipld/go-ipld-prime/traversal/selector"
	"github.com/ipld/go-ipld-prime/traversal/selector/builder"
)

14
func TestValidateMaxRecusionDepth(t *testing.T) {
15 16 17 18 19 20 21
	ssb := builder.NewSelectorSpecBuilder(ipldfree.NodeBuilder())

	successBase := ssb.ExploreRecursive(selector.RecursionLimitDepth(80), ssb.ExploreRecursiveEdge())
	failBase := ssb.ExploreRecursive(selector.RecursionLimitDepth(120), ssb.ExploreRecursiveEdge())
	failNoneBase := ssb.ExploreRecursive(selector.RecursionLimitNone(), ssb.ExploreRecursiveEdge())

	verifyOutcomes := func(t *testing.T, success ipld.Node, fail ipld.Node, failNone ipld.Node) {
22
		err := ValidateMaxRecursionDepth(success, 100)
Hannah Howard's avatar
Hannah Howard committed
23
		require.NoError(t, err, "valid selector should validate")
24
		err = ValidateMaxRecursionDepth(fail, 100)
Hannah Howard's avatar
Hannah Howard committed
25
		require.Equal(t, ErrInvalidLimit, err, "selector should fail on invalid limit")
26
		err = ValidateMaxRecursionDepth(failNone, 100)
Hannah Howard's avatar
Hannah Howard committed
27
		require.Equal(t, ErrInvalidLimit, err, "selector should fail on no limit")
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
	}

	t.Run("ExploreRecursive", func(t *testing.T) {
		success := successBase.Node()
		fail := failBase.Node()
		failNone := failNoneBase.Node()
		verifyOutcomes(t, success, fail, failNone)
	})
	t.Run("ExploreAll", func(t *testing.T) {
		success := ssb.ExploreAll(successBase).Node()
		fail := ssb.ExploreAll(failBase).Node()
		failNone := ssb.ExploreAll(failNoneBase).Node()
		verifyOutcomes(t, success, fail, failNone)
	})
	t.Run("ExploreIndex", func(t *testing.T) {
		success := ssb.ExploreIndex(0, successBase).Node()
		fail := ssb.ExploreIndex(0, failBase).Node()
		failNone := ssb.ExploreIndex(0, failNoneBase).Node()
		verifyOutcomes(t, success, fail, failNone)
	})
	t.Run("ExploreRange", func(t *testing.T) {
		success := ssb.ExploreRange(0, 10, successBase).Node()
		fail := ssb.ExploreRange(0, 10, failBase).Node()
		failNone := ssb.ExploreRange(0, 10, failNoneBase).Node()
		verifyOutcomes(t, success, fail, failNone)
	})
	t.Run("ExploreUnion", func(t *testing.T) {
		success := ssb.ExploreUnion(successBase, successBase).Node()
		fail := ssb.ExploreUnion(successBase, failBase).Node()
		failNone := ssb.ExploreUnion(successBase, failNoneBase).Node()
		verifyOutcomes(t, success, fail, failNone)
	})
	t.Run("ExploreFields", func(t *testing.T) {
		success := ssb.ExploreFields(func(efsb builder.ExploreFieldsSpecBuilder) {
			efsb.Insert("apples", successBase)
			efsb.Insert("oranges", successBase)
		}).Node()
		fail := ssb.ExploreFields(func(efsb builder.ExploreFieldsSpecBuilder) {
			efsb.Insert("apples", successBase)
			efsb.Insert("oranges", failBase)
		}).Node()
		failNone := ssb.ExploreFields(func(efsb builder.ExploreFieldsSpecBuilder) {
			efsb.Insert("apples", successBase)
			efsb.Insert("oranges", failNoneBase)
		}).Node()
		verifyOutcomes(t, success, fail, failNone)
	})
	t.Run("nested ExploreRecursive", func(t *testing.T) {
		success := ssb.ExploreRecursive(
			selector.RecursionLimitDepth(10),
			ssb.ExploreUnion(
				ssb.ExploreAll(ssb.ExploreRecursiveEdge()),
				ssb.ExploreIndex(0, successBase),
			),
		).Node()

		fail := ssb.ExploreRecursive(
			selector.RecursionLimitDepth(10),
			ssb.ExploreUnion(
				ssb.ExploreAll(ssb.ExploreRecursiveEdge()),
				ssb.ExploreIndex(0, failBase),
			),
		).Node()
		failNone := ssb.ExploreRecursive(
			selector.RecursionLimitDepth(10),
			ssb.ExploreUnion(
				ssb.ExploreAll(ssb.ExploreRecursiveEdge()),
				ssb.ExploreIndex(0, failNoneBase),
			),
		).Node()
		verifyOutcomes(t, success, fail, failNone)
	})
}