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 11, 2017
1 parent 0a9cf52 commit 34fe6d0
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 14 deletions.
89 changes: 89 additions & 0 deletions core/coreapi/dag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package coreapi_test

import (
"context"
"strings"
"testing"

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

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)
}

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

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([]string{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)
}
}
}
6 changes: 6 additions & 0 deletions core/coreapi/interface/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@ type UnixfsAPI interface {
}

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
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 34fe6d0

Please sign in to comment.