node_test.go 2.62 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"

11
	node "gx/ipfs/QmYNyRZJBUYPNrLszFmrBrPJbsBh2vMsefz5gnDpB5M1P6/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 44 45 46 47 48
		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
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 63

func TestFindLink(t *testing.T) {
	ds := mdtest.Mock()
64
	k, err := ds.Add(new(ProtoNode))
65 66 67 68
	if err != nil {
		t.Fatal(err)
	}

69
	nd := &ProtoNode{}
Jeromy's avatar
Jeromy committed
70 71 72 73
	nd.SetLinks([]*node.Link{
		{Name: "a", Cid: k},
		{Name: "c", Cid: k},
		{Name: "b", Cid: k},
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

	_, 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)
	}

110
	if olnk.Cid.String() == k.String() {
111 112 113 114 115
		t.Fatal("new link should have different hash")
	}
}

func TestNodeCopy(t *testing.T) {
116
	nd := &ProtoNode{}
Jeromy's avatar
Jeromy committed
117 118 119 120
	nd.SetLinks([]*node.Link{
		{Name: "a"},
		{Name: "c"},
		{Name: "b"},
121 122
	})

123 124
	nd.SetData([]byte("testing"))

125
	ond := nd.Copy().(*ProtoNode)
126 127 128 129 130 131
	ond.SetData(nil)

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

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")
	}
}