utils_test.go 2.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
package dagutils

import (
	"testing"

	key "github.com/ipfs/go-ipfs/blocks/key"
	dag "github.com/ipfs/go-ipfs/merkledag"
	mdtest "github.com/ipfs/go-ipfs/merkledag/test"

	context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
)

func TestAddLink(t *testing.T) {
	ds := mdtest.Mock(t)
	fishnode := &dag.Node{
		Data: []byte("fishcakes!"),
	}

	fk, err := ds.Add(fishnode)
	if err != nil {
		t.Fatal(err)
	}

	nd := new(dag.Node)
	nnode, err := AddLink(context.Background(), ds, nd, "fish", fk)
	if err != nil {
		t.Fatal(err)
	}

	fnprime, err := nnode.GetLinkedNode(context.Background(), ds, "fish")
	if err != nil {
		t.Fatal(err)
	}

	fnpkey, err := fnprime.Key()
	if err != nil {
		t.Fatal(err)
	}

	if fnpkey != fk {
		t.Fatal("wrong child node found!")
	}
}

func assertNodeAtPath(t *testing.T, ds dag.DAGService, root *dag.Node, path []string, exp key.Key) {
	cur := root
	for _, e := range path {
		nxt, err := cur.GetLinkedNode(context.Background(), ds, e)
		if err != nil {
			t.Fatal(err)
		}

		cur = nxt
	}

	curk, err := cur.Key()
	if err != nil {
		t.Fatal(err)
	}

	if curk != exp {
		t.Fatal("node not as expected at end of path")
	}
}

func TestInsertNode(t *testing.T) {
	ds := mdtest.Mock(t)
	root := new(dag.Node)

	childa := &dag.Node{
		Data: []byte("This is child A"),
	}
	ak, err := ds.Add(childa)
	if err != nil {
		t.Fatal(err)
	}

	path := []string{"a", "b", "c", "d"}
	root_a, err := InsertNodeAtPath(context.Background(), ds, root, path, ak, true)
	if err != nil {
		t.Fatal(err)
	}
	assertNodeAtPath(t, ds, root_a, path, ak)

	childb := &dag.Node{Data: []byte("this is the second child")}
	bk, err := ds.Add(childb)
	if err != nil {
		t.Fatal(err)
	}

	// this one should fail, we are specifying a non-existant path
	// with create == false
	path2 := []string{"a", "b", "e", "f"}
	_, err = InsertNodeAtPath(context.Background(), ds, root_a, path2, bk, false)
	if err == nil {
		t.Fatal("that shouldnt have worked")
	}
	if err != dag.ErrNotFound {
		t.Fatal("expected this to fail with 'not found'")
	}

	// inserting a path of length one should work with create == false
	path3 := []string{"x"}
	root_b, err := InsertNodeAtPath(context.Background(), ds, root_a, path3, bk, false)
	if err != nil {
		t.Fatal(err)
	}

	assertNodeAtPath(t, ds, root_b, path3, bk)

	// now try overwriting a path
	root_c, err := InsertNodeAtPath(context.Background(), ds, root_b, path, bk, false)
	if err != nil {
		t.Fatal(err)
	}

	assertNodeAtPath(t, ds, root_c, path, bk)
}