-
Notifications
You must be signed in to change notification settings - Fork 26
/
daghelpers_test.go
142 lines (114 loc) · 2.59 KB
/
daghelpers_test.go
1
2
3
4
5
6
7
8
9
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
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
132
133
134
135
136
137
138
139
140
141
142
package format
import (
"context"
"testing"
"github.com/ipfs/go-cid"
mh "github.com/multiformats/go-multihash"
)
type TestNode struct {
links []*Link
data []byte
builder cid.Builder
}
var v0CidPrefix = cid.Prefix{
Codec: cid.DagProtobuf,
MhLength: -1,
MhType: mh.SHA2_256,
Version: 0,
}
func InitNode(d []byte) *TestNode {
return &TestNode{
data: d,
builder: v0CidPrefix,
}
}
func (n *TestNode) Resolve([]string) (interface{}, []string, error) {
return nil, nil, ErrEmptyNode
}
func (n *TestNode) Tree(string, int) []string {
return nil
}
func (n *TestNode) ResolveLink([]string) (*Link, []string, error) {
return nil, nil, ErrEmptyNode
}
func (n *TestNode) Copy() Node {
return &EmptyNode{}
}
func (n *TestNode) Cid() cid.Cid {
c, err := n.builder.Sum(n.RawData())
if err != nil {
return cid.Cid{}
}
return c
}
func (n *TestNode) Links() []*Link {
return n.links
}
func (n *TestNode) Loggable() map[string]interface{} {
return nil
}
func (n *TestNode) String() string {
return string(n.data)
}
func (n *TestNode) RawData() []byte {
return n.data
}
func (n *TestNode) Size() (uint64, error) {
return 0, nil
}
func (n *TestNode) Stat() (*NodeStat, error) {
return &NodeStat{}, nil
}
// AddNodeLink adds a link to another node.
func (n *TestNode) AddNodeLink(name string, that Node) error {
lnk, err := MakeLink(that)
if err != nil {
return err
}
lnk.Name = name
n.AddRawLink(name, lnk)
return nil
}
func (n *TestNode) AddRawLink(name string, l *Link) error {
n.links = append(n.links, &Link{
Name: name,
Size: l.Size,
Cid: l.Cid,
})
return nil
}
func TestCopy(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
from := newTestDag()
root := InitNode([]byte("level0"))
l11 := InitNode([]byte("leve1_node1"))
l12 := InitNode([]byte("leve1_node2"))
l21 := InitNode([]byte("leve2_node1"))
l22 := InitNode([]byte("leve2_node2"))
l23 := InitNode([]byte("leve2_node3"))
l11.AddNodeLink(l21.Cid().String(), l21)
l11.AddNodeLink(l22.Cid().String(), l22)
l11.AddNodeLink(l23.Cid().String(), l23)
root.AddNodeLink(l11.Cid().String(), l11)
root.AddNodeLink(l12.Cid().String(), l12)
for _, n := range []Node{l23, l22, l21, l12, l11, root} {
err := from.Add(ctx, n)
if err != nil {
t.Fatal(err)
}
}
to := newTestDag()
err := Copy(ctx, from, to, root.Cid())
if err != nil {
t.Error(err)
}
r, err := to.Get(ctx, root.Cid())
if err != nil || len(r.Links()) != 2 {
t.Error("fail to copy dag")
}
l1, err := to.Get(ctx, l11.Cid())
if err != nil || len(l1.Links()) != 3 {
t.Error("fail to copy dag")
}
}