dag_test.go 2.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
package tests_test

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

	coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
	opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options"

	mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash"
)

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"`))
	if err != nil {
		t.Error(err)
	}

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

func TestPutWithHash(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"`), opt.Dag.Hash(mh.ID, -1))
	if err != nil {
		t.Error(err)
	}

	if res.Cid().String() != "z5hRLNd2sv4z1c" {
		t.Errorf("got wrong cid: %s", res.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"`))
	if err != nil {
		t.Error(err)
	}

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

	p, err := coreiface.ParsePath(path.Join(res.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.Cid().String() {
		t.Errorf("got unexpected cid %s, expected %s", nd.Cid().String(), sub.Cid().String())
	}
}

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

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

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

	lst := res.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)
		}
	}
}

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

	batch := api.Dag().Batch(ctx)

	c, err := batch.Put(ctx, strings.NewReader(`"Hello"`))
	if err != nil {
		t.Error(err)
	}

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

	_, err = api.Dag().Get(ctx, c)
	if err == nil || err.Error() != "merkledag: not found" {
		t.Error(err)
	}

	if err := batch.Commit(ctx); err != nil {
		t.Error(err)
	}

	_, err = api.Dag().Get(ctx, c)
	if err != nil {
		t.Error(err)
	}
}