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

import (
Jeromy's avatar
Jeromy committed
4
	"strings"
5 6 7 8 9 10 11 12 13 14
	"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) {
15
	ds := mdtest.Mock()
16 17 18 19 20 21 22 23 24 25
	fishnode := &dag.Node{
		Data: []byte("fishcakes!"),
	}

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

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

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

Jeromy's avatar
Jeromy committed
73 74 75 76 77 78 79 80 81 82 83
	testInsert(t, e, "a", "anodefortesting", false, "")
	testInsert(t, e, "a/b", "data", false, "")
	testInsert(t, e, "a/b/c/d/e", "blah", false, "merkledag: not found")
	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()
84 85 86 87
	if err != nil {
		t.Fatal(err)
	}

Jeromy's avatar
Jeromy committed
88 89
	if k.B58String() != "QmThorWojP6YzLJwDukxiYCoKQSwyrMCvdt4WZ6rPm221t" {
		t.Fatal("output was different than expected")
90
	}
Jeromy's avatar
Jeromy committed
91
}
92

Jeromy's avatar
Jeromy committed
93 94 95
func testInsert(t *testing.T, e *Editor, path, data string, create bool, experr string) {
	child := &dag.Node{Data: []byte(data)}
	ck, err := e.ds.Add(child)
96 97 98 99
	if err != nil {
		t.Fatal(err)
	}

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

Jeromy's avatar
Jeromy committed
107 108 109 110 111 112 113 114 115 116
	err = e.InsertNodeAtPath(context.TODO(), path, ck, c)
	if experr != "" {
		var got string
		if err != nil {
			got = err.Error()
		}
		if got != experr {
			t.Fatalf("expected '%s' but got '%s'", experr, got)
		}
		return
117 118 119 120 121 122
	}

	if err != nil {
		t.Fatal(err)
	}

Jeromy's avatar
Jeromy committed
123
	assertNodeAtPath(t, e.ds, e.root, path, ck)
124
}