Commit 44ea5ccb authored by hannahhoward's avatar hannahhoward

feat(testbridge): mock failed selectors

Provide selectors that will fail validation or encoding
parent 92414049
...@@ -8,13 +8,27 @@ import ( ...@@ -8,13 +8,27 @@ import (
) )
type mockSelectorSpec struct { type mockSelectorSpec struct {
cidsVisited []cid.Cid cidsVisited []cid.Cid
failValidation bool
failEncode bool
} }
// NewMockSelectorSpec returns a new mock selector that will visit the given // NewMockSelectorSpec returns a new mock selector that will visit the given
// cids. // cids.
func NewMockSelectorSpec(cidsVisited []cid.Cid) ipld.Node { func NewMockSelectorSpec(cidsVisited []cid.Cid) ipld.Node {
return &mockSelectorSpec{cidsVisited} return &mockSelectorSpec{cidsVisited, false, false}
}
// NewInvalidSelectorSpec returns a spec that will fail when you attempt to
// validate it or decompose to a node + selector.
func NewInvalidSelectorSpec(cidsVisited []cid.Cid) ipld.Node {
return &mockSelectorSpec{cidsVisited, true, false}
}
// NewUnencodableSelectorSpec returns a spec that will fail when you attempt to
// encode it.
func NewUnencodableSelectorSpec(cidsVisited []cid.Cid) ipld.Node {
return &mockSelectorSpec{cidsVisited, false, true}
} }
func (mss *mockSelectorSpec) Kind() ipld.ReprKind { return ipld.ReprKind_Null } func (mss *mockSelectorSpec) Kind() ipld.ReprKind { return ipld.ReprKind_Null }
......
...@@ -42,8 +42,8 @@ func (mb *mockIPLDBridge) ComposeLinkLoader(actualLoader ipldbridge.RawLoader) i ...@@ -42,8 +42,8 @@ func (mb *mockIPLDBridge) ComposeLinkLoader(actualLoader ipldbridge.RawLoader) i
} }
func (mb *mockIPLDBridge) ValidateSelectorSpec(cidRootedSelector ipld.Node) []error { func (mb *mockIPLDBridge) ValidateSelectorSpec(cidRootedSelector ipld.Node) []error {
_, ok := cidRootedSelector.(*mockSelectorSpec) spec, ok := cidRootedSelector.(*mockSelectorSpec)
if !ok { if !ok || spec.failValidation {
return []error{fmt.Errorf("not a selector")} return []error{fmt.Errorf("not a selector")}
} }
return nil return nil
...@@ -51,7 +51,7 @@ func (mb *mockIPLDBridge) ValidateSelectorSpec(cidRootedSelector ipld.Node) []er ...@@ -51,7 +51,7 @@ func (mb *mockIPLDBridge) ValidateSelectorSpec(cidRootedSelector ipld.Node) []er
func (mb *mockIPLDBridge) EncodeNode(node ipld.Node) ([]byte, error) { func (mb *mockIPLDBridge) EncodeNode(node ipld.Node) ([]byte, error) {
spec, ok := node.(*mockSelectorSpec) spec, ok := node.(*mockSelectorSpec)
if ok { if ok && !spec.failEncode {
data, err := json.Marshal(spec.cidsVisited) data, err := json.Marshal(spec.cidsVisited)
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -65,14 +65,14 @@ func (mb *mockIPLDBridge) DecodeNode(data []byte) (ipld.Node, error) { ...@@ -65,14 +65,14 @@ func (mb *mockIPLDBridge) DecodeNode(data []byte) (ipld.Node, error) {
var cidsVisited []cid.Cid var cidsVisited []cid.Cid
err := json.Unmarshal(data, &cidsVisited) err := json.Unmarshal(data, &cidsVisited)
if err == nil { if err == nil {
return &mockSelectorSpec{cidsVisited}, nil return &mockSelectorSpec{cidsVisited, false, false}, nil
} }
return nil, fmt.Errorf("format not supported") return nil, fmt.Errorf("format not supported")
} }
func (mb *mockIPLDBridge) DecodeSelectorSpec(cidRootedSelector ipld.Node) (ipld.Node, ipldbridge.Selector, error) { func (mb *mockIPLDBridge) DecodeSelectorSpec(cidRootedSelector ipld.Node) (ipld.Node, ipldbridge.Selector, error) {
spec, ok := cidRootedSelector.(*mockSelectorSpec) spec, ok := cidRootedSelector.(*mockSelectorSpec)
if !ok { if !ok || spec.failValidation {
return nil, nil, fmt.Errorf("not a selector") return nil, nil, fmt.Errorf("not a selector")
} }
return nil, newMockSelector(spec), nil return nil, newMockSelector(spec), nil
......
...@@ -80,3 +80,27 @@ func TestEncodeDecodeSelectorSpec(t *testing.T) { ...@@ -80,3 +80,27 @@ func TestEncodeDecodeSelectorSpec(t *testing.T) {
t.Fatal("did not decode correct cids") t.Fatal("did not decode correct cids")
} }
} }
func TestFailValidationSelectorSpec(t *testing.T) {
cids := testutil.GenerateCids(5)
spec := NewInvalidSelectorSpec(cids)
bridge := NewMockIPLDBridge()
errs := bridge.ValidateSelectorSpec(spec)
if len(errs) == 0 {
t.Fatal("Spec should not pass validation")
}
_, _, err := bridge.DecodeSelectorSpec(spec)
if err == nil {
t.Fatal("Spec should not decompose to node and selector")
}
}
func TestFailEncodingSelectorSpec(t *testing.T) {
cids := testutil.GenerateCids(5)
spec := NewUnencodableSelectorSpec(cids)
bridge := NewMockIPLDBridge()
_, err := bridge.EncodeNode(spec)
if err == nil {
t.Fatal("Spec should not be encodable")
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment