dag.go 4.15 KB
Newer Older
1
package tests
2 3 4

import (
	"context"
5
	"math"
6 7 8 9 10 11
	"path"
	"strings"
	"testing"

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

12 13
	ipld "gx/ipfs/QmRL22E4paat7ky7vx9MLpR97JHHbFPrg3ytFQw6qp1y1s/go-ipld-format"
	ipldcbor "gx/ipfs/QmRZxJ7oybgnnwriuRub9JXp5YdFM9wiGSyRq38QC7swpS/go-ipld-cbor"
14 15 16
	mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash"
)

17
func (tp *provider) TestDag(t *testing.T) {
18 19 20 21 22 23 24
	tp.hasApi(t, func(api coreiface.CoreAPI) error {
		if api.Dag() == nil {
			return apiNotImplemented
		}
		return nil
	})

25 26 27 28 29
	t.Run("TestPut", tp.TestPut)
	t.Run("TestPutWithHash", tp.TestPutWithHash)
	t.Run("TestPath", tp.TestDagPath)
	t.Run("TestTree", tp.TestTree)
	t.Run("TestBatch", tp.TestBatch)
30 31
}

32 33 34 35 36 37 38 39 40 41
var (
	treeExpected = map[string]struct{}{
		"a":   {},
		"b":   {},
		"c":   {},
		"c/d": {},
		"c/e": {},
	}
)

42
func (tp *provider) TestPut(t *testing.T) {
43 44
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
45
	api, err := tp.makeAPI(ctx)
46 47 48 49
	if err != nil {
		t.Error(err)
	}

50
	nd, err := ipldcbor.FromJSON(strings.NewReader(`"Hello"`), math.MaxUint64, -1)
51 52 53 54
	if err != nil {
		t.Error(err)
	}

55
	err = api.Dag().Add(ctx, nd)
56
	if err != nil {
57
		t.Fatal(err)
58 59
	}

60 61
	if nd.Cid().String() != "zdpuAqckYF3ToF3gcJNxPZXmnmGuXd3gxHCXhq81HGxBejEvv" {
		t.Errorf("got wrong cid: %s", nd.Cid().String())
62 63 64
	}
}

65
func (tp *provider) TestPutWithHash(t *testing.T) {
66 67
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
68
	api, err := tp.makeAPI(ctx)
69 70 71 72
	if err != nil {
		t.Error(err)
	}

73
	nd, err := ipldcbor.FromJSON(strings.NewReader(`"Hello"`), mh.ID, -1)
74 75 76 77
	if err != nil {
		t.Error(err)
	}

78
	err = api.Dag().Add(ctx, nd)
79
	if err != nil {
80
		t.Fatal(err)
81 82
	}

83 84
	if nd.Cid().String() != "z5hRLNd2sv4z1c" {
		t.Errorf("got wrong cid: %s", nd.Cid().String())
85 86 87
	}
}

88
func (tp *provider) TestDagPath(t *testing.T) {
89 90
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
91
	api, err := tp.makeAPI(ctx)
92 93 94 95
	if err != nil {
		t.Error(err)
	}

96
	snd, err := ipldcbor.FromJSON(strings.NewReader(`"foo"`), math.MaxUint64, -1)
97 98 99 100
	if err != nil {
		t.Error(err)
	}

101
	err = api.Dag().Add(ctx, snd)
102
	if err != nil {
103
		t.Fatal(err)
104 105
	}

106
	nd, err := ipldcbor.FromJSON(strings.NewReader(`{"lnk": {"/": "`+snd.Cid().String()+`"}}`), math.MaxUint64, -1)
107 108 109 110
	if err != nil {
		t.Error(err)
	}

111
	err = api.Dag().Add(ctx, nd)
112
	if err != nil {
113
		t.Fatal(err)
114 115
	}

116
	p, err := coreiface.ParsePath(path.Join(nd.Cid().String(), "lnk"))
117 118 119 120
	if err != nil {
		t.Error(err)
	}

121
	rp, err := api.ResolvePath(ctx, p)
122 123 124 125
	if err != nil {
		t.Error(err)
	}

126
	ndd, err := api.Dag().Get(ctx, rp.Cid())
127 128 129 130
	if err != nil {
		t.Error(err)
	}

131
	if ndd.Cid().String() != snd.Cid().String() {
132
		t.Errorf("got unexpected cid %s, expected %s", ndd.Cid().String(), snd.Cid().String())
133 134 135
	}
}

136
func (tp *provider) TestTree(t *testing.T) {
137 138
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
139
	api, err := tp.makeAPI(ctx)
140 141 142 143
	if err != nil {
		t.Error(err)
	}

144
	nd, err := ipldcbor.FromJSON(strings.NewReader(`{"a": 123, "b": "foo", "c": {"d": 321, "e": 111}}`), math.MaxUint64, -1)
145 146 147 148
	if err != nil {
		t.Error(err)
	}

149
	err = api.Dag().Add(ctx, nd)
150
	if err != nil {
151
		t.Fatal(err)
152 153
	}

154
	res, err := api.Dag().Get(ctx, nd.Cid())
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	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)
		}
	}
}

171
func (tp *provider) TestBatch(t *testing.T) {
172 173
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
174
	api, err := tp.makeAPI(ctx)
175 176 177 178
	if err != nil {
		t.Error(err)
	}

179
	nd, err := ipldcbor.FromJSON(strings.NewReader(`"Hello"`), math.MaxUint64, -1)
180
	if err != nil {
181
		t.Error(err)
182 183
	}

184 185
	if nd.Cid().String() != "zdpuAqckYF3ToF3gcJNxPZXmnmGuXd3gxHCXhq81HGxBejEvv" {
		t.Errorf("got wrong cid: %s", nd.Cid().String())
186 187
	}

188
	_, err = api.Dag().Get(ctx, nd.Cid())
189
	if err == nil || !strings.Contains(err.Error(), "not found") {
190 191 192
		t.Error(err)
	}

193
	if err := api.Dag().AddMany(ctx, []ipld.Node{nd}); err != nil {
194 195 196
		t.Error(err)
	}

197
	_, err = api.Dag().Get(ctx, nd.Cid())
198 199 200 201
	if err != nil {
		t.Error(err)
	}
}