Skip to content

Commit

Permalink
coreapi: add tests for dag
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
  • Loading branch information
magik6k committed Dec 13, 2017
1 parent 0a9cf52 commit 753540a
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 21 deletions.
2 changes: 1 addition & 1 deletion core/coreapi/coreapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (api *CoreAPI) Unixfs() coreiface.UnixfsAPI {
}

func (api *CoreAPI) Dag() coreiface.DagAPI {
return (*DagAPI)(api)
return (*dagAPI)(api)
}

func (api *CoreAPI) ResolveNode(ctx context.Context, p coreiface.Path) (coreiface.Node, error) {
Expand Down
10 changes: 5 additions & 5 deletions core/coreapi/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
mh "gx/ipfs/QmU9a9NV9RdPNwZQDYd5uKsm6N6LJLSvLbywDDYFbaaC6P/go-multihash"
)

type DagAPI CoreAPI
type dagAPI CoreAPI

func (api *DagAPI) Put(ctx context.Context, src io.Reader, inputEnc string, format *cid.Prefix) ([]coreiface.Node, error) {
func (api *dagAPI) Put(ctx context.Context, src io.Reader, inputEnc string, format *cid.Prefix) ([]coreiface.Node, error) {
if format == nil {
format = &cid.Prefix{
Version: 1,
Expand Down Expand Up @@ -51,11 +51,11 @@ func (api *DagAPI) Put(ctx context.Context, src io.Reader, inputEnc string, form
return out, nil
}

func (api *DagAPI) Get(ctx context.Context, path coreiface.Path) (coreiface.Node, error) {
func (api *dagAPI) Get(ctx context.Context, path coreiface.Path) (coreiface.Node, error) {
return api.core().ResolveNode(ctx, path)
}

func (api *DagAPI) Tree(ctx context.Context, p coreiface.Path, depth int) ([]coreiface.Path, error) {
func (api *dagAPI) Tree(ctx context.Context, p coreiface.Path, depth int) ([]coreiface.Path, error) {
n, err := api.Get(ctx, p)
if err != nil {
return nil, err
Expand All @@ -72,6 +72,6 @@ func (api *DagAPI) Tree(ctx context.Context, p coreiface.Path, depth int) ([]cor
return out, nil
}

func (api *DagAPI) core() coreiface.CoreAPI {
func (api *dagAPI) core() coreiface.CoreAPI {
return (*CoreAPI)(api)
}
93 changes: 93 additions & 0 deletions core/coreapi/dag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package coreapi_test

import (
"context"
"path"
"strings"
"testing"

coreapi "github.com/ipfs/go-ipfs/core/coreapi"
)

var (
treeExpected = map[string]struct{}{
"a": {},
"b": {},
"c": {},
"c/d": {},
"c/e": {},
}
)

func TestPut(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}

res, err := api.Dag().Put(ctx, strings.NewReader(`"Hello"`), "json", nil)
if err != nil {
t.Error(err)
}

if res[0].Cid().String() != "zdpuAqckYF3ToF3gcJNxPZXmnmGuXd3gxHCXhq81HGxBejEvv" {
t.Errorf("got wrong cid: %s", res[0].Cid().String())
}
}

func TestPath(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}

sub, err := api.Dag().Put(ctx, strings.NewReader(`"foo"`), "json", nil)
if err != nil {
t.Error(err)
}

res, err := api.Dag().Put(ctx, strings.NewReader(`{"lnk": {"/": "`+sub[0].Cid().String()+`"}}`), "json", nil)
if err != nil {
t.Error(err)
}

p, err := coreapi.ParsePath(path.Join(res[0].Cid().String(), "lnk"))
if err != nil {
t.Error(err)
}

nd, err := api.Dag().Get(ctx, p)
if err != nil {
t.Error(err)
}

if nd.Cid().String() != sub[0].Cid().String() {
t.Errorf("got unexpected cid %s, expected %s", nd.Cid().String(), sub[0].Cid().String())
}
}

func TestTree(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}

res, err := api.Dag().Put(ctx, strings.NewReader(`{"a": 123, "b": "foo", "c": {"d": 321, "e": 111}}`), "json", nil)
if err != nil {
t.Error(err)
}

lst := res[0].Tree("", -1)
if len(lst) != len(treeExpected) {
t.Errorf("tree length of %d doesn't match expected %d", len(lst), len(treeExpected))
}

for _, ent := range lst {
if _, ok := treeExpected[ent]; !ok {
t.Errorf("unexpected tree entry %s", ent)
}
}
}
10 changes: 9 additions & 1 deletion core/coreapi/interface/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type CoreAPI interface {
Dag() DagAPI

ResolvePath(context.Context, Path) (Path, error)
ResolveNode(context.Context, Path) (Node, error) //TODO: should this get dropped in favor of DagAPI.Get?
ResolveNode(context.Context, Path) (Node, error)
}

type UnixfsAPI interface {
Expand All @@ -40,9 +40,17 @@ type UnixfsAPI interface {
Ls(context.Context, Path) ([]*Link, error)
}

// DagAPI specifies the interface to IPLD
type DagAPI interface {
// Put inserts data using specified format and input encoding. If format is
// not specified (nil), default dag-cbor/sha256 is used
Put(ctx context.Context, src io.Reader, inputEnc string, format *cid.Prefix) ([]Node, error)

// Get attempts to resolve and get the node specified by the path
Get(ctx context.Context, path Path) (Node, error)

// Tree returns list of paths within a node specified by the path.
// To get all paths in a tree, set depth to -1
Tree(ctx context.Context, path Path, depth int) ([]Path, error)
}

Expand Down
29 changes: 15 additions & 14 deletions core/coreapi/unixfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"
"testing"

cbor "gx/ipfs/QmWCs8kMecJwCPK8JThue8TjgM2ieJ2HjTLDu7Cv2NEmZi/go-ipld-cbor"

core "github.com/ipfs/go-ipfs/core"
coreapi "github.com/ipfs/go-ipfs/core/coreapi"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
Expand All @@ -17,7 +19,6 @@ import (
config "github.com/ipfs/go-ipfs/repo/config"
ds2 "github.com/ipfs/go-ipfs/thirdparty/datastore2"
unixfs "github.com/ipfs/go-ipfs/unixfs"
cbor "gx/ipfs/QmWCs8kMecJwCPK8JThue8TjgM2ieJ2HjTLDu7Cv2NEmZi/go-ipld-cbor"
)

// `echo -n 'hello, world!' | ipfs add`
Expand All @@ -30,7 +31,7 @@ var emptyDir = coreapi.ResolvedPath("/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbs
// `echo -n | ipfs add`
var emptyFile = coreapi.ResolvedPath("/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH", nil, nil)

func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.UnixfsAPI, error) {
func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.CoreAPI, error) {
r := &repo.Mock{
C: config.Config{
Identity: config.Identity{
Expand All @@ -43,7 +44,7 @@ func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.UnixfsAPI, error) {
if err != nil {
return nil, nil, err
}
api := coreapi.NewCoreAPI(node).Unixfs()
api := coreapi.NewCoreAPI(node)
return node, api, nil
}

Expand All @@ -55,7 +56,7 @@ func TestAdd(t *testing.T) {
}

str := strings.NewReader(helloStr)
p, err := api.Add(ctx, str)
p, err := api.Unixfs().Add(ctx, str)
if err != nil {
t.Error(err)
}
Expand All @@ -64,7 +65,7 @@ func TestAdd(t *testing.T) {
t.Fatalf("expected path %s, got: %s", hello, p)
}

r, err := api.Cat(ctx, hello)
r, err := api.Unixfs().Cat(ctx, hello)
if err != nil {
t.Fatal(err)
}
Expand All @@ -87,7 +88,7 @@ func TestAddEmptyFile(t *testing.T) {
}

str := strings.NewReader("")
p, err := api.Add(ctx, str)
p, err := api.Unixfs().Add(ctx, str)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -115,7 +116,7 @@ func TestCatBasic(t *testing.T) {
t.Fatalf("expected CID %s, got: %s", hello, p)
}

r, err := api.Cat(ctx, hello)
r, err := api.Unixfs().Cat(ctx, hello)
if err != nil {
t.Fatal(err)
}
Expand All @@ -142,7 +143,7 @@ func TestCatEmptyFile(t *testing.T) {
t.Fatal(err)
}

r, err := api.Cat(ctx, emptyFile)
r, err := api.Unixfs().Cat(ctx, emptyFile)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -174,7 +175,7 @@ func TestCatDir(t *testing.T) {
t.Fatalf("expected path %s, got: %s", emptyDir, p)
}

_, err = api.Cat(ctx, emptyDir)
_, err = api.Unixfs().Cat(ctx, emptyDir)
if err != coreiface.ErrIsDir {
t.Fatalf("expected ErrIsDir, got: %s", err)
}
Expand All @@ -192,7 +193,7 @@ func TestCatNonUnixfs(t *testing.T) {
t.Error(err)
}

_, err = api.Cat(ctx, coreapi.ParseCid(c))
_, err = api.Unixfs().Cat(ctx, coreapi.ParseCid(c))
if !strings.Contains(err.Error(), "proto: required field") {
t.Fatalf("expected protobuf error, got: %s", err)
}
Expand All @@ -205,7 +206,7 @@ func TestCatOffline(t *testing.T) {
t.Error(err)
}

_, err = api.Cat(ctx, coreapi.ResolvedPath("/ipns/Qmfoobar", nil, nil))
_, err = api.Unixfs().Cat(ctx, coreapi.ResolvedPath("/ipns/Qmfoobar", nil, nil))
if err != coreiface.ErrOffline {
t.Fatalf("expected ErrOffline, got: %s", err)
}
Expand All @@ -229,7 +230,7 @@ func TestLs(t *testing.T) {
}
p := coreapi.ResolvedPath("/ipfs/"+parts[0], nil, nil)

links, err := api.Ls(ctx, p)
links, err := api.Unixfs().Ls(ctx, p)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -260,7 +261,7 @@ func TestLsEmptyDir(t *testing.T) {
t.Error(err)
}

links, err := api.Ls(ctx, emptyDir)
links, err := api.Unixfs().Ls(ctx, emptyDir)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -288,7 +289,7 @@ func TestLsNonUnixfs(t *testing.T) {
t.Error(err)
}

links, err := api.Ls(ctx, coreapi.ParseCid(c))
links, err := api.Unixfs().Ls(ctx, coreapi.ParseCid(c))
if err != nil {
t.Error(err)
}
Expand Down

0 comments on commit 753540a

Please sign in to comment.