github.com/ipld/go-ipld-prime@v0.21.0/traversal/selector/condition_test.go (about) 1 package selector 2 3 import ( 4 "reflect" 5 "testing" 6 7 qt "github.com/frankban/quicktest" 8 "github.com/google/go-cmp/cmp" 9 "github.com/ipfs/go-cid" 10 11 "github.com/ipld/go-ipld-prime/fluent" 12 cidlink "github.com/ipld/go-ipld-prime/linking/cid" 13 "github.com/ipld/go-ipld-prime/node/basicnode" 14 ) 15 16 var deepEqualsAllowAllUnexported = qt.CmpEquals(cmp.Exporter(func(reflect.Type) bool { return true })) 17 18 func TestParseCondition(t *testing.T) { 19 t.Run("parsing non map node should error", func(t *testing.T) { 20 sn := basicnode.NewInt(0) 21 _, err := ParseContext{}.ParseCondition(sn) 22 qt.Check(t, err, qt.ErrorMatches, "selector spec parse rejected: condition body must be a map") 23 }) 24 t.Run("parsing map node without field should error", func(t *testing.T) { 25 sn := fluent.MustBuildMap(basicnode.Prototype.Map, 0, func(na fluent.MapAssembler) {}) 26 _, err := ParseContext{}.ParseCondition(sn) 27 qt.Check(t, err, qt.ErrorMatches, "selector spec parse rejected: condition is a keyed union and thus must be single-entry map") 28 }) 29 30 t.Run("parsing map node keyed to invalid type should error", func(t *testing.T) { 31 sn := fluent.MustBuildMap(basicnode.Prototype.Map, 1, func(na fluent.MapAssembler) { 32 na.AssembleEntry(string(ConditionMode_Link)).AssignInt(0) 33 }) 34 _, err := ParseContext{}.ParseCondition(sn) 35 qt.Check(t, err, qt.ErrorMatches, "selector spec parse rejected: condition_link must be a link") 36 }) 37 t.Run("parsing map node with condition field with valid selector node should parse", func(t *testing.T) { 38 lnk := cidlink.Link{Cid: cid.Undef} 39 sn := fluent.MustBuildMap(basicnode.Prototype.Map, 1, func(na fluent.MapAssembler) { 40 na.AssembleEntry(string(ConditionMode_Link)).AssignLink(lnk) 41 }) 42 s, err := ParseContext{}.ParseCondition(sn) 43 qt.Check(t, err, qt.IsNil) 44 lnkNode := basicnode.NewLink(lnk) 45 qt.Check(t, s, deepEqualsAllowAllUnexported, Condition{mode: ConditionMode_Link, match: lnkNode}) 46 }) 47 }