node_test.go 2.72 KB
Newer Older
1
package merkledag_test
2 3

import (
Jeromy's avatar
Jeromy committed
4
	"bytes"
Jeromy's avatar
Jeromy committed
5
	"context"
6
	"testing"
7 8 9 10

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

Steven Allen's avatar
Steven Allen committed
11
	node "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
12 13 14
)

func TestRemoveLink(t *testing.T) {
15
	nd := &ProtoNode{}
Jeromy's avatar
Jeromy committed
16 17 18 19 20 21 22
	nd.SetLinks([]*node.Link{
		{Name: "a"},
		{Name: "b"},
		{Name: "a"},
		{Name: "a"},
		{Name: "c"},
		{Name: "a"},
23
	})
24 25 26 27 28 29

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

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

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

38
	if nd.Links()[1].Name != "c" {
39 40 41 42 43
		t.Fatal("link order wrong")
	}

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

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

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

57
	if nd.Links()[1].Name != "c" {
58 59 60
		t.Fatal("link order wrong")
	}
}
61 62

func TestFindLink(t *testing.T) {
63 64
	ctx := context.Background()

65
	ds := mdtest.Mock()
66 67
	ndEmpty := new(ProtoNode)
	err := ds.Add(ctx, ndEmpty)
68 69 70 71
	if err != nil {
		t.Fatal(err)
	}

72 73
	kEmpty := ndEmpty.Cid()

74
	nd := &ProtoNode{}
Jeromy's avatar
Jeromy committed
75
	nd.SetLinks([]*node.Link{
76 77 78
		{Name: "a", Cid: kEmpty},
		{Name: "c", Cid: kEmpty},
		{Name: "b", Cid: kEmpty},
79
	})
80

81
	err = ds.Add(ctx, nd)
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
	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)
	}

115
	if olnk.Cid.String() == kEmpty.String() {
116 117 118 119 120
		t.Fatal("new link should have different hash")
	}
}

func TestNodeCopy(t *testing.T) {
121
	nd := &ProtoNode{}
Jeromy's avatar
Jeromy committed
122 123 124 125
	nd.SetLinks([]*node.Link{
		{Name: "a"},
		{Name: "c"},
		{Name: "b"},
126 127
	})

128 129
	nd.SetData([]byte("testing"))

130
	ond := nd.Copy().(*ProtoNode)
131 132 133 134 135 136
	ond.SetData(nil)

	if nd.Data() == nil {
		t.Fatal("should be different objects")
	}
}
Jeromy's avatar
Jeromy committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

func TestJsonRoundtrip(t *testing.T) {
	nd := new(ProtoNode)
	nd.SetLinks([]*node.Link{
		{Name: "a"},
		{Name: "c"},
		{Name: "b"},
	})
	nd.SetData([]byte("testing"))

	jb, err := nd.MarshalJSON()
	if err != nil {
		t.Fatal(err)
	}

	nn := new(ProtoNode)
	err = nn.UnmarshalJSON(jb)
	if err != nil {
		t.Fatal(err)
	}

	if !bytes.Equal(nn.Data(), nd.Data()) {
		t.Fatal("data wasnt the same")
	}

	if !nn.Cid().Equals(nd.Cid()) {
		t.Fatal("objects differed after marshaling")
	}
}