Commit d28e7cb7 authored by Jeromy's avatar Jeromy

a little more test coverage on merkledag

License: MIT
Signed-off-by: default avatarJeromy <jeromyj@gmail.com>
parent fea3a0bb
......@@ -37,16 +37,6 @@ func (n *Node) Unmarshal(encoded []byte) error {
return nil
}
// MarshalTo encodes a *Node instance into a given byte slice.
// The conversion uses an intermediate PBNode.
func (n *Node) MarshalTo(encoded []byte) error {
pbn := n.getPBNode()
if _, err := pbn.MarshalTo(encoded); err != nil {
return fmt.Errorf("Marshal failed. %v", err)
}
return nil
}
// Marshal encodes a *Node instance into a new byte slice.
// The conversion uses an intermediate PBNode.
func (n *Node) Marshal() ([]byte, error) {
......@@ -82,7 +72,7 @@ func (n *Node) Encoded(force bool) ([]byte, error) {
var err error
n.encoded, err = n.Marshal()
if err != nil {
return []byte{}, err
return nil, err
}
n.cached = u.Hash(n.encoded)
}
......
......@@ -5,6 +5,7 @@ import (
"fmt"
"io"
"io/ioutil"
"strings"
"sync"
"testing"
......@@ -221,3 +222,70 @@ func runBatchFetchTest(t *testing.T, read io.Reader) {
wg.Wait()
}
func TestRecursiveAdd(t *testing.T) {
a := &Node{Data: []byte("A")}
b := &Node{Data: []byte("B")}
c := &Node{Data: []byte("C")}
d := &Node{Data: []byte("D")}
e := &Node{Data: []byte("E")}
err := a.AddNodeLink("blah", b)
if err != nil {
t.Fatal(err)
}
err = b.AddNodeLink("foo", c)
if err != nil {
t.Fatal(err)
}
err = b.AddNodeLink("bar", d)
if err != nil {
t.Fatal(err)
}
err = d.AddNodeLink("baz", e)
if err != nil {
t.Fatal(err)
}
dsp := getDagservAndPinner(t)
err = dsp.ds.AddRecursive(a)
if err != nil {
t.Fatal(err)
}
assertCanGet(t, dsp.ds, a)
assertCanGet(t, dsp.ds, b)
assertCanGet(t, dsp.ds, c)
assertCanGet(t, dsp.ds, d)
assertCanGet(t, dsp.ds, e)
}
func assertCanGet(t *testing.T, ds DAGService, n *Node) {
k, err := n.Key()
if err != nil {
t.Fatal(err)
}
_, err = ds.Get(context.TODO(), k)
if err != nil {
t.Fatal(err)
}
}
func TestCantGet(t *testing.T) {
dsp := getDagservAndPinner(t)
a := &Node{Data: []byte("A")}
k, err := a.Key()
if err != nil {
t.Fatal(err)
}
_, err = dsp.ds.Get(context.TODO(), k)
if !strings.Contains(err.Error(), "not found") {
t.Fatal("expected err not found, got: ", err)
}
}
package merkledag
import (
"testing"
)
func TestRemoveLink(t *testing.T) {
nd := &Node{
Links: []*Link{
&Link{Name: "a"},
&Link{Name: "b"},
&Link{Name: "a"},
&Link{Name: "a"},
&Link{Name: "c"},
&Link{Name: "a"},
},
}
err := nd.RemoveNodeLink("a")
if err != nil {
t.Fatal(err)
}
if len(nd.Links) != 2 {
t.Fatal("number of links incorrect")
}
if nd.Links[0].Name != "b" {
t.Fatal("link order wrong")
}
if nd.Links[1].Name != "c" {
t.Fatal("link order wrong")
}
// should fail
err = nd.RemoveNodeLink("a")
if err != ErrNotFound {
t.Fatal("should have failed to remove link")
}
// ensure nothing else got touched
if len(nd.Links) != 2 {
t.Fatal("number of links incorrect")
}
if nd.Links[0].Name != "b" {
t.Fatal("link order wrong")
}
if nd.Links[1].Name != "c" {
t.Fatal("link order wrong")
}
}
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