node_test.go 2.18 KB
Newer Older
1
package merkledag_test
2 3 4

import (
	"testing"
5 6 7 8 9

	. "github.com/ipfs/go-ipfs/merkledag"
	mdtest "github.com/ipfs/go-ipfs/merkledag/test"

	"gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
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
)

func TestRemoveLink(t *testing.T) {
	nd := &Node{
		Links: []*Link{
			&Link{Name: "a"},
			&Link{Name: "b"},
			&Link{Name: "a"},
			&Link{Name: "a"},
			&Link{Name: "c"},
			&Link{Name: "a"},
		},
	}

	err := nd.RemoveNodeLink("a")
	if err != nil {
		t.Fatal(err)
	}

	if len(nd.Links) != 2 {
		t.Fatal("number of links incorrect")
	}

	if nd.Links[0].Name != "b" {
		t.Fatal("link order wrong")
	}

	if nd.Links[1].Name != "c" {
		t.Fatal("link order wrong")
	}

	// should fail
	err = nd.RemoveNodeLink("a")
	if err != ErrNotFound {
		t.Fatal("should have failed to remove link")
	}

	// ensure nothing else got touched
	if len(nd.Links) != 2 {
		t.Fatal("number of links incorrect")
	}

	if nd.Links[0].Name != "b" {
		t.Fatal("link order wrong")
	}

	if nd.Links[1].Name != "c" {
		t.Fatal("link order wrong")
	}
}
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 119 120 121 122 123 124 125 126 127 128 129 130 131

func TestFindLink(t *testing.T) {
	ds := mdtest.Mock()
	k, err := ds.Add(new(Node))
	if err != nil {
		t.Fatal(err)
	}

	nd := &Node{
		Links: []*Link{
			&Link{Name: "a", Hash: k.ToMultihash()},
			&Link{Name: "c", Hash: k.ToMultihash()},
			&Link{Name: "b", Hash: k.ToMultihash()},
		},
	}

	_, err = ds.Add(nd)
	if err != nil {
		t.Fatal(err)
	}

	lnk, err := nd.GetNodeLink("b")
	if err != nil {
		t.Fatal(err)
	}

	if lnk.Name != "b" {
		t.Fatal("got wrong link back")
	}

	_, err = nd.GetNodeLink("f")
	if err != ErrLinkNotFound {
		t.Fatal("shouldnt have found link")
	}

	_, err = nd.GetLinkedNode(context.Background(), ds, "b")
	if err != nil {
		t.Fatal(err)
	}

	outnd, err := nd.UpdateNodeLink("b", nd)
	if err != nil {
		t.Fatal(err)
	}

	olnk, err := outnd.GetNodeLink("b")
	if err != nil {
		t.Fatal(err)
	}

	if olnk.Hash.B58String() == k.B58String() {
		t.Fatal("new link should have different hash")
	}
}

func TestNodeCopy(t *testing.T) {
	nd := &Node{
		Links: []*Link{
			&Link{Name: "a"},
			&Link{Name: "c"},
			&Link{Name: "b"},
		},
	}
	nd.SetData([]byte("testing"))

	ond := nd.Copy()
	ond.SetData(nil)

	if nd.Data() == nil {
		t.Fatal("should be different objects")
	}
}