utils_test.go 2.61 KB
Newer Older
1 2 3
package dagutils

import (
Jan Winkelmann's avatar
Jan Winkelmann committed
4
	"context"
5 6 7 8
	"testing"

	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

Steven Allen's avatar
Steven Allen committed
11 12
	cid "gx/ipfs/QmYVNvtQkeZ6AKSwDrjQTs432QtL6umrrK41EBq3cu7iSP/go-cid"
	ipld "gx/ipfs/QmZtNq8dArGfnpCZfx2pUNY7UcjGhVp5qqwQ4hH6mpTMRQ/go-ipld-format"
13 14 15
)

func TestAddLink(t *testing.T) {
16 17 18
	ctx, context := context.WithCancel(context.Background())
	defer context()

19
	ds := mdtest.Mock()
20
	fishnode := dag.NodeWithData([]byte("fishcakes!"))
21

22
	err := ds.Add(ctx, fishnode)
23 24 25 26
	if err != nil {
		t.Fatal(err)
	}

27
	nd := new(dag.ProtoNode)
28
	nnode, err := addLink(ctx, ds, nd, "fish", fishnode)
29 30 31 32
	if err != nil {
		t.Fatal(err)
	}

33
	fnprime, err := nnode.GetLinkedNode(ctx, ds, "fish")
34 35 36 37
	if err != nil {
		t.Fatal(err)
	}

Jeromy's avatar
Jeromy committed
38
	fnpkey := fnprime.Cid()
39
	if !fnpkey.Equals(fishnode.Cid()) {
40 41 42 43
		t.Fatal("wrong child node found!")
	}
}

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

		cur = nxt
	}

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

func TestInsertNode(t *testing.T) {
63
	root := new(dag.ProtoNode)
64
	e := NewDagEditor(root, nil)
65

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

Steven Allen's avatar
Steven Allen committed
73 74
	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
75

Jeromy's avatar
Jeromy committed
76
	c := e.GetNode().Cid()
77

Jeromy's avatar
Jeromy committed
78 79
	if c.String() != "QmZ8yeT9uD6ouJPNAYt62XffYuXBT6b4mP4obRSE9cJrSt" {
		t.Fatal("output was different than expected: ", c)
80
	}
Jeromy's avatar
Jeromy committed
81
}
82

Jeromy's avatar
Jeromy committed
83
func testInsert(t *testing.T, e *Editor, path, data string, create bool, experr string) {
84
	child := dag.NodeWithData([]byte(data))
85
	err := e.tmp.Add(context.Background(), child)
86 87 88 89
	if err != nil {
		t.Fatal(err)
	}

90
	var c func() *dag.ProtoNode
Jeromy's avatar
Jeromy committed
91
	if create {
92 93
		c = func() *dag.ProtoNode {
			return &dag.ProtoNode{}
Jeromy's avatar
Jeromy committed
94
		}
95 96
	}

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

	if err != nil {
110
		t.Fatal(err, path, data, create, experr)
111 112
	}

113
	assertNodeAtPath(t, e.tmp, e.root, path, child.Cid())
114
}