utils_test.go 2.46 KB
Newer Older
1 2 3 4 5 6 7
package dagutils

import (
	"testing"

	dag "github.com/ipfs/go-ipfs/merkledag"
	mdtest "github.com/ipfs/go-ipfs/merkledag/test"
rht's avatar
rht committed
8
	path "github.com/ipfs/go-ipfs/path"
9

10
	context "context"
11
	cid "gx/ipfs/QmcEcrBAMrwMyhSjXt4yfyPpzgSuV8HLHavnfmiKCSRqZU/go-cid"
12 13 14
)

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

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

23
	nd := new(dag.ProtoNode)
24
	nnode, err := addLink(context.Background(), ds, nd, "fish", fishnode)
25 26 27 28 29 30 31 32 33
	if err != nil {
		t.Fatal(err)
	}

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

Jeromy's avatar
Jeromy committed
34 35
	fnpkey := fnprime.Cid()
	if !fnpkey.Equals(fk) {
36 37 38 39
		t.Fatal("wrong child node found!")
	}
}

40
func assertNodeAtPath(t *testing.T, ds dag.DAGService, root *dag.ProtoNode, pth string, exp *cid.Cid) {
rht's avatar
rht committed
41
	parts := path.SplitList(pth)
42
	cur := root
Jeromy's avatar
Jeromy committed
43
	for _, e := range parts {
44
		nxt, err := cur.GetLinkedProtoNode(context.Background(), ds, e)
45 46 47 48 49 50 51
		if err != nil {
			t.Fatal(err)
		}

		cur = nxt
	}

Jeromy's avatar
Jeromy committed
52 53
	curc := cur.Cid()
	if !curc.Equals(exp) {
54 55 56 57 58
		t.Fatal("node not as expected at end of path")
	}
}

func TestInsertNode(t *testing.T) {
59
	root := new(dag.ProtoNode)
60
	e := NewDagEditor(root, nil)
61

Jeromy's avatar
Jeromy committed
62 63
	testInsert(t, e, "a", "anodefortesting", false, "")
	testInsert(t, e, "a/b", "data", false, "")
64
	testInsert(t, e, "a/b/c/d/e", "blah", false, "no link by that name")
Jeromy's avatar
Jeromy committed
65 66 67 68 69 70 71
	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!")

Jeromy's avatar
Jeromy committed
72
	c := e.GetNode().Cid()
73

Jeromy's avatar
Jeromy committed
74 75
	if c.String() != "QmZ8yeT9uD6ouJPNAYt62XffYuXBT6b4mP4obRSE9cJrSt" {
		t.Fatal("output was different than expected: ", c)
76
	}
Jeromy's avatar
Jeromy committed
77
}
78

Jeromy's avatar
Jeromy committed
79
func testInsert(t *testing.T, e *Editor, path, data string, create bool, experr string) {
80
	child := dag.NodeWithData([]byte(data))
81
	ck, err := e.tmp.Add(child)
82 83 84 85
	if err != nil {
		t.Fatal(err)
	}

86
	var c func() *dag.ProtoNode
Jeromy's avatar
Jeromy committed
87
	if create {
88 89
		c = func() *dag.ProtoNode {
			return &dag.ProtoNode{}
Jeromy's avatar
Jeromy committed
90
		}
91 92
	}

93
	err = e.InsertNodeAtPath(context.Background(), path, child, c)
Jeromy's avatar
Jeromy committed
94 95 96 97 98 99 100 101 102
	if experr != "" {
		var got string
		if err != nil {
			got = err.Error()
		}
		if got != experr {
			t.Fatalf("expected '%s' but got '%s'", experr, got)
		}
		return
103 104 105
	}

	if err != nil {
106
		t.Fatal(err, path, data, create, experr)
107 108
	}

109
	assertNodeAtPath(t, e.tmp, e.root, path, ck)
110
}