Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-graphsync
Commits
44ea5ccb
Commit
44ea5ccb
authored
Mar 14, 2019
by
hannahhoward
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(testbridge): mock failed selectors
Provide selectors that will fail validation or encoding
parent
92414049
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
7 deletions
+45
-7
testbridge/mocknodes.go
testbridge/mocknodes.go
+16
-2
testbridge/testbridge.go
testbridge/testbridge.go
+5
-5
testbridge/testbridge_test.go
testbridge/testbridge_test.go
+24
-0
No files found.
testbridge/mocknodes.go
View file @
44ea5ccb
...
@@ -9,12 +9,26 @@ import (
...
@@ -9,12 +9,26 @@ 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
}
...
...
testbridge/testbridge.go
View file @
44ea5ccb
...
@@ -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
...
...
testbridge/testbridge_test.go
View file @
44ea5ccb
...
@@ -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"
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment