Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-ld-format
Commits
a8cc0b99
Commit
a8cc0b99
authored
Sep 06, 2018
by
Kejie Zhang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
drop node copy func and improve testing
parent
7b1fa213
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
133 additions
and
11 deletions
+133
-11
daghelpers.go
daghelpers.go
+8
-2
daghelpers_test.go
daghelpers_test.go
+125
-9
No files found.
daghelpers.go
View file @
a8cc0b99
...
...
@@ -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
}
...
...
daghelpers_test.go
View file @
a8cc0b99
...
...
@@ -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
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment