coreapi: add tests for dag
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Dec 11, 2017 at 17:22 UTC
b40a6f88ebd78a51fb68fc43797e20bf3a43b67f
5 files changed
+125
-23
core/coreapi/coreapi.go
+1
-1
@@ -26,7 +26,7 @@ func (api *CoreAPI) Unixfs() coreiface.UnixfsAPI {
26
}
27
28
func (api *CoreAPI) Dag() coreiface.DagAPI {
29
- return (*DagAPI)(api)
29
+ return (*dagAPI)(api)
30
}
31
32
func (api *CoreAPI) ResolveNode(ctx context.Context, p coreiface.Path) (coreiface.Node, error) {
core/coreapi/dag.go
+7
-7
@@ -10,13 +10,13 @@ import (
10
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
11
coredag "github.com/ipfs/go-ipfs/core/coredag"
12
13
- cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
14
- mh "gx/ipfs/QmU9a9NV9RdPNwZQDYd5uKsm6N6LJLSvLbywDDYFbaaC6P/go-multihash"
13
+ mh "gx/ipfs/QmYeKnKpubCMRiq3PGZcTREErthbb5Q9cXsCoSkD9bjEBd/go-multihash"
14
+ cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid"
15
)
16
17
-type DagAPI CoreAPI
17
+type dagAPI CoreAPI
18
19
-func (api *DagAPI) Put(ctx context.Context, src io.Reader, inputEnc string, format *cid.Prefix) ([]coreiface.Node, error) {
19
+func (api *dagAPI) Put(ctx context.Context, src io.Reader, inputEnc string, format *cid.Prefix) ([]coreiface.Node, error) {
20
if format == nil {
21
format = &cid.Prefix{
22
Version: 1,
@@ -51,11 +51,11 @@ func (api *DagAPI) Put(ctx context.Context, src io.Reader, inputEnc string, form
51
return out, nil
52
}
53
54
-func (api *DagAPI) Get(ctx context.Context, path coreiface.Path) (coreiface.Node, error) {
54
+func (api *dagAPI) Get(ctx context.Context, path coreiface.Path) (coreiface.Node, error) {
55
return api.core().ResolveNode(ctx, path)
56
}
57
58
-func (api *DagAPI) Tree(ctx context.Context, p coreiface.Path, depth int) ([]coreiface.Path, error) {
58
+func (api *dagAPI) Tree(ctx context.Context, p coreiface.Path, depth int) ([]coreiface.Path, error) {
59
n, err := api.Get(ctx, p)
60
if err != nil {
61
return nil, err
@@ -72,6 +72,6 @@ func (api *DagAPI) Tree(ctx context.Context, p coreiface.Path, depth int) ([]cor
72
return out, nil
73
}
74
75
-func (api *DagAPI) core() coreiface.CoreAPI {
75
+func (api *dagAPI) core() coreiface.CoreAPI {
76
return (*CoreAPI)(api)
77
}
core/coreapi/dag_test.go
new
+93
@@ -0,0 +1,93 @@
1
+package coreapi_test
2
+
3
+import (
4
+ "context"
5
+ "path"
6
+ "strings"
7
+ "testing"
8
+
9
+ coreapi "github.com/ipfs/go-ipfs/core/coreapi"
10
+)
11
+
12
+var (
13
+ treeExpected = map[string]struct{}{
14
+ "a": {},
15
+ "b": {},
16
+ "c": {},
17
+ "c/d": {},
18
+ "c/e": {},
19
+ }
20
+)
21
+
22
+func TestPut(t *testing.T) {
23
+ ctx := context.Background()
24
+ _, api, err := makeAPI(ctx)
25
+ if err != nil {
26
+ t.Error(err)
27
+ }
28
+
29
+ res, err := api.Dag().Put(ctx, strings.NewReader(`"Hello"`), "json", nil)
30
+ if err != nil {
31
+ t.Error(err)
32
+ }
33
+
34
+ if res[0].Cid().String() != "zdpuAqckYF3ToF3gcJNxPZXmnmGuXd3gxHCXhq81HGxBejEvv" {
35
+ t.Errorf("got wrong cid: %s", res[0].Cid().String())
36
+ }
37
+}
38
+
39
+func TestPath(t *testing.T) {
40
+ ctx := context.Background()
41
+ _, api, err := makeAPI(ctx)
42
+ if err != nil {
43
+ t.Error(err)
44
+ }
45
+
46
+ sub, err := api.Dag().Put(ctx, strings.NewReader(`"foo"`), "json", nil)
47
+ if err != nil {
48
+ t.Error(err)
49
+ }
50
+
51
+ res, err := api.Dag().Put(ctx, strings.NewReader(`{"lnk": {"/": "`+sub[0].Cid().String()+`"}}`), "json", nil)
52
+ if err != nil {
53
+ t.Error(err)
54
+ }
55
+
56
+ p, err := coreapi.ParsePath(path.Join(res[0].Cid().String(), "lnk"))
57
+ if err != nil {
58
+ t.Error(err)
59
+ }
60
+
61
+ nd, err := api.Dag().Get(ctx, p)
62
+ if err != nil {
63
+ t.Error(err)
64
+ }
65
+
66
+ if nd.Cid().String() != sub[0].Cid().String() {
67
+ t.Errorf("got unexpected cid %s, expected %s", nd.Cid().String(), sub[0].Cid().String())
68
+ }
69
+}
70
+
71
+func TestTree(t *testing.T) {
72
+ ctx := context.Background()
73
+ _, api, err := makeAPI(ctx)
74
+ if err != nil {
75
+ t.Error(err)
76
+ }
77
+
78
+ res, err := api.Dag().Put(ctx, strings.NewReader(`{"a": 123, "b": "foo", "c": {"d": 321, "e": 111}}`), "json", nil)
79
+ if err != nil {
80
+ t.Error(err)
81
+ }
82
+
83
+ lst := res[0].Tree("", -1)
84
+ if len(lst) != len(treeExpected) {
85
+ t.Errorf("tree length of %d doesn't match expected %d", len(lst), len(treeExpected))
86
+ }
87
+
88
+ for _, ent := range lst {
89
+ if _, ok := treeExpected[ent]; !ok {
90
+ t.Errorf("unexpected tree entry %s", ent)
91
+ }
92
+ }
93
+}
core/coreapi/interface/interface.go
+10
-2
@@ -41,7 +41,7 @@ type CoreAPI interface {
41
42
// ResolveNode resolves the path (if not resolved already) using Unixfs
43
// resolver, gets and returns the resolved Node
44
- ResolveNode(context.Context, Path) (Node, error) //TODO: should this get dropped in favor of DagAPI.Get?
44
+ ResolveNode(context.Context, Path) (Node, error)
45
}
46
47
// UnixfsAPI is the basic interface to immutable files in IPFS
@@ -56,9 +56,17 @@ type UnixfsAPI interface {
56
Ls(context.Context, Path) ([]*Link, error)
57
}
58
59
+// DagAPI specifies the interface to IPLD
60
type DagAPI interface {
60
- Put(ctx context.Context, src io.Reader, inputEnc string, format *cid.Prefix) ([]Node, error)
61
+ // Put inserts data using specified format and input encoding.
62
+ // If format is not specified (nil), default dag-cbor/sha256 is used
63
+ Put(ctx context.Context, src io.Reader, inputEnc string, format *cid.Prefix) ([]Node, error) //TODO: make format optional
64
+
65
+ // Get attempts to resolve and get the node specified by the path
66
Get(ctx context.Context, path Path) (Node, error)
67
+
68
+ // Tree returns list of paths within a node specified by the path.
69
+ // To get all paths in a tree, set depth to -1
70
Tree(ctx context.Context, path Path, depth int) ([]Path, error)
71
}
72
core/coreapi/unixfs_test.go
+14
-13
@@ -17,6 +17,7 @@ import (
17
config "github.com/ipfs/go-ipfs/repo/config"
18
ds2 "github.com/ipfs/go-ipfs/thirdparty/datastore2"
19
unixfs "github.com/ipfs/go-ipfs/unixfs"
20
+
21
cbor "gx/ipfs/QmeZv9VXw2SfVbX55LV6kGTWASKBc9ZxAVqGBeJcDGdoXy/go-ipld-cbor"
22
)
23
@@ -30,7 +31,7 @@ var emptyDir = coreapi.ResolvedPath("/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbs
31
// `echo -n | ipfs add`
32
var emptyFile = coreapi.ResolvedPath("/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH", nil, nil)
33
33
-func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.UnixfsAPI, error) {
34
+func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.CoreAPI, error) {
35
r := &repo.Mock{
36
C: config.Config{
37
Identity: config.Identity{
@@ -43,7 +44,7 @@ func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.UnixfsAPI, error) {
44
if err != nil {
45
return nil, nil, err
46
}
46
- api := coreapi.NewCoreAPI(node).Unixfs()
47
+ api := coreapi.NewCoreAPI(node)
48
return node, api, nil
49
}
50
@@ -55,7 +56,7 @@ func TestAdd(t *testing.T) {
56
}
57
58
str := strings.NewReader(helloStr)
58
- p, err := api.Add(ctx, str)
59
+ p, err := api.Unixfs().Add(ctx, str)
60
if err != nil {
61
t.Error(err)
62
}
@@ -64,7 +65,7 @@ func TestAdd(t *testing.T) {
65
t.Fatalf("expected path %s, got: %s", hello, p)
66
}
67
67
- r, err := api.Cat(ctx, hello)
68
+ r, err := api.Unixfs().Cat(ctx, hello)
69
if err != nil {
70
t.Fatal(err)
71
}
@@ -87,7 +88,7 @@ func TestAddEmptyFile(t *testing.T) {
88
}
89
90
str := strings.NewReader("")
90
- p, err := api.Add(ctx, str)
91
+ p, err := api.Unixfs().Add(ctx, str)
92
if err != nil {
93
t.Error(err)
94
}
@@ -115,7 +116,7 @@ func TestCatBasic(t *testing.T) {
116
t.Fatalf("expected CID %s, got: %s", hello, p)
117
}
118
118
- r, err := api.Cat(ctx, hello)
119
+ r, err := api.Unixfs().Cat(ctx, hello)
120
if err != nil {
121
t.Fatal(err)
122
}
@@ -142,7 +143,7 @@ func TestCatEmptyFile(t *testing.T) {
143
t.Fatal(err)
144
}
145
145
- r, err := api.Cat(ctx, emptyFile)
146
+ r, err := api.Unixfs().Cat(ctx, emptyFile)
147
if err != nil {
148
t.Fatal(err)
149
}
@@ -174,7 +175,7 @@ func TestCatDir(t *testing.T) {
175
t.Fatalf("expected path %s, got: %s", emptyDir, p)
176
}
177
177
- _, err = api.Cat(ctx, emptyDir)
178
+ _, err = api.Unixfs().Cat(ctx, emptyDir)
179
if err != coreiface.ErrIsDir {
180
t.Fatalf("expected ErrIsDir, got: %s", err)
181
}
@@ -192,7 +193,7 @@ func TestCatNonUnixfs(t *testing.T) {
193
t.Error(err)
194
}
195
195
- _, err = api.Cat(ctx, coreapi.ParseCid(c))
196
+ _, err = api.Unixfs().Cat(ctx, coreapi.ParseCid(c))
197
if !strings.Contains(err.Error(), "proto: required field") {
198
t.Fatalf("expected protobuf error, got: %s", err)
199
}
@@ -205,7 +206,7 @@ func TestCatOffline(t *testing.T) {
206
t.Error(err)
207
}
208
208
- _, err = api.Cat(ctx, coreapi.ResolvedPath("/ipns/Qmfoobar", nil, nil))
209
+ _, err = api.Unixfs().Cat(ctx, coreapi.ResolvedPath("/ipns/Qmfoobar", nil, nil))
210
if err != coreiface.ErrOffline {
211
t.Fatalf("expected ErrOffline, got: %s", err)
212
}
@@ -229,7 +230,7 @@ func TestLs(t *testing.T) {
230
}
231
p := coreapi.ResolvedPath("/ipfs/"+parts[0], nil, nil)
232
232
- links, err := api.Ls(ctx, p)
233
+ links, err := api.Unixfs().Ls(ctx, p)
234
if err != nil {
235
t.Error(err)
236
}
@@ -260,7 +261,7 @@ func TestLsEmptyDir(t *testing.T) {
261
t.Error(err)
262
}
263
263
- links, err := api.Ls(ctx, emptyDir)
264
+ links, err := api.Unixfs().Ls(ctx, emptyDir)
265
if err != nil {
266
t.Error(err)
267
}
@@ -288,7 +289,7 @@ func TestLsNonUnixfs(t *testing.T) {
289
t.Error(err)
290
}
291
291
- links, err := api.Ls(ctx, coreapi.ParseCid(c))
292
+ links, err := api.Unixfs().Ls(ctx, coreapi.ParseCid(c))
293
if err != nil {
294
t.Error(err)
295
}