Commit a8cc0b99 authored by Kejie Zhang's avatar Kejie Zhang

drop node copy func and improve testing

parent 7b1fa213
......@@ -96,8 +96,10 @@ func Copy(ctx context.Context, from, to DAGService, root *cid.Cid) error {
}
links := node.Links()
if len(links) == 0 {
n := node.Copy()
to.Add(ctx, n)
err := to.Add(ctx, node)
if err != nil {
return err
}
return nil
}
for _, link := range links {
......@@ -105,6 +107,10 @@ func Copy(ctx context.Context, from, to DAGService, root *cid.Cid) error {
if err != nil {
return err
}
err = to.Add(ctx, node)
if err != nil {
return err
}
}
return nil
}
......
......@@ -4,25 +4,141 @@ import (
"context"
"github.com/ipfs/go-cid"
mh "github.com/multiformats/go-multihash"
"github.com/stretchr/testify/assert"
"testing"
)
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, EmptyNodeError
}
func (n *TestNode) Tree(string, int) []string {
return nil
}
func (n *TestNode) ResolveLink([]string) (*Link, []string, error) {
return nil, nil, EmptyNodeError
}
func (n *TestNode) Copy() Node {
return &EmptyNode{}
}
func (n *TestNode) Cid() *cid.Cid {
c, err := n.builder.Sum(n.RawData())
if err != nil {
return nil
}
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()
if err := from.Add(ctx, new(EmptyNode)); err != nil {
t.Fatal(err)
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()
id, err := cid.Prefix{
Version: 1,
Codec: cid.Raw,
MhType: mh.ID,
MhLength: 0,
}.Sum(nil)
err = Copy(ctx, from, to, id)
err := Copy(ctx, from, to, root.Cid())
if err != nil {
t.Error(err)
}
r, err := to.Get(ctx, root.Cid())
if err != nil {
t.Error(err)
}
l1, err := to.Get(ctx, l11.Cid())
if err != nil {
t.Error(err)
}
assert.Equal(t, len(r.Links()), 2)
assert.Equal(t, len(l1.Links()), 3)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment