utils_test.go 2.57 KB
Newer Older
1 2 3 4 5 6 7 8
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"
rht's avatar
rht committed
9
	path "github.com/ipfs/go-ipfs/path"
10

11
	context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
12 13 14
)

func TestAddLink(t *testing.T) {
15
	ds := mdtest.Mock()
16
	fishnode := dag.NodeWithData([]byte("fishcakes!"))
17 18 19 20 21 22 23

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

	nd := new(dag.Node)
24
	nnode, err := addLink(context.Background(), ds, nd, "fish", fishnode)
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
	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!")
	}
}

rht's avatar
rht committed
44 45
func assertNodeAtPath(t *testing.T, ds dag.DAGService, root *dag.Node, pth string, exp key.Key) {
	parts := path.SplitList(pth)
46
	cur := root
Jeromy's avatar
Jeromy committed
47
	for _, e := range parts {
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
		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) {
	root := new(dag.Node)
68
	e := NewDagEditor(root, nil)
69

Jeromy's avatar
Jeromy committed
70 71
	testInsert(t, e, "a", "anodefortesting", false, "")
	testInsert(t, e, "a/b", "data", false, "")
72
	testInsert(t, e, "a/b/c/d/e", "blah", false, "no link by that name")
Jeromy's avatar
Jeromy committed
73 74 75 76 77 78 79 80
	testInsert(t, e, "a/b/c/d/e", "foo", true, "")
	testInsert(t, e, "a/b/c/d/f", "baz", true, "")
	testInsert(t, e, "a/b/c/d/f", "bar", true, "")

	testInsert(t, e, "", "bar", true, "cannot create link with no name!")
	testInsert(t, e, "////", "slashes", true, "cannot create link with no name!")

	k, err := e.GetNode().Key()
81 82 83 84
	if err != nil {
		t.Fatal(err)
	}

Jeromy's avatar
Jeromy committed
85 86
	if k.B58String() != "QmZ8yeT9uD6ouJPNAYt62XffYuXBT6b4mP4obRSE9cJrSt" {
		t.Fatal("output was different than expected: ", k)
87
	}
Jeromy's avatar
Jeromy committed
88
}
89

Jeromy's avatar
Jeromy committed
90
func testInsert(t *testing.T, e *Editor, path, data string, create bool, experr string) {
91
	child := dag.NodeWithData([]byte(data))
92
	ck, err := e.tmp.Add(child)
93 94 95 96
	if err != nil {
		t.Fatal(err)
	}

Jeromy's avatar
Jeromy committed
97 98 99 100 101
	var c func() *dag.Node
	if create {
		c = func() *dag.Node {
			return &dag.Node{}
		}
102 103
	}

104
	err = e.InsertNodeAtPath(context.Background(), path, child, c)
Jeromy's avatar
Jeromy committed
105 106 107 108 109 110 111 112 113
	if experr != "" {
		var got string
		if err != nil {
			got = err.Error()
		}
		if got != experr {
			t.Fatalf("expected '%s' but got '%s'", experr, got)
		}
		return
114 115 116
	}

	if err != nil {
117
		t.Fatal(err, path, data, create, experr)
118 119
	}

120
	assertNodeAtPath(t, e.tmp, e.root, path, ck)
121
}